Add "noopener" to autolinks' rel attribute
This commit is contained in:
@@ -46,7 +46,7 @@ class AutoLinkScrubber < Loofah::Scrubber
|
||||
|
||||
links.each do |link|
|
||||
nodes << doc.create_text_node(text[pos...link[:start]]) if link[:start] > pos
|
||||
nodes << doc.create_element("a", link[:text], href: link[:href], rel: "noreferrer")
|
||||
nodes << doc.create_element("a", link[:text], href: link[:href], rel: "noopener noreferrer")
|
||||
pos = link[:start] + link[:length]
|
||||
end
|
||||
nodes << doc.create_text_node(text[pos..]) if pos < text.length
|
||||
|
||||
@@ -3,60 +3,60 @@ require "test_helper"
|
||||
class HtmlHelperTest < ActionView::TestCase
|
||||
test "convert URLs into anchor tags" do
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com" rel="noreferrer">https://example.com</a></p>),
|
||||
%(<p>Check this: <a href="https://example.com" rel="noopener noreferrer">https://example.com</a></p>),
|
||||
format_html("<p>Check this: https://example.com</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a></p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a></p>),
|
||||
format_html("<p>Check this: https://example.com/</p>")
|
||||
end
|
||||
|
||||
test "don't include punctuation in URL autolinking" do
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>!</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>!</p>),
|
||||
format_html("<p>Check this: https://example.com/!</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>.</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>.</p>),
|
||||
format_html("<p>Check this: https://example.com/.</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>?</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>?</p>),
|
||||
format_html("<p>Check this: https://example.com/?</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>,</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>,</p>),
|
||||
format_html("<p>Check this: https://example.com/,</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>:</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>:</p>),
|
||||
format_html("<p>Check this: https://example.com/:</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>;</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>;</p>),
|
||||
format_html("<p>Check this: https://example.com/;</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>"</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"</p>),
|
||||
format_html("<p>Check this: https://example.com/\"</p>")
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>'</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>'</p>),
|
||||
format_html("<p>Check this: https://example.com/'</p>")
|
||||
|
||||
# trailing entities that decode to punctuation
|
||||
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
|
||||
assert_equal \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a><</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a><</p>),
|
||||
format_html("<p>Check this: https://example.com/<</p>")
|
||||
assert_equal \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>></p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>></p>),
|
||||
format_html("<p>Check this: https://example.com/></p>")
|
||||
assert_equal \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>"</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"</p>),
|
||||
format_html("<p>Check this: https://example.com/"</p>")
|
||||
|
||||
# multiple punctuation characters including entities
|
||||
assert_equal_html \
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noreferrer">https://example.com/</a>!?;</p>),
|
||||
%(<p>Check this: <a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>!?;</p>),
|
||||
format_html("<p>Check this: https://example.com/!?;</p>")
|
||||
assert_equal_html \
|
||||
%(<img src="<a href="https://example.com/" rel="noreferrer">https://example.com/</a>">),
|
||||
%(<img src="<a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>">),
|
||||
format_html(%(<img src="https://example.com/">))
|
||||
assert_equal_html \
|
||||
%(<img src="<a href="https://example.com/" rel="noreferrer">https://example.com/</a>"!>),
|
||||
%(<img src="<a href="https://example.com/" rel="noopener noreferrer">https://example.com/</a>"!>),
|
||||
format_html(%(<img src="https://example.com/"!>))
|
||||
end
|
||||
|
||||
@@ -72,11 +72,11 @@ class HtmlHelperTest < ActionView::TestCase
|
||||
test "handle URLs with query parameters" do
|
||||
# use assert_equal and not assert_equal_html to make sure we're getting entities correct
|
||||
assert_equal \
|
||||
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
||||
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noopener noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
||||
format_html("<p>Check this: https://example.com/a?b=c&d=e</p>")
|
||||
|
||||
assert_equal \
|
||||
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
||||
%(<p>Check this: <a href="https://example.com/a?b=c&d=e" rel="noopener noreferrer">https://example.com/a?b=c&d=e</a></p>),
|
||||
format_html("<p>Check this: https://example.com/a?b=c&d=e</p>")
|
||||
end
|
||||
|
||||
@@ -88,7 +88,7 @@ class HtmlHelperTest < ActionView::TestCase
|
||||
|
||||
test "convert email addresses into mailto links" do
|
||||
assert_equal_html \
|
||||
%(<p>Contact us at <a href="mailto:support@example.com" rel="noreferrer">support@example.com</a></p>),
|
||||
%(<p>Contact us at <a href="mailto:support@example.com" rel="noopener noreferrer">support@example.com</a></p>),
|
||||
format_html("<p>Contact us at support@example.com</p>")
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user