Skip to content

Commit b5aeef5

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 ac30e38 commit b5aeef5

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".dup.force_encoding(Encoding::UTF_8).encode!] = "
"
@@ -26,7 +28,7 @@ module JavaScriptHelper
2628
# $('some_element').replaceWith('<%= j render 'some/element_template' %>');
2729
def escape_javascript(javascript)
2830
if javascript
29-
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"'])/u) { |match| JS_ESCAPE_MAP[match] }
31+
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u) { |match| JS_ESCAPE_MAP[match] }
3032
javascript.html_safe? ? result.html_safe : result
3133
else
3234
""

actionview/test/template/javascript_helper_test.rb

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

35+
def test_escape_backtick
36+
assert_equal "\\`", escape_javascript("`")
37+
end
38+
39+
def test_escape_dollar_sign
40+
assert_equal "\\$", escape_javascript("$")
41+
end
42+
3543
def test_escape_javascript_with_safebuffer
3644
given = %('quoted' "double-quoted" new-line:\n </closed>)
3745
expect = %(\\'quoted\\' \\"double-quoted\\" new-line:\\n <\\/closed>)

0 commit comments

Comments
 (0)