Skip to content

Commit

Permalink
Add noopener and noreferer to links in user generated content
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Jan 9, 2019
1 parent 9db4f47 commit a1146c4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config/initializers/sanitize.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sanitize::Config::OSM = Sanitize::Config::RELAXED.dup

Sanitize::Config::OSM[:elements] -= %w[div style]
Sanitize::Config::OSM[:add_attributes] = { "a" => { "rel" => "nofollow" } }
Sanitize::Config::OSM[:add_attributes] = { "a" => { "rel" => "nofollow noopener noreferer" } }
Sanitize::Config::OSM[:remove_contents] = %w[script style]
4 changes: 2 additions & 2 deletions lib/rich_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def sanitize(text)

def linkify(text, mode = :urls)
if text.html_safe?
Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow")).html_safe
Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferer")).html_safe
else
Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow"))
Rinku.auto_link(text, mode, tag_builder.tag_options(:rel => "nofollow noopener noreferer"))
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions test/lib/rich_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def test_html_to_html
assert_html r do
assert_select "a", 1
assert_select "a[href='http://example.com/']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("html", "foo <a href='http://example.com/'>bar</a> baz")
assert_html r do
assert_select "a", 1
assert_select "a[href='http://example.com/']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("html", "foo example@example.com bar")
Expand All @@ -27,7 +27,7 @@ def test_html_to_html
assert_html r do
assert_select "a", 1
assert_select "a[href='mailto:example@example.com']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("html", "foo <div>bar</div> baz")
Expand Down Expand Up @@ -64,28 +64,28 @@ def test_markdown_to_html
assert_html r do
assert_select "a", 1
assert_select "a[href='http://example.com/']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("markdown", "foo [bar](http://example.com/) baz")
assert_html r do
assert_select "a", 1
assert_select "a[href='http://example.com/']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("markdown", "foo example@example.com bar")
assert_html r do
assert_select "a", 1
assert_select "a[href='mailto:example@example.com']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("markdown", "foo [bar](mailto:example@example.com) bar")
assert_html r do
assert_select "a", 1
assert_select "a[href='mailto:example@example.com']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("markdown", "foo ![bar](http://example.com/example.png) bar")
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_text_to_html
assert_html r do
assert_select "a", 1
assert_select "a[href='http://example.com/']", 1
assert_select "a[rel='nofollow']", 1
assert_select "a[rel='nofollow noopener noreferer']", 1
end

r = RichText.new("text", "foo example@example.com bar")
Expand Down

0 comments on commit a1146c4

Please sign in to comment.