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
|
var BASE_URL = "example.com/tests/dom/base/test/referrer_change_server.sjs";
function createTestUrl(aPolicy, aAction, aName) {
return (
"http://" +
BASE_URL +
"?" +
"action=" +
aAction +
"&" +
"policy=" +
aPolicy +
"&" +
"name=" +
aName +
"&" +
"type=link"
);
}
function createTest(aMetaPolicy, aReferrerPolicy, aName) {
return (
"<!DOCTYPE HTML>\n\
<html>" +
'<meta name="referrer" content="' +
aMetaPolicy +
'">' +
"<body>" +
'<a href="' +
createTestUrl(aReferrerPolicy, "test", aName + aReferrerPolicy) +
'" id="link">' +
aReferrerPolicy +
"</a>" +
"<script>" +
// LOAD EVENT (of the test)
// fires when the page is loaded, then click link
// first change meta referrer, then click link
'window.addEventListener("load", function() {\n\
document.getElementsByName("referrer")[0].content = "' +
aReferrerPolicy +
'";\n\
document.getElementById("link").click();\n\
}.bind(window), false);' +
"</script>\n\
</body>\n\
</html>"
);
}
function createTest2(aMetaPolicy, aReferrerPolicy, aName) {
return (
"<!DOCTYPE HTML>\n\
<html>" +
'<meta name="referrer" content="' +
aMetaPolicy +
'">' +
"<body>" +
'<a href="' +
createTestUrl(aReferrerPolicy, "test", aName + aReferrerPolicy) +
'" id="link">' +
aReferrerPolicy +
"</a>" +
"<script>" +
// LOAD EVENT (of the test)
// fires when the page is loaded, then click link
// first change meta referrer, then click link
'window.addEventListener("load", function() {\n\
document.getElementsByName("referrer")[0].setAttribute("content", "' +
aReferrerPolicy +
'");\n\
document.getElementById("link").click();\n\
}.bind(window), false);' +
"</script>\n\
</body>\n\
</html>"
);
}
function handleRequest(request, response) {
var sharedKey = "referrer_change_server.sjs";
var params = request.queryString.split("&");
var action = params[0].split("=")[1];
if (action === "resetState") {
var state = getSharedState(sharedKey);
state = {};
setSharedState(sharedKey, JSON.stringify(state));
response.write("");
return;
} else if (action === "test") {
// ?action=test&policy=origin&name=name
var policy = params[1].split("=")[1];
var name = params[2].split("=")[1];
var type = params[3].split("=")[1];
var 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("referrer_change_server") > 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));
// forward link click to redirect URL to finish test
if (type === "link") {
var loc =
"https://example.com/tests/dom/base/test/file_change_policy_redirect.html";
response.setStatusLine("1.1", 302, "Found");
response.setHeader("Location", loc, false);
}
return;
} else if (action === "get-test-results") {
// ?action=get-result
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Content-Type", "text/plain", false);
response.write(getSharedState(sharedKey));
return;
} else if (action === "generate-policy-test") {
// ?action=generate-policy-test&referrerPolicy=b64-encoded-string&name=name&newPolicy=b64-encoded-string
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
var referrerPolicy = unescape(params[1].split("=")[1]);
var name = unescape(params[2].split("=")[1]);
var newPolicy = params[3].split("=")[1];
response.write(createTest(referrerPolicy, newPolicy, name));
return;
} else if (action === "generate-policy-test2") {
// ?action=generate-policy-test2&referrerPolicy=b64-encoded-string&name=name&newPolicy=b64-encoded-string
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Content-Type", "text/html; charset=utf-8", false);
var referrerPolicy = unescape(params[1].split("=")[1]);
var name = unescape(params[2].split("=")[1]);
var newPolicy = params[3].split("=")[1];
response.write(createTest2(referrerPolicy, newPolicy, name));
return;
} else {
response.write("I don't know action " + action);
return;
}
}
|