summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_serviceWorkersWithStorageAccessGranted.js
blob: 4ae4ba74ddb0bfcebbfc78e81ea04461a891a507 (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
/** This tests that the service worker can be used if we have storage access
 *  permission. We manually write the storage access permission into the
 *  permission manager to simulate the storage access has been granted. We would
 *  test the service worker three times. The fist time is to check the service
 *  work is allowed. The second time is to load again and check it won't hit
 *  assertion, this assertion would only be hit if we have registered a service
 *  worker, see Bug 1631234.
 *
 *  The third time is to load again but in a sandbox iframe to check it won't
 *  hit the assertion. See Bug 1637226 for details.
 *
 *  The fourth time is to load again in a nested iframe to check it won't hit
 *  the assertion. See Bug 1641153 for details.
 *  */

add_task(async _ => {
  // Manually add the storage permission.
  PermissionTestUtils.add(
    TEST_DOMAIN,
    "3rdPartyStorage^https://tracking.example.org",
    Services.perms.ALLOW_ACTION
  );

  registerCleanupFunction(_ => {
    Services.perms.removeAll();
  });

  AntiTracking._createTask({
    name: "Test that we can use service worker if we have the storage access permission",
    cookieBehavior: BEHAVIOR_REJECT_TRACKER,
    allowList: false,
    callback: async _ => {
      await navigator.serviceWorker
        .register("empty.js")
        .then(
          _ => {
            ok(true, "ServiceWorker can be used!");
          },
          _ => {
            ok(false, "ServiceWorker can be used!");
          }
        )
        .catch(e => ok(false, "Promise rejected: " + e));
    },
    extraPrefs: [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
    expectedBlockingNotifications: 0,
    runInPrivateWindow: false,
    iframeSandbox: null,
    accessRemoval: null,
    callbackAfterRemoval: null,
  });

  AntiTracking._createTask({
    name: "Test again to check if we can still use service worker without hit the assertion.",
    cookieBehavior: BEHAVIOR_REJECT_TRACKER,
    allowList: false,
    callback: async _ => {
      await navigator.serviceWorker
        .register("empty.js")
        .then(
          _ => {
            ok(true, "ServiceWorker can be used!");
          },
          _ => {
            ok(false, "ServiceWorker can be used!");
          }
        )
        .catch(e => ok(false, "Promise rejected: " + e));
    },
    extraPrefs: [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
    expectedBlockingNotifications: 0,
    runInPrivateWindow: false,
    iframeSandbox: null,
    accessRemoval: null,
    callbackAfterRemoval: null,
  });

  AntiTracking._createTask({
    name: "Test again to check if we cannot use service worker in a sandbox iframe without hit the assertion.",
    cookieBehavior: BEHAVIOR_REJECT_TRACKER,
    allowList: false,
    callback: async _ => {
      await navigator.serviceWorker
        .register("empty.js")
        .then(
          _ => {
            ok(false, "ServiceWorker cannot be used in sandbox iframe!");
          },
          _ => {
            ok(true, "ServiceWorker cannot be used in sandbox iframe!");
          }
        )
        .catch(e => ok(false, "Promise rejected: " + e));
    },
    extraPrefs: [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
    expectedBlockingNotifications:
      Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, // expect blocking notifications,
    runInPrivateWindow: false,
    iframeSandbox: "allow-scripts allow-same-origin",
    accessRemoval: null,
    callbackAfterRemoval: null,
  });

  const NESTED_THIRD_PARTY_PAGE =
    TEST_DOMAIN + TEST_PATH + "3rdPartyRelay.html?" + TEST_3RD_PARTY_PAGE;

  AntiTracking._createTask({
    name: "Test again to check if we can use service worker in a nested iframe without hit the assertion.",
    cookieBehavior: BEHAVIOR_REJECT_TRACKER,
    allowList: false,
    callback: async _ => {
      await navigator.serviceWorker
        .register("empty.js")
        .then(
          _ => {
            ok(true, "ServiceWorker can be used in nested iframe!");
          },
          _ => {
            ok(false, "ServiceWorker can be used in nested iframe!");
          }
        )
        .catch(e => ok(false, "Promise rejected: " + e));
    },
    extraPrefs: [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
    expectedBlockingNotifications: 0,
    runInPrivateWindow: false,
    iframeSandbox: null,
    accessRemoval: null,
    callbackAfterRemoval: null,
    thirdPartyPage: NESTED_THIRD_PARTY_PAGE,
  });
});