summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_partitionedServiceWorkers.js
blob: bb9baa460feada839db3278b0497fc672085201b (plain)
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
/* import-globals-from storageAccessAPIHelpers.js */

PartitionedStorageHelper.runTest(
  "ServiceWorkers - disable partitioning",
  async (win3rdParty, win1stParty, allowed) => {
    // Partitioned serviceWorkers are disabled in third-party context.
    await win3rdParty.navigator.serviceWorker.register("empty.js").then(
      _ => {
        ok(allowed, "Success: ServiceWorker cannot be used!");
      },
      _ => {
        ok(!allowed, "Failed: ServiceWorker cannot be used!");
      }
    );

    await win1stParty.navigator.serviceWorker.register("empty.js").then(
      _ => {
        ok(true, "Success: ServiceWorker should be available!");
      },
      _ => {
        ok(false, "Failed: ServiceWorker should be available!");
      }
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", false],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - enable partitioning",
  async (win3rdParty, win1stParty) => {
    // Partitioned serviceWorkers are enabled in third-party context.
    await win3rdParty.navigator.serviceWorker.register("empty.js").then(
      _ => {
        ok(
          true,
          "Success: ServiceWorker should be available in third parties."
        );
      },
      _ => {
        ok(
          false,
          "Failed: ServiceWorker should be available in third parties."
        );
      }
    );

    await win1stParty.navigator.serviceWorker.register("empty.js").then(
      _ => {
        ok(true, "Success: ServiceWorker should be available!");
      },
      _ => {
        ok(false, "Failed: ServiceWorker should be available!");
      }
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - MatchAll",
  async (win3rdParty, win1stParty) => {
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(win1stParty, "matchAll.js");
    }

    let msgPromise = new Promise(resolve => {
      win1stParty.navigator.serviceWorker.addEventListener("message", msg => {
        resolve(msg.data);
      });
    });

    win1stParty.sw.postMessage(win3rdParty.location.href);
    let msg = await msgPromise;

    // The service worker will always be partitioned. So, the first party window
    // won't have control on the third-party window.
    is(
      false,
      msg,
      "We won't have the 3rd party window controlled regardless of StorageAccess."
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Partition ScriptContext",
  async (win3rdParty, win1stParty) => {
    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Set a script value to first-party service worker.
    let res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      {
        type: "SetScriptValue",
        value: "1stParty",
      }
    );
    ok(res.result, "OK", "Set script value to first-party service worker.");

    // Set a script value to third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      {
        type: "SetScriptValue",
        value: "3rdParty",
      }
    );
    ok(res.result, "OK", "Set script value to third-party service worker.");

    // Get and check script value from the first-party service worker.
    res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "GetScriptValue" }
    );
    is(
      res.value,
      "1stParty",
      "The script value in first party window is correct"
    );

    // Get and check script value from the third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "GetScriptValue" }
    );

    is(
      res.value,
      "3rdParty",
      "The script value in third party window is correct"
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Partition DOM Cache",
  async (win3rdParty, win1stParty) => {
    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Set DOM cache to first-party service worker.
    let res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      {
        type: "SetCache",
        value: "1stParty",
      }
    );
    ok(res.result, "OK", "Set cache to first-party service worker.");

    // Set DOM cache to third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      {
        type: "SetCache",
        value: "3rdParty",
      }
    );
    ok(res.result, "OK", "Set cache to third-party service worker.");

    // Check DOM cache from the first-party service worker.
    res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "HasCache", value: "1stParty" }
    );
    ok(
      res.value,
      "The '1stParty' cache storage should exist for the first-party window."
    );
    res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "HasCache", value: "3rdParty" }
    );
    ok(
      !res.value,
      "The '3rdParty' cache storage should not exist for the first-party window."
    );

    // Check DOM cache from the third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "HasCache", value: "1stParty" }
    );

    ok(
      !res.value,
      "The '1stParty' cache storage should not exist for the third-party window."
    );

    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "HasCache", value: "3rdParty" }
    );

    ok(
      res.value,
      "The '3rdParty' cache storage should exist for the third-party window."
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Partition IndexedDB",
  async (win3rdParty, win1stParty) => {
    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Set indexedDB value to first-party service worker.
    let res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      {
        type: "SetIndexedDB",
        value: "1stParty",
      }
    );
    ok(res.result, "OK", "Set cache to first-party service worker.");

    // Set indexedDB value to third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      {
        type: "SetIndexedDB",
        value: "3rdParty",
      }
    );
    ok(res.result, "OK", "Set cache to third-party service worker.");

    // Get and check indexedDB value from the first-party service worker.
    res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "GetIndexedDB" }
    );
    is(
      res.value,
      "1stParty",
      "The indexedDB value in first party window is correct"
    );

    // Get and check indexedDB from the third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "GetIndexedDB" }
    );

    is(
      res.value,
      "3rdParty",
      "The indexedDB value in third party window is correct"
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Partition Intercept",
  async (win3rdParty, win1stParty) => {
    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Fetch a resource in the first-party window.
    await win1stParty.fetch("empty.js");

    // Check that only the first-party service worker gets fetch event.
    let res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "GetFetchURL" }
    );
    is(
      res.value,
      "https://not-tracking.example.com/browser/toolkit/components/antitracking/test/browser/empty.js",
      "The first-party service worker received fetch event."
    );
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "GetFetchURL" }
    );
    is(
      res.value,
      "",
      "The third-party service worker received no fetch event."
    );

    // Fetch a resource in the third-party window.
    await win3rdParty.fetch("empty.js");

    // Check if the fetch event only happens in third-party service worker.
    res = await sendAndWaitWorkerMessage(
      win1stParty.sw,
      win1stParty.navigator.serviceWorker,
      { type: "GetFetchURL" }
    );
    is(
      res.value,
      "",
      "The first-party service worker received no fetch event."
    );
    res = await sendAndWaitWorkerMessage(
      win3rdParty.sw,
      win3rdParty.navigator.serviceWorker,
      { type: "GetFetchURL" }
    );
    is(
      res.value,
      "https://not-tracking.example.com/browser/toolkit/components/antitracking/test/browser/empty.js",
      "The third-party service worker received fetch event."
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

// Bug1743236 - Verify the content process won't crash if we create a dedicated
// worker in a service worker controlled third-party page with Storage Access.
PartitionedStorageHelper.runTest(
  "ServiceWorkers - Create Dedicated Worker",
  async (win3rdParty, win1stParty, allowed) => {
    // We only do this test when the storage access is granted.
    if (!allowed) {
      return;
    }

    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Create a dedicated worker in first-party window.
    let firstPartyWorker = new win1stParty.Worker("dedicatedWorker.js");

    // Post a message to the dedicated worker and wait until the message circles
    // back.
    await new Promise(resolve => {
      firstPartyWorker.addEventListener("message", msg => {
        if (msg.data == "1stParty") {
          resolve();
        }
      });

      firstPartyWorker.postMessage("1stParty");
    });

    // Create a dedicated worker in third-party window.
    let thirdPartyWorker = new win3rdParty.Worker("dedicatedWorker.js");

    // Post a message to the dedicated worker and wait until the message circles
    // back.
    await new Promise(resolve => {
      thirdPartyWorker.addEventListener("message", msg => {
        if (msg.data == "3rdParty") {
          resolve();
        }
      });

      thirdPartyWorker.postMessage("3rdParty");
    });

    firstPartyWorker.terminate();
    thirdPartyWorker.terminate();
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

// Bug1768193 - Verify the parent process won't crash if we create a shared
// worker in a service worker controlled third-party page with Storage Access.
PartitionedStorageHelper.runTest(
  "ServiceWorkers - Create Shared Worker",
  async (win3rdParty, win1stParty, allowed) => {
    // We only do this test when the storage access is granted.
    if (!allowed) {
      return;
    }

    // Register service worker for the first-party window.
    if (!win1stParty.sw) {
      win1stParty.sw = await registerServiceWorker(
        win1stParty,
        "serviceWorker.js"
      );
    }

    // Register service worker for the third-party window.
    if (!win3rdParty.sw) {
      win3rdParty.sw = await registerServiceWorker(
        win3rdParty,
        "serviceWorker.js"
      );
    }

    // Create a shared worker in third-party window.
    let thirdPartyWorker = new win3rdParty.SharedWorker("sharedWorker.js");

    // Post a message to the dedicated worker and wait until the message circles
    // back.
    await new Promise(resolve => {
      thirdPartyWorker.port.onmessage = () => {
        resolve();
      };
      thirdPartyWorker.onerror = _ => {
        ok(false, "We should not be here");
        resolve();
      };
      thirdPartyWorker.port.postMessage("count");
    });

    thirdPartyWorker.port.postMessage("close");
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ]
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Private Browsing with partitioning disabled",
  async (win3rdParty, win1stParty) => {
    // Partitioned serviceWorkers are disabled in third-party context.
    ok(
      !win3rdParty.navigator.serviceWorker,
      "ServiceWorker should not be available"
    );
    ok(
      !win1stParty.navigator.serviceWorker,
      "ServiceWorker should not be available"
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", false],
  ],

  {
    runInPrivateWindow: true,
  }
);

PartitionedStorageHelper.runTest(
  "ServiceWorkers - Private Browsing with partitioning enabled",
  async (win3rdParty, win1stParty) => {
    // Partitioned serviceWorkers are disabled in third-party context.
    ok(
      !win3rdParty.navigator.serviceWorker,
      "ServiceWorker should not be available"
    );
    ok(
      !win1stParty.navigator.serviceWorker,
      "ServiceWorker should not be available"
    );
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  },

  [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.ipc.processCount", 1],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true],
    ["privacy.partition.serviceWorkers", true],
  ],

  {
    runInPrivateWindow: true,
  }
);