summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/dynamicfpi_head.js
blob: 6eaa6205087730b8f656d7c7494ce01ae5990de1 (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
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* import-globals-from head.js */

"use strict";

this.DynamicFPIHelper = {
  getTestPageConfig(runInSecureContext) {
    if (runInSecureContext) {
      return {
        topPage: TEST_TOP_PAGE_HTTPS,
        thirdPartyPage: TEST_4TH_PARTY_STORAGE_PAGE_HTTPS,
        partitionKey: "(https,example.net)",
      };
    }
    return {
      topPage: TEST_TOP_PAGE,
      thirdPartyPage: TEST_4TH_PARTY_STORAGE_PAGE,
      partitionKey: "(http,example.net)",
    };
  },

  runTest(
    name,
    callback,
    cleanupFunction,
    extraPrefs,
    runInPrivateWindow,
    { runInSecureContext = false } = {}
  ) {
    add_task(async _ => {
      info(
        "Starting test `" +
          name +
          "' with dynamic FPI running in a " +
          (runInPrivateWindow ? "private" : "normal") +
          " window..."
      );

      await SpecialPowers.flushPrefEnv();
      await setCookieBehaviorPref(
        BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
        runInPrivateWindow
      );
      await SpecialPowers.pushPrefEnv({
        set: [
          ["dom.storage_access.enabled", true],
          [
            "privacy.partition.always_partition_third_party_non_cookie_storage",
            true,
          ],
          ["privacy.trackingprotection.enabled", false],
          ["privacy.trackingprotection.pbmode.enabled", false],
          ["privacy.trackingprotection.annotate_channels", true],
          ["privacy.dynamic_firstparty.use_site", true],
          ["dom.security.https_first_pbm", false],
          [
            "privacy.restrict3rdpartystorage.userInteractionRequiredForHosts",
            "not-tracking.example.com",
          ],
        ],
      });

      if (extraPrefs && Array.isArray(extraPrefs) && extraPrefs.length) {
        await SpecialPowers.pushPrefEnv({ set: extraPrefs });
      }

      let win = window;
      if (runInPrivateWindow) {
        win = OpenBrowserWindow({ private: true });
        await TestUtils.topicObserved("browser-delayed-startup-finished");
      }

      const { topPage, thirdPartyPage, partitionKey } =
        this.getTestPageConfig(runInSecureContext);

      info("Creating a new tab");
      let tab = BrowserTestUtils.addTab(win.gBrowser, topPage);
      win.gBrowser.selectedTab = tab;

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

      info("Check the cookieJarSettings of the browser object");
      ok(
        browser.cookieJarSettings,
        "The browser object has the cookieJarSettings."
      );
      is(
        browser.cookieJarSettings.cookieBehavior,
        Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
        "The cookieJarSettings has the correct cookieBehavior"
      );
      is(
        browser.cookieJarSettings.partitionKey,
        partitionKey,
        "The cookieJarSettings has the correct partitionKey"
      );

      info("Creating a 3rd party content");
      await SpecialPowers.spawn(
        browser,
        [
          {
            page: thirdPartyPage,
            callback: callback.toString(),
            partitionKey,
          },
        ],
        async obj => {
          await new content.Promise(resolve => {
            let ifr = content.document.createElement("iframe");
            ifr.onload = async _ => {
              await SpecialPowers.spawn(ifr, [obj], async obj => {
                is(
                  content.document.nodePrincipal.originAttributes.partitionKey,
                  "",
                  "We don't have first-party set on nodePrincipal"
                );
                is(
                  content.document.effectiveStoragePrincipal.originAttributes
                    .partitionKey,
                  obj.partitionKey,
                  "We have first-party set on storagePrincipal"
                );
              });
              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;
              }

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

            content.document.body.appendChild(ifr);
            ifr.src = obj.page;
          });
        }
      );

      info("Removing the tab");
      BrowserTestUtils.removeTab(tab);

      if (runInPrivateWindow) {
        win.close();
      }
    });

    add_task(async _ => {
      info("Cleaning up.");
      if (cleanupFunction) {
        await cleanupFunction();
      }

      // While running these tests we typically do not have enough idle time to do
      // GC reliably, so force it here.
      /* import-globals-from antitracking_head.js */
      forceGC();
    });
  },
};