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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";
/**
* Tests that when receiving the "clear-site-data" header - with dFPI enabled -
* we clear storage under the correct partition.
*/
const { SiteDataTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/SiteDataTestUtils.sys.mjs"
);
const HOST_A = "example.com";
const HOST_B = "example.org";
const ORIGIN_A = `https://${HOST_A}`;
const ORIGIN_B = `https://${HOST_B}`;
const CLEAR_SITE_DATA_PATH = `/${TEST_PATH}clearSiteData.sjs`;
const CLEAR_SITE_DATA_URL_ORIGIN_B = ORIGIN_B + CLEAR_SITE_DATA_PATH;
const CLEAR_SITE_DATA_URL_ORIGIN_A = ORIGIN_A + CLEAR_SITE_DATA_PATH;
const THIRD_PARTY_FRAME_ID_ORIGIN_B = "thirdPartyFrame";
const THIRD_PARTY_FRAME_ID_ORIGIN_A = "thirdPartyFrame2";
const STORAGE_KEY = "testKey";
// Skip localStorage tests when using legacy localStorage. The legacy
// localStorage implementation does not support clearing data by principal. See
// Bug 1688221, Bug 1688665.
const skipLocalStorageTests = Services.prefs.getBoolPref(
"dom.storage.enable_unsupported_legacy_implementation"
);
/**
* Creates an iframe in the passed browser and waits for it to load.
* @param {Browser} browser - Browser to create the frame in.
* @param {String} src - Frame source url.
* @param {String} id - Frame id.
* @param {boolean} sandbox - Whether the frame should be sandboxed.
* @returns {Promise} - Resolves once the frame has loaded.
*/
function createFrame(browser, src, id, sandbox) {
return SpecialPowers.spawn(
browser,
[{ page: src, frameId: id, sandbox }],
async function (obj) {
await new content.Promise(resolve => {
let frame = content.document.createElement("iframe");
if (obj.sandbox) {
frame.setAttribute("sandbox", "allow-scripts");
}
frame.src = obj.page;
frame.id = obj.frameId;
frame.addEventListener("load", resolve, { once: true });
content.document.body.appendChild(frame);
});
}
);
}
/**
* Creates a new tab, loads a url and creates an iframe.
* Callers need to clean up the tab before the test ends.
* @param {String} firstPartyUrl - Url to load in tab.
* @param {String} thirdPartyUrl - Url to load in frame.
* @param {String} frameId - Id of iframe element.
* @param {boolean} sandbox - Whether the frame should be sandboxed.
* @returns {Promise} - Resolves with the tab and the frame BrowsingContext once
* the tab and the frame have loaded.
*/
async function createTabWithFrame(
firstPartyUrl,
thirdPartyUrl,
frameId,
sandbox
) {
// Create tab and wait for it to be loaded.
let tab = BrowserTestUtils.addTab(gBrowser, firstPartyUrl);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
// Create cross origin iframe.
await createFrame(tab.linkedBrowser, thirdPartyUrl, frameId, sandbox);
// Return BrowsingContext of created iframe.
return { tab, frameBC: tab.linkedBrowser.browsingContext.children[0] };
}
/**
* Test wrapper for the ClearSiteData tests.
* Loads ORIGIN_A and ORIGIN_B in two tabs and inserts a cross origin pointing
* to the other iframe each.
* Both frames ORIGIN_B under ORIGIN_A and ORIGIN_A under ORIGIN_B will be
* storage partitioned.
* Depending on the clearDataContext variable we then either navigate ORIGIN_A
* (as top level) or ORIGIN_B (as third party frame) to the clear-site-data
* endpoint.
* @param {function} cbPreClear - Called after initial setup, once top levels
* and frames have been loaded.
* @param {function} cbPostClear - Called after data has been cleared via the
* "Clear-Site-Data" header.
* @param {("firstParty"|"thirdPartyPartitioned")} clearDataContext - Whether to
* navigate to the path that sets the "Clear-Site-Data" header with the first or
* third party.
* @param {boolean} [sandboxFrame] - Whether the frames should be sandboxed. No
* sandbox by default.
*/
async function runClearSiteDataTest(
cbPreClear,
cbPostClear,
clearDataContext,
sandboxFrame = false
) {
// Create a tabs for origin A and B with cross origins frames B and A
let [
{ frameBC: frameContextB, tab: tabA },
{ frameBC: frameContextA, tab: tabB },
] = await Promise.all([
createTabWithFrame(
ORIGIN_A,
ORIGIN_B,
THIRD_PARTY_FRAME_ID_ORIGIN_B,
sandboxFrame
),
createTabWithFrame(
ORIGIN_B,
ORIGIN_A,
THIRD_PARTY_FRAME_ID_ORIGIN_A,
sandboxFrame
),
]);
let browserA = tabA.linkedBrowser;
let contextA = browserA.browsingContext;
let browserB = tabB.linkedBrowser;
let contextB = browserB.browsingContext;
// Run test callback before clear-site-data
if (cbPreClear) {
await cbPreClear(contextA, contextB, frameContextB, frameContextA);
}
// Navigate to path with clear-site-data header
// Depending on the clearDataContext variable we either do this with the
// top browser or the third party storage partitioned frame (B embedded in A).
info(`Opening path with clear-site-data-header for ${clearDataContext}`);
if (clearDataContext == "firstParty") {
// Open in new tab so we keep our current test tab intact. The
// post-clear-callback might need it.
await BrowserTestUtils.withNewTab(CLEAR_SITE_DATA_URL_ORIGIN_A, () => {});
} else if (clearDataContext == "thirdPartyPartitioned") {
// Navigate frame to path with clear-site-data header
await SpecialPowers.spawn(
browserA,
[
{
page: CLEAR_SITE_DATA_URL_ORIGIN_B,
frameId: THIRD_PARTY_FRAME_ID_ORIGIN_B,
},
],
async function (obj) {
await new content.Promise(resolve => {
let frame = content.document.getElementById(obj.frameId);
frame.addEventListener("load", resolve, { once: true });
frame.src = obj.page;
});
}
);
} else {
ok(false, "Invalid context requested for clear-site-data");
}
if (cbPostClear) {
await cbPostClear(contextA, contextB, frameContextB, frameContextA);
}
info("Cleaning up.");
BrowserTestUtils.removeTab(tabA);
BrowserTestUtils.removeTab(tabB);
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, resolve);
});
}
/**
* Create an origin with partitionKey.
* @param {String} originNoSuffix - Origin without origin attributes.
* @param {String} [firstParty] - First party to create partitionKey.
* @returns {String} Origin with suffix. If not passed this will return the
* umodified origin.
*/
function getOrigin(originNoSuffix, firstParty) {
let origin = originNoSuffix;
if (firstParty) {
let [scheme, host] = firstParty.split("://");
origin += `^partitionKey=(${scheme},${host})`;
}
return origin;
}
/**
* Sets a storage item for an origin.
* @param {("cookie"|"localStorage")} storageType - Which storage type to use.
* @param {String} originNoSuffix - Context to set storage item in.
* @param {String} [firstParty] - Optional first party domain to partition
* under.
* @param {String} key - Key of the entry.
* @param {String} value - Value of the entry.
*/
function setStorageEntry(storageType, originNoSuffix, firstParty, key, value) {
if (storageType != "cookie" && storageType != "localStorage") {
ok(false, "Invalid storageType passed");
return;
}
let origin = getOrigin(originNoSuffix, firstParty);
if (storageType == "cookie") {
SiteDataTestUtils.addToCookies({ origin, name: key, value });
return;
}
// localStorage
SiteDataTestUtils.addToLocalStorage(origin, key, value);
}
/**
* Tests whether a host sets a cookie.
* For the purpose of this test we assume that there is either one or no cookie
* set.
* This performs cookie lookups directly via the cookie service.
* @param {boolean} hasCookie - Whether we expect to see a cookie.
* @param {String} originNoSuffix - Origin the cookie is stored for.
* @param {String|null} firstParty - Whether to test for a partitioned cookie.
* If set this will be used to construct the partitionKey.
* @param {String} [key] - Expected key / name of the cookie.
* @param {String} [value] - Expected value of the cookie.
*/
function testHasCookie(hasCookie, originNoSuffix, firstParty, key, value) {
let origin = getOrigin(originNoSuffix, firstParty);
let label = `${originNoSuffix}${
firstParty ? ` (partitioned under ${firstParty})` : ""
}`;
if (!hasCookie) {
ok(
!SiteDataTestUtils.hasCookies(origin),
`Cookie for ${label} is not set for key ${key}`
);
return;
}
ok(
SiteDataTestUtils.hasCookies(origin, [{ key, value }]),
`Cookie for ${label} is set ${key}=${value}`
);
}
/**
* Tests whether a context has a localStorage entry.
* @param {boolean} hasEntry - Whether we expect to see an entry.
* @param {String} originNoSuffix - Origin to test localStorage for.
* @param {String} [firstParty] - First party context to test under.
* @param {String} key - key of the localStorage item.
* @param {String} [expectedValue] - Expected value of the item.
*/
function testHasLocalStorageEntry(
hasEntry,
originNoSuffix,
firstParty,
key,
expectedValue
) {
if (key == null) {
ok(false, "localStorage key is mandatory");
return;
}
let label = `${originNoSuffix}${
firstParty ? ` (partitioned under ${firstParty})` : ""
}`;
let origin = getOrigin(originNoSuffix, firstParty);
if (hasEntry) {
let hasEntry = SiteDataTestUtils.hasLocalStorage(origin, [
{ key, value: expectedValue },
]);
ok(
hasEntry,
`localStorage for ${label} has expected value ${key}=${expectedValue}`
);
} else {
let hasEntry = SiteDataTestUtils.hasLocalStorage(origin);
ok(!hasEntry, `localStorage for ${label} is not set for key ${key}`);
}
}
/**
* Sets the initial storage entries used by the storage tests in this file.
* 1. first party ( A )
* 2. first party ( B )
* 3. third party partitioned ( B under A)
* 4. third party partitioned ( A under B)
* The entry values reflect which context they are set for.
* @param {("cookie"|"localStorage")} storageType - Storage type to initialize.
*/
async function setupInitialStorageState(storageType) {
if (storageType != "cookie" && storageType != "localStorage") {
ok(false, "Invalid storageType passed");
return;
}
// Set a first party entry
setStorageEntry(storageType, ORIGIN_A, null, STORAGE_KEY, "firstParty");
// Set a storage entry in the storage partitioned third party frame
setStorageEntry(
storageType,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
// Set a storage entry in the non storage partitioned third party page
setStorageEntry(storageType, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
// Set a storage entry in the second storage partitioned third party frame.
setStorageEntry(
storageType,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
info("Test that storage entries are set for all contexts");
if (storageType == "cookie") {
testHasCookie(true, ORIGIN_A, null, STORAGE_KEY, "firstParty");
testHasCookie(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
testHasCookie(
true,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
testHasCookie(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
return;
}
testHasLocalStorageEntry(true, ORIGIN_A, null, STORAGE_KEY, "firstParty");
testHasLocalStorageEntry(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
testHasLocalStorageEntry(
true,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
testHasLocalStorageEntry(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
}
add_setup(async function () {
info("Starting ClearSiteData test");
await SpecialPowers.flushPrefEnv();
await SpecialPowers.pushPrefEnv({
set: [
["dom.storage_access.enabled", true],
[
"network.cookie.cookieBehavior",
Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
],
[
"network.cookie.cookieBehavior.pbmode",
Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
],
["privacy.trackingprotection.enabled", false],
["privacy.trackingprotection.pbmode.enabled", false],
// Needed for SiteDataTestUtils#hasLocalStorage
["dom.storage.client_validation", false],
],
});
});
/**
* Test clearing partitioned cookies via clear-site-data header
* (Cleared via the cookie service).
*/
/**
* Tests that when a storage partitioned third party frame loads a site with
* "Clear-Site-Data", the cookies are cleared for only that partitioned frame.
*/
add_task(async function cookieClearThirdParty() {
await runClearSiteDataTest(
// Pre Clear-Site-Data
() => setupInitialStorageState("cookie"),
// Post Clear-Site-Data
() => {
info("Testing: First party cookie has not changed");
testHasCookie(true, ORIGIN_A, null, STORAGE_KEY, "firstParty");
info("Testing: Unpartitioned cookie has not changed");
testHasCookie(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
info("Testing: Partitioned cookie for HOST_B (HOST_A) has been cleared");
testHasCookie(false, ORIGIN_B, ORIGIN_A);
info("Testing: Partitioned cookie for HOST_A (HOST_B) has not changed");
testHasCookie(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
},
// Send clear-site-data header in partitioned third party context.
"thirdPartyPartitioned"
);
});
/**
* Tests that when a sandboxed storage partitioned third party frame loads a
* site with "Clear-Site-Data", no cookies are cleared and we don't crash.
* Crash details in Bug 1686938.
*/
add_task(async function cookieClearThirdPartySandbox() {
await runClearSiteDataTest(
// Pre Clear-Site-Data
() => setupInitialStorageState("cookie"),
// Post Clear-Site-Data
() => {
info("Testing: First party cookie has not changed");
testHasCookie(true, ORIGIN_A, null, STORAGE_KEY, "firstParty");
info("Testing: Unpartitioned cookie has not changed");
testHasCookie(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
info("Testing: Partitioned cookie for HOST_B (HOST_A) has not changed");
testHasCookie(
true,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
info("Testing: Partitioned cookie for HOST_A (HOST_B) has not changed");
testHasCookie(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
},
// Send clear-site-data header in partitioned third party context.
"thirdPartyPartitioned",
true
);
});
/**
* Tests that when the we load a path with "Clear-Site-Data" at top level, the
* cookies are cleared only in the first party context.
*/
add_task(async function cookieClearFirstParty() {
await runClearSiteDataTest(
// Pre Clear-Site-Data
() => setupInitialStorageState("cookie"),
// Post Clear-Site-Data
() => {
info("Testing: First party cookie has been cleared");
testHasCookie(false, ORIGIN_A, null);
info("Testing: Unpartitioned cookie has not changed");
testHasCookie(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
info("Testing: Partitioned cookie for HOST_B (HOST_A) has not changed");
testHasCookie(
true,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
info("Testing: Partitioned cookie for HOST_A (HOST_B) has not changed");
testHasCookie(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
},
// Send clear-site-data header in first party context.
"firstParty"
);
});
/**
* Test clearing partitioned localStorage via clear-site-data header
* (Cleared via the quota manager).
*/
/**
* Tests that when a storage partitioned third party frame loads a site with
* "Clear-Site-Data", localStorage is cleared for only that partitioned frame.
*/
add_task(async function localStorageClearThirdParty() {
// Bug 1688221, Bug 1688665.
if (skipLocalStorageTests) {
info("Skipping test");
return;
}
await runClearSiteDataTest(
// Pre Clear-Site-Data
() => setupInitialStorageState("localStorage"),
// Post Clear-Site-Data
async () => {
info("Testing: First party localStorage has not changed");
testHasLocalStorageEntry(true, ORIGIN_A, null, STORAGE_KEY, "firstParty");
info("Testing: Unpartitioned localStorage has not changed");
testHasLocalStorageEntry(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
info(
"Testing: Partitioned localStorage for HOST_B (HOST_A) has been cleared"
);
testHasLocalStorageEntry(false, ORIGIN_B, ORIGIN_A, STORAGE_KEY);
info(
"Testing: Partitioned localStorage for HOST_A (HOST_B) has not changed"
);
testHasLocalStorageEntry(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
},
// Send clear-site-data header in partitioned third party context.
"thirdPartyPartitioned"
);
});
/**
* Tests that when the we load a path with "Clear-Site-Data" at top level,
* localStorage is cleared only in the first party context.
*/
add_task(async function localStorageClearFirstParty() {
// Bug 1688221, Bug 1688665.
if (skipLocalStorageTests) {
info("Skipping test");
return;
}
await runClearSiteDataTest(
// Pre Clear-Site-Data
() => setupInitialStorageState("localStorage"),
// Post Clear-Site-Data
() => {
info("Testing: First party localStorage has been cleared");
testHasLocalStorageEntry(false, ORIGIN_A, null, STORAGE_KEY);
info("Testing: Unpartitioned thirdParty localStorage has not changed");
testHasLocalStorageEntry(true, ORIGIN_B, null, STORAGE_KEY, "thirdParty");
info(
"Testing: Partitioned localStorage for HOST_B (HOST_A) has not changed"
);
testHasLocalStorageEntry(
true,
ORIGIN_B,
ORIGIN_A,
STORAGE_KEY,
"thirdPartyPartitioned"
);
info(
"Testing: Partitioned localStorage for HOST_A (HOST_B) has not changed"
);
testHasLocalStorageEntry(
true,
ORIGIN_A,
ORIGIN_B,
STORAGE_KEY,
"thirdPartyPartitioned2"
);
},
// Send clear-site-data header in first party context.
"firstParty"
);
});
|