Skip to content

Commit

Permalink
HTML escape interpolated code in filters
Browse files Browse the repository at this point in the history
HTML escape any interpolated code if the escape_html option is set.
  • Loading branch information
mattwildig committed Apr 17, 2014
1 parent 800095b commit 8591704
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/haml/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def compile(compiler, text)
if contains_interpolation?(text)
return if options[:suppress_eval]

text = unescape_interpolation(text).gsub(/(\\+)n/) do |s|
text = unescape_interpolation(text, options[:escape_html]).gsub(/(\\+)n/) do |s|
escapes = $1.size
next s if escapes % 2 == 0
"#{'\\' * (escapes - 1)}\n"
Expand Down
9 changes: 7 additions & 2 deletions test/filters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def evaluate(scope, locals, &block)
end
end

test "interpolated code should use be escaped in escape_html is set" do
assert_equal "<script>evil</script>\n",
render(":plain\n \#{'<script>evil</script>'}", :escape_html => true)
end

end

class ErbFilterTest < MiniTest::Unit::TestCase
Expand Down Expand Up @@ -140,8 +145,8 @@ class JavascriptFilterTest < MiniTest::Unit::TestCase
assert_match(/bar/, html)
end

test "should never HTML-escape ampersands" do
html = "<script>\n & < > &\n</script>\n"
test "should never HTML-escape non-interpolated ampersands" do
html = "<script>\n & < > &amp;\n</script>\n"
haml = %Q{:javascript\n & < > \#{"&"}}
assert_equal(html, render(haml, :escape_html => true))
end
Expand Down

0 comments on commit 8591704

Please sign in to comment.