Skip to content

Commit 1251d88

Browse files
committed
Fix possible XSS vector in JS escape helper
This commit escapes dollar signs and backticks to prevent JS XSS issues when using the `j` or `javascript_escape` helper CVE-2020-5267
1 parent f33d52c commit 1251d88

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

actionview/lib/action_view/helpers/javascript_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module JavaScriptHelper
1212
"\n" => '\n',
1313
"\r" => '\n',
1414
'"' => '\\"',
15-
"'" => "\\'"
15+
"'" => "\\'",
16+
"`" => "\\`",
17+
"$" => "\\$"
1618
}
1719

1820
JS_ESCAPE_MAP[(+"\342\200\250").force_encoding(Encoding::UTF_8).encode!] = "
"
@@ -29,7 +31,7 @@ def escape_javascript(javascript)
2931
if javascript.empty?
3032
result = ""
3133
else
32-
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] }
34+
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u) { |match| JS_ESCAPE_MAP[match] }
3335
end
3436
javascript.html_safe? ? result.html_safe : result
3537
end

actionview/test/template/javascript_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def test_escape_javascript
3636
assert_equal %(dont <\\/close> tags), j(%(dont </close> tags))
3737
end
3838

39+
def test_escape_backtick
40+
assert_equal "\\`", escape_javascript("`")
41+
end
42+
43+
def test_escape_dollar_sign
44+
assert_equal "\\$", escape_javascript("$")
45+
end
46+
3947
def test_escape_javascript_with_safebuffer
4048
given = %('quoted' "double-quoted" new-line:\n </closed>)
4149
expect = %(\\'quoted\\' \\"double-quoted\\" new-line:\\n <\\/closed>)

0 commit comments

Comments
 (0)