/
dom-processor-test.js
1110 lines (814 loc) · 46.3 KB
/
dom-processor-test.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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var INTERNAL_ATTRS = hammerhead.PROCESSING_INSTRUCTIONS.dom.internal_attributes;
var htmlUtils = hammerhead.utils.html;
var DomProcessor = hammerhead.processors.DomProcessor;
var domProcessor = hammerhead.processors.domProcessor;
var processScript = hammerhead.utils.processing.script.processScript;
var styleProcessor = hammerhead.processors.styleProcessor;
var settings = hammerhead.settings;
var urlUtils = hammerhead.utils.url;
var sharedUrlUtils = hammerhead.sharedUtils.url;
var destLocation = hammerhead.utils.destLocation;
var eventSimulator = hammerhead.sandbox.event.eventSimulator;
var nativeMethods = hammerhead.nativeMethods;
var elementSandbox = hammerhead.sandbox.node.element;
var shadowUI = hammerhead.sandbox.shadowUI;
test('iframe', function () {
var iframe = nativeMethods.createElement.call(document, 'iframe');
var storedAttrName = DomProcessor.getStoredAttrName('sandbox');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-scripts');
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-scripts allow-same-origin');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-scripts');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-same-origin');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-same-origin allow-scripts');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-same-origin');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-scripts allow-same-origin');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-scripts allow-same-origin');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-scripts allow-same-origin');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-same-origin allow-forms');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-same-origin allow-forms allow-scripts');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-same-origin allow-forms');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-scripts allow-forms');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-scripts allow-forms allow-same-origin');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-scripts allow-forms');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-scripts allow-forms allow-same-origin');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-scripts allow-forms allow-same-origin');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-scripts allow-forms allow-same-origin');
nativeMethods.setAttribute.call(iframe, 'sandbox', 'allow-forms');
iframe['hammerhead|element-processed'] = false;
domProcessor.processElement(iframe);
strictEqual(nativeMethods.getAttribute.call(iframe, 'sandbox'), 'allow-forms allow-same-origin allow-scripts');
strictEqual(nativeMethods.getAttribute.call(iframe, storedAttrName), 'allow-forms');
});
test('anchor in iframe', function () {
var iframe = nativeMethods.createElement.call(document, 'iframe');
iframe.id = 'test';
nativeMethods.appendChild.call(document.body, iframe);
var anchor = nativeMethods.createElement.call(document, 'a');
nativeMethods.anchorHrefSetter.call(anchor, '/index.html');
nativeMethods.appendChild.call(document.body, anchor);
var iframeBody = iframe.contentDocument.body;
nativeMethods.elementInnerHTMLSetter.call(iframeBody, '<a href="/index.html"></a>');
domProcessor.processElement(iframeBody.childNodes[0], urlUtils.convertToProxyUrl);
domProcessor.processElement(anchor, urlUtils.convertToProxyUrl);
strictEqual(urlUtils.parseProxyUrl(nativeMethods.anchorHrefGetter.call(iframeBody.childNodes[0])).resourceType, 'i');
ok(!urlUtils.parseProxyUrl(nativeMethods.anchorHrefGetter.call(anchor)).resourceType);
iframe.parentNode.removeChild(iframe);
anchor.parentNode.removeChild(anchor);
});
test('form resoure type if target iframe added on client', function () {
var iframe = document.createElement('iframe');
var form = document.createElement('form');
iframe.name = 'target-iframe';
iframe.src = 'http://localhost:2000/';
nativeMethods.setAttribute.call(form, 'target', 'target-iframe');
form.action = 'http://example.com';
nativeMethods.appendChild.call(document.body, form);
document.body.appendChild(iframe);
strictEqual(urlUtils.parseProxyUrl(nativeMethods.formActionGetter.call(form)).resourceType, 'if');
iframe.parentNode.removeChild(iframe);
form.parentNode.removeChild(form);
});
if (nativeMethods.append) {
test('Element.prototype.append', function () {
var div = document.createElement('div');
var root = shadowUI.getRoot();
document.body.append(div);
strictEqual(nativeMethods.nodeLastChildGetter.call(document.body), root);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(root), div);
var isScriptElementAddedEventRaised = false;
elementSandbox.on(elementSandbox.SCRIPT_ELEMENT_ADDED_EVENT, function () {
isScriptElementAddedEventRaised = true;
});
div.append('text node', document.createTextNode('123'), document.createElement('script'));
ok(isScriptElementAddedEventRaised);
document.body.removeChild(div);
var text = 'foobar';
var span = document.createElement('span');
var p = document.createElement('p');
document.body.append(text, span, p);
strictEqual(nativeMethods.nodeLastChildGetter.call(document.body), root);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(root), p);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(p), span);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(span).data, text);
document.body.removeChild(span.previousSibling);
document.body.removeChild(span);
document.body.removeChild(p);
var onlyText = 'only text';
document.body.append(onlyText);
strictEqual(nativeMethods.nodeLastChildGetter.call(document.body), root);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(root).data, onlyText);
document.body.removeChild(root.previousSibling);
});
test('Element.prototype.append for objects (GH-2730)', function () {
var container = document.createElement('div');
container.append(null);
container.append();
container.append(void 0);
container.append({ test: 'hi' });
container.append(1234);
strictEqual(container.textContent, 'nullundefined[object Object]1234');
});
}
if (nativeMethods.prepend) {
test('Element.prototype.prepend', function () {
var div = document.createElement('div');
document.body.appendChild(div);
var isScriptElementAddedEventRaised = false;
elementSandbox.on(elementSandbox.SCRIPT_ELEMENT_ADDED_EVENT, function () {
isScriptElementAddedEventRaised = true;
});
div.prepend('text node', document.createTextNode('123'), document.createElement('script'));
ok(isScriptElementAddedEventRaised);
document.body.removeChild(div);
});
}
if (nativeMethods.after) {
test('Element.prototype.after', function () {
var parent = document.createElement('div');
var child = document.createElement('p');
parent.appendChild(child);
document.body.appendChild(parent);
var isScriptElementAddedEventRaised = false;
elementSandbox.on(elementSandbox.SCRIPT_ELEMENT_ADDED_EVENT, function () {
isScriptElementAddedEventRaised = true;
});
child.after('text node', document.createElement('script'));
ok(isScriptElementAddedEventRaised);
strictEqual(parent.childNodes[0].tagName.toLowerCase(), 'p');
strictEqual(parent.childNodes[1].data, 'text node');
strictEqual(parent.childNodes[2].tagName.toLowerCase(), 'script');
document.body.removeChild(parent);
});
}
if (nativeMethods.remove) {
test('Element.prototype.remove', function () {
var div = document.createElement('div');
document.body.appendChild(div);
var onElementRemovedCalled = false;
elementSandbox._onElementRemoved = function () {
onElementRemovedCalled = true;
delete elementSandbox._onElementRemoved;
};
div.remove();
ok(onElementRemovedCalled);
strictEqual(nativeMethods.nodeParentNodeGetter.call(div), null);
});
}
test('Element.prototype.insertAdjacentElement', function () {
var isScriptElementAddedEventRaised = false;
elementSandbox.on(elementSandbox.SCRIPT_ELEMENT_ADDED_EVENT, function () {
isScriptElementAddedEventRaised = true;
});
var parent = document.createElement('div');
parent.insertAdjacentElement('beforebegin', document.createElement('script'));
parent.insertAdjacentElement('afterend', document.createElement('script'));
notOk(isScriptElementAddedEventRaised);
var root = shadowUI.getRoot();
var script = document.createElement('script');
document.body.appendChild(parent);
document.body.insertAdjacentElement('beforeend', script);
ok(isScriptElementAddedEventRaised);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(root), script);
strictEqual(nativeMethods.nodePrevSiblingGetter.call(script), parent);
isScriptElementAddedEventRaised = false;
parent.insertAdjacentElement('beforeend', document.createElement('a'));
parent.insertAdjacentElement('afterbegin', document.createElement('script'));
ok(isScriptElementAddedEventRaised);
strictEqual(parent.children[0].tagName.toLowerCase(), 'script');
strictEqual(parent.children[1].tagName.toLowerCase(), 'a');
document.body.removeChild(script);
document.body.removeChild(parent);
});
test('Element.prototype.insertAdjacentText', function () {
var root = shadowUI.getRoot();
var script = document.createElement('script');
script.insertAdjacentText('afterbegin', 'window["insertAdjacentText test data"] = location.host');
document.body.appendChild(script);
strictEqual(window['insertAdjacentText test data'], 'example.com');
document.body.insertAdjacentText('beforeend', 'text before root');
strictEqual(nativeMethods.nodePrevSiblingGetter.call(root).data, 'text before root');
document.body.removeChild(script);
document.body.removeChild(nativeMethods.nodePrevSiblingGetter.call(root));
});
if (nativeMethods.elementReplaceWith) {
test('Element.prototype.replaceWith', function () {
var div = document.createElement('div');
var childDiv = document.createElement('div');
div.appendChild(childDiv);
document.body.appendChild(div);
var script = document.createElement('script');
var isScriptElementAddedEventRaised = false;
elementSandbox.on(elementSandbox.SCRIPT_ELEMENT_ADDED_EVENT, function () {
isScriptElementAddedEventRaised = true;
});
childDiv.replaceWith(script);
ok(isScriptElementAddedEventRaised);
strictEqual(nativeMethods.nodeLastChildGetter.call(div), script);
var anchor2 = document.createElement('a');
var anchor3 = document.createElement('a');
script.replaceWith(anchor2, anchor3);
strictEqual(nativeMethods.nodeFirstChildGetter.call(div), anchor2);
strictEqual(nativeMethods.nodeLastChildGetter.call(div), anchor3);
div.removeChild(div.firstChild);
div.firstChild.replaceWith('Text 0', 'Text 1');
strictEqual(div.childNodes[0].data, 'Text 0');
strictEqual(div.childNodes[1].data, 'Text 1');
div.parentNode.removeChild(div);
});
}
test('comment inside script', function () {
var testScript = function (scriptText) {
var script = nativeMethods.createElement.call(document, 'script');
nativeMethods.scriptTextSetter.call(script, scriptText);
domProcessor.processElement(script);
nativeMethods.appendChild.call(document.head, script);
strictEqual(nativeMethods.getAttribute.call(window.commentTest, 'href'), urlUtils.getProxyUrl('http://domain.com'));
nativeMethods.removeAttribute.call(window.commentTest, 'href');
document.head.removeChild(script);
};
window.commentTest = document.createElement('a');
testScript('<!-- Begin comment\n' + 'window.commentTest.href = "http://domain.com";\n' + '//End comment -->');
testScript('<!-- Begin comment\n' + 'window.commentTest.href = "http://domain.com";\n' + ' -->');
});
test('attribute value', function () {
var html =
'<p class="location test"></p>' +
'<p data-w="dslkfe"></p>' +
'<p ' + hammerhead.DOM_SANDBOX_STORED_ATTR_KEY_PREFIX + 'test="location"></p>' +
'<div id="URL"></div>' +
'<div attr=""></div>' +
'<div data-wrap="{simbols: -904, action: data}"></div>' +
'<span class="Client"></span>' +
'<span test="sdk"></span>' +
'<span id="href"></span>' +
'<div data-src="test"></div>';
var expectedHTML =
'<p class="location test"></p>' +
'<p data-w="dslkfe"></p>' +
'<p ' + hammerhead.DOM_SANDBOX_STORED_ATTR_KEY_PREFIX + 'test="location"></p>' +
'<div id="URL"></div>' +
'<div attr=""></div>' +
'<div data-wrap="{simbols: -904, action: data}"></div>' +
'<span class="Client"></span>' +
'<span test="sdk"></span>' +
'<span id="href"></span>' +
'<div data-src="test"></div>';
var container = nativeMethods.createElement.call(document, 'div');
nativeMethods.elementInnerHTMLSetter.call(container, html);
var elems = container.querySelectorAll('*');
for (var i = 0; i < elems.length; i++)
domProcessor.processElement(elems[i]);
strictEqual(nativeMethods.elementInnerHTMLGetter.call(container), expectedHTML);
strictEqual(container.innerHTML, expectedHTML);
});
test('script src', function () {
var storedSessionId = settings.get().sessionId;
settings.get().sessionId = 'uid';
var script = nativeMethods.createElement.call(document, 'script');
nativeMethods.scriptSrcSetter.call(script, 'http://domain.com');
domProcessor.processElement(script, urlUtils.convertToProxyUrl);
strictEqual(urlUtils.parseProxyUrl(nativeMethods.scriptSrcGetter.call(script)).resourceType, 's');
settings.get().sessionId = storedSessionId;
});
test('event attributes', function () {
var div = nativeMethods.createElement.call(document, 'div');
var attrValue = 'window.location="test";';
var processedValue = processScript(attrValue);
var storedAttrName = DomProcessor.getStoredAttrName('onclick');
notEqual(processedValue, attrValue);
nativeMethods.setAttribute.call(div, 'onclick', attrValue);
domProcessor.processElement(div, function () {
});
strictEqual(nativeMethods.getAttribute.call(div, 'onclick'), processedValue);
strictEqual(nativeMethods.getAttribute.call(div, storedAttrName), attrValue);
});
test('javascript protocol', function () {
var anchor = nativeMethods.createElement.call(document, 'a');
var attrValue = 'javascript:window.location="test";';
var processedValueForUrlAttr = 'javascript:' + processScript(attrValue.replace('javascript:', ''), false, true);
var processedValueForEventAttr = 'javascript:' + processScript(attrValue.replace('javascript:', ''));
notEqual(processedValueForUrlAttr, attrValue);
notEqual(processedValueForEventAttr, attrValue);
nativeMethods.setAttribute.call(anchor, 'onclick', attrValue);
nativeMethods.setAttribute.call(anchor, 'href', attrValue);
domProcessor.processElement(anchor, function () {
});
strictEqual(nativeMethods.getAttribute.call(anchor, 'onclick'), processedValueForEventAttr);
strictEqual(nativeMethods.getAttribute.call(anchor, 'href'), processedValueForUrlAttr);
strictEqual(nativeMethods.getAttribute.call(anchor, DomProcessor.getStoredAttrName('onclick')), attrValue);
strictEqual(nativeMethods.getAttribute.call(anchor, DomProcessor.getStoredAttrName('href')), attrValue);
});
test('autocomplete attribute', function () {
var input1 = nativeMethods.createElement.call(document, 'input');
var input2 = nativeMethods.createElement.call(document, 'input');
var input3 = nativeMethods.createElement.call(document, 'input');
var input4 = nativeMethods.createElement.call(document, 'input');
nativeMethods.setAttribute.call(input1, 'autocomplete', 'on');
nativeMethods.setAttribute.call(input2, 'autocomplete', 'off');
nativeMethods.setAttribute.call(input3, 'autocomplete', '');
domProcessor.processElement(input1);
domProcessor.processElement(input2);
domProcessor.processElement(input3);
domProcessor.processElement(input4);
var storedAutocompleteAttr = DomProcessor.getStoredAttrName('autocomplete');
strictEqual(nativeMethods.getAttribute.call(input1, 'autocomplete'), 'off');
strictEqual(nativeMethods.getAttribute.call(input1, storedAutocompleteAttr), 'on');
strictEqual(nativeMethods.getAttribute.call(input2, 'autocomplete'), 'off');
strictEqual(nativeMethods.getAttribute.call(input2, storedAutocompleteAttr), 'off');
strictEqual(nativeMethods.getAttribute.call(input3, 'autocomplete'), 'off');
strictEqual(nativeMethods.getAttribute.call(input3, storedAutocompleteAttr), '');
strictEqual(nativeMethods.getAttribute.call(input4, 'autocomplete'), 'off');
strictEqual(nativeMethods.getAttribute.call(input4, storedAutocompleteAttr), domProcessor.AUTOCOMPLETE_ATTRIBUTE_ABSENCE_MARKER);
});
test('crossdomain src', function () {
var url = 'http://cross.domain.com/';
var proxyUrl = urlUtils.getProxyUrl(url, {
proxyHostname: location.hostname,
proxyPort: 2001,
resourceType: 'i'
});
var processed = htmlUtils.processHtml('<iframe src="' + url + '"></iframe>');
ok(processed.indexOf('src="' + proxyUrl) !== -1);
ok(processed.indexOf(DomProcessor.getStoredAttrName('src') + '="' + url + '"') !== -1);
});
test('stylesheet', function () {
var urlReplacer = function () {
return 'replaced';
};
var check = function (css, expected) {
strictEqual(styleProcessor.process(css, urlReplacer), expected);
};
check('a:hover {}', 'a[' + INTERNAL_ATTRS.hoverPseudoClass + '] {}');
check('div { background-image: url(""); }', 'div { background-image: url(""); }');
check('div { background-image: url(\'\'); }', 'div { background-image: url(\'\'); }');
check('div { background-image: url(); }', 'div { background-image: url(); }');
check('div { background-image: url("/image.png"); }', 'div { background-image: url("replaced"); }');
check('div { background-image: url(\'/image.png\'); }', 'div { background-image: url(\'replaced\'); }');
check('div { background-image: url(/image.png); }', 'div { background-image: url(replaced); }');
check('@import "/image.png"', '@import "replaced"');
check('@import \'/image.png\'', '@import \'replaced\'');
check('@import ""', '@import ""');
check('@import \'\'', '@import \'\'');
});
test('clean up stylesheet', function () {
var url = 'http://google.com/image.png';
var proxyUrl = urlUtils.getProxyUrl(url);
var check = function (css, expected) {
strictEqual(styleProcessor.cleanUp(css, urlUtils.parseProxyUrl), expected);
};
check('a[' + INTERNAL_ATTRS.hoverPseudoClass + '] {}', 'a:hover {}');
check('div { background-image: url(""); }', 'div { background-image: url(""); }');
check('div { background-image: url(\'\'); }', 'div { background-image: url(\'\'); }');
check('div { background-image: url(); }', 'div { background-image: url(); }');
check('div { background-image: url("' + proxyUrl + '"); }', 'div { background-image: url("' + url + '"); }');
check('div { background-image: url(\'' + proxyUrl + '\'); }', 'div { background-image: url(\'' + url + '\'); }');
check('div { background-image: url(' + proxyUrl + '); }', 'div { background-image: url(' + url + '); }');
check('@import "' + proxyUrl + '"', '@import "' + url + '"');
check('@import \'' + proxyUrl + '\'', '@import \'' + url + '\'');
check('@import ""', '@import ""');
check('@import \'\'', '@import \'\'');
check('img[src' + INTERNAL_ATTRS.storedAttrPostfix + '="url.png"] {}', 'img[src="url.png"] {}');
});
test('style processor clean up edge cases', function () {
var STYLESHEET_PROCESSING_START_COMMENT = '/*hammerhead|stylesheet|start*/';
var STYLESHEET_PROCESSING_END_COMMENT = '/*hammerhead|stylesheet|end*/';
var css1 = 'START';
var css2 = 'START';
var css3 = 'START';
for (var i = 0; i < 5; i++) {
css1 += ' ';
css1 += STYLESHEET_PROCESSING_START_COMMENT;
css1 += ' * ';
css1 += STYLESHEET_PROCESSING_END_COMMENT;
css1 += ' + ';
css2 += ' ';
css2 += STYLESHEET_PROCESSING_START_COMMENT;
css2 += ' ';
css2 += STYLESHEET_PROCESSING_END_COMMENT;
css2 += ' ';
css3 += ' ';
css3 += STYLESHEET_PROCESSING_START_COMMENT;
css3 += ' 1 2 ';
css3 += STYLESHEET_PROCESSING_END_COMMENT;
css3 += ' 3 4 ';
}
css1 += 'END';
css2 += 'END';
css3 += 'END';
css1 = styleProcessor.cleanUp(css1, urlUtils.parseProxyUrl);
css2 = styleProcessor.cleanUp(css2, urlUtils.parseProxyUrl);
css3 = styleProcessor.cleanUp(css3, urlUtils.parseProxyUrl);
strictEqual(css1, 'START * + * + * + * + * + END');
strictEqual(css2, 'START END');
strictEqual(css3, 'START 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 END');
});
test('style processor clean up performance', function () {
var STYLESHEET_PROCESSING_START_COMMENT = '/*hammerhead|stylesheet|start*/';
var STYLESHEET_PROCESSING_END_COMMENT = '/*hammerhead|stylesheet|end*/';
var css = 'START';
for (var i = 0; i < 10000; i++) {
for (var j = 0; j < 500; j++)
css += ' ';
css += '<td data-cell-id="168_216§40_279"></td>';
for (var j = 0; j < 500; j++)
css += ' ';
}
css += STYLESHEET_PROCESSING_START_COMMENT;
css += '\n \n';
css += STYLESHEET_PROCESSING_END_COMMENT;
css += 'END';
var start = Date.now();
styleProcessor.cleanUp(css, urlUtils.parseProxyUrl);
var end = Date.now();
ok(end - start < 5000);
});
test('special pages (GH-339)', function () {
var anchor = document.createElement('a');
var image = document.createElement('img');
var iframe = document.createElement('iframe');
sharedUrlUtils.SPECIAL_PAGES.forEach(function (specialPagUrl) {
anchor.setAttribute('href', specialPagUrl);
image.setAttribute('src', specialPagUrl);
iframe.setAttribute('src', specialPagUrl);
var proxyUrl = urlUtils.getProxyUrl(specialPagUrl);
var anchorHrefUrl = nativeMethods.getAttribute.call(anchor, 'href');
var imageSrcUrl = nativeMethods.getAttribute.call(image, 'src');
var iframeSrcUrl = nativeMethods.getAttribute.call(iframe, 'src');
strictEqual(anchorHrefUrl, proxyUrl);
strictEqual(imageSrcUrl, specialPagUrl);
strictEqual(iframeSrcUrl, specialPagUrl);
});
});
test('add element with `formaction` tag to the form', function () {
var form = document.createElement('form');
var input = document.createElement('input');
nativeMethods.formActionSetter.call(form, urlUtils.getProxyUrl('./form.html', { resourceType: 'if' }));
input.setAttribute('formAction', './input.html');
strictEqual(nativeMethods.inputFormActionGetter.call(input), urlUtils.getProxyUrl('./input.html', { resourceType: 'f' }));
form.appendChild(input);
strictEqual(nativeMethods.inputFormActionGetter.call(input), urlUtils.getProxyUrl('./input.html', { resourceType: 'if' }));
});
module('should create a proxy url for the img src attribute if the image has the load handler (GH-651)', function () {
module('onload property', function () {
var origin = location.origin || location.protocol + location.host;
test('attach the load handler before setting up the src', function () {
var img = document.createElement('img');
var imgUrl = window.QUnitGlobals.getResourceUrl('../../../data/node-sandbox/image.png');
var imgProxyUrl = urlUtils.getProxyUrl(imgUrl);
img.onload = function () {};
img.setAttribute('src', imgUrl);
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
img.onload = null;
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
img.setAttribute('src', imgUrl);
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
});
test('attach the load handler after setting up the src', function () {
var img = document.createElement('img');
var imgUrl = window.QUnitGlobals.getResourceUrl('../../../data/node-sandbox/image.png');
var imgProxyUrl = urlUtils.getProxyUrl(imgUrl);
img.setAttribute('src', imgUrl);
strictEqual(urlUtils.parseUrl(nativeMethods.imageSrcGetter.call(img)).partAfterHost, imgUrl);
img.onload = null;
strictEqual(urlUtils.parseUrl(nativeMethods.imageSrcGetter.call(img)).partAfterHost, imgUrl);
img.setAttribute('src', imgUrl);
strictEqual(urlUtils.parseUrl(nativeMethods.imageSrcGetter.call(img)).partAfterHost, imgUrl);
img.onload = function () {};
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
});
asyncTest('attach the load handler after setting up the src(image is loaded)', function () {
var img = document.createElement('img');
var storedForcedLocation = destLocation.getLocation();
destLocation.forceLocation('http://localhost/sessionId/' + location.origin);
img.src = origin + '/image.png?rand=' + Math.random() + '&timeout=0';
nativeMethods.htmlElementOnloadSetter.call(img, function () {
var onloadHandlerCalled = false;
img.onload = function () {
onloadHandlerCalled = true;
};
img.addEventListener('load', function () {
onloadHandlerCalled = true;
});
setTimeout(function () {
img.onload = null;
ok(!onloadHandlerCalled);
destLocation.forceLocation(storedForcedLocation);
start();
}, 1000);
});
});
asyncTest('attach the load handler after setting up the src(image is not loaded)', function () {
var img = document.createElement('img');
var storedForcedLocation = destLocation.getLocation();
destLocation.forceLocation('http://localhost/sessionId/' + location.origin);
img.src = origin + '/image.png?rand=' + Math.random() + '&timeout=300';
img.onload = function () {
ok(true);
destLocation.forceLocation(storedForcedLocation);
start();
};
});
asyncTest('attach the load handler after setting up the src(image is loaded and set new src)', function () {
var img = document.createElement('img');
var storedForcedLocation = destLocation.getLocation();
destLocation.forceLocation('http://localhost/sessionId/' + location.origin);
img.src = origin + '/image.png?rand=' + Math.random() + '&timeout=0';
nativeMethods.htmlElementOnloadSetter.call(img, function () {
img.onload = function () {
img.onload = null;
ok(true);
destLocation.forceLocation(storedForcedLocation);
start();
};
img.src = origin + '/image.png?rand=' + Math.random() + '&timeout=0';
});
});
asyncTest('attach the load handler after setting up the cached src(image is loaded, but load event is not emitted) (GH-1959)', function () {
var img = document.createElement('img');
var imgSrc = origin + '/image.png?rand=' + Math.random() + '&timeout=0&expires=' +
new Date(Date.now() + 1e6).toUTCString();
var storedForcedLocation = destLocation.getLocation();
destLocation.forceLocation('http://localhost/sessionId/' + location.origin);
img.src = imgSrc;
nativeMethods.htmlElementOnloadSetter.call(img, function () {
var anotherImg = document.createElement('img');
anotherImg.src = imgSrc;
anotherImg.onload = function () {
ok(true);
destLocation.forceLocation(storedForcedLocation);
start();
};
});
});
});
module('addEventListener', function () {
test('attach the load handler before setting up the src', function () {
var img = document.createElement('img');
var imgUrl = window.QUnitGlobals.getResourceUrl('../../../data/node-sandbox/image.png');
var imgProxyUrl = urlUtils.getProxyUrl(imgUrl);
var listener = function () {};
img.addEventListener('load', listener);
img.setAttribute('src', imgUrl);
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
img.removeEventListener('load', listener);
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
img.setAttribute('src', imgUrl);
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
});
test('attach the load handler after setting up the src', function () {
var img = document.createElement('img');
var imgUrl = window.QUnitGlobals.getResourceUrl('../../../data/node-sandbox/image.png');
var imgProxyUrl = urlUtils.getProxyUrl(imgUrl);
img.setAttribute('src', imgUrl);
strictEqual(urlUtils.parseUrl(nativeMethods.imageSrcGetter.call(img)).partAfterHost, imgUrl);
img.addEventListener('load', function () {});
strictEqual(nativeMethods.imageSrcGetter.call(img), imgProxyUrl);
});
});
});
module('regression');
test('process the "integrity" attribute in the link and script tags (GH-235)', function () {
var script = nativeMethods.createElement.call(document, 'script');
var link = nativeMethods.createElement.call(document, 'link');
var integrityValue = 'sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC';
var urlReplacer = function (url) {
return url;
};
function checkIntegrityAttr (el) {
ok(el.hasAttribute('integrity'));
strictEqual(el.getAttribute('integrity'), integrityValue);
strictEqual(nativeMethods.getAttribute.call(el, 'integrity'), null);
}
nativeMethods.setAttribute.call(script, 'integrity', integrityValue);
nativeMethods.setAttribute.call(link, 'integrity', integrityValue);
domProcessor.processElement(script, urlReplacer);
domProcessor.processElement(link, urlReplacer);
checkIntegrityAttr(script);
checkIntegrityAttr(link);
});
test('link with target="_parent" in iframe (T216999)', function () {
return createTestIframe({ src: getSameDomainPageUrl('../../../data/dom-processor/iframe.html') })
.then(function (iframe) {
var anchor = nativeMethods.getElementById.call(iframe.contentDocument, 'anchor');
var storedAttrName = DomProcessor.getStoredAttrName('href');
strictEqual(nativeMethods.getAttribute.call(anchor, storedAttrName), '/index.html');
});
});
test('iframe with javascript protocol in "src" attribute value must be processed (T135513)', function () {
var iframe = nativeMethods.createElement.call(document, 'iframe');
var src = 'javascript:"<html><body><a id=\'test\' data-attr=\\"123\\">link</a></body></html>"';
nativeMethods.setAttribute.call(iframe, 'src', src);
domProcessor.processElement(iframe, function (url) {
return url;
});
var srcAttr = nativeMethods.getAttribute.call(iframe, 'src');
var storedSrcAttr = nativeMethods.getAttribute.call(iframe, DomProcessor.getStoredAttrName('src'));
notEqual(srcAttr, src);
strictEqual(srcAttr, 'javascript:' + processScript(src.replace('javascript:', ''), false, true));
strictEqual(storedSrcAttr, src);
});
test('the URL attribute must be set to an empty string on the server only once (T295078) (GH-159)', function () {
var iframe = nativeMethods.createElement.call(document, 'iframe');
nativeMethods.setAttribute.call(iframe, 'src', '/should_not_be_changed');
// NOTE: Simulates processing an iframe on the server.
nativeMethods.setAttribute.call(iframe, DomProcessor.getStoredAttrName('src'), '');
domProcessor.processElement(iframe, function () {
return 'fail';
});
strictEqual(nativeMethods.getAttribute.call(iframe, 'src'), '/should_not_be_changed');
});
test('remove the meta tag with http-equiv="Content-Security-Policy" attribute from document (GH-243)', function () {
var metaTag = nativeMethods.createElement.call(document, 'meta');
nativeMethods.setAttribute.call(metaTag, 'http-equiv', 'Content-Security-Policy');
nativeMethods.setAttribute.call(metaTag, 'content', 'script-src https: \'unsafe-eval\';');
domProcessor.processElement(metaTag);
ok(!metaTag.hasAttribute('http-equiv'));
ok(!metaTag.hasAttribute('content'));
});
test('remove the meta tag with http-equiv="Content-Security-Policy" attribute from document (tag properties added via setAttribute) (GH-243)', function () {
var metaTag = document.createElement('meta');
metaTag.setAttribute('id', 'metaContentSecurityPolicy');
metaTag.setAttribute('http-equiv', 'Content-Security-Policy');
metaTag.setAttribute('content', 'script-src https: \'unsafe-eval\';');
document.head.appendChild(metaTag);
ok(!metaTag.hasAttribute('http-equiv'));
ok(metaTag.hasAttribute('content'));
metaTag.parentNode.removeChild(metaTag);
});
test('allow to set the content attribute to meta tag', function () {
var metaTag = document.createElement('meta');
metaTag.setAttribute('http-equiv', 'refresh');
metaTag.setAttribute('content', 'url');
strictEqual(metaTag.getAttribute('http-equiv'), null);
strictEqual(metaTag.getAttribute('content'), 'url');
metaTag.setAttribute('name', 'csrf-token');
metaTag.setAttribute('content', '1234567');
strictEqual(metaTag.getAttribute('name'), 'csrf-token');
strictEqual(metaTag.getAttribute('content'), '1234567');
metaTag.setAttribute('http-equiv', 'Content-Security-Policy');
metaTag.setAttribute('content', 'script-src');
strictEqual(metaTag.getAttribute('http-equiv'), null);
strictEqual(metaTag.getAttribute('content'), 'script-src');
metaTag.httpEquiv = 'Content-Security-Policy';
metaTag.content = 'style-src';
strictEqual(metaTag.getAttribute('http-equiv'), null);
strictEqual(metaTag.getAttribute('content'), 'style-src');
});
test('script and style content added via a child text node must be overridden (GH-259)', function () {
var style = document.createElement('style');
var styleTextNode1 = document.createTextNode('div.class1 { background-image: url("/image1.png"); }');
var styleTextNode2 = document.createTextNode('div.class2 { background-image: url("/image2.png"); }');
style.appendChild(styleTextNode1);
ok(style.childNodes[0].data.indexOf(urlUtils.getProxyUrl('/image1.png')) > -1);
style.insertBefore(styleTextNode2, styleTextNode1);
ok(style.childNodes[0].data.indexOf(urlUtils.getProxyUrl('/image2.png')) > -1);
var script = document.createElement('script');
var scriptTextNode1 = document.createTextNode('var host1 = location.host');
var scriptTextNode2 = document.createTextNode('var host2 = location.host');
script.appendChild(scriptTextNode1);
ok(script.childNodes[0].data.indexOf('var host1 = __get$Loc(location) .host') > -1);
script.insertBefore(scriptTextNode2, scriptTextNode1);
ok(script.childNodes[0].data.indexOf('var host2 = __get$Loc(location) .host') > -1);
if (!nativeMethods.append)
return;
style.append('div.class3 { background-image: url("/image3.png"); }',
'div.class4 { background-image: url("/image4.png"); }',
document.createTextNode('div.class5 { background-image: url("/image5.png"); }'));
ok(style.childNodes[2].data.indexOf(urlUtils.getProxyUrl('/image3.png')) > -1);
ok(style.childNodes[3].data.indexOf(urlUtils.getProxyUrl('/image4.png')) > -1);
ok(style.childNodes[4].data.indexOf(urlUtils.getProxyUrl('/image5.png')) > -1);
script.append('var port = location.port',
document.createTextNode('var hostname = location.hostname'),
'var protocol = location.protocol');
ok(script.childNodes[2].data.indexOf('var port = __get$Loc(location) .port') > -1);
ok(script.childNodes[3].data.indexOf('var hostname = __get$Loc(location) .hostname') > -1);
ok(script.childNodes[4].data.indexOf('var protocol = __get$Loc(location) .protocol') > -1);
});
test('node.replaceChild must be overridden (GH-264)', function () {
var style = document.createElement('style');
var styleTextNode1 = document.createTextNode('div.class1 { background-image: url("/image1.png"); }');
var styleTextNode2 = document.createTextNode('div.class2 { background-image: url("/image2.png"); }');
style.appendChild(styleTextNode1);
ok(nativeMethods.elementInnerHTMLGetter.call(style).indexOf(urlUtils.getProxyUrl('/image1.png')) > -1);
style.replaceChild(styleTextNode2, styleTextNode1);
ok(nativeMethods.elementInnerHTMLGetter.call(style).indexOf(urlUtils.getProxyUrl('/image2.png')) > -1);
});
test('script error when a new element is added to a "body" element that is not in the DOM (GH-296)', function () {
return createTestIframe()
.then(function (iframe) {
var iframeDocument = iframe.contentDocument;
iframeDocument.documentElement.removeChild(iframeDocument.body);
var newIframeBody = iframeDocument.createElement('body');
var div1 = iframeDocument.createElement('div');
var div2 = iframeDocument.createElement('div');
var div3 = iframeDocument.createElement('div');
newIframeBody.appendChild(div1);
strictEqual(newIframeBody.lastChild, div1);
newIframeBody.appendChild(div2);
strictEqual(newIframeBody.lastChild, div2);
newIframeBody.insertBefore(div3, null);
strictEqual(newIframeBody.lastChild, div3);
});
});
test('xlink:href attribute of svg elements should be overriden (GH-434)(GH-514)', function () {
var svgNameSpaceUrl = 'http://www.w3.org/2000/svg';
var xlinkNameSpaceUrl = 'http://www.w3.org/1999/xlink';
var use = document.createElementNS(svgNameSpaceUrl, 'use');
var svgUrl = 'http://domain.com/test.svg#rect';
var div = document.createElement('div');
use.setAttribute('xlink:href', '#svg-rect');