Skip to content

Commit 041c068

Browse files
committed
Escape </ to prevent a style element from being closed prematurely
1 parent a92f21c commit 041c068

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/sanitize/transformers/clean_css.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def call(env)
4848
if css.strip.empty?
4949
node.unlink
5050
else
51+
css.gsub!('</', '<\/')
5152
node.children.unlink
5253
node << Nokogiri::XML::Text.new(css, node.document)
5354
end

test/test_malicious_css.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@
3939
it 'should not allow behaviors' do
4040
_(@s.properties(%[behavior: url(xss.htc);])).must_equal ''
4141
end
42+
43+
describe 'sanitization bypass via CSS at-rule in HTML <style> element' do
44+
before do
45+
@s = Sanitize.new(Sanitize::Config::RELAXED)
46+
end
47+
48+
it 'is not possible to prematurely end a <style> element' do
49+
assert_equal(
50+
%[<style>@media<\\/style><iframe srcdoc='<script>alert(document.domain)<\\/script>'>{}</style>],
51+
@s.fragment(%[<style>@media</sty/**/le><iframe srcdoc='<script>alert(document.domain)</script>'></style>])
52+
)
53+
end
54+
end
4255
end

0 commit comments

Comments
 (0)