summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_hasStorageAccess_alwaysPartition.js
blob: d89b0b1103e187e5f58ff4143b7a1f07777149cd (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
// This test ensures HasStorageAccess API returns the right value under different
// scenarios.

var settings = [
  // same-origin no-tracker
  {
    name: "Test whether same-origin non-tracker frame has storage access",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_DOMAIN + TEST_PATH + "3rdParty.html",
  },
  // 3rd-party no-tracker
  {
    name: "Test whether 3rd-party non-tracker frame has storage access",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_4TH_PARTY_PAGE,
  },
  // 3rd-party no-tracker with permission
  {
    name: "Test whether 3rd-party non-tracker frame has storage access when storage permission is granted before",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_4TH_PARTY_PAGE,
    setup: () => {
      let type = "3rdPartyStorage^http://not-tracking.example.com";
      let permission = Services.perms.ALLOW_ACTION;
      let expireType = Services.perms.EXPIRE_SESSION;
      PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);

      registerCleanupFunction(_ => {
        Services.perms.removeAll();
      });
    },
  },
  // 3rd-party tracker
  {
    name: "Test whether 3rd-party tracker frame has storage access",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_3RD_PARTY_PAGE,
  },
  // 3rd-party tracker with permission
  {
    name: "Test whether 3rd-party tracker frame has storage access when storage access permission is granted before",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_3RD_PARTY_PAGE,
    setup: () => {
      let type = "3rdPartyStorage^https://tracking.example.org";
      let permission = Services.perms.ALLOW_ACTION;
      let expireType = Services.perms.EXPIRE_SESSION;
      PermissionTestUtils.add(TEST_DOMAIN, type, permission, expireType, 0);

      registerCleanupFunction(_ => {
        Services.perms.removeAll();
      });
    },
  },
  // same-site 3rd-party tracker
  {
    name: "Test whether same-site 3rd-party tracker frame has storage access",
    topPage: TEST_TOP_PAGE,
    thirdPartyPage: TEST_ANOTHER_3RD_PARTY_PAGE,
  },
  // same-origin 3rd-party tracker
  {
    name: "Test whether same-origin 3rd-party tracker frame has storage access",
    topPage: TEST_ANOTHER_3RD_PARTY_DOMAIN + TEST_PATH + "page.html",
    thirdPartyPage: TEST_ANOTHER_3RD_PARTY_PAGE,
  },
];

const allBlocked = Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_ALL;
const foreignBlocked = Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_FOREIGN;
const trackerBlocked = Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER;

var testCases = [
  {
    behavior: BEHAVIOR_ACCEPT, // 0
    cases: [
      [true] /* same-origin non-tracker */,
      [true] /* 3rd-party non-tracker */,
      [true] /* 3rd-party non-tracker with permission */,
      [true] /* 3rd-party tracker */,
      [true] /* 3rd-party tracker with permission */,
      [true] /* same-site tracker */,
      [true] /* same-origin tracker */,
    ],
  },
  {
    behavior: BEHAVIOR_REJECT_FOREIGN, // 1
    cases: [
      [true] /* same-origin non-tracker */,
      [false, foreignBlocked] /* 3rd-party non-tracker */,
      [
        SpecialPowers.Services.prefs.getBoolPref(
          "network.cookie.rejectForeignWithExceptions.enabled"
        ),
        foreignBlocked,
      ] /* 3rd-party tracker with permission */,
      [false, foreignBlocked] /* 3rd-party tracker */,
      [
        SpecialPowers.Services.prefs.getBoolPref(
          "network.cookie.rejectForeignWithExceptions.enabled"
        ),
        foreignBlocked,
      ] /* 3rd-party non-tracker with permission */,
      [true] /* same-site tracker */,
      [true] /* same-origin tracker */,
    ],
  },
  {
    behavior: BEHAVIOR_REJECT, // 2
    cases: [
      [false, allBlocked] /* same-origin non-tracker */,
      [false, allBlocked] /* 3rd-party non-tracker */,
      [false, allBlocked] /* 3rd-party non-tracker with permission */,
      [false, allBlocked] /* 3rd-party tracker */,
      [false, allBlocked] /* 3rd-party tracker with permission */,
      [false, allBlocked] /* same-site tracker */,
      [false, allBlocked] /* same-origin tracker */,
    ],
  },
  {
    behavior: BEHAVIOR_LIMIT_FOREIGN, // 3
    cases: [
      [true] /* same-origin non-tracker */,
      [false, foreignBlocked] /* 3rd-party non-tracker */,
      [false, foreignBlocked] /* 3rd-party non-tracker with permission */,
      [false, foreignBlocked] /* 3rd-party tracker */,
      [false, foreignBlocked] /* 3rd-party tracker with permission */,
      [true] /* same-site tracker */,
      [true] /* same-origin tracker */,
    ],
  },
  {
    behavior: BEHAVIOR_REJECT_TRACKER, // 4
    cases: [
      [true] /* same-origin non-tracker */,
      [true] /* 3rd-party non-tracker */,
      [true] /* 3rd-party non-tracker with permission */,
      [false, trackerBlocked] /* 3rd-party tracker */,
      [true] /* 3rd-party tracker with permission */,
      [true] /* same-site tracker */,
      [true] /* same-origin tracker */,
    ],
  },
  {
    behavior: BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN, // 5
    cases: [
      [true] /* same-origin non-tracker */,
      [false] /* 3rd-party non-tracker */,
      [true] /* 3rd-party non-tracker with permission */,
      [false, trackerBlocked] /* 3rd-party tracker */,
      [true] /* 3rd-party tracker with permission */,
      [true] /* same-site tracker */,
      [true] /* same-origin tracker */,
    ],
  },
];

(function () {
  settings.forEach(setting => {
    ok(true, JSON.stringify(setting));
    if (setting.setup) {
      add_task(async _ => {
        setting.setup();
      });
    }

    testCases.forEach(test => {
      let [hasStorageAccess, expectedBlockingNotifications] =
        test.cases[settings.indexOf(setting)];
      let callback = hasStorageAccess
        ? async _ => {
            /* import-globals-from storageAccessAPIHelpers.js */
            await hasStorageAccessInitially();
          }
        : async _ => {
            /* import-globals-from storageAccessAPIHelpers.js */
            await noStorageAccessInitially();
          };

      AntiTracking._createTask({
        name: setting.name,
        cookieBehavior: test.behavior,
        allowList: false,
        callback,
        extraPrefs: [
          [
            "privacy.partition.always_partition_third_party_non_cookie_storage",
            true,
          ],
        ],
        expectedBlockingNotifications,
        runInPrivateWindow: false,
        iframeSandbox: null,
        accessRemoval: null,
        callbackAfterRemoval: null,
        topPage: setting.topPage,
        thirdPartyPage: setting.thirdPartyPage,
      });
    });

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