Skip to content

Commit

Permalink
Set nofollow on links in Markdown content
Browse files Browse the repository at this point in the history
To disincentivize spamdexing, links in user-generated content should be
disavowed by annotation with `rel="nofollow"` attributes:

  - https://en.wikipedia.org/wiki/Nofollow

Automated spam has already targeted OSEM in the wild:

  - SeaGL/organization#274

Ideally link annotation would be performed during Markdown rendering or
a single sanitization pass, but this is currently an unresolved issue:

  - vmg/redcarpet#720
  • Loading branch information
AndrewKvalheim committed Mar 4, 2023
1 parent c54a8a1 commit 80d7ac5
Show file tree
Hide file tree
Showing 2 changed files with 6 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
5 changes: 5 additions & 0 deletions spec/helpers/format_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@
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 80d7ac5

Please sign in to comment.