Skip to content

Commit

Permalink
Protect against reverse tabnabbing (#19)
Browse files Browse the repository at this point in the history
* Protect against reverse tabnabbing

https://owasp.org/www-community/attacks/Reverse_Tabnabbing

* Use faster regular expression method
  • Loading branch information
n00dle committed Dec 9, 2020
1 parent 4c5cffe commit 184b60d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/text_helpers/translation.rb
Expand Up @@ -12,7 +12,7 @@ def link(link, title, content)
attributes = [
("href=\"#{link}\"" if link),
("title=\"#{title}\"" if title),
("target=\"_blank\"" if link =~ PROTOCOL_MATCHER),
("target=\"_blank\" rel=\"noopener\"" if link.match?(PROTOCOL_MATCHER)),
]

"<a #{attributes.compact.join(" ")}>#{content}</a>"
Expand Down Expand Up @@ -44,7 +44,7 @@ def text(key, options = {})
interpolation_options = { cascade: true }.merge(options)

# Interpolate any keypaths (e.g., `!some.lookup.path/key!`) found in the text.
while text =~ KEYPATH_MATCHER do
while text.match?(KEYPATH_MATCHER) do
text = text.gsub(KEYPATH_MATCHER) { |match| I18n.t($1, **interpolation_options) }
end

Expand Down
6 changes: 3 additions & 3 deletions test/lib/text_helpers/translation_test.rb
Expand Up @@ -119,12 +119,12 @@
assert_equal "<em>#{@scoped_text}</em>\n", @helper.html(:test_key, inline: true, orphans: true)
end

it "renders internal links without a target" do
it "renders internal links without a target or rel" do
assert_equal "<a href=\"/internal/path\">Internal&nbsp;link</a>\n", @helper.html(:internal_link, inline: true)
end

it "renders external links with target='_blank'" do
assert_equal "<a href=\"http://external.com\" target=\"_blank\">External&nbsp;link</a>\n", @helper.html(:external_link, inline: true)
it "renders external links with target='_blank' and rel='noopener'" do
assert_equal "<a href=\"http://external.com\" target=\"_blank\" rel=\"noopener\">External&nbsp;link</a>\n", @helper.html(:external_link, inline: true)
end

it "interpolates values wrapped in !!" do
Expand Down

0 comments on commit 184b60d

Please sign in to comment.