-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test-sanitizer.js
514 lines (461 loc) · 18.1 KB
/
test-sanitizer.js
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 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {sanitizeHtml, sanitizeTagsForTripleMustache} from '../../src/sanitizer';
let sanitize;
let html;
describe('Caja-based', () => {
beforeEach(() => {
html = document.createElement('html');
const documentEl = {documentElement: html};
sanitize = (html) => sanitizeHtml(html, documentEl);
});
runSanitizerTests();
describe('Caja-specific sanitization', () => {
// DOMPurify doesn't perform HTML4-specific sanitizations.
it('should apply html4/caja restrictions', () => {
expect(sanitize('a<dialog>b</dialog>c')).to.be.equal('ac');
expect(sanitize('a<dialog>b<img>d</dialog>c')).to.be.equal('ac');
expect(sanitize('<div class="c" src="d">b</div>')).to.be.equal(
'<div class="c" src="">b</div>'
);
});
// DOMPurify doesn't do special whitespace handling in attribute values.
it('should catch attribute value whitespace variations', () => {
allowConsoleError(() => {
expect(
sanitize('a<a href=" j\na\tv\ra s�cript:alert">b</a>')
).to.be.equal('a<a target="_top">b</a>');
});
});
// DOMPurify URL encodes attr chars e.g. `<` -> %3C`.
it('should ignore invalid characters in attributes', () => {
allowConsoleError(() => {
expect(sanitize('a<a href="<script">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href="</script">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a [onclick]="alert">b</a>')).to.be.equal(
'a<a>b</a>'
);
});
});
});
describe('for <amp-bind>', () => {
it('should output [text] and [class] attributes', () => {
expect(sanitize('<p [text]="foo" [class]="bar"></p>')).to.be.equal(
'<p [text]="foo" [class]="bar" i-amphtml-binding=""></p>'
);
});
it('should add "i-amphtml-binding" for data-amp-bind-*', () => {
expect(sanitize('<p data-amp-bind-text="foo"></p>')).to.be.equal(
'<p data-amp-bind-text="foo" i-amphtml-binding=""></p>'
);
});
it('should NOT rewrite values of binding attributes', () => {
// Should not change "foo.bar". Adding `target` attribute is not necessary
// (but harmless) since <amp-bind> will use rewriteAttributesForElement().
expect(sanitize('<a [href]="foo.bar">link</a>')).to.equal(
'<a [href]="foo.bar" target="_top" i-amphtml-binding="">link</a>'
);
});
});
});
function runSanitizerTests() {
describe('sanitizeHtml', () => {
/**
* @param {string} html
* @return {!NodeList}
*/
function serialize(html) {
const div = document.createElement('div');
div.innerHTML = html;
return div.childNodes;
}
/**
* @param {!NodeList} a
* @param {!NodeList} b
*/
function expectEqualNodeLists(a, b) {
expect(a.length).to.equal(b.length);
for (let i = 0; i < a.length; i++) {
expect(a[i].isEqualNode(b[i])).to.be.true;
}
}
it('should output basic text', () => {
expect(sanitize('abc')).to.be.equal('abc');
});
it('should output valid markup', () => {
expect(sanitize('<h1>abc</h1>')).to.be.equal('<h1>abc</h1>');
expect(sanitize('<h1>a<i>b</i>c</h1>')).to.be.equal(
'<h1>a<i>b</i>c</h1>'
);
expect(sanitize('<h1>a<i>b</i><br>c</h1>')).to.be.equal(
'<h1>a<i>b</i><br>c</h1>'
);
expect(
sanitize(
'<h1>a<i>b</i>c' +
'<amp-img src="http://example.com/1.png"></amp-img></h1>'
)
).to.be.equal(
'<h1>a<i>b</i>c' +
'<amp-img src="http://example.com/1.png"></amp-img></h1>'
);
});
it('should NOT output security-sensitive markup', () => {
expect(sanitize('a<script>b</script>c')).to.be.equal('ac');
expect(sanitize('a<script>b<img>d</script>c')).to.be.equal('ac');
expect(sanitize('a<img>c')).to.be.equal('ac');
expect(sanitize('a<iframe></iframe>c')).to.be.equal('ac');
expect(sanitize('a<frame></frame>c')).to.be.equal('ac');
expect(sanitize('a<video></video>c')).to.be.equal('ac');
expect(sanitize('a<audio></audio>c')).to.be.equal('ac');
expect(sanitize('a<applet></applet>c')).to.be.equal('ac');
expect(sanitize('a<link>c')).to.be.equal('ac');
expect(sanitize('a<meta>c')).to.be.equal('ac');
});
it('should NOT output security-sensitive markup when nested', () => {
expect(sanitize('a<script><style>b</style></script>c')).to.be.equal('ac');
expect(sanitize('a<style><iframe>b</iframe></style>c')).to.be.equal('ac');
expect(sanitize('a<script><img></script>c')).to.be.equal('ac');
});
it('should NOT output security-sensitive markup when broken', () => {
expect(sanitize('a<script>bc')).to.be.equal('a');
expect(sanitize('a<SCRIPT>bc')).to.be.equal('a');
});
it('should output "on" attribute', () => {
expect(sanitize('a<a on="tap:AMP.print">b</a>')).to.be.equal(
'a<a on="tap:AMP.print">b</a>'
);
});
it('should output "data-, aria-, and role" attributes', () => {
// Can't use string equality since DOMPurify will reorder attributes.
const actual = serialize(
sanitize('<a aria-label="bar" data-foo="bar" role="button">b</a>')
);
const expected = serialize(
'<a aria-label="bar" data-foo="bar" role="button">b</a>'
);
expectEqualNodeLists(actual, expected);
});
it('should output "href" attribute', () => {
// Can't use string equality since DOMPurify will reorder attributes.
const actual = serialize(sanitize('a<a href="http://acme.com/">b</a>'));
const expected = serialize(
'a<a target="_top" href="http://acme.com/">b</a>'
);
expectEqualNodeLists(actual, expected);
});
it('should output "rel" attribute', () => {
// Can't use string equality since DOMPurify will reorder attributes.
const actual = serialize(
sanitize('a<a href="http://acme.com/" rel="amphtml">b</a>')
);
const expected = serialize(
'a<a href="http://acme.com/" rel="amphtml" target="_top">b</a>'
);
expectEqualNodeLists(actual, expected);
});
it('should output "layout" attribute', () => {
const img = '<amp-img layout="responsive"></amp-img>';
expect(sanitize(img)).to.equal(img);
});
it('should output "media" attribute', () => {
const img = '<amp-img media="(min-width: 650px)"></amp-img>';
expect(sanitize(img)).to.equal(img);
});
it('should output "sizes" attribute', () => {
const img = '<amp-img sizes="(min-width: 650px) 50vw, 100vw"></amp-img>';
expect(sanitize(img)).to.equal(img);
});
it('should output "heights" attribute', () => {
const img = '<amp-img heights="(min-width:500px) 200px, 80%"></amp-img>';
expect(sanitize(img)).to.equal(img);
});
it('should default target to _top with href', () => {
// Can't use string equality since DOMPurify will reorder attributes.
const actual = serialize(
sanitize('<a href="">a</a><a href="" target="">c</a>')
);
const expected = serialize(
'<a href="" target="_top">a</a><a href="" target="_top">c</a>'
);
expectEqualNodeLists(actual, expected);
});
it('should NOT default target to _top w/o href', () => {
expect(sanitize('<a>b</a><a target="">d</a>')).to.equal(
'<a>b</a><a target="_top">d</a>'
);
});
it('should output a valid target', () => {
expect(
sanitize('<a target="_top">a</a><a target="_blank">b</a>')
).to.equal('<a target="_top">a</a><a target="_blank">b</a>');
});
it('should output a valid target in different case', () => {
expect(
sanitize('<a target="_TOP">a</a><a target="_BLANK">b</a>')
).to.equal('<a target="_top">a</a><a target="_blank">b</a>');
});
it('should override a unallowed target', () => {
expect(
sanitize(
'<a target="_self">_self</a>' +
'<a target="_parent">_parent</a>' +
'<a target="_other">_other</a>' +
'<a target="_OTHER">_OTHER</a>' +
'<a target="other">other</a>'
)
).to.equal(
'<a target="_top">_self</a>' +
'<a target="_top">_parent</a>' +
'<a target="_top">_other</a>' +
'<a target="_top">_OTHER</a>' +
'<a target="_top">other</a>'
);
});
it('should NOT output security-sensitive attributes', () => {
allowConsoleError(() => {
expect(sanitize('a<a onclick="alert">b</a>')).to.be.equal('a<a>b</a>');
expect(sanitize('a<a href="javascript:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href=" JAVASCRIPT:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href="vbscript:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href=" VBSCRIPT:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href="data:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href=" DATA:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href="blob:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(sanitize('a<a href=" BLOB:alert">b</a>')).to.be.equal(
'a<a target="_top">b</a>'
);
expect(
sanitize('a<a href="?__amp_source_origin=foo">b</a>')
).to.be.equal('a<a target="_top">b</a>');
});
});
it('should NOT output denylisted values for class attributes', () => {
allowConsoleError(() => {
expect(sanitize('<p class="i-amphtml-">hello</p>')).to.be.equal(
'<p>hello</p>'
);
expect(sanitize('<p class="i-amphtml-class">hello</p>')).to.be.equal(
'<p>hello</p>'
);
expect(sanitize('<p class="foo-i-amphtml-bar">hello</p>')).to.be.equal(
'<p>hello</p>'
);
});
});
it('should allow amp-subscriptions attributes', () => {
expect(sanitize('<div subscriptions-action="login">link</div>')).to.equal(
'<div subscriptions-action="login">link</div>'
);
expect(
sanitize('<div subscriptions-section="actions">link</div>')
).to.equal('<div subscriptions-section="actions">link</div>');
expect(sanitize('<div subscriptions-actions="">link</div>')).to.equal(
'<div subscriptions-actions="">link</div>'
);
expect(sanitize('<div subscriptions-display="">link</div>')).to.equal(
'<div subscriptions-display="">link</div>'
);
expect(sanitize('<div subscriptions-dialog="">link</div>')).to.equal(
'<div subscriptions-dialog="">link</div>'
);
});
it('should allow source::src with valid protocol', () => {
expect(sanitize('<source src="https://www.foo.com/">')).to.equal(
'<source src="https://www.foo.com/">'
);
});
it('should not allow source::src with invalid protocol', () => {
expect(sanitize('<source src="http://www.foo.com">')).to.equal(
'<source src="">'
);
expect(sanitize('<source src="<script>bad()</script>">')).to.equal(
'<source src="">'
);
});
it('should allow div::template', () => {
expect(sanitize('<div template="my-template-id"></div>')).to.equal(
'<div template="my-template-id"></div>'
);
});
it('should allow form::action-xhr', () => {
expect(
sanitize('<form action-xhr="https://foo.com/bar"></form>')
).to.equal('<form action-xhr="https://foo.com/bar"></form>');
});
it('should allow <amp-form>-related attributes', () => {
expect(sanitize('<div submitting></div>')).to.equal(
'<div submitting=""></div>'
);
expect(sanitize('<div submit-success></div>')).to.equal(
'<div submit-success=""></div>'
);
expect(sanitize('<div submit-error></div>')).to.equal(
'<div submit-error=""></div>'
);
expect(sanitize('<div verify-error></div>')).to.equal(
'<div verify-error=""></div>'
);
expect(
sanitize('<span visible-when-invalid="valueMissing"></span>')
).to.equal('<span visible-when-invalid="valueMissing"></span>');
expect(sanitize('<span validation-for="form1"></span>')).to.equal(
'<span validation-for="form1"></span>'
);
});
it('should allow <amp-lightbox> attributes', () => {
expect(sanitize('<amp-lightbox scrollable></amp-lightbox>')).to.equal(
'<amp-lightbox scrollable=""></amp-lightbox>'
);
});
it('should sanitize invalid attributes', () => {
allowConsoleError(() => {
expect(sanitize('<input type="button">')).to.equal('<input>');
expect(sanitize('<input type="image">')).to.equal('<input>');
});
});
});
describe('should sanitize based on AMP doc format type', () => {
afterEach(() => {
html.removeAttribute('amp4email');
});
it('should allow for input type file and password', () => {
expect(sanitize('<input type="file">')).to.equal('<input type="file">');
expect(sanitize('<input type="password">')).to.equal(
'<input type="password">'
);
});
it('should disallow certain attributes on form for AMP4Email', () => {
html.setAttribute('amp4email', '');
allowConsoleError(() => {
expect(sanitize('<input type="password">')).to.equal('<input>');
expect(sanitize('<input type="file">')).to.equal('<input>');
expect(sanitize('<form name="form-name"></form>')).to.equal(
'<form></form>'
);
expect(sanitize('<amp-anim controls></amp-anim>')).to.equal(
'<amp-anim></amp-anim>'
);
});
});
it('should only allow allowlisted AMP elements in AMP4EMAIL', () => {
html.setAttribute('amp4email', '');
expect(sanitize('<amp-analytics>')).to.equal('');
expect(sanitize('<amp-iframe>')).to.equal('');
expect(sanitize('<amp-list>')).to.equal('');
expect(sanitize('<amp-pixel>')).to.equal('');
expect(sanitize('<amp-twitter>')).to.equal('');
expect(sanitize('<amp-video>')).to.equal('');
expect(sanitize('<amp-youtube>')).to.equal('');
expect(sanitize('<amp-accordion>')).to.equal('<amp-accordion>');
expect(sanitize('<amp-anim>')).to.equal('<amp-anim>');
expect(sanitize('<amp-bind-macro>')).to.equal('<amp-bind-macro>');
expect(sanitize('<amp-carousel>')).to.equal('<amp-carousel>');
expect(sanitize('<amp-fit-text>')).to.equal('<amp-fit-text>');
expect(sanitize('<amp-img>')).to.equal('<amp-img>');
expect(sanitize('<amp-layout>')).to.equal('<amp-layout>');
expect(sanitize('<amp-selector>')).to.equal('<amp-selector>');
expect(sanitize('<amp-sidebar>')).to.equal('<amp-sidebar>');
expect(sanitize('<amp-timeago>')).to.equal('<amp-timeago>');
});
});
describe('sanitizeTagsForTripleMustache', () => {
it('should output basic text', () => {
expect(sanitizeTagsForTripleMustache('abc')).to.be.equal('abc');
});
it('should output valid markup', () => {
expect(sanitizeTagsForTripleMustache('<b>abc</b>')).to.be.equal(
'<b>abc</b>'
);
expect(sanitizeTagsForTripleMustache('<b>ab<br>c</b>')).to.be.equal(
'<b>ab<br>c</b>'
);
expect(sanitizeTagsForTripleMustache('<b>a<i>b</i>c</b>')).to.be.equal(
'<b>a<i>b</i>c</b>'
);
const markupWithClassAttribute = '<p class="some-class">heading</p>';
expect(
sanitizeTagsForTripleMustache(markupWithClassAttribute)
).to.be.equal(markupWithClassAttribute);
const markupWithClassesAttribute =
'<div class="some-class another"><span>heading</span></div>';
expect(
sanitizeTagsForTripleMustache(markupWithClassesAttribute)
).to.be.equal(markupWithClassesAttribute);
const markupParagraph = '<p class="valid-class">paragraph</p>';
expect(sanitizeTagsForTripleMustache(markupParagraph)).to.be.equal(
markupParagraph
);
});
it('should NOT output non-allowlisted markup', () => {
expect(sanitizeTagsForTripleMustache('a<style>b</style>c')).to.be.equal(
'ac'
);
expect(sanitizeTagsForTripleMustache('a<img>c')).to.be.equal('ac');
});
it('should compensate for broken markup', () => {
expect(sanitizeTagsForTripleMustache('<b>a<i>b')).to.be.equal(
'<b>a<i>b</i></b>'
);
});
describe('should sanitize `style` attribute', () => {
it('should allow valid styles', () => {
expect(sanitize('<div style="color:blue">Test</div>')).to.equal(
'<div style="color:blue">Test</div>'
);
});
it('should ignore styles containing `!important`', () => {
allowConsoleError(() => {
expect(
sanitize('<div style="color:blue!important">Test</div>')
).to.equal('<div>Test</div>');
});
});
it('should ignore styles containing `position:fixed`', () => {
allowConsoleError(() => {
expect(sanitize('<div style="position:fixed">Test</div>')).to.equal(
'<div>Test</div>'
);
});
});
it('should ignore styles containing `position:sticky`', () => {
allowConsoleError(() => {
expect(sanitize('<div style="position:sticky">Test</div>')).to.equal(
'<div>Test</div>'
);
});
});
});
});
}