summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_permissionPropagation.js
blob: 73dc9e7145faa90b39efac78a88a48ce7c5ed6e9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable mozilla/no-arbitrary-setTimeout */

/**
 * This test makes sure the when we grant the storage permission, the
 * permission is also propagated to iframes within the same agent cluster,
 * but not to iframes in the other tabs.
 */

async function createTab(topUrl, iframeCount, opener, params) {
  let newTab;
  let browser;
  if (opener) {
    let promise = BrowserTestUtils.waitForNewTab(gBrowser, topUrl);
    await SpecialPowers.spawn(opener, [topUrl], function (url) {
      content.window.open(url, "_blank");
    });
    newTab = await promise;
    browser = gBrowser.getBrowserForTab(newTab);
  } else {
    newTab = BrowserTestUtils.addTab(gBrowser, topUrl);

    browser = gBrowser.getBrowserForTab(newTab);
    await BrowserTestUtils.browserLoaded(browser);
  }

  await SpecialPowers.spawn(
    browser,
    [params, iframeCount, createTrackerFrame.toString()],
    async function (params, count, fn) {
      // eslint-disable-next-line no-eval
      let fnCreateTrackerFrame = eval(`(() => (${fn}))()`);
      await fnCreateTrackerFrame(params, count, ifr => {
        ifr.contentWindow.postMessage(
          { callback: params.msg.blockingCallback },
          "*"
        );
      });
    }
  );

  return Promise.resolve(newTab);
}

async function createTrackerFrame(params, count, callback) {
  let iframes = [];
  for (var i = 0; i < count; i++) {
    iframes[i] = content.document.createElement("iframe");
    await new content.Promise(resolve => {
      iframes[i].id = "ifr" + i;
      iframes[i].src = params.page;
      iframes[i].onload = resolve;
      content.document.body.appendChild(iframes[i]);
    });

    await new content.Promise(resolve => {
      content.addEventListener("message", function msg(event) {
        if (event.data.type == "finish") {
          content.removeEventListener("message", msg);
          resolve();
          return;
        }

        if (event.data.type == "ok") {
          ok(event.data.what, event.data.msg);
          return;
        }

        if (event.data.type == "info") {
          info(event.data.msg);
          return;
        }

        ok(false, "Unknown message");
      });

      callback(iframes[i]);
    });
  }
}

async function testPermission(browser, block, params, frameNumber) {
  await SpecialPowers.spawn(
    browser,
    [block, params, frameNumber],
    async function (block, params, frameNumber) {
      for (let i = 0; ; i++) {
        let ifr = content.document.getElementById("ifr" + i);
        if (!ifr || (frameNumber !== undefined && i != frameNumber)) {
          break;
        }

        await new content.Promise(resolve => {
          content.addEventListener("message", function msg(event) {
            if (event.data.type == "finish") {
              content.removeEventListener("message", msg);
              resolve();
              return;
            }

            if (event.data.type == "ok") {
              ok(event.data.what, event.data.msg);
              return;
            }

            if (event.data.type == "info") {
              info(event.data.msg);
              return;
            }

            ok(false, "Unknown message");
          });

          if (block) {
            ifr.contentWindow.postMessage(
              { callback: params.msg.blockingCallback },
              "*"
            );
          } else {
            ifr.contentWindow.postMessage(
              { callback: params.msg.nonBlockingCallback },
              "*"
            );
          }
        });
      }
    }
  );
}

add_task(async function testPermissionGrantedOn3rdParty() {
  info("Starting permission propagation test");

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.storage_access.enabled", true],
      [
        "network.cookie.cookieBehavior",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      [
        "network.cookie.cookieBehavior.pbmode",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", true],
      [
        "privacy.restrict3rdpartystorage.userInteractionRequiredForHosts",
        "tracking.example.com,tracking.example.org",
      ],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  let msg = {};
  msg.blockingCallback = (async _ => {
    /* import-globals-from storageAccessAPIHelpers.js */
    await noStorageAccessInitially();

    await new Promise(resolve => {
      // eslint-disable-next-line no-undef
      let w = worker;
      w.addEventListener(
        "message",
        e => {
          ok(!e.data, "IDB is disabled");
          resolve();
        },
        { once: true }
      );
      w.postMessage("go");
    });
  }).toString();

  msg.nonBlockingCallback = (async _ => {
    /* import-globals-from storageAccessAPIHelpers.js */
    console.log("test hasStorageAccessInitially\n");
    await hasStorageAccessInitially();

    await new Promise(resolve => {
      // eslint-disable-next-line no-undef
      let w = worker;
      w.addEventListener(
        "message",
        e => {
          ok(e.data, "IDB is enabled");
          resolve();
        },
        { once: true }
      );
      w.postMessage("go");
    });
  }).toString();

  let top = TEST_TOP_PAGE;
  let page = TEST_3RD_PARTY_PAGE_WORKER;
  let pageOther =
    TEST_ANOTHER_3RD_PARTY_DOMAIN + TEST_PATH + "3rdPartyWorker.html";
  let params = { page, msg, pageOther };
  // Create 4 tabs:
  // 1. The first tab has two tracker iframes, said A & B.
  // 2. The second tab is opened by the first tab, and it has one tracker iframe, said C.
  // 3. The third tab has one tracker iframe, said D.
  // 4. The fourth tab is opened by the first tab but with a different top-level url).
  //    The tab has one tracker iframe, said E.
  //
  // This test grants permission on iframe A, which then should not propagate storage
  // permission to iframe B, C, D, E

  info("Creating the first tab");
  let tab1 = await createTab(top, 2, null, params);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  info("Creating the second tab");
  let tab2 = await createTab(top, 1, browser1 /* opener */, params);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  info("Creating the third tab");
  let tab3 = await createTab(top, 1, null, params);
  let browser3 = gBrowser.getBrowserForTab(tab3);

  info("Creating the fourth tab");
  let tab4 = await createTab(TEST_TOP_PAGE_2, 1, browser1, params);
  let browser4 = gBrowser.getBrowserForTab(tab4);

  info("Grant storage permission to the first iframe in the first tab");
  await SpecialPowers.spawn(browser1, [page, msg], async function (page, msg) {
    await new content.Promise(resolve => {
      content.addEventListener("message", function msg(event) {
        if (event.data.type == "finish") {
          content.removeEventListener("message", msg);
          resolve();
          return;
        }

        if (event.data.type == "ok") {
          ok(event.data.what, event.data.msg);
          return;
        }

        if (event.data.type == "info") {
          info(event.data.msg);
          return;
        }

        ok(false, "Unknown message");
      });

      let ifr = content.document.getElementById("ifr0");
      ifr.contentWindow.postMessage(
        {
          callback: (async _ => {
            /* import-globals-from storageAccessAPIHelpers.js */
            await callRequestStorageAccess();
          }).toString(),
        },
        "*"
      );
    });
  });

  info("Second iframe of the first tab should not have stroage permission");
  await testPermission(browser1, false /* block */, params, 0);
  await testPermission(browser1, true /* block */, params, 1);

  info("The iframe of the second tab should not have storage permission");
  await testPermission(browser2, true /* block */, params);

  info("The iframe of the third tab should not have storage permission");
  await testPermission(browser3, true /* block */, params);

  info("The iframe of the fourth tab should not have storage permission");
  await testPermission(browser4, true /* block */, params);

  info("Removing the tabs");
  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
  BrowserTestUtils.removeTab(tab3);
  BrowserTestUtils.removeTab(tab4);

  UrlClassifierTestUtils.cleanupTestTrackers();
});

add_task(async function () {
  info("Cleaning up.");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value =>
      resolve()
    );
  });
});

