summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_storageAccessDoorHanger.js
blob: 733eb342403929eb37260f13d6cc2bcf988b5375 (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
/* eslint-disable mozilla/no-arbitrary-setTimeout */
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/browser/modules/test/browser/head.js",
  this
);

const BLOCK = 0;
const ALLOW = 1;

async function testDoorHanger(
  choice,
  showPrompt,
  useEscape,
  topPage,
  maxConcurrent,
  disableWebcompat = false
) {
  info(
    `Running doorhanger test with choice #${choice}, showPrompt: ${showPrompt} and ` +
      `useEscape: ${useEscape}, topPage: ${topPage}, maxConcurrent: ${maxConcurrent}`
  );

  if (!showPrompt) {
    is(choice, ALLOW, "When not showing a prompt, we can only auto-grant");
    ok(
      !useEscape,
      "When not showing a prompt, we should not be trying to use the Esc key"
    );
  }

  await SpecialPowers.flushPrefEnv();
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.antitracking.enableWebcompat", !disableWebcompat],
      ["dom.storage_access.auto_grants", true],
      ["dom.storage_access.auto_grants.delayed", false],
      ["dom.storage_access.enabled", true],
      ["dom.storage_access.max_concurrent_auto_grants", maxConcurrent],
      ["dom.storage_access.prompt.testing", false],
      [
        "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",
      ],
      // Bug 1617611: Fix all the tests broken by "cookies SameSite=lax by default"
      ["network.cookie.sameSite.laxByDefault", false],
    ],
  });

  await UrlClassifierTestUtils.addTestTrackers();

  let tab = BrowserTestUtils.addTab(gBrowser, topPage);
  gBrowser.selectedTab = tab;

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  async function runChecks() {
    // We need to repeat this constant here since runChecks is stringified
    // and sent to the content process.
    const BLOCK = 0;

    await new Promise(resolve => {
      addEventListener(
        "message",
        function onMessage(e) {
          if (e.data.startsWith("choice:")) {
            window.choice = e.data.split(":")[1];
            window.useEscape = e.data.split(":")[3];
            removeEventListener("message", onMessage);
            resolve();
          }
        },
        false
      );
      parent.postMessage("getchoice", "*");
    });

    /* import-globals-from storageAccessAPIHelpers.js */
    await noStorageAccessInitially();

    is(document.cookie, "", "No cookies for me");
    document.cookie = "name=value";
    is(document.cookie, "", "No cookies for me");

    await fetch("server.sjs")
      .then(r => r.text())
      .then(text => {
        is(text, "cookie-not-present", "We should not have cookies");
      });
    // Let's do it twice.
    await fetch("server.sjs")
      .then(r => r.text())
      .then(text => {
        is(text, "cookie-not-present", "We should not have cookies");
      });

    is(document.cookie, "", "Still no cookies for me");

    /* import-globals-from storageAccessAPIHelpers.js */
    await callRequestStorageAccess();

    if (choice == BLOCK) {
      // We've said no, so cookies are still blocked
      is(document.cookie, "", "Still no cookies for me");
      document.cookie = "name=value";
      is(document.cookie, "", "No cookies for me");
    } else {
      // We've said yes, so cookies are allowed now
      is(document.cookie, "", "No cookies for me");
      document.cookie = "name=value";
      is(document.cookie, "name=value", "I have the cookies!");
    }
  }

  let permChanged;
  // Only create the promise when we're going to click one of the allow buttons.
  if (choice != BLOCK) {
    permChanged = TestUtils.topicObserved("perm-changed", (subject, data) => {
      let result;
      if (choice == ALLOW) {
        result =
          subject &&
          subject
            .QueryInterface(Ci.nsIPermission)
            .type.startsWith("3rdPartyStorage^") &&
          subject.principal.origin == new URL(topPage).origin &&
          data == "added";
      }
      return result;
    });
  }
  let shownPromise = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  shownPromise.then(async _ => {
    if (topPage != gBrowser.currentURI.spec) {
      return;
    }
    ok(showPrompt, "We shouldn't show the prompt when we don't intend to");
    let notification = await new Promise(function poll(resolve) {
      let notification = PopupNotifications.getNotification(
        "storage-access",
        browser
      );
      if (notification) {
        resolve(notification);
        return;
      }
      setTimeout(poll, 10);
    });
    Assert.ok(notification, "Should have gotten the notification");

    if (choice == BLOCK) {
      if (useEscape) {
        EventUtils.synthesizeKey("KEY_Escape", {}, window);
      } else {
        await clickSecondaryAction();
      }
    } else if (choice == ALLOW) {
      await clickMainAction();
    }
    if (choice != BLOCK) {
      await permChanged;
    }
  });

  let url = TEST_3RD_PARTY_PAGE + "?disableWaitUntilPermission";
  let ct = SpecialPowers.spawn(
    browser,
    [{ page: url, callback: runChecks.toString(), choice, useEscape }],
    async function (obj) {
      await new content.Promise(resolve => {
        let ifr = content.document.createElement("iframe");
        ifr.onload = function () {
          info("Sending code to the 3rd party content");
          ifr.contentWindow.postMessage(obj.callback, "*");
        };

        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;
          }

          if (event.data == "getchoice") {
            ifr.contentWindow.postMessage(
              "choice:" + obj.choice + ":useEscape:" + obj.useEscape,
              "*"
            );
            return;
          }

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

        content.document.body.appendChild(ifr);
        ifr.src = obj.page;
      });
    }
  );
  if (showPrompt) {
    await Promise.all([ct, shownPromise]);
  } else {
    await Promise.all([ct, permChanged]);
  }

  let permissionPopupPromise = BrowserTestUtils.waitForEvent(
    window,
    "popupshown",
    true,
    event => event.target == gPermissionPanel._permissionPopup
  );
  gPermissionPanel._identityPermissionBox.click();
  await permissionPopupPromise;
  let permissionItem = document.querySelector(
    ".permission-popup-permission-item-3rdPartyStorage"
  );
  ok(permissionItem, "Permission item exists");
  ok(
    BrowserTestUtils.is_visible(permissionItem),
    "Permission item visible in the identity panel"
  );
  let permissionLearnMoreLink = document.getElementById(
    "permission-popup-storage-access-permission-learn-more"
  );
  ok(permissionLearnMoreLink, "Permission learn more link exists");
  ok(
    BrowserTestUtils.is_visible(permissionLearnMoreLink),
    "Permission learn more link is visible in the identity panel"
  );
  permissionPopupPromise = BrowserTestUtils.waitForEvent(
    gPermissionPanel._permissionPopup,
    "popuphidden"
  );
  gPermissionPanel._permissionPopup.hidePopup();
  await permissionPopupPromise;

  BrowserTestUtils.removeTab(tab);

  UrlClassifierTestUtils.cleanupTestTrackers();
}

