1
1
require "helper"
2
2
3
3
class IntegrationTestAdHoc < Loofah ::TestCase
4
-
5
4
context "blank input string" do
6
5
context "fragment" do
7
6
it "return a blank string" do
@@ -33,9 +32,9 @@ def test_removal_of_illegal_attribute
33
32
html = "<p class=bar foo=bar abbr=bar />"
34
33
sane = Nokogiri ::HTML ( Loofah . scrub_fragment ( html , :escape ) . to_xml )
35
34
node = sane . xpath ( "//p" ) . first
36
- assert node . attributes [ ' class' ]
37
- assert node . attributes [ ' abbr' ]
38
- assert_nil node . attributes [ ' foo' ]
35
+ assert node . attributes [ " class" ]
36
+ assert node . attributes [ " abbr" ]
37
+ assert_nil node . attributes [ " foo" ]
39
38
end
40
39
41
40
def test_removal_of_illegal_url_in_href
@@ -45,14 +44,14 @@ def test_removal_of_illegal_url_in_href
45
44
HTML
46
45
sane = Nokogiri ::HTML ( Loofah . scrub_fragment ( html , :escape ) . to_xml )
47
46
nodes = sane . xpath ( "//a" )
48
- assert_nil nodes . first . attributes [ ' href' ]
49
- assert nodes . last . attributes [ ' href' ]
47
+ assert_nil nodes . first . attributes [ " href" ]
48
+ assert nodes . last . attributes [ " href" ]
50
49
end
51
50
52
51
def test_css_sanitization
53
52
html = "<p style='background-color: url(\" http://foo.com/\" ) ; background-color: #000 ;' />"
54
53
sane = Nokogiri ::HTML ( Loofah . scrub_fragment ( html , :escape ) . to_xml )
55
- assert_match %r/#000/ , sane . inner_html
54
+ assert_match %r/#000/ , sane . inner_html
56
55
refute_match %r/foo\. com/ , sane . inner_html
57
56
end
58
57
@@ -75,7 +74,7 @@ def test_fragment_with_text_nodes_leading_and_trailing
75
74
def test_whitewash_on_fragment
76
75
html = "safe<frameset rows=\" *\" ><frame src=\" http://example.com\" ></frameset> <b>description</b>"
77
76
whitewashed = Loofah . scrub_document ( html , :whitewash ) . xpath ( "/html/body/*" ) . to_s
78
- assert_equal "<p>safe</p><b>description</b>" , whitewashed . gsub ( "\n " , "" )
77
+ assert_equal "<p>safe</p><b>description</b>" , whitewashed . gsub ( "\n " , "" )
79
78
end
80
79
81
80
def test_fragment_whitewash_on_microsofty_markup
@@ -86,11 +85,11 @@ def test_fragment_whitewash_on_microsofty_markup
86
85
def test_document_whitewash_on_microsofty_markup
87
86
whitewashed = Loofah . document ( MSWORD_HTML ) . scrub! ( :whitewash )
88
87
assert_match %r(<p>Foo <b>BOLD</b></p>) , whitewashed . to_s
89
- assert_equal "<p>Foo <b>BOLD</b></p>" , whitewashed . xpath ( "/html/body/*" ) . to_s
88
+ assert_equal "<p>Foo <b>BOLD</b></p>" , whitewashed . xpath ( "/html/body/*" ) . to_s
90
89
end
91
90
92
91
def test_return_empty_string_when_nothing_left
93
- assert_equal "" , Loofah . scrub_document ( ' <script>test</script>' , :prune ) . text
92
+ assert_equal "" , Loofah . scrub_document ( " <script>test</script>" , :prune ) . text
94
93
end
95
94
96
95
def test_nested_script_cdata_tags_should_be_scrubbed
@@ -145,21 +144,20 @@ def test_dont_remove_whitespace_between_tags
145
144
#
146
145
# https://git.gnome.org/browse/libxml2/tree/HTMLtree.c?h=v2.9.2#n714
147
146
#
148
- { tag : "a" , attr : "href" } ,
149
- { tag : "div" , attr : "href" } ,
150
- { tag : "a" , attr : "action" } ,
151
- { tag : "div" , attr : "action" } ,
152
- { tag : "a" , attr : "src" } ,
153
- { tag : "div" , attr : "src" } ,
154
- { tag : "a" , attr : "name" } ,
147
+ { tag : "a" , attr : "href" } ,
148
+ { tag : "div" , attr : "href" } ,
149
+ { tag : "a" , attr : "action" } ,
150
+ { tag : "div" , attr : "action" } ,
151
+ { tag : "a" , attr : "src" } ,
152
+ { tag : "div" , attr : "src" } ,
153
+ { tag : "a" , attr : "name" } ,
155
154
#
156
155
# note that div+name is _not_ affected by the libxml2 issue.
157
156
# but we test it anyway to ensure our logic isn't modifying
158
157
# attributes that don't need modifying.
159
158
#
160
- { tag : "div" , attr : "name" , unescaped : true } ,
159
+ { tag : "div" , attr : "name" , unescaped : true } ,
161
160
] . each do |config |
162
-
163
161
define_method "test_uri_escaping_of_#{ config [ :attr ] } _attr_in_#{ config [ :tag ] } _tag" do
164
162
html = %{<#{ config [ :tag ] } #{ config [ :attr ] } ='examp<!--" unsafeattr=foo()>-->le.com'>test</#{ config [ :tag ] } >}
165
163
@@ -190,14 +188,32 @@ def test_dont_remove_whitespace_between_tags
190
188
end
191
189
end
192
190
193
- # see:
194
- # - https://github.com/flavorjones/loofah/issues/154
195
- # - https://hackerone.com/reports/429267
196
- context "xss protection from svg xmlns:xlink animate attribute" do
197
- it "sanitizes appropriate attributes" do
198
- html = %Q{<svg><a xmlns:xlink=http://www.w3.org/1999/xlink xlink:href=?><circle r=400 /><animate attributeName=xlink:href begin=0 from=javascript:alert(1) to=%26>}
191
+ context "xss protection from svg animate attributes" do
192
+ # see recommendation from https://html5sec.org/#137
193
+ # to sanitize "to", "from", "values", and "by" attributes
194
+
195
+ it "sanitizes 'from', 'to', and 'by' attributes" do
196
+ # for CVE-2018-16468
197
+ # see:
198
+ # - https://github.com/flavorjones/loofah/issues/154
199
+ # - https://hackerone.com/reports/429267
200
+ html = %Q{<svg><a xmlns:xlink=http://www.w3.org/1999/xlink xlink:href=?><circle r=400 /><animate attributeName=xlink:href begin=0 from=javascript:alert(1) to=%26 by=5>}
201
+
199
202
sanitized = Loofah . scrub_fragment ( html , :escape )
200
203
assert_nil sanitized . at_css ( "animate" ) [ "from" ]
204
+ assert_nil sanitized . at_css ( "animate" ) [ "to" ]
205
+ assert_nil sanitized . at_css ( "animate" ) [ "by" ]
206
+ end
207
+
208
+ it "sanitizes 'values' attribute" do
209
+ # for CVE-2019-15587
210
+ # see:
211
+ # - https://github.com/flavorjones/loofah/issues/171
212
+ # - https://hackerone.com/reports/709009
213
+ html = %Q{<svg> <animate href="#foo" attributeName="href" values="javascript:alert('xss')"/> <a id="foo"> <circle r=400 /> </a> </svg>}
214
+
215
+ sanitized = Loofah . scrub_fragment ( html , :escape )
216
+ assert_nil sanitized . at_css ( "animate" ) [ "values" ]
201
217
end
202
218
end
203
219
end
0 commit comments