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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
"use strict";
requestLongerTimeout(8);
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/browser/base/content/test/general/head.js",
this
);
async function openAWindow(usePrivate) {
info("Creating a new " + (usePrivate ? "private" : "normal") + " window");
let win = OpenBrowserWindow({ private: usePrivate });
await TestUtils.topicObserved(
"browser-delayed-startup-finished",
subject => subject == win
).then(() => win);
await BrowserTestUtils.firstBrowserLoaded(win);
return win;
}
async function testOnWindowBody(win, expectedReferrer, rp) {
let browser = win.gBrowser;
let tab = browser.selectedTab;
let b = browser.getBrowserForTab(tab);
await promiseTabLoadEvent(tab, TEST_TOP_PAGE);
info("Loading tracking scripts and tracking images");
let referrer = await SpecialPowers.spawn(
b,
[{ rp }],
async function ({ rp }) {
{
let src = content.document.createElement("script");
let p = new content.Promise(resolve => {
src.onload = resolve;
});
content.document.body.appendChild(src);
if (rp) {
src.referrerPolicy = rp;
}
src.src =
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?what=script";
await p;
}
{
let img = content.document.createElement("img");
let p = new content.Promise(resolve => {
img.onload = resolve;
});
content.document.body.appendChild(img);
if (rp) {
img.referrerPolicy = rp;
}
img.src =
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?what=image";
await p;
}
{
let iframe = content.document.createElement("iframe");
let p = new content.Promise(resolve => {
iframe.onload = resolve;
});
content.document.body.appendChild(iframe);
if (rp) {
iframe.referrerPolicy = rp;
}
iframe.src =
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?what=iframe";
await p;
p = new content.Promise(resolve => {
content.onmessage = event => {
resolve(event.data);
};
});
iframe.contentWindow.postMessage("ping", "*");
return p;
}
}
);
is(referrer, expectedReferrer, "The correct referrer must be read from DOM");
await fetch(
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?result&what=script"
)
.then(r => r.text())
.then(text => {
is(text, expectedReferrer, "We sent the correct Referer header");
});
await fetch(
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?result&what=image"
)
.then(r => r.text())
.then(text => {
is(text, expectedReferrer, "We sent the correct Referer header");
});
await fetch(
"https://tracking.example.org/browser/toolkit/components/antitracking/test/browser/referrer.sjs?result&what=iframe"
)
.then(r => r.text())
.then(text => {
is(text, expectedReferrer, "We sent the correct Referer header");
});
}
async function closeAWindow(win) {
await BrowserTestUtils.closeWindow(win);
}
let gRecording = true;
let gScenarios = [];
let gRPs = [];
let gTests = { private: [], nonPrivate: [] };
const kPBPref = "network.http.referer.defaultPolicy.trackers.pbmode";
const kNonPBPref = "network.http.referer.defaultPolicy.trackers";
function recordScenario(isPrivate, expectedReferrer, rp) {
if (!gRPs.includes(rp)) {
gRPs.push(rp);
}
gScenarios.push({
private: isPrivate,
expectedReferrer,
rp,
pbPref: Services.prefs.getIntPref(kPBPref),
nonPBPref: Services.prefs.getIntPref(kNonPBPref),
});
}
async function testOnWindow(isPrivate, expectedReferrer, rp) {
if (gRecording) {
recordScenario(isPrivate, expectedReferrer, rp);
}
}
function compileScenarios() {
let keys = { false: [], true: [] };
for (let s of gScenarios) {
let key = {
rp: s.rp,
pbPref: s.pbPref,
nonPBPref: s.nonPBPref,
};
let skip = false;
for (let k of keys[s.private]) {
if (
key.rp == k.rp &&
key.pbPref == k.pbPref &&
key.nonPBPref == k.nonPBPref
) {
skip = true;
break;
}
}
if (!skip) {
keys[s.private].push(key);
gTests[s.private ? "private" : "nonPrivate"].push({
rp: s.rp,
pbPref: s.pbPref,
nonPBPref: s.nonPBPref,
expectedReferrer: s.expectedReferrer,
});
}
}
// Verify that all scenarios are checked
let counter = 1;
for (let s of gScenarios) {
let checked = false;
for (let tt in gTests) {
let isPrivate = tt == "private";
for (let t of gTests[tt]) {
if (
isPrivate == s.private &&
t.rp == s.rp &&
t.pbPref == s.pbPref &&
t.nonPBPref == s.nonPBPref &&
t.expectedReferrer == s.expectedReferrer
) {
checked = true;
break;
}
}
}
ok(checked, `Scenario number ${counter++} checked`);
}
}
async function executeTests() {
compileScenarios();
gRecording = false;
for (let mode in gTests) {
info(`Open a ${mode} window`);
while (gTests[mode].length) {
let test = gTests[mode].shift();
info(`Running test ${test.toSource()}`);
await SpecialPowers.pushPrefEnv({
set: [
["network.http.referer.defaultPolicy.trackers", test.nonPBPref],
["network.http.referer.defaultPolicy.trackers.pbmode", test.pbPref],
["dom.security.https_first_pbm", false],
["network.http.referer.disallowCrossSiteRelaxingDefault", false],
],
});
let win = await openAWindow(mode == "private");
await testOnWindowBody(win, test.expectedReferrer, test.rp);
await closeAWindow(win);
}
}
Services.prefs.clearUserPref(kPBPref);
Services.prefs.clearUserPref(kNonPBPref);
}
function pn(name, isPrivate) {
return isPrivate ? name + ".pbmode" : name;
}
async function testOnNoReferrer(isPrivate) {
// no-referrer pref when no-referrer is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, "", "no-referrer");
// strict-origin-when-cross-origin pref when no-referrer is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, "", "no-referrer");
// same-origin pref when no-referrer is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
await testOnWindow(isPrivate, "", "no-referrer");
// no-referrer pref when no-referrer is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
await testOnWindow(isPrivate, "", "no-referrer");
}
async function testOnSameOrigin(isPrivate) {
// same-origin pref when same-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, "", "same-origin");
// strict-origin-when-cross-origin pref when same-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, "", "same-origin");
// same-origin pref when same-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
await testOnWindow(isPrivate, "", "same-origin");
// same-origin pref when same-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
await testOnWindow(isPrivate, "", "same-origin");
}
async function testOnNoReferrerWhenDowngrade(isPrivate) {
// The setting referrer policy will be ignored if it is
// no-referrer-when-downgrade in private mode. It will fallback to the default
// value.
//
// The pref 'network.http.referer.disallowCrossSiteRelaxingDefault.pbmode'
// controls this behavior in private mode.
// no-referrer-when-downgrade pref when no-referrer-when-downgrade is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, TEST_TOP_PAGE, "no-referrer-when-downgrade");
// strict-origin-when-cross-origin pref when no-referrer-when-downgrade is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
if (isPrivate) {
await testOnWindow(isPrivate, TEST_DOMAIN, "no-referrer-when-downgrade");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "no-referrer-when-downgrade");
}
// same-origin pref when no-referrer-when-downgrade is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "no-referrer-when-downgrade");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "no-referrer-when-downgrade");
}
// no-referrer pref when no-referrer-when-downgrade is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "no-referrer-when-downgrade");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "no-referrer-when-downgrade");
}
}
async function testOnOrigin(isPrivate) {
// origin pref when origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "origin");
// strict-origin pref when origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "origin");
// same-origin pref when origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "origin");
// no-referrer pref when origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "origin");
}
async function testOnStrictOrigin(isPrivate) {
// strict-origin pref when strict-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin");
// strict-origin pref when strict-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin");
// same-origin pref when strict-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin");
// no-referrer pref when strict-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin");
}
async function testOnOriginWhenCrossOrigin(isPrivate) {
// The setting referrer policy will be ignored if it is
// origin-when-cross-origin in private mode. It will fallback to the default
// value. The pref controls this behavior mentioned above.
// no-referrer-when-downgrade pref when origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
if (isPrivate) {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "origin-when-cross-origin");
} else {
await testOnWindow(isPrivate, TEST_DOMAIN, "origin-when-cross-origin");
}
// strict-origin-when-cross-origin pref when origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "origin-when-cross-origin");
// same-origin pref when origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "origin-when-cross-origin");
} else {
await testOnWindow(isPrivate, TEST_DOMAIN, "origin-when-cross-origin");
}
// no-referrer pref when origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "origin-when-cross-origin");
} else {
await testOnWindow(isPrivate, TEST_DOMAIN, "origin-when-cross-origin");
}
}
async function testOnStrictOriginWhenCrossOrigin(isPrivate) {
// origin-when-cross-origin pref when strict-origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin-when-cross-origin");
// strict-origin-when-cross-origin pref when strict-origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin-when-cross-origin");
// same-origin pref when strict-origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin-when-cross-origin");
// no-referrer pref when strict-origin-when-cross-origin is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
await testOnWindow(isPrivate, TEST_DOMAIN, "strict-origin-when-cross-origin");
}
async function testOnUnsafeUrl(isPrivate) {
// The setting referrer policy will be ignored if it is unsafe in private
// mode. It will fallback to the default value. The pref controls this
// behavior mentioned above.
// no-referrer-when-downgrade pref when unsafe-url is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 3]],
});
await testOnWindow(isPrivate, TEST_TOP_PAGE, "unsafe-url");
// strict-origin-when-cross-origin pref when unsafe-url is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 2]],
});
if (isPrivate) {
await testOnWindow(isPrivate, TEST_DOMAIN, "unsafe-url");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "unsafe-url");
}
// same-origin pref when unsafe-url is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 1]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "unsafe-url");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "unsafe-url");
}
// no-referrer pref when unsafe-url is forced
await SpecialPowers.pushPrefEnv({
set: [[pn("network.http.referer.defaultPolicy.trackers", isPrivate), 0]],
});
if (isPrivate) {
await testOnWindow(isPrivate, "", "unsafe-url");
} else {
await testOnWindow(isPrivate, TEST_TOP_PAGE, "unsafe-url");
}
}
add_task(async function () {
info("Starting referrer default policy test");
await SpecialPowers.flushPrefEnv();
await SpecialPowers.pushPrefEnv({
set: [
[
"network.cookie.cookieBehavior",
Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
],
[
"network.cookie.cookieBehavior.pbmode",
Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
],
["network.http.referer.defaultPolicy", 3],
["privacy.trackingprotection.enabled", false],
["privacy.trackingprotection.pbmode.enabled", false],
["privacy.trackingprotection.annotate_channels", true],
],
});
// no-referrer-when-downgrade
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers", 3]],
});
await testOnWindow(false, TEST_TOP_PAGE, null);
// strict-origin-when-cross-origin
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers", 2]],
});
await testOnWindow(false, TEST_DOMAIN, null);
// same-origin
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers", 1]],
});
await testOnWindow(false, "", null);
// no-referrer
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers", 0]],
});
await testOnWindow(false, "", null);
// override with no-referrer
await testOnNoReferrer(false);
// override with same-origin
await testOnSameOrigin(false);
// override with no-referrer-when-downgrade
await testOnNoReferrerWhenDowngrade(false);
// override with origin
await testOnOrigin(false);
// override with strict-origin
await testOnStrictOrigin(false);
// override with origin-when-cross-origin
await testOnOriginWhenCrossOrigin(false);
// override with strict-origin-when-cross-origin
await testOnStrictOriginWhenCrossOrigin(false);
// override with unsafe-url
await testOnUnsafeUrl(false);
// Reset the pref.
Services.prefs.clearUserPref("network.http.referer.defaultPolicy.trackers");
// no-referrer-when-downgrade
await SpecialPowers.pushPrefEnv({
set: [
// Set both prefs, because if we only set the trackers pref, then the PB
// mode default policy pref (2) would apply!
["network.http.referer.defaultPolicy.pbmode", 3],
["network.http.referer.defaultPolicy.trackers.pbmode", 3],
],
});
await testOnWindow(true, TEST_TOP_PAGE, null);
// strict-origin-when-cross-origin
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers.pbmode", 2]],
});
await testOnWindow(true, TEST_DOMAIN, null);
// same-origin
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers.pbmode", 1]],
});
await testOnWindow(true, "", null);
// no-referrer
await SpecialPowers.pushPrefEnv({
set: [["network.http.referer.defaultPolicy.trackers.pbmode", 0]],
});
await testOnWindow(true, "", null);
// override with no-referrer
await testOnNoReferrer(true);
// override with same-origin
await testOnSameOrigin(true);
// override with no-referrer-when-downgrade
await testOnNoReferrerWhenDowngrade(true);
// override with origin
await testOnOrigin(true);
// override with strict-origin
await testOnStrictOrigin(true);
// override with origin-when-cross-origin
await testOnOriginWhenCrossOrigin(true);
// override with strict-origin-when-cross-origin
await testOnStrictOriginWhenCrossOrigin(true);
// override with unsafe-url
await testOnUnsafeUrl(true);
// Reset the pref.
Services.prefs.clearUserPref(
"network.http.referer.defaultPolicy.trackers.pbmode"
);
});
add_task(async function () {
await UrlClassifierTestUtils.addTestTrackers();
await executeTests();
UrlClassifierTestUtils.cleanupTestTrackers();
});
add_task(async function () {
info("Cleaning up.");
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value =>
resolve()
);
});
});
|