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
|
var BASE_URL =
"example.com/tests/dom/security/test/referrer-policy/img_referrer_testserver.sjs";
const IMG_BYTES = atob(
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12" +
"P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
);
function createTestUrl(aPolicy, aAction, aName, aContent) {
var content = aContent || "text";
return (
"http://" +
BASE_URL +
"?" +
"action=" +
aAction +
"&" +
"policy=" +
aPolicy +
"&" +
"name=" +
aName +
"&" +
"content=" +
content
);
}
function createTestPage(aHead, aImgPolicy, aName) {
var _createTestUrl = createTestUrl.bind(null, aImgPolicy, "test", aName);
return (
"<!DOCTYPE HTML>\n\
<html>" +
aHead +
'<body>\n\
<img src="' +
_createTestUrl("img") +
'" referrerpolicy="' +
aImgPolicy +
'" id="image"></img>\n\
<script>' +
// LOAD EVENT (of the test)
// fires when the img resource for the page is loaded
'window.addEventListener("load", function() {\n\
parent.postMessage("childLoadComplete", "http://mochi.test:8888");\n\
}.bind(window), false);' +
"</script>\n\
</body>\n\
</html>"
);
}
// Creates the following test cases for the specified referrer
// policy combination:
// <img> with referrer
function createTest(aPolicy, aImgPolicy, aName) {
var headString = "<head>";
if (aPolicy) {
headString += '<meta name="referrer" content="' + aPolicy + '">';
}
headString += "<script></script>";
return createTestPage(headString, aImgPolicy, aName);
}
// testing regular load img with referrer policy
// speculative parser should not kick in here
function createTest2(aImgPolicy, name) {
return createTestPage("", aImgPolicy, name);
}
function createTest3(aImgPolicy1, aImgPolicy2, aImgPolicy3, aName) {
return (
'<!DOCTYPE HTML>\n\
<html>\n\
<body>\n\
<img src="' +
createTestUrl(aImgPolicy1, "test", aName + aImgPolicy1) +
'" referrerpolicy="' +
aImgPolicy1 +
'" id="image"></img>\n\
<img src="' +
createTestUrl(aImgPolicy2, "test", aName + aImgPolicy2) +
'" referrerpolicy="' +
aImgPolicy2 +
'" id="image"></img>\n\
<img src="' +
createTestUrl(aImgPolicy3, "test", aName + aImgPolicy3) +
'" referrerpolicy="' +
aImgPolicy3 +
'" id="image"></img>\n\
<script>\n\
var _numLoads = 0;' +
// LOAD EVENT (of the test)
// fires when the img resource for the page is loaded
'window.addEventListener("load", function() {\n\
parent.postMessage("childLoadComplete", "http://mochi.test:8888");\n\
}.bind(window), false);' +
"</script>\n\
</body>\n\
</html>"
);
}
function createTestPage2(aHead, aPolicy, aName) {
return (
"<!DOCTYPE HTML>\n\
<html>" +
aHead +
'<body>\n\
<img src="' +
createTestUrl(aPolicy, "test", aName) +
'" id="image"></img>\n\
<script>' +
// LOAD EVENT (of the test)
// fires when the img resource for the page is loaded
'window.addEventListener("load", function() {\n\
parent.postMessage("childLoadComplete", "http://mochi.test:8888");\n\
}.bind(window), false);' +
"</script>\n\
</body>\n\
</html>"
);
}
function createTestPage3(aHead, aPolicy, aName) {
return (
"<!DOCTYPE HTML>\n\
<html>" +
aHead +
"<body>\n\
<script>" +
'var image = new Image();\n\
image.src = "' +
createTestUrl(aPolicy, "test", aName, "image") +
'";\n\
image.referrerPolicy = "' +
aPolicy +
'";\n\
image.onload = function() {\n\
window.parent.postMessage("childLoadComplete", "http://mochi.test:8888");\n\
}\n\
document.body.appendChild(image);' +
"</script>\n\
</body>\n\
</html>"
);
}
function createTestPage4(aHead, aPolicy, aName) {
return (
"<!DOCTYPE HTML>\n\
<html>" +
aHead +
"<body>\n\
<script>" +
'var image = new Image();\n\
image.referrerPolicy = "' +
aPolicy +
'";\n\
image.src = "' +
createTestUrl(aPolicy, "test", aName, "image") +
'";\n\
image.onload = function() {\n\
window.parent.postMessage("childLoadComplete", "http://mochi.test:8888");\n\
}\n\
document.body.appendChild(image);' +
"</script>\n\
</body>\n\
</html>"
);
}
function createSetAttributeTest1(aPolicy, aImgPolicy, aName) {
var headString = "<head>";
headString += '<meta name="referrer" content="' + aPolicy + '">';
headString += "<script></script>";
return createTestPage3(headString, aImgPolicy, aName);
}
function createSetAttributeTest2(aPolicy, aImgPolicy, aName) {
var headString = "<head>";
headString += '<meta name="referrer" content="' + aPolicy + '">';
headString += "<script></script>";
return createTestPage4(headString, aImgPolicy, aName);
}
function createTest4(aPolicy, aName) {
var headString = "<head>";
headString += '<meta name="referrer" content="' + aPolicy + '">';
headString += "<script></script>";
return createTestPage2(headString, aPolicy, aName);
}
function createTest5(aPolicy, aName) {
var headString = "<head>";
headString += '<meta name="referrer" content="' + aPolicy + '">';
return createTestPage2(headString, aPolicy, aName);
}
function handleRequest(request, response) {
var sharedKey = "img_referrer_testserver.sjs";
var params = request.queryString.split("&");
var action = params[0].split("=")[1];
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
if (action === "resetState") {
let state = getSharedState(sharedKey);
state = {};
setSharedState(sharedKey, JSON.stringify(state));
response.write("");
return;
}
if (action === "test") {
// ?action=test&policy=origin&name=name&content=content
let policy = params[1].split("=")[1];
let name = params[2].split("=")[1];
let content = params[3].split("=")[1];
let result = getSharedState(sharedKey);
if (result === "") {
result = {};
} else {
result = JSON.parse(result);
}
if (!result.tests) {
result.tests = {};
}
var referrerLevel = "none";
var test = {};
if (request.hasHeader("Referer")) {
let referrer = request.getHeader("Referer");
if (referrer.indexOf("img_referrer_testserver") > 0) {
referrerLevel = "full";
} else if (referrer == "http://mochi.test:8888/") {
referrerLevel = "origin";
}
test.referrer = request.getHeader("Referer");
} else {
test.referrer = "";
}
test.policy = referrerLevel;
test.expected = policy;
result.tests[name] = test;
setSharedState(sharedKey, JSON.stringify(result));
if (content === "image") {
response.setHeader("Content-Type", "image/png");
response.write(IMG_BYTES);
}
return;
}
if (action === "get-test-results") {
// ?action=get-result
response.write(getSharedState(sharedKey));
return;
}
if (action === "generate-img-policy-test") {
// ?action=generate-img-policy-test&imgPolicy=b64-encoded-string&name=name&policy=b64-encoded-string
let imgPolicy = unescape(params[1].split("=")[1]);
let name = unescape(params[2].split("=")[1]);
let metaPolicy = "";
if (params[3]) {
metaPolicy = params[3].split("=")[1];
}
response.write(createTest(metaPolicy, imgPolicy, name));
return;
}
if (action === "generate-img-policy-test2") {
// ?action=generate-img-policy-test2&imgPolicy=b64-encoded-string&name=name
let imgPolicy = unescape(params[1].split("=")[1]);
let name = unescape(params[2].split("=")[1]);
response.write(createTest2(imgPolicy, name));
return;
}
if (action === "generate-img-policy-test3") {
// ?action=generate-img-policy-test3&imgPolicy1=b64-encoded-string&imgPolicy2=b64-encoded-string&imgPolicy3=b64-encoded-string&name=name
let imgPolicy1 = unescape(params[1].split("=")[1]);
let imgPolicy2 = unescape(params[2].split("=")[1]);
let imgPolicy3 = unescape(params[3].split("=")[1]);
let name = unescape(params[4].split("=")[1]);
response.write(createTest3(imgPolicy1, imgPolicy2, imgPolicy3, name));
return;
}
if (action === "generate-img-policy-test4") {
// ?action=generate-img-policy-test4&imgPolicy=b64-encoded-string&name=name
let policy = unescape(params[1].split("=")[1]);
let name = unescape(params[2].split("=")[1]);
response.write(createTest4(policy, name));
return;
}
if (action === "generate-img-policy-test5") {
// ?action=generate-img-policy-test5&policy=b64-encoded-string&name=name
let policy = unescape(params[1].split("=")[1]);
let name = unescape(params[2].split("=")[1]);
response.write(createTest5(policy, name));
return;
}
if (action === "generate-setAttribute-test1") {
// ?action=generate-setAttribute-test1&policy=b64-encoded-string&name=name
let imgPolicy = unescape(params[1].split("=")[1]);
let policy = unescape(params[2].split("=")[1]);
let name = unescape(params[3].split("=")[1]);
response.write(createSetAttributeTest1(policy, imgPolicy, name));
return;
}
if (action === "generate-setAttribute-test2") {
// ?action=generate-setAttribute-test2&policy=b64-encoded-string&name=name
let imgPolicy = unescape(params[1].split("=")[1]);
let policy = unescape(params[2].split("=")[1]);
let name = unescape(params[3].split("=")[1]);
response.write(createSetAttributeTest2(policy, imgPolicy, name));
return;
}
response.write("I don't know action " + action);
}
|