add_task(async function testPermissionGrantedOnFirstParty() {
  info("Starting permission propagation test");

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["dom.storage_access.enabled", true],
      [
        "network.cookie.cookieBehavior",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      [
        "network.cookie.cookieBehavior.pbmode",
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
      ],
      ["privacy.trackingprotection.enabled", false],
      ["privacy.trackingprotection.pbmode.enabled", false],
      ["privacy.trackingprotection.annotate_channels", true],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  let msg = {};
  msg.blockingCallback = (async _ => {
    /* import-globals-from storageAccessAPIHelpers.js */
    await noStorageAccessInitially();

    await new Promise(resolve => {
      // eslint-disable-next-line no-undef
      let w = worker;
      w.addEventListener(
        "message",
        e => {
          ok(!e.data, "IDB is disabled");
          resolve();
        },
        { once: true }
      );
      w.postMessage("go");
    });
  }).toString();

  msg.nonBlockingCallback = (async _ => {
    /* import-globals-from storageAccessAPIHelpers.js */
    console.log("test hasStorageAccessInitially\n");
    await hasStorageAccessInitially();

    await new Promise(resolve => {
      // eslint-disable-next-line no-undef
      let w = worker;
      w.addEventListener(
        "message",
        e => {
          ok(e.data, "IDB is enabled");
          resolve();
        },
        { once: true }
      );
      w.postMessage("go");
    });
  }).toString();

  let top = TEST_TOP_PAGE;
  let page = TEST_3RD_PARTY_PAGE_WORKER;
  let params = { page, msg };
  // Create 4 tabs:
  // 1. The first tab has two tracker iframes, said A & B.
  // 2. The second tab is opened by the first tab, and it has one tracker iframe, said C.
  // 3. The third tab has one tracker iframe, said D.
  // 4. The fourth tab is opened by the first tab but with a different top-level url).
  //    The tab has one tracker iframe, said E.
  //
  // This test grants permission on iframe A, which then should propagate storage
  // permission to iframe B & C, but not D, E

  info("Creating the first tab");
  let tab1 = await createTab(top, 2, null, params);
  let browser1 = gBrowser.getBrowserForTab(tab1);

  info("Creating the second tab");
  let tab2 = await createTab(top, 1, browser1 /* opener */, params);
  let browser2 = gBrowser.getBrowserForTab(tab2);

  info("Creating the third tab");
  let tab3 = await createTab(top, 1, null, params);
  let browser3 = gBrowser.getBrowserForTab(tab3);

  info("Creating the fourth tab");
  let tab4 = await createTab(TEST_TOP_PAGE_2, 1, browser1, params);
  let browser4 = gBrowser.getBrowserForTab(tab4);

  info("Grant storage permission to the first iframe in the first tab");
  let promise = BrowserTestUtils.waitForNewTab(gBrowser, page);
  await SpecialPowers.spawn(browser1, [page], async function (page) {
    content.window.open(page, "_blank");
  });
  let tab = await promise;
  BrowserTestUtils.removeTab(tab);

  info("Both iframs of the first tab should have stroage permission");
  await testPermission(browser1, false /* block */, params);

  info("The iframe of the second tab should have storage permission");
  await testPermission(browser2, false /* block */, params);

  info("The iframe of the third tab should not have storage permission");
  await testPermission(browser3, true /* block */, params);

  info("The iframe of the fourth tab should not have storage permission");
  await testPermission(browser4, true /* block */, params);

  info("Removing the tabs");
  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
  BrowserTestUtils.removeTab(tab3);
  BrowserTestUtils.removeTab(tab4);

  UrlClassifierTestUtils.cleanupTestTrackers();
});

add_task(async function () {
  info("Cleaning up.");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value =>
      resolve()
    );
  });
});