mirror of https://github.com/rails/rails
fix XSS vulnerability when using translation
[CVE-2024-26143]
This commit is contained in:
parent
a3f3c3e5d6
commit
857f2e4a6a
|
@ -23,7 +23,25 @@ module AbstractController
|
|||
key = "#{path}.#{action_name}#{key}"
|
||||
end
|
||||
|
||||
ActiveSupport::HtmlSafeTranslation.translate(key, **options)
|
||||
if options[:default]
|
||||
options[:default] = [options[:default]] unless options[:default].is_a?(Array)
|
||||
options[:default] = options[:default].map do |value|
|
||||
value.is_a?(String) ? ERB::Util.html_escape(value) : value
|
||||
end
|
||||
end
|
||||
|
||||
if options[:raise].nil?
|
||||
options[:default] = [] unless options[:default]
|
||||
options[:default] << MISSING_TRANSLATION
|
||||
end
|
||||
|
||||
result = ActiveSupport::HtmlSafeTranslation.translate(key, **options)
|
||||
|
||||
if result == MISSING_TRANSLATION
|
||||
+"translation missing: #{key}"
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
alias :t :translate
|
||||
|
||||
|
@ -32,5 +50,9 @@ module AbstractController
|
|||
I18n.localize(object, **options)
|
||||
end
|
||||
alias :l :localize
|
||||
|
||||
private
|
||||
MISSING_TRANSLATION = -(2**60)
|
||||
private_constant :MISSING_TRANSLATION
|
||||
end
|
||||
end
|
||||
|
|
|
@ -83,6 +83,22 @@ module AbstractController
|
|||
end
|
||||
end
|
||||
|
||||
def test_default_translation_as_safe_html
|
||||
@controller.stub :action_name, :index do
|
||||
translation = @controller.t(".twoz", default: ["<tag>"])
|
||||
assert_equal "<tag>", translation
|
||||
assert_equal true, translation.html_safe?
|
||||
end
|
||||
end
|
||||
|
||||
def test_default_translation_with_raise_as_safe_html
|
||||
@controller.stub :action_name, :index do
|
||||
translation = @controller.t(".twoz", raise: true, default: ["<tag>"])
|
||||
assert_equal "<tag>", translation
|
||||
assert_equal true, translation.html_safe?
|
||||
end
|
||||
end
|
||||
|
||||
def test_localize
|
||||
time, expected = Time.gm(2000), "Sat, 01 Jan 2000 00:00:00 +0000"
|
||||
I18n.stub :localize, expected do
|
||||
|
@ -126,6 +142,21 @@ module AbstractController
|
|||
assert_equal true, translation.html_safe?
|
||||
end
|
||||
end
|
||||
|
||||
def test_translate_marks_translation_with_missing_html_key_as_safe_html
|
||||
@controller.stub :action_name, :index do
|
||||
translation = @controller.t("<tag>.html")
|
||||
assert_equal "translation missing: <tag>.html", translation
|
||||
assert_equal false, translation.html_safe?
|
||||
end
|
||||
end
|
||||
def test_translate_marks_translation_with_missing_nested_html_key_as_safe_html
|
||||
@controller.stub :action_name, :index do
|
||||
translation = @controller.t(".<tag>.html")
|
||||
assert_equal "translation missing: abstract_controller.testing.translation.index.<tag>.html", translation
|
||||
assert_equal false, translation.html_safe?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue