Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML escape interpolated code in filters #770

Merged
merged 1 commit into from Apr 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/haml/filters.rb
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
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