@tokioapp/adblock 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -2
- package/THIRD_PARTY_NOTICES.md +50 -0
- package/dist/content/cosmetics.d.ts +22 -0
- package/dist/content/cosmetics.js +173 -0
- package/dist/content/mainWorld.d.ts +8 -0
- package/dist/content/mainWorld.js +10 -0
- package/dist/content/preload.d.ts +1 -0
- package/dist/content/preload.js +34 -0
- package/dist/content/procedural.d.ts +19 -0
- package/dist/content/procedural.js +212 -0
- package/dist/cosmetic/engine.d.ts +95 -0
- package/dist/cosmetic/engine.js +268 -0
- package/dist/electron/blocker.d.ts +84 -0
- package/dist/electron/blocker.js +320 -0
- package/dist/electron/channels.d.ts +10 -0
- package/dist/electron/channels.js +6 -0
- package/dist/electron/scheme.d.ts +21 -0
- package/dist/electron/scheme.js +21 -0
- package/dist/engine.d.ts +86 -0
- package/dist/engine.js +163 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/lists/defaults.d.ts +6 -0
- package/dist/lists/defaults.js +59 -0
- package/dist/lists/manager.d.ts +86 -0
- package/dist/lists/manager.js +154 -0
- package/dist/lists/preprocessor.d.ts +21 -0
- package/dist/lists/preprocessor.js +152 -0
- package/dist/lists/storage.d.ts +37 -0
- package/dist/lists/storage.js +66 -0
- package/dist/network/engine.d.ts +105 -0
- package/dist/network/engine.js +431 -0
- package/dist/network/filterIndex.d.ts +40 -0
- package/dist/network/filterIndex.js +343 -0
- package/dist/network/modifiers.d.ts +23 -0
- package/dist/network/modifiers.js +192 -0
- package/dist/network/pattern.d.ts +7 -0
- package/dist/network/pattern.js +145 -0
- package/dist/network/request.d.ts +50 -0
- package/dist/network/request.js +136 -0
- package/dist/network/tokens.d.ts +5 -0
- package/dist/network/tokens.js +150 -0
- package/dist/parser/cosmetic.d.ts +21 -0
- package/dist/parser/cosmetic.js +171 -0
- package/dist/parser/hosts.d.ts +20 -0
- package/dist/parser/hosts.js +143 -0
- package/dist/parser/index.d.ts +40 -0
- package/dist/parser/index.js +158 -0
- package/dist/parser/network.d.ts +6 -0
- package/dist/parser/network.js +474 -0
- package/dist/parser/selector.d.ts +25 -0
- package/dist/parser/selector.js +522 -0
- package/dist/parser/types.d.ts +175 -0
- package/dist/parser/types.js +75 -0
- package/dist/preload.cjs +1988 -0
- package/dist/psl/psl.d.ts +32 -0
- package/dist/psl/psl.js +119 -0
- package/dist/resources/binary.d.ts +9 -0
- package/dist/resources/binary.js +54 -0
- package/dist/resources/defusers.d.ts +29 -0
- package/dist/resources/defusers.js +180 -0
- package/dist/resources/documents.d.ts +12 -0
- package/dist/resources/documents.js +56 -0
- package/dist/resources/ima.d.ts +6 -0
- package/dist/resources/ima.js +313 -0
- package/dist/resources/index.d.ts +14 -0
- package/dist/resources/index.js +85 -0
- package/dist/resources/surrogates.d.ts +30 -0
- package/dist/resources/surrogates.js +486 -0
- package/dist/scriptlets/abort.d.ts +5 -0
- package/dist/scriptlets/abort.js +97 -0
- package/dist/scriptlets/dom/attributes.d.ts +5 -0
- package/dist/scriptlets/dom/attributes.js +92 -0
- package/dist/scriptlets/dom/click.d.ts +2 -0
- package/dist/scriptlets/dom/click.js +107 -0
- package/dist/scriptlets/dom/cookies.d.ts +16 -0
- package/dist/scriptlets/dom/cookies.js +194 -0
- package/dist/scriptlets/dom/createHtml.d.ts +2 -0
- package/dist/scriptlets/dom/createHtml.js +27 -0
- package/dist/scriptlets/dom/dialogs.d.ts +3 -0
- package/dist/scriptlets/dom/dialogs.js +21 -0
- package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
- package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
- package/dist/scriptlets/dom/index.d.ts +3 -0
- package/dist/scriptlets/dom/index.js +38 -0
- package/dist/scriptlets/dom/navigation.d.ts +5 -0
- package/dist/scriptlets/dom/navigation.js +60 -0
- package/dist/scriptlets/dom/nodeText.d.ts +3 -0
- package/dist/scriptlets/dom/nodeText.js +98 -0
- package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
- package/dist/scriptlets/dom/spoofCss.js +58 -0
- package/dist/scriptlets/dom/storage.d.ts +5 -0
- package/dist/scriptlets/dom/storage.js +50 -0
- package/dist/scriptlets/dom/watch.d.ts +8 -0
- package/dist/scriptlets/dom/watch.js +41 -0
- package/dist/scriptlets/events.d.ts +2 -0
- package/dist/scriptlets/events.js +42 -0
- package/dist/scriptlets/helpers.d.ts +58 -0
- package/dist/scriptlets/helpers.js +206 -0
- package/dist/scriptlets/index.d.ts +6 -0
- package/dist/scriptlets/index.js +47 -0
- package/dist/scriptlets/network/edit.d.ts +9 -0
- package/dist/scriptlets/network/edit.js +104 -0
- package/dist/scriptlets/network/eval.d.ts +3 -0
- package/dist/scriptlets/network/eval.js +22 -0
- package/dist/scriptlets/network/fetch.d.ts +10 -0
- package/dist/scriptlets/network/fetch.js +145 -0
- package/dist/scriptlets/network/index.d.ts +3 -0
- package/dist/scriptlets/network/index.js +43 -0
- package/dist/scriptlets/network/jsonPath.d.ts +13 -0
- package/dist/scriptlets/network/jsonPath.js +482 -0
- package/dist/scriptlets/network/media.d.ts +3 -0
- package/dist/scriptlets/network/media.js +97 -0
- package/dist/scriptlets/network/methods.d.ts +5 -0
- package/dist/scriptlets/network/methods.js +139 -0
- package/dist/scriptlets/network/prune.d.ts +13 -0
- package/dist/scriptlets/network/prune.js +194 -0
- package/dist/scriptlets/network/rendering.d.ts +4 -0
- package/dist/scriptlets/network/rendering.js +46 -0
- package/dist/scriptlets/network/replace.d.ts +5 -0
- package/dist/scriptlets/network/replace.js +168 -0
- package/dist/scriptlets/network/request.d.ts +19 -0
- package/dist/scriptlets/network/request.js +124 -0
- package/dist/scriptlets/network/util.d.ts +19 -0
- package/dist/scriptlets/network/util.js +80 -0
- package/dist/scriptlets/network/webrtc.d.ts +3 -0
- package/dist/scriptlets/network/webrtc.js +62 -0
- package/dist/scriptlets/network/windowOpen.d.ts +2 -0
- package/dist/scriptlets/network/windowOpen.js +28 -0
- package/dist/scriptlets/network/xhr.d.ts +9 -0
- package/dist/scriptlets/network/xhr.js +177 -0
- package/dist/scriptlets/runner.d.ts +8 -0
- package/dist/scriptlets/runner.js +19 -0
- package/dist/scriptlets/setConstant.d.ts +5 -0
- package/dist/scriptlets/setConstant.js +117 -0
- package/dist/scriptlets/timers.d.ts +5 -0
- package/dist/scriptlets/timers.js +66 -0
- package/dist/scriptlets/types.d.ts +32 -0
- package/dist/scriptlets/types.js +1 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +16 -0
- package/package.json +83 -4
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
import { F_BADFILTER, F_EXCEPTION, F_IMPORTANT, F_LEFT_ANCHOR, P_ALL, P_PATTERN, T_COSMETIC, T_DOCUMENT, T_INLINE_FONT, T_INLINE_SCRIPT, T_SUBDOCUMENT, } from "../parser/types.js";
|
|
2
|
+
import { PublicSuffixList } from "../psl/psl.js";
|
|
3
|
+
import { findResource } from "../resources/index.js";
|
|
4
|
+
import { tryRegex } from "../util.js";
|
|
5
|
+
import { FilterIndex } from "./filterIndex.js";
|
|
6
|
+
import { INLINE_FONT_CSP, INLINE_SCRIPT_CSP, paramNames, removeParams, skipsBlocked, uriTransform, urlSkip, } from "./modifiers.js";
|
|
7
|
+
import { ruleMatches } from "./pattern.js";
|
|
8
|
+
import { Request } from "./request.js";
|
|
9
|
+
const NO_DECISION = {
|
|
10
|
+
block: false,
|
|
11
|
+
filter: undefined,
|
|
12
|
+
redirect: undefined,
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The modifier filters that matched a request, exceptions applied: an
|
|
16
|
+
* exception with the same value lifts that value, one without a value lifts
|
|
17
|
+
* every value of its kind, and `important` ignores both.
|
|
18
|
+
*/
|
|
19
|
+
class Modifiers {
|
|
20
|
+
found = [];
|
|
21
|
+
lifted;
|
|
22
|
+
add(rule) {
|
|
23
|
+
const { type, value } = rule.modifier;
|
|
24
|
+
if (!(rule.flags & F_EXCEPTION))
|
|
25
|
+
this.found.push(rule);
|
|
26
|
+
else
|
|
27
|
+
(this.lifted ??= new Set()).add(value === "" ? type : `${type}=${value}`);
|
|
28
|
+
}
|
|
29
|
+
of(type) {
|
|
30
|
+
const { lifted } = this;
|
|
31
|
+
return this.found.filter((rule) => rule.modifier.type === type &&
|
|
32
|
+
((rule.flags & F_IMPORTANT) !== 0 ||
|
|
33
|
+
lifted === undefined ||
|
|
34
|
+
!(lifted.has(type) || lifted.has(`${type}=${rule.modifier.value}`))));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function hostListKey(list) {
|
|
38
|
+
return list === undefined
|
|
39
|
+
? ""
|
|
40
|
+
: `${[...list.include].sort().join("|")}~${[...list.exclude].sort().join("|")}`;
|
|
41
|
+
}
|
|
42
|
+
/** What makes two filters the same filter, whatever their options' order. */
|
|
43
|
+
function ruleKey(rule, withFrom) {
|
|
44
|
+
const { modifier, extra } = rule;
|
|
45
|
+
return [
|
|
46
|
+
rule.patternKind,
|
|
47
|
+
rule.pattern,
|
|
48
|
+
rule.flags & ~F_BADFILTER,
|
|
49
|
+
rule.types,
|
|
50
|
+
withFrom ? hostListKey(rule.from) : "",
|
|
51
|
+
modifier === undefined
|
|
52
|
+
? ""
|
|
53
|
+
: `${modifier.type}=${modifier.value}:${modifier.priority}`,
|
|
54
|
+
extra === undefined
|
|
55
|
+
? ""
|
|
56
|
+
: [
|
|
57
|
+
hostListKey(extra.to),
|
|
58
|
+
hostListKey(extra.top),
|
|
59
|
+
extra.denyallow?.join("|"),
|
|
60
|
+
extra.methods,
|
|
61
|
+
extra.notMethods,
|
|
62
|
+
JSON.stringify(extra.header),
|
|
63
|
+
extra.ipaddress,
|
|
64
|
+
].join("\x02"),
|
|
65
|
+
].join("\x01");
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A filter for every URL (`*`, `|http://`, `|https://`) limited to some sites
|
|
69
|
+
* counts as one filter per site, so a `badfilter` can remove a single site.
|
|
70
|
+
*/
|
|
71
|
+
function perSite(rule) {
|
|
72
|
+
const everyUrl = rule.patternKind === P_ALL ||
|
|
73
|
+
(rule.patternKind === P_PATTERN &&
|
|
74
|
+
(rule.flags & F_LEFT_ANCHOR) !== 0 &&
|
|
75
|
+
(rule.pattern === "http://" || rule.pattern === "https://"));
|
|
76
|
+
const type = rule.modifier?.type;
|
|
77
|
+
return (everyUrl &&
|
|
78
|
+
rule.from !== undefined &&
|
|
79
|
+
rule.from.include.length > 0 &&
|
|
80
|
+
rule.from.exclude.length === 0 &&
|
|
81
|
+
type !== "csp" &&
|
|
82
|
+
type !== "redirect" &&
|
|
83
|
+
type !== "redirect-rule");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Decides what happens to requests: uBlock Origin's static network filtering.
|
|
87
|
+
* Add every list's rules, call `finalize()`, then ask.
|
|
88
|
+
*/
|
|
89
|
+
export class NetworkEngine {
|
|
90
|
+
psl;
|
|
91
|
+
stats = {
|
|
92
|
+
rules: 0,
|
|
93
|
+
badfiltered: 0,
|
|
94
|
+
unknownResources: {},
|
|
95
|
+
};
|
|
96
|
+
resource;
|
|
97
|
+
pending = [];
|
|
98
|
+
/** Block and exception filters, the block half of `redirect=` included. */
|
|
99
|
+
blocks = new FilterIndex();
|
|
100
|
+
/** Redirect directives: `redirect-rule=` and the other half of `redirect=`. */
|
|
101
|
+
redirects = new FilterIndex();
|
|
102
|
+
/** `removeparam=` filters for every URL, by the parameter they remove. */
|
|
103
|
+
params = new Map();
|
|
104
|
+
/** The other `removeparam=` filters, and `urlskip=` and `uritransform=`. */
|
|
105
|
+
rewrites = new FilterIndex();
|
|
106
|
+
/** `header=`, `csp=` and `permissions=`: decided when the response comes. */
|
|
107
|
+
responses = new FilterIndex();
|
|
108
|
+
/** Exceptions that switch cosmetic filtering off for a document. */
|
|
109
|
+
switches = new FilterIndex();
|
|
110
|
+
constructor(options = {}) {
|
|
111
|
+
this.psl = options.psl ?? new PublicSuffixList();
|
|
112
|
+
this.resource = options.resource ?? ((token) => findResource(token)?.name);
|
|
113
|
+
}
|
|
114
|
+
/** Adds a list's network rules. They take effect at `finalize()`. */
|
|
115
|
+
add(rules) {
|
|
116
|
+
for (const rule of rules)
|
|
117
|
+
this.pending.push(rule);
|
|
118
|
+
}
|
|
119
|
+
/** Applies the `badfilter`s and files every rule. */
|
|
120
|
+
finalize() {
|
|
121
|
+
const steps = this.finalizing();
|
|
122
|
+
while (steps.next().done !== true)
|
|
123
|
+
;
|
|
124
|
+
}
|
|
125
|
+
/** How each kind of rule is filed, for `yarn bench`. */
|
|
126
|
+
describe() {
|
|
127
|
+
return {
|
|
128
|
+
blocks: this.blocks.describe(),
|
|
129
|
+
redirects: this.redirects.describe(),
|
|
130
|
+
rewrites: `${this.params.size} parameters, ${this.rewrites.describe()}`,
|
|
131
|
+
responses: this.responses.describe(),
|
|
132
|
+
switches: this.switches.describe(),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* `finalize()` in steps: a host with an event loop gives it a turn between
|
|
137
|
+
* them, so filing 230,000 rules doesn't freeze its main process.
|
|
138
|
+
*/
|
|
139
|
+
*finalizing() {
|
|
140
|
+
const bad = new Set();
|
|
141
|
+
const badSites = new Map();
|
|
142
|
+
for (const rule of this.pending) {
|
|
143
|
+
if (!(rule.flags & F_BADFILTER))
|
|
144
|
+
continue;
|
|
145
|
+
if (perSite(rule)) {
|
|
146
|
+
const key = ruleKey(rule, false);
|
|
147
|
+
const sites = badSites.get(key) ?? new Set();
|
|
148
|
+
for (const site of rule.from.include)
|
|
149
|
+
sites.add(site);
|
|
150
|
+
badSites.set(key, sites);
|
|
151
|
+
}
|
|
152
|
+
else
|
|
153
|
+
bad.add(ruleKey(rule, true));
|
|
154
|
+
}
|
|
155
|
+
let filed = 0;
|
|
156
|
+
for (let rule of this.pending) {
|
|
157
|
+
if (++filed % 5000 === 0)
|
|
158
|
+
yield;
|
|
159
|
+
if (rule.flags & F_BADFILTER)
|
|
160
|
+
continue;
|
|
161
|
+
if (bad.size > 0 && bad.has(ruleKey(rule, true))) {
|
|
162
|
+
this.stats.badfiltered++;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (badSites.size > 0 && perSite(rule)) {
|
|
166
|
+
const sites = badSites.get(ruleKey(rule, false));
|
|
167
|
+
if (sites !== undefined) {
|
|
168
|
+
const include = rule.from.include.filter((site) => !sites.has(site));
|
|
169
|
+
if (include.length === 0) {
|
|
170
|
+
this.stats.badfiltered++;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
rule = { ...rule, from: { include, exclude: [] } };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
this.file(rule);
|
|
177
|
+
}
|
|
178
|
+
this.pending = [];
|
|
179
|
+
for (const index of [
|
|
180
|
+
this.blocks,
|
|
181
|
+
this.redirects,
|
|
182
|
+
this.rewrites,
|
|
183
|
+
this.responses,
|
|
184
|
+
this.switches,
|
|
185
|
+
]) {
|
|
186
|
+
yield* index.finalizing();
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
file(rule) {
|
|
190
|
+
this.stats.rules++;
|
|
191
|
+
if (rule.types & T_COSMETIC) {
|
|
192
|
+
this.switches.add(rule);
|
|
193
|
+
if ((rule.types & ~T_COSMETIC) === 0)
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (rule.extra?.header !== undefined) {
|
|
197
|
+
this.responses.add(rule);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const { modifier } = rule;
|
|
201
|
+
switch (modifier?.type) {
|
|
202
|
+
case undefined:
|
|
203
|
+
this.blocks.add(rule);
|
|
204
|
+
return;
|
|
205
|
+
case "csp":
|
|
206
|
+
case "permissions":
|
|
207
|
+
this.responses.add(rule);
|
|
208
|
+
return;
|
|
209
|
+
case "removeparam":
|
|
210
|
+
// One for every URL is found by the parameter's name, not by the URL.
|
|
211
|
+
if (rule.patternKind === P_ALL &&
|
|
212
|
+
/^[^~/]/.test(modifier.value) &&
|
|
213
|
+
!(rule.flags & F_EXCEPTION)) {
|
|
214
|
+
const bucket = this.params.get(modifier.value);
|
|
215
|
+
if (bucket === undefined)
|
|
216
|
+
this.params.set(modifier.value, [rule]);
|
|
217
|
+
else
|
|
218
|
+
bucket.push(rule);
|
|
219
|
+
}
|
|
220
|
+
else
|
|
221
|
+
this.rewrites.add(rule);
|
|
222
|
+
return;
|
|
223
|
+
case "urlskip":
|
|
224
|
+
case "uritransform":
|
|
225
|
+
this.rewrites.add(rule);
|
|
226
|
+
return;
|
|
227
|
+
case "redirect":
|
|
228
|
+
case "redirect-rule": {
|
|
229
|
+
if (modifier.value === "none") {
|
|
230
|
+
// The old way to write an exception to every redirect.
|
|
231
|
+
this.redirects.add({
|
|
232
|
+
...rule,
|
|
233
|
+
flags: rule.flags | F_EXCEPTION,
|
|
234
|
+
modifier: { type: "redirect-rule", value: "", priority: 0 },
|
|
235
|
+
});
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
let name = "";
|
|
239
|
+
if (modifier.value !== "") {
|
|
240
|
+
const resource = this.resource(modifier.value);
|
|
241
|
+
if (resource === undefined) {
|
|
242
|
+
const unknown = this.stats.unknownResources;
|
|
243
|
+
unknown[modifier.value] = (unknown[modifier.value] ?? 0) + 1;
|
|
244
|
+
this.stats.rules--;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
name = resource;
|
|
248
|
+
}
|
|
249
|
+
// `redirect=` is a block filter and a redirect directive.
|
|
250
|
+
this.redirects.add({
|
|
251
|
+
...rule,
|
|
252
|
+
modifier: { ...modifier, type: "redirect-rule", value: name },
|
|
253
|
+
});
|
|
254
|
+
if (modifier.type === "redirect")
|
|
255
|
+
this.blocks.add(rule);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** Works out what filters ask about a request, once. */
|
|
260
|
+
request(info) {
|
|
261
|
+
return new Request(info, this.psl);
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Whether to block a request: an `important` block wins, then an
|
|
265
|
+
* exception, then a block.
|
|
266
|
+
*/
|
|
267
|
+
match(request) {
|
|
268
|
+
const decision = this.decide(request);
|
|
269
|
+
if (!decision.block)
|
|
270
|
+
return decision;
|
|
271
|
+
return { ...decision, redirect: this.redirectFor(request) };
|
|
272
|
+
}
|
|
273
|
+
decide(request) {
|
|
274
|
+
// An object: TypeScript doesn't follow a callback's assignments to locals.
|
|
275
|
+
const found = {};
|
|
276
|
+
this.blocks.forEachMatch(request, (rule) => {
|
|
277
|
+
if (rule.flags & F_EXCEPTION)
|
|
278
|
+
found.exception ??= rule;
|
|
279
|
+
else if (rule.flags & F_IMPORTANT) {
|
|
280
|
+
found.important = rule;
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
else
|
|
284
|
+
found.block ??= rule;
|
|
285
|
+
return false;
|
|
286
|
+
});
|
|
287
|
+
const decided = found.important ?? found.exception ?? found.block;
|
|
288
|
+
if (decided === undefined)
|
|
289
|
+
return NO_DECISION;
|
|
290
|
+
return {
|
|
291
|
+
block: (decided.flags & F_EXCEPTION) === 0,
|
|
292
|
+
filter: decided.raw,
|
|
293
|
+
redirect: undefined,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
modifiers(index, request) {
|
|
297
|
+
const modifiers = new Modifiers();
|
|
298
|
+
index.forEachMatch(request, (rule) => {
|
|
299
|
+
if (rule.modifier !== undefined)
|
|
300
|
+
modifiers.add(rule);
|
|
301
|
+
});
|
|
302
|
+
return modifiers;
|
|
303
|
+
}
|
|
304
|
+
/** The directive with the highest priority, `important` ones first. */
|
|
305
|
+
redirectFor(request) {
|
|
306
|
+
let best;
|
|
307
|
+
const rank = (rule) => (rule.flags & F_IMPORTANT ? 1e9 : 0) + rule.modifier.priority;
|
|
308
|
+
for (const rule of this.modifiers(this.redirects, request).of("redirect-rule")) {
|
|
309
|
+
if (best === undefined || rank(rule) > rank(best))
|
|
310
|
+
best = rule;
|
|
311
|
+
}
|
|
312
|
+
return best?.modifier.value;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* The URL to send a request to instead: its own without the parameters
|
|
316
|
+
* `removeparam=` names, or the one `urlskip=` or `uritransform=` gives.
|
|
317
|
+
* Undefined when the URL stays. A blocked request is only rewritten by a
|
|
318
|
+
* `urlskip=` that says `-blocked`.
|
|
319
|
+
*/
|
|
320
|
+
rewrite(request, blocked = false) {
|
|
321
|
+
let url = request.url;
|
|
322
|
+
const modifiers = this.modifiers(this.rewrites, request);
|
|
323
|
+
for (const rule of modifiers.of("urlskip")) {
|
|
324
|
+
const { value } = rule.modifier;
|
|
325
|
+
if (blocked && !skipsBlocked(value))
|
|
326
|
+
continue;
|
|
327
|
+
const skipped = urlSkip(url, value);
|
|
328
|
+
if (skipped !== undefined)
|
|
329
|
+
return skipped;
|
|
330
|
+
}
|
|
331
|
+
if (blocked)
|
|
332
|
+
return undefined;
|
|
333
|
+
for (const rule of modifiers.of("uritransform")) {
|
|
334
|
+
url = uriTransform(url, rule.modifier.value) ?? url;
|
|
335
|
+
}
|
|
336
|
+
if (url.includes("?")) {
|
|
337
|
+
if (this.params.size > 0) {
|
|
338
|
+
for (const name of paramNames(url)) {
|
|
339
|
+
for (const rule of this.params.get(name) ?? []) {
|
|
340
|
+
if (ruleMatches(rule, request))
|
|
341
|
+
modifiers.add(rule);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
url = removeParams(url, modifiers.of("removeparam").map((rule) => rule.modifier.value));
|
|
346
|
+
}
|
|
347
|
+
return url === request.url ? undefined : url;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* What a response changes: a `header=` filter can block it, and a document
|
|
351
|
+
* gets the policies of `csp=`, `permissions=`, `inline-script` and
|
|
352
|
+
* `inline-font`. `header` gives a response header's value by its lowercase
|
|
353
|
+
* name.
|
|
354
|
+
*/
|
|
355
|
+
response(request, header) {
|
|
356
|
+
const decision = {
|
|
357
|
+
block: false,
|
|
358
|
+
filter: undefined,
|
|
359
|
+
csp: [],
|
|
360
|
+
permissions: [],
|
|
361
|
+
};
|
|
362
|
+
const found = {
|
|
363
|
+
exception: false,
|
|
364
|
+
};
|
|
365
|
+
const modifiers = new Modifiers();
|
|
366
|
+
this.responses.forEachMatch(request, (rule) => {
|
|
367
|
+
const wanted = rule.extra?.header;
|
|
368
|
+
if (wanted === undefined) {
|
|
369
|
+
modifiers.add(rule);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
if (wanted.name !== "") {
|
|
373
|
+
const value = header(wanted.name);
|
|
374
|
+
if (value === undefined)
|
|
375
|
+
return;
|
|
376
|
+
if (wanted.value !== undefined) {
|
|
377
|
+
const matches = wanted.regex
|
|
378
|
+
? tryRegex(wanted.value, wanted.regexFlags)?.test(value) === true
|
|
379
|
+
: value === wanted.value;
|
|
380
|
+
if (matches === wanted.negated)
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (rule.flags & F_EXCEPTION)
|
|
385
|
+
found.exception = true;
|
|
386
|
+
else if (rule.flags & F_IMPORTANT || found.block === undefined) {
|
|
387
|
+
found.block = rule;
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
const { block } = found;
|
|
391
|
+
if (block !== undefined) {
|
|
392
|
+
// A plain exception for the request lifts a header= block too.
|
|
393
|
+
const plain = this.decide(request);
|
|
394
|
+
if (block.flags & F_IMPORTANT ||
|
|
395
|
+
!(found.exception || (!plain.block && plain.filter !== undefined))) {
|
|
396
|
+
decision.block = true;
|
|
397
|
+
decision.filter = block.raw;
|
|
398
|
+
return decision;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (request.type & (T_DOCUMENT | T_SUBDOCUMENT)) {
|
|
402
|
+
const values = (type) => modifiers.of(type).map((rule) => rule.modifier.value);
|
|
403
|
+
decision.csp = values("csp");
|
|
404
|
+
decision.permissions = values("permissions");
|
|
405
|
+
const type = request.type;
|
|
406
|
+
request.type = T_INLINE_SCRIPT;
|
|
407
|
+
if (this.decide(request).block)
|
|
408
|
+
decision.csp.push(INLINE_SCRIPT_CSP);
|
|
409
|
+
request.type = T_INLINE_FONT;
|
|
410
|
+
if (this.decide(request).block)
|
|
411
|
+
decision.csp.push(INLINE_FONT_CSP);
|
|
412
|
+
request.type = type;
|
|
413
|
+
}
|
|
414
|
+
return decision;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Which cosmetic filtering exceptions switch off for a document, as
|
|
418
|
+
* `T_GENERICHIDE`, `T_SPECIFICHIDE` and `T_ELEMHIDE` bits. `request` is the
|
|
419
|
+
* document's own, of type `document`.
|
|
420
|
+
*/
|
|
421
|
+
cosmeticSwitches(request) {
|
|
422
|
+
let bits = 0;
|
|
423
|
+
const type = request.type;
|
|
424
|
+
request.type = T_COSMETIC;
|
|
425
|
+
this.switches.forEachMatch(request, (rule) => {
|
|
426
|
+
bits |= rule.types & T_COSMETIC;
|
|
427
|
+
});
|
|
428
|
+
request.type = type;
|
|
429
|
+
return bits;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type NetworkRule } from "../parser/types.js";
|
|
2
|
+
import type { Request } from "./request.js";
|
|
3
|
+
/**
|
|
4
|
+
* Rules filed so that a request only tests a few of them: by the hostname
|
|
5
|
+
* they start with, else by a token of their pattern, else by the site they
|
|
6
|
+
* are limited to. Only what can't be filed is tested against every request.
|
|
7
|
+
*/
|
|
8
|
+
export declare class FilterIndex {
|
|
9
|
+
/** How many rules the index holds. */
|
|
10
|
+
size: number;
|
|
11
|
+
private pending;
|
|
12
|
+
/** `||hostname^` rules, packed, by hostname. */
|
|
13
|
+
private readonly simpleHosts;
|
|
14
|
+
/** `||hostname/literal` rules, packed, by hostname. */
|
|
15
|
+
private readonly simplePaths;
|
|
16
|
+
/** The other rules that start with a whole hostname. */
|
|
17
|
+
private readonly hosts;
|
|
18
|
+
private readonly tokens;
|
|
19
|
+
private readonly sources;
|
|
20
|
+
private readonly rest;
|
|
21
|
+
add(rule: NetworkRule): void;
|
|
22
|
+
/** How the rules are filed, for `yarn bench`. */
|
|
23
|
+
describe(): string;
|
|
24
|
+
/** Files what `add()` couldn't file alone. Call it before matching. */
|
|
25
|
+
finalize(): void;
|
|
26
|
+
/** `finalize()` in steps, for a caller that has an event loop to feed. */
|
|
27
|
+
finalizing(): Generator<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Calls `visit` with each rule that matches the request, until it returns
|
|
30
|
+
* true.
|
|
31
|
+
*/
|
|
32
|
+
forEachMatch(request: Request, visit: (rule: NetworkRule) => boolean | void): void;
|
|
33
|
+
private visit;
|
|
34
|
+
/**
|
|
35
|
+
* Visits the packed paths the URL starts with, from where the hostname
|
|
36
|
+
* they share starts. The patterns are sorted, so each one is found by
|
|
37
|
+
* bisection, the longest first.
|
|
38
|
+
*/
|
|
39
|
+
private visitPaths;
|
|
40
|
+
}
|