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
|
/* global allowListed */
async function hasStorageAccessInitially() {
let hasAccess = await document.hasStorageAccess();
ok(hasAccess, "Has storage access");
}
async function noStorageAccessInitially() {
let hasAccess = await document.hasStorageAccess();
ok(!hasAccess, "Doesn't yet have storage access");
}
async function stillNoStorageAccess() {
let hasAccess = await document.hasStorageAccess();
ok(!hasAccess, "Still doesn't have storage access");
}
async function callRequestStorageAccess(callback, expectFail) {
SpecialPowers.wrap(document).notifyUserGestureActivation();
let origin = new URL(location.href).origin;
let effectiveCookieBehavior = SpecialPowers.isContentWindowPrivate(window)
? SpecialPowers.Services.prefs.getIntPref(
"network.cookie.cookieBehavior.pbmode"
)
: SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior");
let success = true;
// We only grant storage exceptions when the reject tracker behavior is enabled.
let rejectTrackers =
[
SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
SpecialPowers.Ci.nsICookieService
.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
].includes(effectiveCookieBehavior) && !isOnContentBlockingAllowList();
const TEST_ANOTHER_3RD_PARTY_ORIGIN = SpecialPowers.useRemoteSubframes
? "http://another-tracking.example.net"
: "https://another-tracking.example.net";
// With another-tracking.example.net, we're same-eTLD+1, so the first try succeeds.
if (origin != TEST_ANOTHER_3RD_PARTY_ORIGIN) {
if (rejectTrackers) {
let p;
let threw = false;
try {
p = document.requestStorageAccess();
} catch (e) {
threw = true;
}
ok(!threw, "requestStorageAccess should not throw");
try {
if (callback) {
if (expectFail) {
await p.catch(_ => callback());
success = false;
} else {
await p.then(_ => callback());
}
} else {
await p;
}
} catch (e) {
success = false;
} finally {
SpecialPowers.wrap(document).clearUserGestureActivation();
}
ok(!success, "Should not have worked without user interaction");
await noStorageAccessInitially();
await interactWithTracker();
SpecialPowers.wrap(document).notifyUserGestureActivation();
}
if (
effectiveCookieBehavior ==
SpecialPowers.Ci.nsICookieService.BEHAVIOR_ACCEPT &&
!isOnContentBlockingAllowList()
) {
try {
if (callback) {
if (expectFail) {
await document.requestStorageAccess().catch(_ => callback());
success = false;
} else {
await document.requestStorageAccess().then(_ => callback());
}
} else {
await document.requestStorageAccess();
}
} catch (e) {
success = false;
} finally {
SpecialPowers.wrap(document).clearUserGestureActivation();
}
ok(success, "Should not have thrown");
await hasStorageAccessInitially();
await interactWithTracker();
SpecialPowers.wrap(document).notifyUserGestureActivation();
}
}
let p;
let threw = false;
try {
p = document.requestStorageAccess();
} catch (e) {
threw = true;
}
let rejected = false;
try {
if (callback) {
if (expectFail) {
await p.catch(_ => callback());
rejected = true;
} else {
await p.then(_ => callback());
}
} else {
await p;
}
} catch (e) {
rejected = true;
} finally {
SpecialPowers.wrap(document).clearUserGestureActivation();
}
success = !threw && !rejected;
let hasAccess = await document.hasStorageAccess();
is(
hasAccess,
success,
"Should " + (success ? "" : "not ") + "have storage access now"
);
if (
success &&
rejectTrackers &&
window.location.search != "?disableWaitUntilPermission" &&
origin != TEST_ANOTHER_3RD_PARTY_ORIGIN
) {
let protocol = isSecureContext ? "https" : "http";
// Wait until the permission is visible in parent process to avoid race
// conditions. We don't need to wait the permission to be visible in content
// processes since the content process doesn't rely on the permission to
// know the storage access is updated.
let originURI = SpecialPowers.Services.io.newURI(window.origin);
let site = SpecialPowers.Services.eTLD.getSite(originURI);
await waitUntilPermission(
`${protocol}://example.net/browser/toolkit/components/antitracking/test/browser/page.html`,
"3rdPartyFrameStorage^" + site
);
}
return [threw, rejected];
}
async function waitUntilPermission(
url,
name,
value = SpecialPowers.Services.perms.ALLOW_ACTION
) {
let originAttributes = SpecialPowers.isContentWindowPrivate(window)
? { privateBrowsingId: 1 }
: {};
await new Promise(resolve => {
let id = setInterval(async _ => {
if (
await SpecialPowers.testPermission(name, value, {
url,
originAttributes,
})
) {
clearInterval(id);
resolve();
}
}, 0);
});
}
async function interactWithTracker() {
await new Promise(resolve => {
let orionmessage = onmessage;
onmessage = _ => {
onmessage = orionmessage;
resolve();
};
info("Let's interact with the tracker");
window.open(
"/browser/toolkit/components/antitracking/test/browser/3rdPartyOpenUI.html?messageme"
);
});
// Wait until the user interaction permission becomes visible in our process
await waitUntilPermission(window.origin, "storageAccessAPI");
}
function isOnContentBlockingAllowList() {
// We directly check the window.allowListed here instead of checking the
// permission. The allow list permission might not be available since it is
// not in the preload list.
return window.allowListed;
}
async function registerServiceWorker(win, url) {
let reg = await win.navigator.serviceWorker.register(url);
if (reg.installing.state !== "activated") {
await new Promise(resolve => {
let w = reg.installing;
w.addEventListener("statechange", function onStateChange() {
if (w.state === "activated") {
w.removeEventListener("statechange", onStateChange);
resolve();
}
});
});
}
return reg.active;
}
function sendAndWaitWorkerMessage(target, worker, message) {
return new Promise(resolve => {
worker.addEventListener(
"message",
msg => {
resolve(msg.data);
},
{ once: true }
);
target.postMessage(message);
});
}
|