Skip to content

Commit

Permalink
Merge pull request #3131 from AndrewKvalheim/nofollow
Browse files Browse the repository at this point in the history
Set `nofollow` on user-generated links
  • Loading branch information
hennevogel authored Apr 19, 2023
2 parents 3aaf860 + 80d7ac5 commit 8b909a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/format_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def markdown(text, escape_html=true)
safe_links_only: true
}
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(render_options), markdown_options)
sanitize(markdown.render(text))
sanitize(sanitize(markdown.render(text)), scrubber: Loofah::Scrubbers::NoFollow.new)
end

def markdown_hint(text='')
Expand Down
13 changes: 13 additions & 0 deletions spec/helpers/format_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,18 @@
it 'should return HTML for header markdown' do
expect(markdown('# this is my header')).to eq "<h1>this is my header</h1>\n"
end

it 'escapes input HTML' do
expect(markdown('<em>*a*</em>')).to eq "<p>&lt;em&gt;<em>a</em>&lt;/em&gt;</p>\n"
end

it 'removes unallowed elements' do
expect(markdown('<em>*<style>a</style>*</em>', false)).to eq "<p><em><em>a</em></em></p>\n"
end

it 'sets nofollow on links' do
expect(markdown('[a](https://example.com/)'))
.to eq "<p><a href=\"https://example.com/\" rel=\"nofollow\">a</a></p>\n"
end
end
end

0 comments on commit 8b909a9

Please sign in to comment.