3 const ReferrerInfo
= Components
.Constructor(
4 "@mozilla.org/referrer-info;1",
9 function getTestReferrer(server_uri
, referer_uri
, isPrivate
= false) {
10 var uri
= NetUtil
.newURI(server_uri
);
11 let referrer
= NetUtil
.newURI(referer_uri
);
12 let principal
= Services
.scriptSecurityManager
.createContentPrincipal(
14 { privateBrowsingId
: isPrivate
? 1 : 0 }
16 var chan
= NetUtil
.newChannel({
18 loadingPrincipal
: principal
,
19 contentPolicyType
: Ci
.nsIContentPolicy
.TYPE_OTHER
,
20 securityFlags
: Ci
.nsILoadInfo
.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
,
23 chan
.QueryInterface(Ci
.nsIHttpChannel
);
24 chan
.referrerInfo
= new ReferrerInfo(
25 Ci
.nsIReferrerInfo
.EMPTY
,
31 header
= chan
.getRequestHeader("Referer");
32 } catch (NS_ERROR_NOT_AVAILABLE
) {}
37 var prefs
= Services
.prefs
;
39 var server_uri
= "http://bar.examplesite.com/path2";
40 var server_uri_2
= "http://bar.example.com/anotherpath";
41 var referer_uri
= "http://foo.example.com/path";
42 var referer_uri_2
= "http://bar.examplesite.com/path3?q=blah";
43 var referer_uri_2_anchor
= "http://bar.examplesite.com/path3?q=blah#anchor";
44 var referer_uri_idn
= "http://sub1.\xe4lt.example/path";
47 var server_uri_https
= "https://bar.example.com/anotherpath";
48 var referer_uri_https
= "https://bar.example.com/path3?q=blah";
49 var referer_uri_2_https
= "https://bar.examplesite.com/path3?q=blah";
51 // tests for sendRefererHeader
52 prefs
.setIntPref("network.http.sendRefererHeader", 0);
53 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
));
54 prefs
.setIntPref("network.http.sendRefererHeader", 2);
56 getTestReferrer(server_uri
, referer_uri
),
57 "http://foo.example.com/"
60 // test that https ref is not sent to http
61 Assert
.equal(null, getTestReferrer(server_uri_2
, referer_uri_https
));
63 // tests for referer.defaultPolicy
64 prefs
.setIntPref("network.http.referer.defaultPolicy", 0);
65 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
));
66 prefs
.setIntPref("network.http.referer.defaultPolicy", 1);
67 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
));
68 Assert
.equal(getTestReferrer(server_uri
, referer_uri_2
), referer_uri_2
);
69 prefs
.setIntPref("network.http.referer.defaultPolicy", 2);
70 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri_https
));
72 getTestReferrer(server_uri_https
, referer_uri_https
),
76 getTestReferrer(server_uri_https
, referer_uri_2_https
),
77 "https://bar.examplesite.com/"
79 Assert
.equal(getTestReferrer(server_uri
, referer_uri_2
), referer_uri_2
);
81 getTestReferrer(server_uri
, referer_uri
),
82 "http://foo.example.com/"
84 prefs
.setIntPref("network.http.referer.defaultPolicy", 3);
85 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
86 Assert
.equal(null, getTestReferrer(server_uri_2
, referer_uri_https
));
88 // tests for referer.defaultPolicy.pbmode
89 prefs
.setIntPref("network.http.referer.defaultPolicy.pbmode", 0);
90 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
, true));
91 prefs
.setIntPref("network.http.referer.defaultPolicy.pbmode", 1);
92 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
, true));
93 Assert
.equal(getTestReferrer(server_uri
, referer_uri_2
, true), referer_uri_2
);
94 prefs
.setIntPref("network.http.referer.defaultPolicy.pbmode", 2);
95 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri_https
, true));
97 getTestReferrer(server_uri_https
, referer_uri_https
, true),
101 getTestReferrer(server_uri_https
, referer_uri_2_https
, true),
102 "https://bar.examplesite.com/"
104 Assert
.equal(getTestReferrer(server_uri
, referer_uri_2
, true), referer_uri_2
);
106 getTestReferrer(server_uri
, referer_uri
, true),
107 "http://foo.example.com/"
109 prefs
.setIntPref("network.http.referer.defaultPolicy.pbmode", 3);
110 Assert
.equal(getTestReferrer(server_uri
, referer_uri
, true), referer_uri
);
111 Assert
.equal(null, getTestReferrer(server_uri_2
, referer_uri_https
, true));
113 // tests for referer.spoofSource
114 prefs
.setBoolPref("network.http.referer.spoofSource", true);
115 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), server_uri
);
116 prefs
.setBoolPref("network.http.referer.spoofSource", false);
117 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
119 // tests for referer.XOriginPolicy
120 prefs
.setIntPref("network.http.referer.XOriginPolicy", 2);
121 Assert
.equal(null, getTestReferrer(server_uri_2
, referer_uri
));
122 Assert
.equal(getTestReferrer(server_uri
, referer_uri_2
), referer_uri_2
);
123 prefs
.setIntPref("network.http.referer.XOriginPolicy", 1);
124 Assert
.equal(getTestReferrer(server_uri_2
, referer_uri
), referer_uri
);
125 Assert
.equal(null, getTestReferrer(server_uri
, referer_uri
));
128 getTestReferrer(server_uri_https
, referer_uri_https
),
131 prefs
.setIntPref("network.http.referer.XOriginPolicy", 0);
132 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
134 // tests for referer.trimmingPolicy
135 prefs
.setIntPref("network.http.referer.trimmingPolicy", 1);
137 getTestReferrer(server_uri
, referer_uri_2
),
138 "http://bar.examplesite.com/path3"
141 getTestReferrer(server_uri
, referer_uri_idn
),
142 "http://sub1.xn--lt-uia.example/path"
144 prefs
.setIntPref("network.http.referer.trimmingPolicy", 2);
146 getTestReferrer(server_uri
, referer_uri_2
),
147 "http://bar.examplesite.com/"
150 getTestReferrer(server_uri
, referer_uri_idn
),
151 "http://sub1.xn--lt-uia.example/"
155 getTestReferrer(server_uri_https
, referer_uri_https
),
156 "https://bar.example.com/"
158 prefs
.setIntPref("network.http.referer.trimmingPolicy", 0);
159 // test that anchor is lopped off in ordinary case
161 getTestReferrer(server_uri
, referer_uri_2_anchor
),
165 // tests for referer.XOriginTrimmingPolicy
166 prefs
.setIntPref("network.http.referer.XOriginTrimmingPolicy", 1);
168 getTestReferrer(server_uri
, referer_uri
),
169 "http://foo.example.com/path"
172 getTestReferrer(server_uri
, referer_uri_idn
),
173 "http://sub1.xn--lt-uia.example/path"
176 getTestReferrer(server_uri
, referer_uri_2
),
177 "http://bar.examplesite.com/path3?q=blah"
179 prefs
.setIntPref("network.http.referer.trimmingPolicy", 1);
181 getTestReferrer(server_uri
, referer_uri_2
),
182 "http://bar.examplesite.com/path3"
184 prefs
.setIntPref("network.http.referer.XOriginTrimmingPolicy", 2);
186 getTestReferrer(server_uri
, referer_uri
),
187 "http://foo.example.com/"
190 getTestReferrer(server_uri
, referer_uri_idn
),
191 "http://sub1.xn--lt-uia.example/"
194 getTestReferrer(server_uri
, referer_uri_2
),
195 "http://bar.examplesite.com/path3"
197 prefs
.setIntPref("network.http.referer.trimmingPolicy", 0);
199 getTestReferrer(server_uri
, referer_uri_2
),
200 "http://bar.examplesite.com/path3?q=blah"
204 getTestReferrer(server_uri_https
, referer_uri_https
),
205 "https://bar.example.com/path3?q=blah"
208 getTestReferrer(server_uri_https
, referer_uri_2_https
),
209 "https://bar.examplesite.com/"
211 prefs
.setIntPref("network.http.referer.XOriginTrimmingPolicy", 0);
212 // test that anchor is lopped off in ordinary case
214 getTestReferrer(server_uri
, referer_uri_2_anchor
),
218 // test referrer length limitation
219 // referer_uri's length is 27 and origin's length is 23
220 prefs
.setIntPref("network.http.referer.referrerLengthLimit", 27);
221 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
222 prefs
.setIntPref("network.http.referer.referrerLengthLimit", 26);
224 getTestReferrer(server_uri
, referer_uri
),
225 "http://foo.example.com/"
227 prefs
.setIntPref("network.http.referer.referrerLengthLimit", 22);
228 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), null);
229 prefs
.setIntPref("network.http.referer.referrerLengthLimit", 0);
230 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
231 prefs
.setIntPref("network.http.referer.referrerLengthLimit", 4096);
232 Assert
.equal(getTestReferrer(server_uri
, referer_uri
), referer_uri
);
234 // combination test: send spoofed path-only when hosts match
235 var combo_referer_uri
= "http://blah.foo.com/path?q=hot";
236 var dest_uri
= "http://blah.foo.com:9999/spoofedpath?q=bad";
237 prefs
.setIntPref("network.http.referer.trimmingPolicy", 1);
238 prefs
.setBoolPref("network.http.referer.spoofSource", true);
239 prefs
.setIntPref("network.http.referer.XOriginPolicy", 2);
241 getTestReferrer(dest_uri
, combo_referer_uri
),
242 "http://blah.foo.com:9999/spoofedpath"
246 getTestReferrer(dest_uri
, "http://gah.foo.com/anotherpath")