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
|
/**
* Bug 1720869 - Testing the referrer policy telemetry.
*/
"use strict";
const TEST_DOMAIN = "https://example.com/";
const TEST_CROSS_SITE_DOMAIN = "https://test1.example.org/";
const TEST_PATH = "browser/dom/security/test/referrer-policy/";
const TEST_PAGE = `${TEST_DOMAIN}${TEST_PATH}referrer_page.sjs`;
const TEST_CROSS_SITE_PAGE = `${TEST_CROSS_SITE_DOMAIN}${TEST_PATH}referrer_page.sjs`;
// This matches to the order in ReferrerPolicy.webidl
const REFERRER_POLICY_INDEX = {
empty: 0,
"no-referrer": 1,
"no-referrer-when-downgrade": 2,
origin: 3,
"origin-when-cross-origin": 4,
"unsafe-url": 5,
"same-origin": 6,
"strict-origin": 7,
"strict-origin-when-cross-origin": 8,
};
const TEST_CASES = [
{
policy: "",
expected: REFERRER_POLICY_INDEX.empty,
},
{
policy: "no-referrer",
expected: REFERRER_POLICY_INDEX["no-referrer"],
},
{
policy: "no-referrer-when-downgrade",
expected: REFERRER_POLICY_INDEX["no-referrer-when-downgrade"],
},
{
policy: "origin",
expected: REFERRER_POLICY_INDEX.origin,
},
{
policy: "origin-when-cross-origin",
expected: REFERRER_POLICY_INDEX["origin-when-cross-origin"],
},
{
policy: "same-origin",
expected: REFERRER_POLICY_INDEX["same-origin"],
},
{
policy: "strict-origin",
expected: REFERRER_POLICY_INDEX["strict-origin"],
},
{
policy: "strict-origin-when-cross-origin",
expected: REFERRER_POLICY_INDEX["strict-origin-when-cross-origin"],
},
{
policy: "unsafe-url",
expected: REFERRER_POLICY_INDEX["unsafe-url"],
},
];
function clearTelemetry() {
Services.telemetry.getSnapshotForHistograms("main", true /* clear */);
Services.telemetry.getHistogramById("REFERRER_POLICY_COUNT").clear();
}
add_setup(async function () {
// Clear Telemetry probes before testing.
clearTelemetry();
});
function verifyTelemetry(expected, isSameSite) {
// The record of cross-site loads is placed in the second half of the
// telemetry.
const offset = isSameSite ? 0 : Object.keys(REFERRER_POLICY_INDEX).length;
let histograms = Services.telemetry.getSnapshotForHistograms(
"main",
false /* clear */
).parent;
let referrerPolicyCountProbe = histograms.REFERRER_POLICY_COUNT;
ok(referrerPolicyCountProbe, "The telemetry probe has been recorded.");
is(
referrerPolicyCountProbe.values[expected + offset],
1,
"The telemetry is added correctly."
);
}
add_task(async function run_tests() {
for (let test of TEST_CASES) {
for (let sameSite of [true, false]) {
clearTelemetry();
let referrerURL = `${TEST_PAGE}?header=${test.policy}`;
await BrowserTestUtils.withNewTab(referrerURL, async browser => {
let iframeURL = sameSite
? TEST_PAGE + "?show"
: TEST_CROSS_SITE_PAGE + "?show";
// Create an iframe and load the url.
await SpecialPowers.spawn(browser, [iframeURL], async url => {
let iframe = content.document.createElement("iframe");
iframe.src = url;
await new content.Promise(resolve => {
iframe.onload = () => {
resolve();
};
content.document.body.appendChild(iframe);
});
});
verifyTelemetry(test.expected, sameSite);
});
}
}
});
|