Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_referrer_cross_origin.js
blobada64fcced54e7de85b0606fb85e2d3ed6b7f61f
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
4 "use strict";
6 const ReferrerInfo = Components.Constructor(
7 "@mozilla.org/referrer-info;1",
8 "nsIReferrerInfo",
9 "init"
12 function test_policy(test) {
13 info("Running test: " + test.toSource());
15 let prefs = Services.prefs;
17 if (test.trimmingPolicy !== undefined) {
18 prefs.setIntPref(
19 "network.http.referer.trimmingPolicy",
20 test.trimmingPolicy
22 } else {
23 prefs.setIntPref("network.http.referer.trimmingPolicy", 0);
26 if (test.XOriginTrimmingPolicy !== undefined) {
27 prefs.setIntPref(
28 "network.http.referer.XOriginTrimmingPolicy",
29 test.XOriginTrimmingPolicy
31 } else {
32 prefs.setIntPref("network.http.referer.XOriginTrimmingPolicy", 0);
35 if (test.disallowRelaxingDefault) {
36 prefs.setBoolPref(
37 "network.http.referer.disallowCrossSiteRelaxingDefault",
38 test.disallowRelaxingDefault
40 } else {
41 prefs.setBoolPref(
42 "network.http.referer.disallowCrossSiteRelaxingDefault",
43 false
47 let referrer = NetUtil.newURI(test.referrer);
48 let triggeringPrincipal =
49 Services.scriptSecurityManager.createContentPrincipal(referrer, {});
50 let chan = NetUtil.newChannel({
51 uri: test.url,
52 loadingPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
53 triggeringPrincipal,
54 contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
55 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
56 });
58 chan.QueryInterface(Ci.nsIHttpChannel);
59 chan.referrerInfo = new ReferrerInfo(test.policy, true, referrer);
61 if (test.expectedReferrerSpec === undefined) {
62 try {
63 chan.getRequestHeader("Referer");
64 do_throw("Should not find a Referer header!");
65 } catch (e) {}
66 } else {
67 let header = chan.getRequestHeader("Referer");
68 Assert.equal(header, test.expectedReferrerSpec);
72 const nsIReferrerInfo = Ci.nsIReferrerInfo;
73 var gTests = [
74 // Test same origin policy w/o cross origin
76 policy: nsIReferrerInfo.SAME_ORIGIN,
77 url: "https://test.example/foo?a",
78 referrer: "https://test.example/foo?a",
79 expectedReferrerSpec: "https://test.example/foo?a",
82 policy: nsIReferrerInfo.SAME_ORIGIN,
83 url: "https://test.example/foo?a",
84 referrer: "https://foo.example/foo?a",
85 expectedReferrerSpec: undefined,
88 policy: nsIReferrerInfo.SAME_ORIGIN,
89 trimmingPolicy: 1,
90 url: "https://test.example/foo?a",
91 referrer: "https://test.example/foo?a",
92 expectedReferrerSpec: "https://test.example/foo",
95 policy: nsIReferrerInfo.SAME_ORIGIN,
96 trimmingPolicy: 1,
97 url: "https://test.example/foo?a",
98 referrer: "https://foo.example/foo?a",
99 expectedReferrerSpec: undefined,
102 policy: nsIReferrerInfo.SAME_ORIGIN,
103 trimmingPolicy: 2,
104 url: "https://test.example/foo?a",
105 referrer: "https://test.example/foo?a",
106 expectedReferrerSpec: "https://test.example/",
109 policy: nsIReferrerInfo.SAME_ORIGIN,
110 trimmingPolicy: 2,
111 url: "https://test.example/foo?a",
112 referrer: "https://foo.example/foo?a",
113 expectedReferrerSpec: undefined,
116 // Test origin when xorigin policy w/o cross origin
118 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
119 url: "https://test.example/foo?a",
120 referrer: "https://test.example/foo?a",
121 expectedReferrerSpec: "https://test.example/foo?a",
124 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
125 url: "https://test.example/foo?a",
126 referrer: "https://foo.example/foo?a",
127 expectedReferrerSpec: "https://foo.example/",
130 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
131 trimmingPolicy: 1,
132 url: "https://test.example/foo?a",
133 referrer: "https://test.example/foo?a",
134 expectedReferrerSpec: "https://test.example/foo",
137 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
138 trimmingPolicy: 1,
139 url: "https://test.example/foo?a",
140 referrer: "https://foo.example/foo?a",
141 expectedReferrerSpec: "https://foo.example/",
144 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
145 trimmingPolicy: 2,
146 url: "https://test.example/foo?a",
147 referrer: "https://test.example/foo?a",
148 expectedReferrerSpec: "https://test.example/",
151 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
152 trimmingPolicy: 2,
153 url: "https://test.example/foo?a",
154 referrer: "https://foo.example/foo?a",
155 expectedReferrerSpec: "https://foo.example/",
158 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
159 XOriginTrimmingPolicy: 1,
160 url: "https://test.example/foo?a",
161 referrer: "https://test.example/foo?a",
162 expectedReferrerSpec: "https://test.example/foo?a",
165 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
166 XOriginTrimmingPolicy: 1,
167 url: "https://test.example/foo?a",
168 referrer: "https://foo.example/foo?a",
169 expectedReferrerSpec: "https://foo.example/",
172 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
173 XOriginTrimmingPolicy: 2,
174 url: "https://test.example/foo?a",
175 referrer: "https://test.example/foo?a",
176 expectedReferrerSpec: "https://test.example/foo?a",
179 policy: nsIReferrerInfo.ORIGIN_WHEN_CROSS_ORIGIN,
180 XOriginTrimmingPolicy: 2,
181 url: "https://test.example/foo?a",
182 referrer: "https://foo.example/foo?a",
183 expectedReferrerSpec: "https://foo.example/",
186 // Test strict origin when xorigin policy w/o cross origin
188 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
189 url: "https://test.example/foo?a",
190 referrer: "https://test.example/foo?a",
191 expectedReferrerSpec: "https://test.example/foo?a",
194 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
195 url: "https://test.example/foo?a",
196 referrer: "https://foo.example/foo?a",
197 expectedReferrerSpec: "https://foo.example/",
200 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
201 url: "http://test.example/foo?a",
202 referrer: "https://foo.example/foo?a",
203 expectedReferrerSpec: undefined,
206 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
207 trimmingPolicy: 1,
208 url: "https://test.example/foo?a",
209 referrer: "https://test.example/foo?a",
210 expectedReferrerSpec: "https://test.example/foo",
213 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
214 trimmingPolicy: 1,
215 url: "https://test.example/foo?a",
216 referrer: "https://foo.example/foo?a",
217 expectedReferrerSpec: "https://foo.example/",
220 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
221 trimmingPolicy: 1,
222 url: "http://test.example/foo?a",
223 referrer: "https://foo.example/foo?a",
224 expectedReferrerSpec: undefined,
227 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
228 trimmingPolicy: 2,
229 url: "https://test.example/foo?a",
230 referrer: "https://test.example/foo?a",
231 expectedReferrerSpec: "https://test.example/",
234 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
235 trimmingPolicy: 2,
236 url: "https://test.example/foo?a",
237 referrer: "https://foo.example/foo?a",
238 expectedReferrerSpec: "https://foo.example/",
241 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
242 trimmingPolicy: 2,
243 url: "http://test.example/foo?a",
244 referrer: "https://foo.example/foo?a",
245 expectedReferrerSpec: undefined,
248 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
249 XOriginTrimmingPolicy: 1,
250 url: "https://test.example/foo?a",
251 referrer: "https://test.example/foo?a",
252 expectedReferrerSpec: "https://test.example/foo?a",
255 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
256 XOriginTrimmingPolicy: 1,
257 url: "https://test.example/foo?a",
258 referrer: "https://foo.example/foo?a",
259 expectedReferrerSpec: "https://foo.example/",
262 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
263 XOriginTrimmingPolicy: 1,
264 url: "http://test.example/foo?a",
265 referrer: "https://foo.example/foo?a",
266 expectedReferrerSpec: undefined,
269 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
270 XOriginTrimmingPolicy: 2,
271 url: "https://test.example/foo?a",
272 referrer: "https://test.example/foo?a",
273 expectedReferrerSpec: "https://test.example/foo?a",
276 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
277 XOriginTrimmingPolicy: 2,
278 url: "https://test.example/foo?a",
279 referrer: "https://foo.example/foo?a",
280 expectedReferrerSpec: "https://foo.example/",
283 policy: nsIReferrerInfo.STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
284 XOriginTrimmingPolicy: 2,
285 url: "http://test.example/foo?a",
286 referrer: "https://foo.example/foo?a",
287 expectedReferrerSpec: undefined,
290 // Test mix and choose max of XOriginTrimmingPolicy and trimmingPolicy
292 policy: nsIReferrerInfo.UNSAFE_URL,
293 XOriginTrimmingPolicy: 2,
294 trimmingPolicy: 1,
295 url: "https://test.example/foo?a",
296 referrer: "https://test1.example/foo?a",
297 expectedReferrerSpec: "https://test1.example/",
300 policy: nsIReferrerInfo.UNSAFE_URL,
301 XOriginTrimmingPolicy: 2,
302 trimmingPolicy: 1,
303 url: "https://test.example/foo?a",
304 referrer: "https://test.example/foo?a",
305 expectedReferrerSpec: "https://test.example/foo",
308 policy: nsIReferrerInfo.UNSAFE_URL,
309 XOriginTrimmingPolicy: 1,
310 trimmingPolicy: 2,
311 url: "https://test.example/foo?a",
312 referrer: "https://test.example/foo?a",
313 expectedReferrerSpec: "https://test.example/",
316 policy: nsIReferrerInfo.UNSAFE_URL,
317 XOriginTrimmingPolicy: 1,
318 trimmingPolicy: 0,
319 url: "https://test.example/foo?a",
320 referrer: "https://test1.example/foo?a",
321 expectedReferrerSpec: "https://test1.example/foo",
325 function run_test() {
326 gTests.forEach(test => test_policy(test));
327 Services.prefs.clearUserPref("network.http.referer.trimmingPolicy");
328 Services.prefs.clearUserPref("network.http.referer.XOriginTrimmingPolicy");
329 Services.prefs.clearUserPref(
330 "network.http.referer.disallowCrossSiteRelaxingDefault"