Skip to content

Commit b21a2d0

Browse files
committed
Escape $ in javascript_escape but reinforce template literals are NOT supported
1 parent c1b46f0 commit b21a2d0

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ use PhoenixHTMLHelpers
190190

191191
* Enhancements
192192
* Allow safe content to be given to label
193-
* Also escale template literals in `javascript_escape/1`
193+
* Also escape template literals in `javascript_escape/1`
194194

195195
* Bug fixes
196196
* Fix deprecation warnings to point to the correct alternative

lib/phoenix_html.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ defmodule Phoenix.HTML do
329329
$("#container").append("<%= javascript_escape(render("post.html", post: @post)) %>");
330330
331331
It escapes quotes (double and single), double backslashes and others.
332+
333+
> #### Do not inject into template literals {: .error}
334+
>
335+
> Do not inject the result of `javascript_escape` into JavaScript
336+
> template literals (defined with backticks), given template functions
337+
> can change the meaning of the string in unsafe ways.
332338
"""
333339
@spec javascript_escape(binary) :: binary
334340
@spec javascript_escape(safe) :: safe
@@ -353,7 +359,7 @@ defmodule Phoenix.HTML do
353359
defp javascript_escape(<<"\r\n", t::binary>>, acc),
354360
do: javascript_escape(t, <<acc::binary, ?\\, ?n>>)
355361

356-
defp javascript_escape(<<h, t::binary>>, acc) when h in [?", ?', ?\\, ?`],
362+
defp javascript_escape(<<h, t::binary>>, acc) when h in [?", ?', ?\\, ?`, ?$],
357363
do: javascript_escape(t, <<acc::binary, ?\\, h>>)
358364

359365
defp javascript_escape(<<h, t::binary>>, acc) when h in [?\r, ?\n],

test/phoenix_html_test.exs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Phoenix.HTMLTest do
99
assert javascript_escape("\\Double backslash") == "\\\\Double backslash"
1010
assert javascript_escape("\"Double quote\"") == "\\\"Double quote\\\""
1111
assert javascript_escape("'Single quote'") == "\\'Single quote\\'"
12-
assert javascript_escape("`Backtick`") == "\\`Backtick\\`"
12+
assert javascript_escape("`Bac${kt}ick`") == "\\`Bac\\${kt}ick\\`"
1313
assert javascript_escape("New line\r") == "New line\\n"
1414
assert javascript_escape("New line\n") == "New line\\n"
1515
assert javascript_escape("New line\r\n") == "New line\\n"
@@ -134,8 +134,18 @@ defmodule Phoenix.HTMLTest do
134134
end
135135

136136
test "raises on invalid binary attribute name" do
137-
for invalid <- ["", "foo\"bar", "foo'bar", "foo>bar", "foo/bar", "foo=bar",
138-
"foo\tbar", "foo\nbar", "foo\0bar", "foo\x7Fbar"] do
137+
for invalid <- [
138+
"",
139+
"foo\"bar",
140+
"foo'bar",
141+
"foo>bar",
142+
"foo/bar",
143+
"foo=bar",
144+
"foo\tbar",
145+
"foo\nbar",
146+
"foo\0bar",
147+
"foo\x7Fbar"
148+
] do
139149
assert_raise ArgumentError, ~r/expected attribute name/, fn ->
140150
attributes_escape([{invalid, "value"}])
141151
end

0 commit comments

Comments
 (0)