Skip to content

Commit

Permalink
Escape </ to prevent a style element from being closed prematurely
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrove committed Jun 26, 2023
1 parent a92f21c commit 041c068
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/sanitize/transformers/clean_css.rb
Expand Up @@ -48,6 +48,7 @@ def call(env)
if css.strip.empty?
node.unlink
else
css.gsub!('</', '<\/')
node.children.unlink
node << Nokogiri::XML::Text.new(css, node.document)
end
Expand Down
13 changes: 13 additions & 0 deletions test/test_malicious_css.rb
Expand Up @@ -39,4 +39,17 @@
it 'should not allow behaviors' do
_(@s.properties(%[behavior: url(xss.htc);])).must_equal ''
end

describe 'sanitization bypass via CSS at-rule in HTML <style> element' do
before do
@s = Sanitize.new(Sanitize::Config::RELAXED)
end

it 'is not possible to prematurely end a <style> element' do
assert_equal(
%[<style>@media<\\/style><iframe srcdoc='<script>alert(document.domain)<\\/script>'>{}</style>],
@s.fragment(%[<style>@media</sty/**/le><iframe srcdoc='<script>alert(document.domain)</script>'></style>])
)
end
end
end

0 comments on commit 041c068

Please sign in to comment.