-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathHtmlSanitizerTest.java
More file actions
514 lines (461 loc) · 16.9 KB
/
HtmlSanitizerTest.java
File metadata and controls
514 lines (461 loc) · 16.9 KB
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
// Copyright (c) 2011, Mike Samuel
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the OWASP nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package org.owasp.html;
import junit.framework.TestCase;
import java.util.Arrays;
import javax.annotation.Nullable;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class HtmlSanitizerTest extends TestCase {
@Test
public static final void testEmpty() {
assertEquals("", sanitize(""));
assertEquals("", sanitize(null));
}
@Test
public static final void testSimpleText() {
assertEquals("hello world", sanitize("hello world"));
}
@Test
public static final void testEntities1() {
assertEquals("<hello world>", sanitize("<hello world>"));
}
@Test
public static final void testEntities2() {
assertEquals("<b>hello <i>world</i></b>",
sanitize("<b>hello <i>world</i></b>"));
}
@Test
public static final void testUnknownTagsRemoved() {
assertEquals("<b>hello <i>world</i></b>",
sanitize("<b>hello <bogus></bogus><i>world</i></b>"));
}
@Test
public static final void testUnsafeTagsRemoved() {
assertEquals("<b>hello <i>world</i></b>",
sanitize("<b>hello <i>world</i>"
+ "<script src=foo.js></script></b>"));
}
@Test
public static final void testUnsafeAttributesRemoved() {
assertEquals(
"<b>hello <i>world</i></b>",
sanitize("<b>hello <i onclick=\"takeOverWorld(this)\">world</i></b>"));
}
@Test
public static final void testCruftEscaped() {
assertEquals("<b>hello <i>world<</i></b> & tomorrow the universe",
sanitize(
"<b>hello <i>world<</i></b> & tomorrow the universe"));
}
@Test
public static final void testTagCruftRemoved() {
assertEquals("<b id=\"p-foo\">hello <i>world<</i></b>",
sanitize("<b id=\"foo\" / -->hello <i>world<</i></b>"));
}
@Test
public static final void testIdsAndClassesPrefixed() {
assertEquals(
"<b id=\"p-foo\" class=\"p-boo p-bar p-baz\">"
+ "hello <i>world<</i></b>",
sanitize(
"<b id=\"foo\" class=\"boo bar baz\">hello <i>world<</i></b>"));
}
@Test
public static final void testSpecialCharsInAttributes() {
assertEquals(
"<b title=\"a<b && c>b\">bar</b>",
sanitize("<b title=\"a<b && c>b\">bar</b>"));
}
@Test
public static final void testUnclosedTags() {
assertEquals("<div id=\"p-foo\">Bar<br />Baz</div>",
sanitize("<div id=\"foo\">Bar<br>Baz"));
}
@Test
public static final void testUnopenedTags() {
assertEquals("Foo<b>Bar</b>Baz",
sanitize("Foo<b></select>Bar</b></b>Baz</select>"));
}
@Test
public static final void testUnsafeEndTags() {
assertEquals(
"",
sanitize(
"</meta http-equiv=\"refesh\""
+ " content=\"1;URL=http://evilgadget.com\">"));
}
@Test
public static final void testEmptyEndTags() {
assertEquals("<input />", sanitize("<input></input>"));
}
@Test
public static final void testOnLoadStripped() {
assertEquals(
"<img />",
sanitize("<img src=http://foo.com/bar ONLOAD=alert(1)>"));
}
@Test
public static final void testClosingTagParameters() {
assertEquals(
"<p>Hello world</p>",
sanitize("<p>Hello world</b style=\"width:expression(alert(1))\">"));
}
@Test
public static final void testOptionalEndTags() {
// Should not be
// "<ol> <li>A</li> <li>B<li>C </li></li></ol>"
// The difference is significant because in the first, the item contains no
// space after 'A", but in the third, the item contains 'C' and a space.
assertEquals(
"<ol><li>A</li><li>B</li><li>C </li></ol>",
sanitize("<ol> <li>A</li> <li>B<li>C </ol>"));
}
@Test
public static final void testFoldingOfHtmlAndBodyTags() {
assertEquals(
"<p>P 1</p>",
sanitize("<html><head><title>Foo</title></head>"
+ "<body><p>P 1</p></body></html>"));
assertEquals(
"Hello",
sanitize("<body bgcolor=\"blue\">Hello</body>"));
assertEquals(
"<p>Foo</p><p>One</p><p>Two</p>Three<p>Four</p>",
sanitize(
"<html>"
+ "<head>"
+ "<title>Blah</title>"
+ "<p>Foo</p>"
+ "</head>"
+ "<body>"
+ "<p>One"
+ "<p>Two</p>"
+ "Three"
+ "<p>Four</p>"
+ "</body>"
+ "</html>"));
}
@Test
public static final void testEmptyAndValuelessAttributes() {
assertEquals(
"<input checked=\"checked\" type=\"checkbox\" id=\"\" class=\"\" />",
sanitize("<input checked type=checkbox id=\"\" class=>"));
}
@Test
public final void testAllowedAttributes() {
assertEquals(
"<div __foo=\"__foo\" __bar=\"foo\" foo-bar=\"foo-bar\"></div>",
sanitize("<div __foo __bar=\"foo\" foo-bar></div>"));
}
@Test
public static final void testSgmlShortTags() {
// We make no attempt to correctly handle SGML short tags since they are
// not implemented consistently across browsers, and have been removed from
// HTML 5.
//
// According to http://www.w3.org/QA/2007/10/shorttags.html
// Shorttags - the odd side of HTML 4.01
// ...
// It uses an ill-known feature of SGML called shorthand markup, which
// was authorized in HTML up to HTML 4.01. But what used to be a "cool"
// feature for SGML experts becomes a liability in HTML, where the
// construct is more likely to appear as a typo than as a conscious
// choice.
//
// All could be fine if this form typo-that-happens-to-be-legal was
// properly implemented in contemporary HTML user-agents. It is not.
assertEquals("<p></p>", sanitize("<p/b/")); // Short-tag discarded.
assertEquals("<p></p>", sanitize("<p<b>")); // Discard <b attribute
assertEquals(
// This behavior for short tags is not ideal, but it is safe.
"<p href=\"/\">first part of the text</> second part</p>",
sanitize("<p<a href=\"/\">first part of the text</> second part"));
}
@Test
public static final void testNul() {
assertEquals(
"<a title="
+ "\"harmless SCRIPT=javascript:alert(1) ignored=ignored\">"
+ "</a>",
sanitize(
"<A TITLE="
+ "\"harmless\0 SCRIPT=javascript:alert(1) ignored=ignored\">"
));
}
@Test
public static final void testDigitsInAttrNames() {
// See bug 614 for details.
assertEquals(
"<div>Hello</div>",
sanitize(
"<div style1=\"expression(\'alert(1)\")\">Hello</div>"
));
}
@Test
public static final void testSupplementaryCodepointEncoding()
{
// �� is not appropriate.
// 冬 is appropriate as is the unencoded form.
assertEquals(
"冬 | 冬 | 冬",
sanitize("冬 | \ud87e\udc1a | ��"));
}
@Test
public static final void testDeeplyNestedTagsDoS() {
String sanitized = sanitize(stringRepeatedTimes("<div>", 20000));
int n = sanitized.length() / "<div></div>".length();
assertTrue("" + n, 50 <= n && n <= 1000);
int middle = n * "<div>".length();
assertEquals(sanitized.substring(0, middle),
stringRepeatedTimes("<div>", n));
assertEquals(sanitized.substring(middle),
stringRepeatedTimes("</div>", n));
}
@Test
public static final void testInnerHTMLIE8() {
// Apparently, in quirks mode, IE8 does a poor job producing innerHTML
// values. Given
// <div attr="``foo=bar">
// we encode ` but if JavaScript does:
// nodeA.innerHTML = nodeB.innerHTML;
// and nodeB contains the DIV above, then IE8 will produce
// <div attr=``foo=bar>
// as the value of nodeB.innerHTML and assign it to nodeA.
// IE8's HTML parser treats `` as a blank attribute value and foo=bar
// becomes a separate attribute.
// Adding a space at the end of the attribute prevents this by forcing
// IE8 to put double quotes around the attribute when computing
// nodeB.innerHTML.
assertEquals(
"<div title=\"``onmouseover=alert(1337) \"></div>",
sanitize("<div title=\"``onmouseover=alert(1337)\">"));
}
@Test
public static final void testNabobsOfNegativism() {
// Treating <noscript> as raw-text gains us nothing security-wise
// and we don't want to push tag content outside.
assertEquals("<noscript></noscript>",
sanitize("<noscript><evil></noscript>"));
assertEquals("<noscript>I <b><3</b> Ponies</noscript>",
sanitize("<noscript>I <b><3</b> Ponies</noscript>"));
assertEquals("<noscript>I <b><3</b> Ponies</noscript>",
sanitize("<NOSCRIPT>I <b><3</b> Ponies</noscript><evil>"));
assertEquals("<noframes>I <b><3</b> Ponies</noframes>",
sanitize("<noframes>I <b><3</b> Ponies</noframes><evil>"));
assertEquals("<noembed>I <b><3</b> Ponies</noembed>",
sanitize("<noembed>I <b><3</b> Ponies</noembed><evil>"));
assertEquals("<noxss>I <b><3</b> Ponies</noxss>",
sanitize("<noxss>I <b><3</b> Ponies</noxss><evil>"));
assertEquals(
"<noscript>I <b><3</b> Ponies</noscript>",
sanitize("<xmp><noscript>I <b><3</b> Ponies</noscript></xmp>"));
}
@Test
public static final void testNULs() {
assertEquals("<b>Hello, </b>", sanitize("<b>Hello, \u0000</b>"));
assertEquals("<b>Hello, </b>", sanitize("<b>Hello, \u0000"));
assertEquals("", sanitize("\u0000"));
assertEquals("<b>Hello, </b>", sanitize("<b>Hello, �</b>"));
assertEquals("", sanitize("�"));
}
@Test
public static final void testQMarkMeta() {
assertEquals(
"Hello, <b>World</b>!",
sanitize(
""
// An XML Prologue.
// HTML5 treats it as ignorable content via the bogus comment state.
+ "<?xml version=\"1\" ?>"
+ "Hello, "
// An XML Processing instruction.
// HTML5 treats it as ignorable content via the bogus comment state.
+ "<?processing instruction?>"
+ "<b>World"
// Appears in HTML copied from outlook.
+ "<?xml:namespace prefix = o ns = "
+ "\"urn:schemas-microsoft-com:office:office\" />"
+ "</b>!"));
}
@Test
public static final void testScriptInIframe() {
assertEquals(
"<iframe></iframe>",
sanitize(
"<iframe>\n"
+ " <script>alert(Hi)</script>\n"
+ "</iframe>"));
}
@Test
public static final void testBalancingOfEmptyTags() {
assertEquals(
"<span style=\"color:rgb( 72 , 72 , 72 );font-family:'helveticaneue'\">"
+ " "
+ "my \u00A0"
+ " list of style names or a "
+ "</span>",
sanitize(
"<span style=\"color:rgb(72, 72, 72); font-family:helveticaneue\">"
+ " "
+ "<span>my </span>"
+ " list of style names or a "
+ "</span>"));
}
@Test
public static final void testDuplicateAttributes() {
assertEquals(
sanitize("<br id=\"foo\">"),
sanitize("<br id=foo id=bar>"));
}
@Test
public static final void testNbsps() {
String input =
"test bob";
PolicyFactory policy = new HtmlPolicyBuilder()
.toFactory();
String got = policy.sanitize(input);
int[] codeUnits = new int[got.length()];
for (int i = 0, n = got.length(); i < n; ++i) {
codeUnits[i] = got.charAt(i);
}
assertTrue(
Arrays.toString(codeUnits),
Arrays.equals(
new int[] {
116, 101, 115, 116,
160, 160, 160, 160, 160, 160, 160, 160,
98, 111, 98,
},
codeUnits));
}
@Test
public static final void testMacOSAndIOSQueryOfDeath() {
// https://manishearth.github.io/blog/2018/02/15/picking-apart-the-crashing-ios-string/
String[][] tests = {
{
"\u0C1C\u0C4D\u0C1E\u200C\u0C3E",
"\u0C1C\u0C4D\u0C1E\u0C3E",
},
{
"\u09B8\u09CD\u09B0<interrupted>\u200C\u09C1",
"\u09B8\u09CD\u09B0\u09C1",
},
{
"\u0C1C\u0C4D\u0C1E\u200C\u0C3E",
"\u0C1C\u0C4D\u0C1E\u0C3E",
},
{
"\u09B8\u09CD\u09B0\u200C<interrupted>\u09C1",
"\u09B8\u09CD\u09B0\u09C1",
},
{
"జ్ఞ‌ా",
"\u0C1C\u0C4D\u0C1E\u0C3E",
},
{
"జ్ఞ<interrupted>‌ా",
"\u0C1C\u0C4D\u0C1E\u0C3E",
},
{
"স্র‌ু",
"\u09B8\u09CD\u09B0\u09C1",
},
{
"স্র‌<interrupted>ু",
"\u09B8\u09CD\u09B0\u09C1",
},
{
"\u0915\u094D\u0930\u200C\u093E",
"\u0915\u094D\u0930\u093E",
},
};
for (int i = 0, n = tests.length; i < n; ++i) {
String[] test = tests[i];
assertEquals(i + " : " + test[0], test[1], sanitize(test[0]));
}
}
@Test
public static final void testIssue254SemicolonlessNamedCharactersInUrls() {
String input = "<a href=\"/test/?param1=valueOne¶m2=valueTwo\">click me</a>";
String want = "<a href=\"/test/?param1=valueOne&param2=valueTwo\">click me</a>";
assertEquals(want, sanitize(input));
}
@Test
public static final void testStylingCornerCase() {
String input = "<a style=\\006-\\000038";
String want = "";
assertEquals(want, sanitize(input));
}
private static String sanitize(@Nullable String html) {
StringBuilder sb = new StringBuilder();
HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
sb,
new Handler<String>() {
public void handle(String errorMessage) {
fail(errorMessage);
}
});
HtmlSanitizer.Policy policy = new HtmlPolicyBuilder()
// Allow these tags.
.allowElements(
"a", "b", "br", "div", "i", "iframe", "img", "input", "li",
"ol", "p", "span", "ul", "noscript", "noframes", "noembed", "noxss")
// And these attributes.
.allowAttributes(
"dir", "checked", "class", "href", "id", "target", "title", "type",
"__foo", "__bar", "foo-bar")
.globally()
// Cleanup IDs and CLASSes and prefix them with p- to move to a separate
// name-space.
.allowAttributes("id", "class")
.matching(
new AttributePolicy() {
public String apply(
String elementName, String attributeName, String value) {
return value.replaceAll("(?:^|\\s)([a-zA-Z])", " p-$1")
.replaceAll("\\s+", " ")
.trim();
}
})
.globally()
.allowStyling()
// Don't throw out useless <img> and <input> elements to ease debugging.
.allowWithoutAttributes("img", "input")
.build(renderer);
HtmlSanitizer.sanitize(html, policy);
return sb.toString();
}
private static final String stringRepeatedTimes(String s, int n) {
StringBuilder sb = new StringBuilder(s.length() * n);
for (int nToAppend = n; --nToAppend >= 0;) {
sb.append(s);
}
return sb.toString();
}
}