/
test_clean_element.rb
545 lines (447 loc) · 20.1 KB
/
test_clean_element.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# encoding: utf-8
require_relative 'common'
describe 'Sanitize::Transformers::CleanElement' do
make_my_diffs_pretty!
parallelize_me!
strings = {
:basic => {
:html => '<b>Lo<!-- comment -->rem</b> <a href="pants" title="foo" style="text-decoration: underline;">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <style>.foo { color: #fff; }</style> <script>alert("hello world");</script>',
:default => 'Lorem ipsum dolor sit amet ',
:restricted => '<b>Lorem</b> ipsum <strong>dolor</strong> sit amet ',
:basic => '<b>Lorem</b> <a href="pants" rel="nofollow">ipsum</a> <a href="http://foo.com/" rel="nofollow"><strong>dolor</strong></a> sit<br>amet ',
:relaxed => '<b>Lorem</b> <a href="pants" title="foo" style="text-decoration: underline;">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet <style>.foo { color: #fff; }</style> '
},
:malformed => {
:html => 'Lo<!-- comment -->rem</b> <a href=pants title="foo>ipsum <a href="http://foo.com/"><strong>dolor</a></strong> sit<br/>amet <script>alert("hello world");',
:default => 'Lorem dolor sit amet ',
:restricted => 'Lorem <strong>dolor</strong> sit amet ',
:basic => 'Lorem <a href="pants" rel="nofollow"><strong>dolor</strong></a> sit<br>amet ',
:relaxed => 'Lorem <a href="pants" title="foo>ipsum <a href="><strong>dolor</strong></a> sit<br>amet ',
},
:unclosed => {
:html => '<p>a</p><blockquote>b',
:default => ' a b ',
:restricted => ' a b ',
:basic => '<p>a</p><blockquote>b</blockquote>',
:relaxed => '<p>a</p><blockquote>b</blockquote>'
},
:malicious => {
:html => '<b>Lo<!-- comment -->rem</b> <a href="javascript:pants" title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br/>amet <<foo>script>alert("hello world");</script>',
:default => 'Lorem ipsum dolor sit amet <script>alert("hello world");',
:restricted => '<b>Lorem</b> ipsum <strong>dolor</strong> sit amet <script>alert("hello world");',
:basic => '<b>Lorem</b> <a rel="nofollow">ipsum</a> <a href="http://foo.com/" rel="nofollow"><strong>dolor</strong></a> sit<br>amet <script>alert("hello world");',
:relaxed => '<b>Lorem</b> <a title="foo">ipsum</a> <a href="http://foo.com/"><strong>dolor</strong></a> sit<br>amet <script>alert("hello world");'
}
}
protocols = {
'protocol-based JS injection: simple, no spaces' => {
:html => '<a href="javascript:alert(\'XSS\');">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: simple, spaces before' => {
:html => '<a href="javascript :alert(\'XSS\');">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: simple, spaces after' => {
:html => '<a href="javascript: alert(\'XSS\');">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: simple, spaces before and after' => {
:html => '<a href="javascript : alert(\'XSS\');">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: preceding colon' => {
:html => '<a href=":javascript:alert(\'XSS\');">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: UTF-8 encoding' => {
:html => '<a href="javascript:">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: long UTF-8 encoding' => {
:html => '<a href="javascript:">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: long UTF-8 encoding without semicolons' => {
:html => '<a href=javascript:alert('XSS')>foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: hex encoding' => {
:html => '<a href="javascript:">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: long hex encoding' => {
:html => '<a href="javascript:">foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: hex encoding without semicolons' => {
:html => '<a href=javascript:alert('XSS')>foo</a>',
:default => 'foo',
:restricted => 'foo',
:basic => '<a rel="nofollow">foo</a>',
:relaxed => '<a>foo</a>'
},
'protocol-based JS injection: null char' => {
:html => "<img src=java\0script:alert(\"XSS\")>",
:default => '',
:restricted => '',
:basic => '',
:relaxed => '<img>'
},
'protocol-based JS injection: invalid URL char' => {
:html => '<img src=java\script:alert("XSS")>',
:default => '',
:restricted => '',
:basic => '',
:relaxed => '<img>'
},
'protocol-based JS injection: spaces and entities' => {
:html => '<img src="  javascript:alert(\'XSS\');">',
:default => '',
:restricted => '',
:basic => '',
:relaxed => '<img>'
},
'protocol whitespace' => {
:html => '<a href=" http://example.com/"></a>',
:default => '',
:restricted => '',
:basic => '<a href="http://example.com/" rel="nofollow"></a>',
:relaxed => '<a href="http://example.com/"></a>'
}
}
describe 'Default config' do
it 'should remove non-allowlisted elements, leaving safe contents behind' do
Sanitize.fragment('foo <b>bar</b> <strong><a href="#a">baz</a></strong> quux')
.must_equal 'foo bar baz quux'
Sanitize.fragment('<script>alert("<xss>");</script>')
.must_equal ''
Sanitize.fragment('<<script>script>alert("<xss>");</<script>>')
.must_equal '<'
Sanitize.fragment('< script <>> alert("<xss>");</script>')
.must_equal '< script <>> alert("");'
end
it 'should surround the contents of :whitespace_elements with space characters when removing the element' do
Sanitize.fragment('foo<div>bar</div>baz')
.must_equal 'foo bar baz'
Sanitize.fragment('foo<br>bar<br>baz')
.must_equal 'foo bar baz'
Sanitize.fragment('foo<hr>bar<hr>baz')
.must_equal 'foo bar baz'
end
it 'should not choke on several instances of the same element in a row' do
Sanitize.fragment('<img src="http://www.google.com/intl/en_ALL/images/logo.gif"><img src="http://www.google.com/intl/en_ALL/images/logo.gif"><img src="http://www.google.com/intl/en_ALL/images/logo.gif"><img src="http://www.google.com/intl/en_ALL/images/logo.gif">')
.must_equal ''
end
it 'should not preserve the content of removed `iframe` elements' do
Sanitize.fragment('<iframe>hello! <script>alert(0)</script></iframe>')
.must_equal ''
end
it 'should not preserve the content of removed `math` elements' do
Sanitize.fragment('<math>hello! <script>alert(0)</script></math>')
.must_equal ''
end
it 'should not preserve the content of removed `noembed` elements' do
Sanitize.fragment('<noembed>hello! <script>alert(0)</script></noembed>')
.must_equal ''
end
it 'should not preserve the content of removed `noframes` elements' do
Sanitize.fragment('<noframes>hello! <script>alert(0)</script></noframes>')
.must_equal ''
end
it 'should not preserve the content of removed `noscript` elements' do
Sanitize.fragment('<noscript>hello! <script>alert(0)</script></noscript>')
.must_equal ''
end
it 'should not preserve the content of removed `plaintext` elements' do
Sanitize.fragment('<plaintext>hello! <script>alert(0)</script>')
.must_equal ''
end
it 'should not preserve the content of removed `script` elements' do
Sanitize.fragment('<script>hello! <script>alert(0)</script></script>')
.must_equal ''
end
it 'should not preserve the content of removed `style` elements' do
Sanitize.fragment('<style>hello! <script>alert(0)</script></style>')
.must_equal ''
end
it 'should not preserve the content of removed `svg` elements' do
Sanitize.fragment('<svg>hello! <script>alert(0)</script></svg>')
.must_equal ''
end
it 'should not preserve the content of removed `xmp` elements' do
Sanitize.fragment('<xmp>hello! <script>alert(0)</script></xmp>')
.must_equal ''
end
strings.each do |name, data|
it "should clean #{name} HTML" do
Sanitize.fragment(data[:html]).must_equal(data[:default])
end
end
protocols.each do |name, data|
it "should not allow #{name}" do
Sanitize.fragment(data[:html]).must_equal(data[:default])
end
end
end
describe 'Restricted config' do
before do
@s = Sanitize.new(Sanitize::Config::RESTRICTED)
end
strings.each do |name, data|
it "should clean #{name} HTML" do
@s.fragment(data[:html]).must_equal(data[:restricted])
end
end
protocols.each do |name, data|
it "should not allow #{name}" do
@s.fragment(data[:html]).must_equal(data[:restricted])
end
end
end
describe 'Basic config' do
before do
@s = Sanitize.new(Sanitize::Config::BASIC)
end
it 'should not choke on valueless attributes' do
@s.fragment('foo <a href>foo</a> bar')
.must_equal 'foo <a href="" rel="nofollow">foo</a> bar'
end
it 'should downcase attribute names' do
@s.fragment('<a HREF="javascript:alert(\'foo\')">bar</a>')
.must_equal '<a rel="nofollow">bar</a>'
end
strings.each do |name, data|
it "should clean #{name} HTML" do
@s.fragment(data[:html]).must_equal(data[:basic])
end
end
protocols.each do |name, data|
it "should not allow #{name}" do
@s.fragment(data[:html]).must_equal(data[:basic])
end
end
end
describe 'Relaxed config' do
before do
@s = Sanitize.new(Sanitize::Config::RELAXED)
end
it 'should encode special chars in attribute values' do
@s.fragment('<a href="http://example.com" title="<b>éxamples</b> & things">foo</a>')
.must_equal '<a href="http://example.com" title="<b>éxamples</b> & things">foo</a>'
end
strings.each do |name, data|
it "should clean #{name} HTML" do
@s.fragment(data[:html]).must_equal(data[:relaxed])
end
end
protocols.each do |name, data|
it "should not allow #{name}" do
@s.fragment(data[:html]).must_equal(data[:relaxed])
end
end
end
describe 'Custom configs' do
it 'should allow attributes on all elements if allowlisted under :all' do
input = '<p class="foo">bar</p>'
Sanitize.fragment(input).must_equal ' bar '
Sanitize.fragment(input, {
:elements => ['p'],
:attributes => {:all => ['class']}
}).must_equal input
Sanitize.fragment(input, {
:elements => ['p'],
:attributes => {'div' => ['class']}
}).must_equal '<p>bar</p>'
Sanitize.fragment(input, {
:elements => ['p'],
:attributes => {'p' => ['title'], :all => ['class']}
}).must_equal input
end
it "should not allow relative URLs when relative URLs aren't allowlisted" do
input = '<a href="/foo/bar">Link</a>'
Sanitize.fragment(input,
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => ['http']}}
).must_equal '<a>Link</a>'
end
it 'should allow relative URLs containing colons when the colon is not in the first path segment' do
input = '<a href="/wiki/Special:Random">Random Page</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => [:relative]}}
}).must_equal input
end
it 'should allow relative URLs containing colons when the colon is part of an anchor' do
input = '<a href="#fn:1">Footnote 1</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => [:relative]}}
}).must_equal input
input = '<a href="somepage#fn:1">Footnote 1</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => [:relative]}}
}).must_equal input
end
it 'should remove the contents of filtered nodes when :remove_contents is true' do
Sanitize.fragment('foo bar <div>baz<span>quux</span></div>',
:remove_contents => true
).must_equal 'foo bar '
end
it 'should remove the contents of specified nodes when :remove_contents is an Array or Set of element names as strings' do
Sanitize.fragment('foo bar <div>baz<span>quux</span> <b>hi</b><script>alert("hello!");</script></div>',
:remove_contents => ['script', 'span']
).must_equal 'foo bar baz hi '
Sanitize.fragment('foo bar <div>baz<span>quux</span> <b>hi</b><script>alert("hello!");</script></div>',
:remove_contents => Set.new(['script', 'span'])
).must_equal 'foo bar baz hi '
end
it 'should remove the contents of specified nodes when :remove_contents is an Array or Set of element names as symbols' do
Sanitize.fragment('foo bar <div>baz<span>quux</span> <b>hi</b><script>alert("hello!");</script></div>',
:remove_contents => [:script, :span]
).must_equal 'foo bar baz hi '
Sanitize.fragment('foo bar <div>baz<span>quux</span> <b>hi</b><script>alert("hello!");</script></div>',
:remove_contents => Set.new([:script, :span])
).must_equal 'foo bar baz hi '
end
it 'should remove the contents of allowlisted iframes' do
Sanitize.fragment('<iframe>hi <script>hello</script></iframe>',
:elements => ['iframe']
).must_equal '<iframe></iframe>'
end
it 'should not allow arbitrary HTML5 data attributes by default' do
Sanitize.fragment('<b data-foo="bar"></b>',
:elements => ['b']
).must_equal '<b></b>'
Sanitize.fragment('<b class="foo" data-foo="bar"></b>',
:attributes => {'b' => ['class']},
:elements => ['b']
).must_equal '<b class="foo"></b>'
end
it 'should allow arbitrary HTML5 data attributes when the :attributes config includes :data' do
s = Sanitize.new(
:attributes => {'b' => [:data]},
:elements => ['b']
)
s.fragment('<b data-foo="valid" data-bar="valid"></b>')
.must_equal '<b data-foo="valid" data-bar="valid"></b>'
s.fragment('<b data-="invalid"></b>')
.must_equal '<b></b>'
s.fragment('<b data-="invalid"></b>')
.must_equal '<b></b>'
s.fragment('<b data-xml="invalid"></b>')
.must_equal '<b></b>'
s.fragment('<b data-xmlfoo="invalid"></b>')
.must_equal '<b></b>'
s.fragment('<b data-f:oo="valid"></b>')
.must_equal '<b></b>'
s.fragment('<b data-f/oo="partial"></b>')
.must_equal '<b data-f=""></b>' # Nokogiri quirk; not ideal, but harmless
s.fragment('<b data-éfoo="valid"></b>')
.must_equal '<b></b>' # Another annoying Nokogiri quirk.
end
it 'should replace whitespace_elements with configured :before and :after values' do
s = Sanitize.new(
:whitespace_elements => {
'p' => { :before => "\n", :after => "\n" },
'div' => { :before => "\n", :after => "\n" },
'br' => { :before => "\n", :after => "\n" },
}
)
s.fragment('<p>foo</p>').must_equal "\nfoo\n"
s.fragment('<p>foo</p><p>bar</p>').must_equal "\nfoo\n\nbar\n"
s.fragment('foo<div>bar</div>baz').must_equal "foo\nbar\nbaz"
s.fragment('foo<br>bar<br>baz').must_equal "foo\nbar\nbaz"
end
it 'should handle protocols correctly regardless of case' do
input = '<a href="hTTpS://foo.com/">Text</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => ['https']}}
}).must_equal input
input = '<a href="mailto:someone@example.com?Subject=Hello">Text</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => ['href']},
:protocols => {'a' => {'href' => ['https']}}
}).must_equal "<a>Text</a>"
end
it 'should sanitize protocols in data attributes even if data attributes are generically allowed' do
input = '<a data-url="mailto:someone@example.com">Text</a>'
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => [:data]},
:protocols => {'a' => {'data-url' => ['https']}}
}).must_equal "<a>Text</a>"
Sanitize.fragment(input, {
:elements => ['a'],
:attributes => {'a' => [:data]},
:protocols => {'a' => {'data-url' => ['mailto']}}
}).must_equal input
end
it 'should prevent `<meta>` tags from being used to set a non-UTF-8 charset' do
Sanitize.document('<html><head><meta charset="utf-8"></head><body>Howdy!</body></html>',
:elements => %w[html head meta body],
:attributes => {'meta' => ['charset']}
).must_equal "<html><head><meta charset=\"utf-8\"></head><body>Howdy!</body></html>"
Sanitize.document('<html><meta charset="utf-8">Howdy!</html>',
:elements => %w[html meta],
:attributes => {'meta' => ['charset']}
).must_equal "<html><meta charset=\"utf-8\">Howdy!</html>"
Sanitize.document('<html><meta charset="us-ascii">Howdy!</html>',
:elements => %w[html meta],
:attributes => {'meta' => ['charset']}
).must_equal "<html><meta charset=\"utf-8\">Howdy!</html>"
Sanitize.document('<html><meta http-equiv="content-type" content=" text/html; charset=us-ascii">Howdy!</html>',
:elements => %w[html meta],
:attributes => {'meta' => %w[content http-equiv]}
).must_equal "<html><meta http-equiv=\"content-type\" content=\" text/html;charset=utf-8\">Howdy!</html>"
Sanitize.document('<html><meta http-equiv="Content-Type" content="text/plain;charset = us-ascii">Howdy!</html>',
:elements => %w[html meta],
:attributes => {'meta' => %w[content http-equiv]}
).must_equal "<html><meta http-equiv=\"Content-Type\" content=\"text/plain;charset=utf-8\">Howdy!</html>"
end
it 'should not modify `<meta>` tags that already set a UTF-8 charset' do
Sanitize.document('<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"></head><body>Howdy!</body></html>',
:elements => %w[html head meta body],
:attributes => {'meta' => %w[content http-equiv]}
).must_equal "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"></head><body>Howdy!</body></html>"
end
end
end