1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
6 const ReferrerInfo
= Components
.Constructor(
7 "@mozilla.org/referrer-info;1",
12 function test_policy(test
) {
13 info("Running test: " + test
.toSource());
15 let prefs
= Services
.prefs
;
17 if (test
.trimmingPolicy
!== undefined) {
19 "network.http.referer.trimmingPolicy",
23 prefs
.setIntPref("network.http.referer.trimmingPolicy", 0);
26 if (test
.XOriginTrimmingPolicy
!== undefined) {
28 "network.http.referer.XOriginTrimmingPolicy",
29 test
.XOriginTrimmingPolicy
32 prefs
.setIntPref("network.http.referer.XOriginTrimmingPolicy", 0);
35 if (test
.disallowRelaxingDefault
) {
37 "network.http.referer.disallowCrossSiteRelaxingDefault",
38 test
.disallowRelaxingDefault
42 "network.http.referer.disallowCrossSiteRelaxingDefault",
47 let referrer
= NetUtil
.newURI(test
.referrer
);
48 let triggeringPrincipal
=
49 Services
.scriptSecurityManager
.createContentPrincipal(referrer
, {});
50 let chan
= NetUtil
.newChannel({
52 loadingPrincipal
: Services
.scriptSecurityManager
.getSystemPrincipal(),
54 contentPolicyType
: Ci
.nsIContentPolicy
.TYPE_OTHER
,
55 securityFlags
: Ci
.nsILoadInfo
.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
,
58 chan
.QueryInterface(Ci
.nsIHttpChannel
);
59 chan
.referrerInfo
= new ReferrerInfo(test
.policy
, true, referrer
);
61 if (test
.expectedReferrerSpec
=== undefined) {
63 chan
.getRequestHeader("Referer");
64 do_throw("Should not find a Referer header!");
67 let header
= chan
.getRequestHeader("Referer");
68 Assert
.equal(header
, test
.expectedReferrerSpec
);
72 const nsIReferrerInfo
= Ci
.nsIReferrerInfo
;
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
,
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
,
97 url
: "https://test.example/foo?a",
98 referrer
: "https://foo.example/foo?a",
99 expectedReferrerSpec
: undefined,
102 policy
: nsIReferrerInfo
.SAME_ORIGIN
,
104 url
: "https://test.example/foo?a",
105 referrer
: "https://test.example/foo?a",
106 expectedReferrerSpec
: "https://test.example/",
109 policy
: nsIReferrerInfo
.SAME_ORIGIN
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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,
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,
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,
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,
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"