-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspoofing.js
316 lines (269 loc) · 11.1 KB
/
spoofing.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Firefox addon "Toggle Referrer"
Copyright (C) 2020 Manuel Reimer <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// The actual referrer spoofing rules are inside this function.
// Feel free to request more websites as long as the following rules are met:
// - Only simple cases will be added here! No "super-complex" rules!
// - Only for "big" websites. For small ones: Ask webmaster to fix his site!
function CreateSpoofedReferrer(url, origin) {
// console.log("URL: " + url.href);
// console.log("Host: " + url.host);
// console.log("Path: " + url.pathname);
// More properties: https://developer.mozilla.org/en-US/docs/Web/API/URL
// Helper used for common result strategies
const h = new RuleHelper(url, origin);
// Special case that we can not catch based on the host name (as this is
// used by many hosts). This makes us pass Cloudflare browser check.
// Unfortunately they have added referrer check to some of their endpoints.
if (url.pathname.startsWith("/cdn-cgi/challenge-platform/") ||
url.searchParams.has("__cf_chl_f_tk")) {
if (h.SameOriginHost()) {
return origin.protocol + "//" + origin.host + origin.pathname;
}
}
// Spoofing rules follow. First array element is always the "matching rule"
// which can be a String, Array of strings or RegExp.
const rules = [
// Several issues on aliexpress (no login, no search results, ...)
[/\.aliexpress\.com$/, () => {
return h.OriginHostIf(/\.aliexpress\.com$/);
}],
// https://lists.openstreetmap.org/pipermail/talk-de/2017-April/113989.html
["tile.openstreetmap.org", () => {
return "https://www.openstreetmap.org/";
}],
// Since around october 2021 "[abc].tile.openstreetmap.org" send status
// 418 ("I'm a teapot") if Referer is "https://www.openstreetmap.org/".
[/tile\.openstreetmap\.org$/, () => {
return "https://" + url.host + "/";
}],
// Really silly referrer check functionality in FluxBB. Won't work with
// just the same origin host but needs a valid pathname, too...
["bbs.archlinux.org", () => {
if (h.SameOriginHost() &&
["/post.php",
"/profile.php",
"/viewtopic.php",
"/edit.php"].includes(origin.pathname))
return origin.protocol + "//" + origin.host + origin.pathname;
}],
// No access to datasheets if referrer is off
[/^pdf1\.alldatasheet\.(com|net)$/, () => {
return "https://" + url.host + "/";
}],
[/^htmlimg2\.alldatasheet\.(com|net)$/, () => {
return "https://" + url.host.replace("htmlimg2", "html") + "/";
}],
[/^html\.alldatasheet\.(com|net)$/, () => {
return "https://" + url.host + "/html-pdf/";
}],
// No images on www.pixiv.net without referrer
["i.pximg.net", () => {
return "https://www.pixiv.net/";
}],
// "CodePen requires a referrer to render this..."
[["s.codepen.io", "cdpn.io"], () => {
return h.OriginHostIf("codepen.io");
}],
// JSFiddle result is broken without referrer
["fiddle.jshell.net", () => {
return h.OriginHostIf("jsfiddle.net");
}],
// TinyMCE's own "fiddle" tool. "Error: 42" without referrer
["fiddle.tinymce.com", () => {
return h.SameOriginHost();
}],
// "Endless spinning" spinner on userstyles.org
["userstyles.org", () => {
return (url.pathname.startsWith("/api/")) && h.SameOriginHost();
}],
// "Data Tables warning" in Arch Linux ARM packages browser and mirror list
["archlinuxarm.org", () => {
if (url.pathname == "/data/packages/list")
return "https://archlinuxarm.org/packages";
if (url.pathname == "/data/mirrors/list")
return "https://archlinuxarm.org/about/mirrors";
}],
// Amazon rules (so far only .de and .com)
// Sending referrer for "/ap/signin" prevents captcha when logging in
// The path "/gp/twister/" seems to be some API which needs a referrer
[/^www\.amazon\.(de|com)$/, () => {
return (url.pathname == "/ap/signin" ||
url.pathname.startsWith("/gp/twister/")) && h.SameOriginHost();
}],
// No way to log in to twitter without referrer.
["twitter.com", () => {
return h.SameOriginHost();
}],
// No SVG downloads on freesvg.org
["freesvg.org", () => {
return url.pathname.startsWith("/download/") && "https://freesvg.org/";
}],
// No downloads without referrer on amd.com
["drivers.amd.com", () => {
return h.OriginHostIf(/\.amd\.com$/);
}],
// At least some videos not playing without referrer
[/^v\d+-web\.tiktok\.com$/, () => {
return h.OriginHostIf("www.tiktok.com");
}],
// Fail with status: 498 No Reason Phrase
["web.archive.org", () => {
return h.SameOriginHost();
}],
// "Access denied" to some API requests without referrer
["getpocket.com", () => {
return h.SameOriginHost();
}],
// No "open link in new tab" on www.bing.com
["www.bing.com", () => {
return (url.pathname == "/newtabredir") && h.SameOriginHost();
}],
// Deeplink protection on www.researchgate.net
["www.researchgate.net", () => {
return url.pathname.endsWith("/download") && h.SameOriginHost();
}],
// Referrer check on the search feature of drimble.nl
["drimble.nl", () => {
return (url.pathname == "/autovliegtuig.php") && "https://drimble.nl/";
}],
// Often no embeds without referrer (Video unavailable)
["www.youtube.com", () => {
return (url.pathname.startsWith("/embed/")) && "https://www.youtube.com/";
}],
// Switching to "Split diff" fails on GitHub
["github.com", () => {
if (h.SameOriginHost() && url.pathname == "/users/diffview")
return origin.protocol + "//" + origin.host + origin.pathname;
}],
// AUR login fails with 400 - Bad request
// https://gitlab.archlinux.org/archlinux/aurweb/-/issues/325
["aur.archlinux.org", () => {
if (h.SameOriginHost() && url.pathname == "/login")
return h.SameOriginHost() + "login";
}],
// Bad "browser check" on gitlab.com
// Seems to redirect "over" some URL with a token and checks referrer
["gitlab.com", () => {
if (h.SameOriginHost() && url.pathname.startsWith("/users/sign_in"))
return origin.protocol + "//" + origin.host + origin.pathname;
}],
// This fixes bidding on ebay.de
["www.ebay.de", () => {
if (h.SameOriginHost() && url.pathname.startsWith("/bfl/placebid"))
return origin.protocol + "//" + origin.host + origin.pathname;
}]
];
// Match each rule against the URL's host and return the spoofing result
for (let rule of rules) {
const type = rule[0].constructor.name;
if ((type == "RegExp" && url.host.match(rule[0])) ||
(type == "String" && url.host == rule[0]) ||
(type == "Array" && rule[0].includes(url.host)))
return rule[1]();
}
}
// Helper class used in the rules above.
// Contains some common referrer result strategies.
class RuleHelper {
constructor(url, origin) {
this.url = url;
this.origin = origin;
}
// For a "same origin" request, create a referrer with just the origin host
SameOriginHost() {
return (this.origin.host === this.url.host) &&
(this.origin.protocol === this.url.protocol) &&
this.origin.protocol + "//" + this.origin.host + "/";
}
// Allow (white list) the given origin host to be used as referrer
// Parameter may be String or RegExp representative of valid host(s)
OriginHostIf(originhost) {
const type = originhost.constructor.name;
return (
(type == "String" && this.origin.host === originhost) ||
(type == "RegExp" && this.origin.host.match(originhost))
) &&
(this.origin.protocol === this.url.protocol) &&
this.origin.protocol + "//" + this.origin.host + "/";
}
}
//
// The actual Referrer spoofing "backend" code follows
//
// Header rewrite handler. Rewrites "Referer".
function RewriteReferrerHeader(e) {
// Check for specific rules first
let referrer = CreateSpoofedReferrer(
new URL(e.url),
new URL(e.originUrl || "http://no-origin.invalid/")
);
// Helper for the following code to be used with "find"
function findheader(name) {
return function(header) {
return header.name.toLowerCase() == name
};
}
// Current Firefox versions support an privacy improved header called "Origin"
// which is meant to be used for CSRF protection.
// This header is only sent in very limited cases. Whenever the browser
// decides to send one, we duplicate the "Origin" header over to "Referer".
//
// This should "mass unlock" many websites where the "Referer" header is used
// for CSRF protection. Especially those where framework designers decide to
// enforce the Referrer even after long discussion *cough* django *cough*.
if (!referrer) {
const originheader = e.requestHeaders.find(findheader("origin"));
if (originheader)
referrer = originheader.value + "/"
}
// If we found a suitable spoofing value, then add this as "Referer" header
if (referrer) {
const refererheader = e.requestHeaders.find(findheader("referer"));
if (refererheader)
refererheader.value = referrer;
else
e.requestHeaders.push({"name": "Referer", "value": referrer});
}
return {requestHeaders: e.requestHeaders};
}
// Updates our "spoof status". Triggered by clicking the toolbar button and
// by changing the spoof checkbox in our "options" page.
async function UpdateSpoofStatus() {
// We only have access to the API if the user actually opts into the spoofing
// feature (this triggers a permission request, the user has to accept).
if (browser.webRequest === undefined)
return;
// Read status of the "referrer enabled" browser preference
const referrers_enabled = (await browser.privacy.websites.referrersEnabled.get({})).value;
const obsh = browser.webRequest.onBeforeSendHeaders;
// We only register for "onBeforeSendHeaders" if referrers are disabled
if (!referrers_enabled) {
obsh.addListener(
RewriteReferrerHeader,
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
}
// In all other cases, get sure we are not registered to "onBeforeSendHeaders"
else {
if (obsh.hasListener(RewriteReferrerHeader))
obsh.removeListener(RewriteReferrerHeader);
}
}
// Register event listener to receive option update notifications
browser.runtime.onMessage.addListener((data, sender) => {
if (data.type == "OptionsChanged")
UpdateSpoofStatus();
});