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
|
"use strict";
const ReferrerInfo = Components.Constructor(
"@mozilla.org/referrer-info;1",
"nsIReferrerInfo",
"init"
);
function getTestReferrer(server_uri, referer_uri, isPrivate = false) {
var uri = NetUtil.newURI(server_uri);
let referrer = NetUtil.newURI(referer_uri);
let principal = Services.scriptSecurityManager.createContentPrincipal(
referrer,
{ privateBrowsingId: isPrivate ? 1 : 0 }
);
var chan = NetUtil.newChannel({
uri,
loadingPrincipal: principal,
contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER,
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
});
chan.QueryInterface(Ci.nsIHttpChannel);
chan.referrerInfo = new ReferrerInfo(
Ci.nsIReferrerInfo.EMPTY,
true,
referrer
);
var header = null;
try {
header = chan.getRequestHeader("Referer");
} catch (NS_ERROR_NOT_AVAILABLE) {}
return header;
}
function run_test() {
var prefs = Services.prefs;
var server_uri = "http://bar.examplesite.com/path2";
var server_uri_2 = "http://bar.example.com/anotherpath";
var referer_uri = "http://foo.example.com/path";
var referer_uri_2 = "http://bar.examplesite.com/path3?q=blah";
var referer_uri_2_anchor = "http://bar.examplesite.com/path3?q=blah#anchor";
var referer_uri_idn = "http://sub1.\xe4lt.example/path";
// for https tests
var server_uri_https = "https://bar.example.com/anotherpath";
var referer_uri_https = "https://bar.example.com/path3?q=blah";
var referer_uri_2_https = "https://bar.examplesite.com/path3?q=blah";
// tests for sendRefererHeader
prefs.setIntPref("network.http.sendRefererHeader", 0);
Assert.equal(null, getTestReferrer(server_uri, referer_uri));
prefs.setIntPref("network.http.sendRefererHeader", 2);
Assert.equal(
getTestReferrer(server_uri, referer_uri),
"http://foo.example.com/"
);
// test that https ref is not sent to http
Assert.equal(null, getTestReferrer(server_uri_2, referer_uri_https));
// tests for referer.defaultPolicy
prefs.setIntPref("network.http.referer.defaultPolicy", 0);
Assert.equal(null, getTestReferrer(server_uri, referer_uri));
prefs.setIntPref("network.http.referer.defaultPolicy", 1);
Assert.equal(null, getTestReferrer(server_uri, referer_uri));
Assert.equal(getTestReferrer(server_uri, referer_uri_2), referer_uri_2);
prefs.setIntPref("network.http.referer.defaultPolicy", 2);
Assert.equal(null, getTestReferrer(server_uri, referer_uri_https));
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_https),
referer_uri_https
);
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_2_https),
"https://bar.examplesite.com/"
);
Assert.equal(getTestReferrer(server_uri, referer_uri_2), referer_uri_2);
Assert.equal(
getTestReferrer(server_uri, referer_uri),
"http://foo.example.com/"
);
prefs.setIntPref("network.http.referer.defaultPolicy", 3);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
Assert.equal(null, getTestReferrer(server_uri_2, referer_uri_https));
// tests for referer.defaultPolicy.pbmode
prefs.setIntPref("network.http.referer.defaultPolicy.pbmode", 0);
Assert.equal(null, getTestReferrer(server_uri, referer_uri, true));
prefs.setIntPref("network.http.referer.defaultPolicy.pbmode", 1);
Assert.equal(null, getTestReferrer(server_uri, referer_uri, true));
Assert.equal(getTestReferrer(server_uri, referer_uri_2, true), referer_uri_2);
prefs.setIntPref("network.http.referer.defaultPolicy.pbmode", 2);
Assert.equal(null, getTestReferrer(server_uri, referer_uri_https, true));
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_https, true),
referer_uri_https
);
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_2_https, true),
"https://bar.examplesite.com/"
);
Assert.equal(getTestReferrer(server_uri, referer_uri_2, true), referer_uri_2);
Assert.equal(
getTestReferrer(server_uri, referer_uri, true),
"http://foo.example.com/"
);
prefs.setIntPref("network.http.referer.defaultPolicy.pbmode", 3);
Assert.equal(getTestReferrer(server_uri, referer_uri, true), referer_uri);
Assert.equal(null, getTestReferrer(server_uri_2, referer_uri_https, true));
// tests for referer.spoofSource
prefs.setBoolPref("network.http.referer.spoofSource", true);
Assert.equal(getTestReferrer(server_uri, referer_uri), server_uri);
prefs.setBoolPref("network.http.referer.spoofSource", false);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
// tests for referer.XOriginPolicy
prefs.setIntPref("network.http.referer.XOriginPolicy", 2);
Assert.equal(null, getTestReferrer(server_uri_2, referer_uri));
Assert.equal(getTestReferrer(server_uri, referer_uri_2), referer_uri_2);
prefs.setIntPref("network.http.referer.XOriginPolicy", 1);
Assert.equal(getTestReferrer(server_uri_2, referer_uri), referer_uri);
Assert.equal(null, getTestReferrer(server_uri, referer_uri));
// https test
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_https),
referer_uri_https
);
prefs.setIntPref("network.http.referer.XOriginPolicy", 0);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
// tests for referer.trimmingPolicy
prefs.setIntPref("network.http.referer.trimmingPolicy", 1);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/path3"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_idn),
"http://sub1.xn--lt-uia.example/path"
);
prefs.setIntPref("network.http.referer.trimmingPolicy", 2);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_idn),
"http://sub1.xn--lt-uia.example/"
);
// https test
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_https),
"https://bar.example.com/"
);
prefs.setIntPref("network.http.referer.trimmingPolicy", 0);
// test that anchor is lopped off in ordinary case
Assert.equal(
getTestReferrer(server_uri, referer_uri_2_anchor),
referer_uri_2
);
// tests for referer.XOriginTrimmingPolicy
prefs.setIntPref("network.http.referer.XOriginTrimmingPolicy", 1);
Assert.equal(
getTestReferrer(server_uri, referer_uri),
"http://foo.example.com/path"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_idn),
"http://sub1.xn--lt-uia.example/path"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/path3?q=blah"
);
prefs.setIntPref("network.http.referer.trimmingPolicy", 1);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/path3"
);
prefs.setIntPref("network.http.referer.XOriginTrimmingPolicy", 2);
Assert.equal(
getTestReferrer(server_uri, referer_uri),
"http://foo.example.com/"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_idn),
"http://sub1.xn--lt-uia.example/"
);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/path3"
);
prefs.setIntPref("network.http.referer.trimmingPolicy", 0);
Assert.equal(
getTestReferrer(server_uri, referer_uri_2),
"http://bar.examplesite.com/path3?q=blah"
);
// https tests
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_https),
"https://bar.example.com/path3?q=blah"
);
Assert.equal(
getTestReferrer(server_uri_https, referer_uri_2_https),
"https://bar.examplesite.com/"
);
prefs.setIntPref("network.http.referer.XOriginTrimmingPolicy", 0);
// test that anchor is lopped off in ordinary case
Assert.equal(
getTestReferrer(server_uri, referer_uri_2_anchor),
referer_uri_2
);
// test referrer length limitation
// referer_uri's length is 27 and origin's length is 23
prefs.setIntPref("network.http.referer.referrerLengthLimit", 27);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
prefs.setIntPref("network.http.referer.referrerLengthLimit", 26);
Assert.equal(
getTestReferrer(server_uri, referer_uri),
"http://foo.example.com/"
);
prefs.setIntPref("network.http.referer.referrerLengthLimit", 22);
Assert.equal(getTestReferrer(server_uri, referer_uri), null);
prefs.setIntPref("network.http.referer.referrerLengthLimit", 0);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
prefs.setIntPref("network.http.referer.referrerLengthLimit", 4096);
Assert.equal(getTestReferrer(server_uri, referer_uri), referer_uri);
// combination test: send spoofed path-only when hosts match
var combo_referer_uri = "http://blah.foo.com/path?q=hot";
var dest_uri = "http://blah.foo.com:9999/spoofedpath?q=bad";
prefs.setIntPref("network.http.referer.trimmingPolicy", 1);
prefs.setBoolPref("network.http.referer.spoofSource", true);
prefs.setIntPref("network.http.referer.XOriginPolicy", 2);
Assert.equal(
getTestReferrer(dest_uri, combo_referer_uri),
"http://blah.foo.com:9999/spoofedpath"
);
Assert.equal(
null,
getTestReferrer(dest_uri, "http://gah.foo.com/anotherpath")
);
}
|