Skip to content

Commit

Permalink
Merge pull request from GHSA-cfjv-5498-mph5
Browse files Browse the repository at this point in the history
Prior to this commit, when a translation key indicated that the
translation text was HTML, the value returned by `I18n.translate` would
always be marked as `html_safe`.  However, the value returned by
`I18n.translate` could be an untrusted value directly from
`options[:default]`.

This commit ensures values directly from `options[:default]` are not
marked as `html_safe`.
  • Loading branch information
jonathanhefner authored Sep 9, 2020
1 parent 5259062 commit e663f08
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion actionview/lib/action_view/helpers/translation_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@ def translate(key, **options)
end
end

html_safe_options[:default] = MISSING_TRANSLATION unless html_safe_options[:default].blank?

translation = I18n.translate(fully_resolved_key, **html_safe_options.merge(raise: i18n_raise))

if translation.respond_to?(:map)
if translation.equal?(MISSING_TRANSLATION)
translated_text = options[:default].first
elsif translation.respond_to?(:map)
translated_text = translation.map { |element| element.respond_to?(:html_safe) ? element.html_safe : element }
else
translated_text = translation.respond_to?(:html_safe) ? translation.html_safe : translation
Expand Down Expand Up @@ -150,6 +154,9 @@ def localize(object, **options)
alias :l :localize

private
MISSING_TRANSLATION = Object.new
private_constant :MISSING_TRANSLATION

def scope_key_by_partial(key)
stringified_key = key.to_s
if stringified_key.start_with?(".")
Expand Down
7 changes: 7 additions & 0 deletions actionview/test/template/translation_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ def test_translate_with_last_default_not_named_html
assert_equal false, translation.html_safe?
end

def test_translate_does_not_mark_unsourced_string_default_as_html_safe
untrusted_string = "<script>alert()</script>"
translation = translate(:"translations.missing", default: [:"translations.missing_html", untrusted_string])
assert_equal untrusted_string, translation
assert_not_predicate translation, :html_safe?
end

def test_translate_with_string_default
translation = translate(:'translations.missing', default: "A Generic String")
assert_equal "A Generic String", translation
Expand Down

0 comments on commit e663f08

Please sign in to comment.