-
Notifications
You must be signed in to change notification settings - Fork 14
/
engine.js
267 lines (243 loc) · 6.02 KB
/
engine.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
/*
* Referer Modifier: Modify the Referer header in HTTP requests
* Copyright (C) 2017-2022 Fiona Klute
*
* 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 <https://www.gnu.org/licenses/>.
*/
"use strict";
/* exported RefererModEngine */
class RuleMatch
{
#target_len;
#origin_len;
#rule;
constructor(rule, target_len, origin_len)
{
this.#rule = rule;
this.#target_len = target_len;
this.#origin_len = origin_len;
}
/*
* The rule that matched.
*/
get rule()
{
return this.#rule;
}
/*
* Determine if this match is better than another one. This match
* is "better" if any of the following is true:
*
* - another is null
* - the target_len of this match is larger than that of the other
* - target_len of both matches is equal and the origin_len of
* this match is larger than origin_len of the other
*/
better(another)
{
return (another == null
|| this.#target_len > another.#target_len
|| (this.#target_len == another.#target_len
&& this.#origin_len > another.#origin_len));
}
}
/*
* One rule as defined in the configuration, with expressions compiled
* for matching requests.
*/
class Rule
{
// regular expression for the target domain
#target;
// regular expression for the origin domain, if any
#origin;
// action to take for matching requests
#action;
// Referer to set for "replace" rule
#referer;
constructor(rule)
{
/*
* A rule is expected to have this structure in the stored
* configuration:
*
* { domain: "www.example.com", action: "keep", referer: "" }
*/
this.#target = Rule.#createDomainRegex(rule.domain);
this.#origin = "origin" in rule ?
Rule.#createDomainRegex(rule.origin) : null;
this.#action = rule.action;
this.#referer = rule.referer;
}
toString()
{
return `{target: ${this.#target}, origin: ${this.#origin}, action: ${this.#action}, referer: ${JSON.stringify(this.#referer)}}`;
}
/*
* Match source and target URL against this rule, return match
* object. Target must be a URL object, source must be a URL
* object or null.
*/
match(source, target)
{
let target_match = target.hostname.match(this.#target);
if (target_match == null)
{
return null;
}
let origin_len = -1;
if (this.#origin != null)
{
let origin = source != null ? source.hostname : "";
let origin_match = origin.match(this.#origin);
if (origin_match == null)
{
return null;
}
origin_len = origin_match[0].length;
}
return new RuleMatch(this, target_match[0].length, origin_len);
}
get action()
{
return this.#action;
}
get referer()
{
return this.#referer;
}
/*
* Escape function from
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
static #escapeRegExp(string)
{
// eslint-disable-next-line no-useless-escape
return string.replace(/[.*+?^${}()|\[\]\\]/g, "\\$&");
// $& means the whole matched string
}
/*
* Create regular expressions from domains to also match subdomains.
*/
static #createDomainRegex(domain)
{
let pattern;
if (domain.endsWith("$"))
{
pattern = domain;
}
else
{
pattern = "(\\.|^)"
+ Rule.#escapeRegExp(domain) + "$";
}
return new RegExp(pattern);
}
}
class RefererModEngine
{
#domains;
#sameConf;
#anyConf;
constructor(config)
{
if (!config)
{
config = {
/* Default empty domain configuration */
domains: [],
/* Default configuration for same domain requests */
sameConf: { action: "keep", referer: "" },
/* Default configuration for any other requests */
anyConf: { action: "prune", referer: "" }
};
}
this.setConfig(config);
}
setConfig(config)
{
/* Configuration for specific domains. See Rule constructor
* for the expected structure. */
this.#domains = config.domains.map(d => new Rule(d));
/* Configuration for same domain requests */
this.#sameConf = config.sameConf;
/* Configuration for any other requests */
this.#anyConf = config.anyConf;
}
/*
* Look up the action to take on any referer based on target URL (url)
* and URL of the request origin (originURL).
*/
findHostConf(url, originUrl)
{
let target = new URL(url);
let source = (
originUrl === "" || originUrl === null || originUrl === undefined)
? null : new URL(originUrl);
/* Check if we have a specific rule that matches the
* source/target domain combination, if yes return it. The
* reduce step chooses the best match in case we have multiple
* filter matches, as determined by RuleMatch.better(). */
let match = this.#domains
.map(d => d.match(source, target))
.filter(d => d != null)
.reduce((acc, current) =>
current.better(acc) ? current : acc, null);
if (match != null)
{
return match.rule;
}
if (source != null && target.hostname === source.hostname)
{
/* same domain */
return this.#sameConf;
}
else
{
/* default */
return this.#anyConf;
}
}
/*
* Compute referrer for a given pair of URL and its origin.
*/
computeReferrer(url, originUrl)
{
if (url.startsWith("about:") || url.startsWith("data:"))
{
return originUrl;
}
const conf = this.findHostConf(url, originUrl);
var referrer = null;
switch (conf.action)
{
case "prune":
referrer = originUrl ? new URL(originUrl).origin + "/" : "";
break;
case "target":
referrer = new URL(url).origin + "/";
break;
case "replace":
referrer = conf.referer;
break;
case "remove":
referrer = "";
break;
default:
/* "keep" */
referrer = originUrl;
}
return referrer;
}
}