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
|
var origin = "http://mochi.test:8888";
function fetchXHRWithMethod(name, method, onload, onerror, headers) {
expectAsyncResult();
onload =
onload ||
function () {
my_ok(false, "XHR load should not complete successfully");
finish();
};
onerror =
onerror ||
function () {
my_ok(
false,
"XHR load for " + name + " should be intercepted successfully"
);
finish();
};
var x = new XMLHttpRequest();
x.open(method, name, true);
x.onload = function () {
onload(x);
};
x.onerror = function () {
onerror(x);
};
headers = headers || [];
headers.forEach(function (header) {
x.setRequestHeader(header[0], header[1]);
});
x.send();
}
var corsServerPath =
"/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs";
var corsServerURL = "http://example.com" + corsServerPath;
function redirectURL(hops) {
return (
hops[0].server +
corsServerPath +
"?hop=1&hops=" +
encodeURIComponent(JSON.stringify(hops))
);
}
function fetchXHR(name, onload, onerror, headers) {
return fetchXHRWithMethod(name, "GET", onload, onerror, headers);
}
fetchXHR("bare-synthesized.txt", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "synthesized response body",
"load should have synthesized response"
);
finish();
});
fetchXHR("test-respondwith-response.txt", function (xhr) {
my_ok(
xhr.status == 200,
"test-respondwith-response load should be successful"
);
my_ok(
xhr.responseText == "test-respondwith-response response body",
"load should have response"
);
finish();
});
fetchXHR("synthesized-404.txt", function (xhr) {
my_ok(xhr.status == 404, "load should 404");
my_ok(
xhr.responseText == "synthesized response body",
"404 load should have synthesized response"
);
finish();
});
fetchXHR("synthesized-headers.txt", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.getResponseHeader("X-Custom-Greeting") === "Hello",
"custom header should be set"
);
my_ok(
xhr.responseText == "synthesized response body",
"custom header load should have synthesized response"
);
finish();
});
fetchXHR("synthesized-redirect-real-file.txt", function (xhr) {
dump("Got status AARRGH " + xhr.status + " " + xhr.responseText + "\n");
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "This is a real file.\n",
"Redirect to real file should complete."
);
finish();
});
fetchXHR("synthesized-redirect-twice-real-file.txt", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "This is a real file.\n",
"Redirect to real file (twice) should complete."
);
finish();
});
fetchXHR("synthesized-redirect-synthesized.txt", function (xhr) {
my_ok(xhr.status == 200, "synth+redirect+synth load should be successful");
my_ok(
xhr.responseText == "synthesized response body",
"load should have redirected+synthesized response"
);
finish();
});
fetchXHR("synthesized-redirect-twice-synthesized.txt", function (xhr) {
my_ok(
xhr.status == 200,
"synth+redirect+synth (twice) load should be successful"
);
my_ok(
xhr.responseText == "synthesized response body",
"load should have redirected+synthesized (twice) response"
);
finish();
});
fetchXHR("redirect.sjs", function (xhr) {
my_ok(xhr.status == 404, "redirected load should be uninterrupted");
finish();
});
fetchXHR("ignored.txt", function (xhr) {
my_ok(xhr.status == 404, "load should be uninterrupted");
finish();
});
fetchXHR("rejected.txt", null, function (xhr) {
my_ok(xhr.status == 0, "load should not complete");
finish();
});
fetchXHR("nonresponse.txt", null, function (xhr) {
my_ok(xhr.status == 0, "load should not complete");
finish();
});
fetchXHR("nonresponse2.txt", null, function (xhr) {
my_ok(xhr.status == 0, "load should not complete");
finish();
});
fetchXHR("nonpromise.txt", null, function (xhr) {
my_ok(xhr.status == 0, "load should not complete");
finish();
});
fetchXHR(
"headers.txt",
function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(xhr.responseText == "1", "request header checks should have passed");
finish();
},
null,
[
["X-Test1", "header1"],
["X-Test2", "header2"],
]
);
fetchXHR("http://user:pass@mochi.test:8888/user-pass", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "http://user:pass@mochi.test:8888/user-pass",
"The username and password should be preserved"
);
finish();
});
fetchXHR("readable-stream.txt", function (xhr) {
my_ok(xhr.status == 200, "loading completed");
my_ok(xhr.responseText == "Hello!", "The message is correct!");
finish();
});
fetchXHR(
"readable-stream-locked.txt",
function (xhr) {
my_ok(false, "This should not be called!");
finish();
},
function () {
my_ok(true, "The exception has been correctly handled!");
finish();
}
);
fetchXHR(
"readable-stream-with-exception.txt",
function (xhr) {
my_ok(false, "This should not be called!");
finish();
},
function () {
my_ok(true, "The exception has been correctly handled!");
finish();
}
);
fetchXHR(
"readable-stream-with-exception2.txt",
function (xhr) {
my_ok(false, "This should not be called!");
finish();
},
function () {
my_ok(true, "The exception has been correctly handled!");
finish();
}
);
fetchXHR(
"readable-stream-already-consumed.txt",
function (xhr) {
my_ok(false, "This should not be called!");
finish();
},
function () {
my_ok(true, "The exception has been correctly handled!");
finish();
}
);
var expectedUncompressedResponse = "";
for (let i = 0; i < 10; ++i) {
expectedUncompressedResponse += "hello";
}
expectedUncompressedResponse += "\n";
// ServiceWorker does not intercept, at which point the network request should
// be correctly decoded.
fetchXHR("deliver-gzip.sjs", function (xhr) {
my_ok(xhr.status == 200, "network gzip load should be successful");
my_ok(
xhr.responseText == expectedUncompressedResponse,
"network gzip load should have synthesized response."
);
my_ok(
xhr.getResponseHeader("Content-Encoding") == "gzip",
"network Content-Encoding should be gzip."
);
my_ok(
xhr.getResponseHeader("Content-Length") == "35",
"network Content-Length should be of original gzipped file."
);
finish();
});
fetchXHR("hello.gz", function (xhr) {
my_ok(xhr.status == 200, "gzip load should be successful");
my_ok(
xhr.responseText == expectedUncompressedResponse,
"gzip load should have synthesized response."
);
my_ok(
xhr.getResponseHeader("Content-Encoding") == "gzip",
"Content-Encoding should be gzip."
);
my_ok(
xhr.getResponseHeader("Content-Length") == "35",
"Content-Length should be of original gzipped file."
);
finish();
});
fetchXHR("hello-after-extracting.gz", function (xhr) {
my_ok(xhr.status == 200, "gzip load after extracting should be successful");
my_ok(
xhr.responseText == expectedUncompressedResponse,
"gzip load after extracting should have synthesized response."
);
my_ok(
xhr.getResponseHeader("Content-Encoding") == "gzip",
"Content-Encoding after extracting should be gzip."
);
my_ok(
xhr.getResponseHeader("Content-Length") == "35",
"Content-Length after extracting should be of original gzipped file."
);
finish();
});
fetchXHR(corsServerURL + "?status=200&allowOrigin=*", function (xhr) {
my_ok(
xhr.status == 200,
"cross origin load with correct headers should be successful"
);
my_ok(
xhr.getResponseHeader("access-control-allow-origin") == null,
"cors headers should be filtered out"
);
finish();
});
// Verify origin header is sent properly even when we have a no-intercept SW.
var uriOrigin = encodeURIComponent(origin);
fetchXHR(
"http://example.org" +
corsServerPath +
"?ignore&status=200&origin=" +
uriOrigin +
"&allowOrigin=" +
uriOrigin,
function (xhr) {
my_ok(
xhr.status == 200,
"cross origin load with correct headers should be successful"
);
my_ok(
xhr.getResponseHeader("access-control-allow-origin") == null,
"cors headers should be filtered out"
);
finish();
}
);
// Verify that XHR is considered CORS tainted even when original URL is same-origin
// redirected to cross-origin.
fetchXHR(
redirectURL([
{ server: origin },
{ server: "http://example.org", allowOrigin: origin },
]),
function (xhr) {
my_ok(
xhr.status == 200,
"cross origin load with correct headers should be successful"
);
my_ok(
xhr.getResponseHeader("access-control-allow-origin") == null,
"cors headers should be filtered out"
);
finish();
}
);
// Test that CORS preflight requests cannot be intercepted. Performs a
// cross-origin XHR that the SW chooses not to intercept. This requires a
// preflight request, which the SW must not be allowed to intercept.
fetchXHR(
corsServerURL + "?status=200&allowOrigin=*",
null,
function (xhr) {
my_ok(
xhr.status == 0,
"cross origin load with incorrect headers should be a failure"
);
finish();
},
[["X-Unsafe", "unsafe"]]
);
// Test that CORS preflight requests cannot be intercepted. Performs a
// cross-origin XHR that the SW chooses to intercept and respond with a
// cross-origin fetch. This requires a preflight request, which the SW must not
// be allowed to intercept.
fetchXHR(
"http://example.org" + corsServerPath + "?status=200&allowOrigin=*",
null,
function (xhr) {
my_ok(
xhr.status == 0,
"cross origin load with incorrect headers should be a failure"
);
finish();
},
[["X-Unsafe", "unsafe"]]
);
// Test that when the page fetches a url the controlling SW forces a redirect to
// another location. This other location fetch should also be intercepted by
// the SW.
fetchXHR("something.txt", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "something else response body",
"load should have something else"
);
finish();
});
// Test fetch will internally get it's SkipServiceWorker flag set. The request is
// made from the SW through fetch(). fetch() fetches a server-side JavaScript
// file that force a redirect. The redirect location fetch does not go through
// the SW.
fetchXHR("redirect_serviceworker.sjs", function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "// empty worker, always succeed!\n",
"load should have redirection content"
);
finish();
});
fetchXHR(
"empty-header",
function (xhr) {
my_ok(xhr.status == 200, "load should be successful");
my_ok(
xhr.responseText == "emptyheader",
"load should have the expected content"
);
finish();
},
null,
[["emptyheader", ""]]
);
expectAsyncResult();
fetch(
"http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200&allowOrigin=*"
).then(
function (res) {
my_ok(res.ok, "Valid CORS request should receive valid response");
my_ok(res.type == "cors", "Response type should be CORS");
res.text().then(function (body) {
my_ok(
body === "<res>hello pass</res>\n",
"cors response body should match"
);
finish();
});
},
function (e) {
my_ok(false, "CORS Fetch failed");
finish();
}
);
expectAsyncResult();
fetch(
"http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200",
{ mode: "no-cors" }
).then(
function (res) {
my_ok(res.type == "opaque", "Response type should be opaque");
my_ok(res.status == 0, "Status should be 0");
res.text().then(function (body) {
my_ok(body === "", "opaque response body should be empty");
finish();
});
},
function (e) {
my_ok(false, "no-cors Fetch failed");
finish();
}
);
expectAsyncResult();
fetch("opaque-on-same-origin").then(
function (res) {
my_ok(
false,
"intercepted opaque response for non no-cors request should fail."
);
finish();
},
function (e) {
my_ok(
true,
"intercepted opaque response for non no-cors request should fail."
);
finish();
}
);
expectAsyncResult();
fetch("http://example.com/opaque-no-cors", { mode: "no-cors" }).then(
function (res) {
my_ok(
res.type == "opaque",
"intercepted opaque response for no-cors request should have type opaque."
);
finish();
},
function (e) {
my_ok(
false,
"intercepted opaque response for no-cors request should pass."
);
finish();
}
);
expectAsyncResult();
fetch("http://example.com/cors-for-no-cors", { mode: "no-cors" }).then(
function (res) {
my_ok(
res.type == "cors",
"synthesize CORS response should result in outer CORS response"
);
finish();
},
function (e) {
my_ok(false, "cors-for-no-cors request should not reject");
finish();
}
);
function arrayBufferFromString(str) {
var arr = new Uint8Array(str.length);
for (let i = 0; i < str.length; ++i) {
arr[i] = str.charCodeAt(i);
}
return arr;
}
expectAsyncResult();
fetch(new Request("body-simple", { method: "POST", body: "my body" }))
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body == "my bodymy body",
"the body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
fetch(
new Request("body-arraybufferview", {
method: "POST",
body: arrayBufferFromString("my body"),
})
)
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body == "my bodymy body",
"the ArrayBufferView body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
fetch(
new Request("body-arraybuffer", {
method: "POST",
body: arrayBufferFromString("my body").buffer,
})
)
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body == "my bodymy body",
"the ArrayBuffer body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
var usp = new URLSearchParams();
usp.set("foo", "bar");
usp.set("baz", "qux");
fetch(new Request("body-urlsearchparams", { method: "POST", body: usp }))
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body == "foo=bar&baz=quxfoo=bar&baz=qux",
"the URLSearchParams body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
var fd = new FormData();
fd.set("foo", "bar");
fd.set("baz", "qux");
fetch(new Request("body-formdata", { method: "POST", body: fd }))
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body.indexOf('Content-Disposition: form-data; name="foo"\r\n\r\nbar') <
body.indexOf('Content-Disposition: form-data; name="baz"\r\n\r\nqux'),
"the FormData body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
fetch(
new Request("body-blob", {
method: "POST",
body: new Blob(new String("my body")),
})
)
.then(function (res) {
return res.text();
})
.then(function (body) {
my_ok(
body == "my bodymy body",
"the Blob body of the intercepted fetch should be visible in the SW"
);
finish();
});
expectAsyncResult();
fetch("interrupt.sjs").then(
function (res) {
my_ok(true, "interrupted fetch succeeded");
res.text().then(
function (body) {
my_ok(false, "interrupted fetch shouldn't have complete body");
finish();
},
function () {
my_ok(true, "interrupted fetch shouldn't have complete body");
finish();
}
);
},
function (e) {
my_ok(false, "interrupted fetch failed");
finish();
}
);
["DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT"].forEach(function (method) {
fetchXHRWithMethod("xhr-method-test.txt", method, function (xhr) {
my_ok(xhr.status == 200, method + " load should be successful");
if (method === "HEAD") {
my_ok(
xhr.responseText == "",
method + "load should not have synthesized response"
);
} else {
my_ok(
xhr.responseText == "intercepted " + method,
method + " load should have synthesized response"
);
}
finish();
});
});
expectAsyncResult();
fetch(new Request("empty-header", { headers: { emptyheader: "" } }))
.then(function (res) {
return res.text();
})
.then(
function (body) {
my_ok(
body == "emptyheader",
"The empty header was observed in the fetch event"
);
finish();
},
function (err) {
my_ok(false, "A promise was rejected with " + err);
finish();
}
);
expectAsyncResult();
fetch("fetchevent-extendable")
.then(function (res) {
return res.text();
})
.then(
function (body) {
my_ok(body == "extendable", "FetchEvent inherits from ExtendableEvent");
finish();
},
function (err) {
my_ok(false, "A promise was rejected with " + err);
finish();
}
);
expectAsyncResult();
fetch("fetchevent-request")
.then(function (res) {
return res.text();
})
.then(
function (body) {
my_ok(body == "non-nullable", "FetchEvent.request must be non-nullable");
finish();
},
function (err) {
my_ok(false, "A promise was rejected with " + err);
finish();
}
);
|