async function preparePermissionsFromOtherSites(topPage) {
  info("Faking permissions from other sites");
  let type = "3rdPartyStorage^https://tracking.example.org";
  let permission = Services.perms.ALLOW_ACTION;
  let expireType = Services.perms.EXPIRE_SESSION;
  if (topPage == TEST_TOP_PAGE) {
    // For the first page, don't do anything
  } else if (topPage == TEST_TOP_PAGE_2) {
    // For the second page, only add the permission from the first page
    PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);
  } else if (topPage == TEST_TOP_PAGE_3) {
    // For the third page, add the permissions from the first two pages
    PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_2, type, permission, expireType, 0);
  } else if (topPage == TEST_TOP_PAGE_4) {
    // For the fourth page, add the permissions from the first three pages
    PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_2, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_3, type, permission, expireType, 0);
  } else if (topPage == TEST_TOP_PAGE_5) {
    // For the fifth page, add the permissions from the first four pages
    PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_2, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_3, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_4, type, permission, expireType, 0);
  } else if (topPage == TEST_TOP_PAGE_6) {
    // For the sixth page, add the permissions from the first five pages
    PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_2, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_3, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_4, type, permission, expireType, 0);
    PermissionTestUtils.add(TEST_DOMAIN_5, type, permission, expireType, 0);
  } else {
    ok(false, "Unexpected top page: " + topPage);
  }
}

async function cleanUp() {
  info("Cleaning up.");
  SpecialPowers.clearUserPref("network.cookie.sameSite.laxByDefault");
  await new Promise(resolve => {
    Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value =>
      resolve()
    );
  });
}

async function runRound(topPage, showPrompt, maxConcurrent, disableWebcompat) {
  if (showPrompt) {
    await preparePermissionsFromOtherSites(topPage);
    await testDoorHanger(
      BLOCK,
      showPrompt,
      true,
      topPage,
      maxConcurrent,
      disableWebcompat
    );
    await cleanUp();
    await preparePermissionsFromOtherSites(topPage);
    await testDoorHanger(
      BLOCK,
      showPrompt,
      false,
      topPage,
      maxConcurrent,
      disableWebcompat
    );
    await cleanUp();
    await preparePermissionsFromOtherSites(topPage);
    await testDoorHanger(
      ALLOW,
      showPrompt,
      false,
      topPage,
      maxConcurrent,
      disableWebcompat
    );
    await cleanUp();
  } else {
    await preparePermissionsFromOtherSites(topPage);
    await testDoorHanger(
      ALLOW,
      showPrompt,
      false,
      topPage,
      maxConcurrent,
      disableWebcompat
    );
  }
  await cleanUp();
}

add_task(async function test_combinations() {
  await runRound(TEST_TOP_PAGE, false, 1);
  await runRound(TEST_TOP_PAGE_2, true, 1);
  await runRound(TEST_TOP_PAGE, false, 5);
  await runRound(TEST_TOP_PAGE_2, false, 5);
  await runRound(TEST_TOP_PAGE_3, false, 5);
  await runRound(TEST_TOP_PAGE_4, false, 5);
  await runRound(TEST_TOP_PAGE_5, false, 5);
  await runRound(TEST_TOP_PAGE_6, true, 5);
});

add_task(async function test_disableWebcompat() {
  await runRound(TEST_TOP_PAGE, true, 5, true);
});