fix XSS vulnerability when using translation

[CVE-2024-26143]
This commit is contained in:
ooooooo_q 2024-01-05 12:00:02 +09:00 committed by Aaron Patterson
parent a3f3c3e5d6
commit 857f2e4a6a
No known key found for this signature in database
GPG Key ID: 953170BCB4FFAFC6
2 changed files with 54 additions and 1 deletions

View File

@ -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

View File

@ -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 "&lt;tag&gt;", 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 "&lt;tag&gt;", 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