Add rel="noopener" to all external links with a target attribute (#37713).

Patch by Jens Krämer.


git-svn-id: https://svn.redmine.org/redmine/trunk@21849 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Marius Balteanu
2022-09-26 22:34:44 +00:00
parent f733508d8a
commit 0bb977cb41
2 changed files with 11 additions and 0 deletions

View File

@@ -43,6 +43,12 @@ module Redmine
klass, klass,
(scheme == "mailto" ? "email" : "external") (scheme == "mailto" ? "email" : "external")
].compact.join " " ].compact.join " "
if node["target"].present? && scheme != "mailto"
rel = node["rel"]&.split || []
rel << "noopener"
node["rel"] = rel.join(" ")
end
end end
doc doc
end end

View File

@@ -50,5 +50,10 @@ if Object.const_defined?(:CommonMarker)
filter(%(<a href="http://example.com/foo#bar#">Malformed URI</a>)) filter(%(<a href="http://example.com/foo#bar#">Malformed URI</a>))
end end
end end
def test_external_links_with_target_get_rel_noopener
assert_equal %(<a target="_blank" href="http://example.net/" class="external" rel="noopener">link</a>), filter(%(<a target="_blank" href="http://example.net/">link</a>))
assert_equal %(<a target="_blank" href="http://example.net/" rel="nofollow noopener" class="external">link</a>), filter(%(<a target="_blank" href="http://example.net/" rel="nofollow">link</a>))
end
end end
end end