@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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +72 -2
  3. package/THIRD_PARTY_NOTICES.md +50 -0
  4. package/dist/content/cosmetics.d.ts +22 -0
  5. package/dist/content/cosmetics.js +173 -0
  6. package/dist/content/mainWorld.d.ts +8 -0
  7. package/dist/content/mainWorld.js +10 -0
  8. package/dist/content/preload.d.ts +1 -0
  9. package/dist/content/preload.js +34 -0
  10. package/dist/content/procedural.d.ts +19 -0
  11. package/dist/content/procedural.js +212 -0
  12. package/dist/cosmetic/engine.d.ts +95 -0
  13. package/dist/cosmetic/engine.js +268 -0
  14. package/dist/electron/blocker.d.ts +84 -0
  15. package/dist/electron/blocker.js +320 -0
  16. package/dist/electron/channels.d.ts +10 -0
  17. package/dist/electron/channels.js +6 -0
  18. package/dist/electron/scheme.d.ts +21 -0
  19. package/dist/electron/scheme.js +21 -0
  20. package/dist/engine.d.ts +86 -0
  21. package/dist/engine.js +163 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +12 -0
  24. package/dist/lists/defaults.d.ts +6 -0
  25. package/dist/lists/defaults.js +59 -0
  26. package/dist/lists/manager.d.ts +86 -0
  27. package/dist/lists/manager.js +154 -0
  28. package/dist/lists/preprocessor.d.ts +21 -0
  29. package/dist/lists/preprocessor.js +152 -0
  30. package/dist/lists/storage.d.ts +37 -0
  31. package/dist/lists/storage.js +66 -0
  32. package/dist/network/engine.d.ts +105 -0
  33. package/dist/network/engine.js +431 -0
  34. package/dist/network/filterIndex.d.ts +40 -0
  35. package/dist/network/filterIndex.js +343 -0
  36. package/dist/network/modifiers.d.ts +23 -0
  37. package/dist/network/modifiers.js +192 -0
  38. package/dist/network/pattern.d.ts +7 -0
  39. package/dist/network/pattern.js +145 -0
  40. package/dist/network/request.d.ts +50 -0
  41. package/dist/network/request.js +136 -0
  42. package/dist/network/tokens.d.ts +5 -0
  43. package/dist/network/tokens.js +150 -0
  44. package/dist/parser/cosmetic.d.ts +21 -0
  45. package/dist/parser/cosmetic.js +171 -0
  46. package/dist/parser/hosts.d.ts +20 -0
  47. package/dist/parser/hosts.js +143 -0
  48. package/dist/parser/index.d.ts +40 -0
  49. package/dist/parser/index.js +158 -0
  50. package/dist/parser/network.d.ts +6 -0
  51. package/dist/parser/network.js +474 -0
  52. package/dist/parser/selector.d.ts +25 -0
  53. package/dist/parser/selector.js +522 -0
  54. package/dist/parser/types.d.ts +175 -0
  55. package/dist/parser/types.js +75 -0
  56. package/dist/preload.cjs +1988 -0
  57. package/dist/psl/psl.d.ts +32 -0
  58. package/dist/psl/psl.js +119 -0
  59. package/dist/resources/binary.d.ts +9 -0
  60. package/dist/resources/binary.js +54 -0
  61. package/dist/resources/defusers.d.ts +29 -0
  62. package/dist/resources/defusers.js +180 -0
  63. package/dist/resources/documents.d.ts +12 -0
  64. package/dist/resources/documents.js +56 -0
  65. package/dist/resources/ima.d.ts +6 -0
  66. package/dist/resources/ima.js +313 -0
  67. package/dist/resources/index.d.ts +14 -0
  68. package/dist/resources/index.js +85 -0
  69. package/dist/resources/surrogates.d.ts +30 -0
  70. package/dist/resources/surrogates.js +486 -0
  71. package/dist/scriptlets/abort.d.ts +5 -0
  72. package/dist/scriptlets/abort.js +97 -0
  73. package/dist/scriptlets/dom/attributes.d.ts +5 -0
  74. package/dist/scriptlets/dom/attributes.js +92 -0
  75. package/dist/scriptlets/dom/click.d.ts +2 -0
  76. package/dist/scriptlets/dom/click.js +107 -0
  77. package/dist/scriptlets/dom/cookies.d.ts +16 -0
  78. package/dist/scriptlets/dom/cookies.js +194 -0
  79. package/dist/scriptlets/dom/createHtml.d.ts +2 -0
  80. package/dist/scriptlets/dom/createHtml.js +27 -0
  81. package/dist/scriptlets/dom/dialogs.d.ts +3 -0
  82. package/dist/scriptlets/dom/dialogs.js +21 -0
  83. package/dist/scriptlets/dom/hrefSanitizer.d.ts +2 -0
  84. package/dist/scriptlets/dom/hrefSanitizer.js +91 -0
  85. package/dist/scriptlets/dom/index.d.ts +3 -0
  86. package/dist/scriptlets/dom/index.js +38 -0
  87. package/dist/scriptlets/dom/navigation.d.ts +5 -0
  88. package/dist/scriptlets/dom/navigation.js +60 -0
  89. package/dist/scriptlets/dom/nodeText.d.ts +3 -0
  90. package/dist/scriptlets/dom/nodeText.js +98 -0
  91. package/dist/scriptlets/dom/spoofCss.d.ts +2 -0
  92. package/dist/scriptlets/dom/spoofCss.js +58 -0
  93. package/dist/scriptlets/dom/storage.d.ts +5 -0
  94. package/dist/scriptlets/dom/storage.js +50 -0
  95. package/dist/scriptlets/dom/watch.d.ts +8 -0
  96. package/dist/scriptlets/dom/watch.js +41 -0
  97. package/dist/scriptlets/events.d.ts +2 -0
  98. package/dist/scriptlets/events.js +42 -0
  99. package/dist/scriptlets/helpers.d.ts +58 -0
  100. package/dist/scriptlets/helpers.js +206 -0
  101. package/dist/scriptlets/index.d.ts +6 -0
  102. package/dist/scriptlets/index.js +47 -0
  103. package/dist/scriptlets/network/edit.d.ts +9 -0
  104. package/dist/scriptlets/network/edit.js +104 -0
  105. package/dist/scriptlets/network/eval.d.ts +3 -0
  106. package/dist/scriptlets/network/eval.js +22 -0
  107. package/dist/scriptlets/network/fetch.d.ts +10 -0
  108. package/dist/scriptlets/network/fetch.js +145 -0
  109. package/dist/scriptlets/network/index.d.ts +3 -0
  110. package/dist/scriptlets/network/index.js +43 -0
  111. package/dist/scriptlets/network/jsonPath.d.ts +13 -0
  112. package/dist/scriptlets/network/jsonPath.js +482 -0
  113. package/dist/scriptlets/network/media.d.ts +3 -0
  114. package/dist/scriptlets/network/media.js +97 -0
  115. package/dist/scriptlets/network/methods.d.ts +5 -0
  116. package/dist/scriptlets/network/methods.js +139 -0
  117. package/dist/scriptlets/network/prune.d.ts +13 -0
  118. package/dist/scriptlets/network/prune.js +194 -0
  119. package/dist/scriptlets/network/rendering.d.ts +4 -0
  120. package/dist/scriptlets/network/rendering.js +46 -0
  121. package/dist/scriptlets/network/replace.d.ts +5 -0
  122. package/dist/scriptlets/network/replace.js +168 -0
  123. package/dist/scriptlets/network/request.d.ts +19 -0
  124. package/dist/scriptlets/network/request.js +124 -0
  125. package/dist/scriptlets/network/util.d.ts +19 -0
  126. package/dist/scriptlets/network/util.js +80 -0
  127. package/dist/scriptlets/network/webrtc.d.ts +3 -0
  128. package/dist/scriptlets/network/webrtc.js +62 -0
  129. package/dist/scriptlets/network/windowOpen.d.ts +2 -0
  130. package/dist/scriptlets/network/windowOpen.js +28 -0
  131. package/dist/scriptlets/network/xhr.d.ts +9 -0
  132. package/dist/scriptlets/network/xhr.js +177 -0
  133. package/dist/scriptlets/runner.d.ts +8 -0
  134. package/dist/scriptlets/runner.js +19 -0
  135. package/dist/scriptlets/setConstant.d.ts +5 -0
  136. package/dist/scriptlets/setConstant.js +117 -0
  137. package/dist/scriptlets/timers.d.ts +5 -0
  138. package/dist/scriptlets/timers.js +66 -0
  139. package/dist/scriptlets/types.d.ts +32 -0
  140. package/dist/scriptlets/types.js +1 -0
  141. package/dist/util.d.ts +5 -0
  142. package/dist/util.js +16 -0
  143. package/package.json +83 -4
@@ -0,0 +1,343 @@
1
+ import { F_EXCEPTION, F_FIRST_PARTY, F_HOST_ANCHOR, F_RIGHT_ANCHOR, F_STRICT_FIRST_PARTY, F_STRICT_THIRD_PARTY, F_THIRD_PARTY, P_HOST, P_PATTERN, } from "../parser/types.js";
2
+ import { ruleMatches } from "./pattern.js";
3
+ import { ruleTokens } from "./tokens.js";
4
+ // Rules filed between two turns of the event loop.
5
+ const STEP = 5000;
6
+ // More patterns than this under one hostname are filed by token instead.
7
+ const MAX_HOST_BUCKET = 8;
8
+ // A rule with nothing but a pattern, flags and types packs into its pattern
9
+ // and one number: uBlock Origin's lists hold 200,000 of them, and a rule
10
+ // object each would cost five times the memory.
11
+ const TYPE_BITS = 18;
12
+ const TYPE_MASK = (1 << TYPE_BITS) - 1;
13
+ const FLAG_MASK = (1 << 11) - 1;
14
+ /** A packed path ends with `^`: a separator or the URL's end must follow. */
15
+ const SEPARATOR_FOLLOWS = 1 << 29;
16
+ /** A hostname a host-anchored pattern starts with, whole: `||a.example/x`. */
17
+ function leadingHostname(rule) {
18
+ if (rule.patternKind !== P_PATTERN || !(rule.flags & F_HOST_ANCHOR)) {
19
+ return undefined;
20
+ }
21
+ const end = rule.pattern.search(/[/^:?*]/);
22
+ // A wildcard right after could continue the hostname: `||a.example*`.
23
+ if (end <= 0 || rule.pattern[end] === "*")
24
+ return undefined;
25
+ const hostname = rule.pattern.slice(0, end);
26
+ return /^[a-z0-9_.-]+\.[a-z0-9_-]+$/.test(hostname) ? hostname : undefined;
27
+ }
28
+ function isPlain(rule) {
29
+ return (rule.from === undefined &&
30
+ rule.modifier === undefined &&
31
+ rule.extra === undefined);
32
+ }
33
+ function push(map, key, rule) {
34
+ const bucket = map.get(key);
35
+ if (bucket === undefined)
36
+ map.set(key, [rule]);
37
+ else
38
+ bucket.push(rule);
39
+ }
40
+ /** `^` in a pattern: anything but a letter, a digit or one of `_-.%`. */
41
+ function isSeparator(c) {
42
+ // NaN, past the end of the URL, is one too.
43
+ return !((c >= 0x61 && c <= 0x7a) ||
44
+ (c >= 0x30 && c <= 0x39) ||
45
+ c === 0x5f ||
46
+ c === 0x2d ||
47
+ c === 0x2e ||
48
+ c === 0x25);
49
+ }
50
+ /**
51
+ * Rules filed so that a request only tests a few of them: by the hostname
52
+ * they start with, else by a token of their pattern, else by the site they
53
+ * are limited to. Only what can't be filed is tested against every request.
54
+ */
55
+ export class FilterIndex {
56
+ /** How many rules the index holds. */
57
+ size = 0;
58
+ pending = [];
59
+ /** `||hostname^` rules, packed, by hostname. */
60
+ simpleHosts = new Map();
61
+ /** `||hostname/literal` rules, packed, by hostname. */
62
+ simplePaths = new Map();
63
+ /** The other rules that start with a whole hostname. */
64
+ hosts = new Map();
65
+ tokens = new Map();
66
+ sources = new Map();
67
+ rest = [];
68
+ add(rule) {
69
+ this.size++;
70
+ const packed = (rule.flags << TYPE_BITS) | rule.types;
71
+ if (rule.patternKind === P_HOST) {
72
+ if (!isPlain(rule)) {
73
+ push(this.hosts, rule.pattern, rule);
74
+ return;
75
+ }
76
+ const existing = this.simpleHosts.get(rule.pattern);
77
+ if (existing === undefined)
78
+ this.simpleHosts.set(rule.pattern, packed);
79
+ else if (typeof existing === "number") {
80
+ if (existing !== packed) {
81
+ this.simpleHosts.set(rule.pattern, [existing, packed]);
82
+ }
83
+ }
84
+ else if (!existing.includes(packed))
85
+ existing.push(packed);
86
+ return;
87
+ }
88
+ const hostname = leadingHostname(rule);
89
+ if (hostname === undefined) {
90
+ this.pending.push(rule);
91
+ return;
92
+ }
93
+ // A literal after the hostname, with at most a `^` to end it.
94
+ const separator = rule.pattern.endsWith("^");
95
+ const literal = separator ? rule.pattern.slice(0, -1) : rule.pattern;
96
+ if (!isPlain(rule) ||
97
+ /[*^]/.test(literal) ||
98
+ (separator && rule.flags & F_RIGHT_ANCHOR)) {
99
+ push(this.hosts, hostname, rule);
100
+ return;
101
+ }
102
+ let bucket = this.simplePaths.get(hostname);
103
+ if (bucket === undefined) {
104
+ bucket = { patterns: [], packed: [] };
105
+ this.simplePaths.set(hostname, bucket);
106
+ }
107
+ bucket.patterns.push(literal);
108
+ bucket.packed.push(separator ? packed | SEPARATOR_FOLLOWS : packed);
109
+ }
110
+ /** How the rules are filed, for `yarn bench`. */
111
+ describe() {
112
+ const longest = (buckets) => Math.max(0, ...[...buckets].map((bucket) => bucket.length));
113
+ const paths = [...this.simplePaths.values()].map((bucket) => bucket.packed);
114
+ return [
115
+ `${this.simpleHosts.size} packed hosts`,
116
+ `${this.simplePaths.size} hosts with packed paths (most ${longest(paths)})`,
117
+ `${this.hosts.size} hosts (most ${longest(this.hosts.values())})`,
118
+ `${this.tokens.size} tokens (most ${longest(this.tokens.values())})`,
119
+ `${this.sources.size} sites`,
120
+ `${this.rest.length} unfiled`,
121
+ ].join(", ");
122
+ }
123
+ /** Files what `add()` couldn't file alone. Call it before matching. */
124
+ finalize() {
125
+ const steps = this.finalizing();
126
+ while (steps.next().done !== true)
127
+ ;
128
+ }
129
+ /** `finalize()` in steps, for a caller that has an event loop to feed. */
130
+ *finalizing() {
131
+ const pending = this.pending ?? [];
132
+ this.pending = undefined;
133
+ // Sorted, a bucket is searched by bisection: one hostname can have
134
+ // 50,000 patterns (a file host with that many bad files).
135
+ for (const bucket of this.simplePaths.values()) {
136
+ const order = bucket.patterns
137
+ .map((_, index) => index)
138
+ .sort((a, b) => bucket.patterns[a] < bucket.patterns[b]
139
+ ? -1
140
+ : bucket.patterns[a] > bucket.patterns[b]
141
+ ? 1
142
+ : 0);
143
+ bucket.patterns = order.map((index) => bucket.patterns[index]);
144
+ bucket.packed = order.map((index) => bucket.packed[index]);
145
+ if (order.length > STEP)
146
+ yield;
147
+ }
148
+ // A hostname with many other patterns would test them all for each of
149
+ // its requests: those are filed by token.
150
+ for (const [hostname, bucket] of this.hosts) {
151
+ if (bucket.length <= MAX_HOST_BUCKET)
152
+ continue;
153
+ const kept = bucket.filter((rule) => rule.patternKind === P_HOST);
154
+ for (const rule of bucket) {
155
+ if (rule.patternKind !== P_HOST)
156
+ pending.push(rule);
157
+ }
158
+ if (kept.length === 0)
159
+ this.hosts.delete(hostname);
160
+ else
161
+ this.hosts.set(hostname, kept);
162
+ }
163
+ // Each rule goes under the token the fewest other rules could go under,
164
+ // so no bucket grows long.
165
+ const candidates = [];
166
+ const counts = new Map();
167
+ for (const rule of pending) {
168
+ const tokens = ruleTokens(rule);
169
+ candidates.push(tokens);
170
+ for (const token of tokens) {
171
+ counts.set(token, (counts.get(token) ?? 0) + 1);
172
+ }
173
+ if (candidates.length % STEP === 0)
174
+ yield;
175
+ }
176
+ for (const [index, rule] of pending.entries()) {
177
+ if (index % STEP === 0)
178
+ yield;
179
+ let best;
180
+ let bestCount = Infinity;
181
+ for (const token of candidates[index]) {
182
+ const count = counts.get(token);
183
+ if (count < bestCount) {
184
+ best = token;
185
+ bestCount = count;
186
+ }
187
+ }
188
+ if (best !== undefined) {
189
+ push(this.tokens, best, rule);
190
+ continue;
191
+ }
192
+ // No token: a rule limited to some sites is filed under each of them.
193
+ const sites = rule.from?.include ?? [];
194
+ if (sites.length > 0 &&
195
+ sites.every((site) => site !== "*" && !site.startsWith("/"))) {
196
+ for (const site of sites)
197
+ push(this.sources, site, rule);
198
+ }
199
+ else
200
+ this.rest.push(rule);
201
+ }
202
+ }
203
+ /**
204
+ * Calls `visit` with each rule that matches the request, until it returns
205
+ * true.
206
+ */
207
+ forEachMatch(request, visit) {
208
+ const { hostname } = request;
209
+ // The hostname and each of its parents: a.b.example, b.example, example.
210
+ for (let start = 0; start !== -1;) {
211
+ const suffix = start === 0 ? hostname : hostname.slice(start);
212
+ const packed = this.simpleHosts.get(suffix);
213
+ if (packed !== undefined) {
214
+ if (typeof packed === "number") {
215
+ if (simpleMatches(packed, request) && visit(unpack(suffix, packed))) {
216
+ return;
217
+ }
218
+ }
219
+ else {
220
+ for (const one of packed) {
221
+ if (simpleMatches(one, request) && visit(unpack(suffix, one))) {
222
+ return;
223
+ }
224
+ }
225
+ }
226
+ }
227
+ const paths = this.simplePaths.get(suffix);
228
+ if ((paths !== undefined &&
229
+ this.visitPaths(paths, request, suffix.length, visit)) ||
230
+ this.visit(this.hosts.get(suffix), request, visit)) {
231
+ return;
232
+ }
233
+ const dot = hostname.indexOf(".", start);
234
+ start = dot === -1 ? -1 : dot + 1;
235
+ }
236
+ if (this.tokens.size > 0) {
237
+ for (const token of request.tokens) {
238
+ if (this.visit(this.tokens.get(token), request, visit))
239
+ return;
240
+ }
241
+ }
242
+ if (this.sources.size > 0) {
243
+ const source = request.sourceHostname;
244
+ const entity = request.sourceEntity;
245
+ for (let start = 0; start !== -1;) {
246
+ if (this.visit(this.sources.get(source.slice(start)), request, visit) ||
247
+ (start < entity.length - 2 &&
248
+ this.visit(this.sources.get(entity.slice(start)), request, visit))) {
249
+ return;
250
+ }
251
+ const dot = source.indexOf(".", start);
252
+ start = dot === -1 ? -1 : dot + 1;
253
+ }
254
+ }
255
+ this.visit(this.rest, request, visit);
256
+ }
257
+ visit(bucket, request, visit) {
258
+ if (bucket === undefined)
259
+ return false;
260
+ for (const rule of bucket) {
261
+ if (ruleMatches(rule, request) && visit(rule))
262
+ return true;
263
+ }
264
+ return false;
265
+ }
266
+ /**
267
+ * Visits the packed paths the URL starts with, from where the hostname
268
+ * they share starts. The patterns are sorted, so each one is found by
269
+ * bisection, the longest first.
270
+ */
271
+ visitPaths({ patterns, packed }, request, hostLength, visit) {
272
+ const { lower } = request;
273
+ const at = request.hostEnd - hostLength;
274
+ // Shortened as patterns are ruled out; `lower` stays whole for what
275
+ // follows a pattern.
276
+ let text = lower.slice(at);
277
+ let high = patterns.length;
278
+ while (high > 0 && text.length > hostLength) {
279
+ // The last pattern that sorts at or before the text.
280
+ let low = 0;
281
+ for (let end = high; low < end;) {
282
+ const middle = (low + end) >>> 1;
283
+ if (patterns[middle] <= text)
284
+ low = middle + 1;
285
+ else
286
+ end = middle;
287
+ }
288
+ if (low === 0)
289
+ return false;
290
+ const pattern = patterns[low - 1];
291
+ if (!text.startsWith(pattern)) {
292
+ // No pattern longer than what the two share can match.
293
+ let shared = 0;
294
+ while (pattern.charCodeAt(shared) === text.charCodeAt(shared))
295
+ shared++;
296
+ text = text.slice(0, shared);
297
+ high = low - 1;
298
+ continue;
299
+ }
300
+ // The same pattern can be there twice, with other flags and types.
301
+ for (high = low; high > 0 && patterns[high - 1] === pattern; high--) {
302
+ const bits = packed[high - 1];
303
+ const flags = (bits >>> TYPE_BITS) & FLAG_MASK;
304
+ if (simpleMatches(bits, request) &&
305
+ (!(bits & SEPARATOR_FOLLOWS) ||
306
+ isSeparator(lower.charCodeAt(at + pattern.length))) &&
307
+ (!(flags & F_RIGHT_ANCHOR) || at + pattern.length === lower.length) &&
308
+ visit(unpack(pattern, bits, P_PATTERN))) {
309
+ return true;
310
+ }
311
+ }
312
+ }
313
+ return false;
314
+ }
315
+ }
316
+ function simpleMatches(packed, request) {
317
+ if ((packed & request.type) === 0)
318
+ return false;
319
+ const flags = packed >>> TYPE_BITS;
320
+ return !((flags & F_THIRD_PARTY && !request.thirdParty) ||
321
+ (flags & F_FIRST_PARTY && request.thirdParty) ||
322
+ (flags & F_STRICT_THIRD_PARTY && !request.strictThirdParty) ||
323
+ (flags & F_STRICT_FIRST_PARTY && request.strictThirdParty));
324
+ }
325
+ /** The rule a packed rule stands for, made when it matches. */
326
+ function unpack(pattern, packed, patternKind = P_HOST) {
327
+ const flags = (packed >>> TYPE_BITS) & FLAG_MASK;
328
+ const separator = patternKind === P_HOST || packed & SEPARATOR_FOLLOWS;
329
+ return {
330
+ kind: "network",
331
+ // Without its options: they aren't kept apart from the bits.
332
+ raw: (flags & F_EXCEPTION ? "@@||" : "||") +
333
+ pattern +
334
+ (separator ? "^" : flags & F_RIGHT_ANCHOR ? "|" : ""),
335
+ patternKind,
336
+ pattern: patternKind === P_PATTERN && separator ? `${pattern}^` : pattern,
337
+ flags,
338
+ types: packed & TYPE_MASK,
339
+ from: undefined,
340
+ modifier: undefined,
341
+ extra: undefined,
342
+ };
343
+ }
@@ -0,0 +1,23 @@
1
+ export declare const INLINE_SCRIPT_CSP = "script-src 'unsafe-eval' * blob: data:";
2
+ export declare const INLINE_FONT_CSP = "font-src *";
3
+ /**
4
+ * Applies `removeparam=` values to a URL: a name removes that parameter, a
5
+ * `/regex/` the parameters whose `name=value` it matches, `~` what doesn't
6
+ * match, and an empty value all of them. Returns the URL as given when
7
+ * nothing was removed.
8
+ */
9
+ export declare function removeParams(url: string, values: readonly string[]): string;
10
+ /** The names of a URL's query parameters, as written. */
11
+ export declare function paramNames(url: string): string[];
12
+ /** Whether `urlskip=` steps also apply to a request another filter blocks. */
13
+ export declare function skipsBlocked(value: string): boolean;
14
+ /**
15
+ * Applies `urlskip=` steps: extracts the URL a tracking link wraps. Undefined
16
+ * when a step fails or the result isn't an http(s) URL.
17
+ */
18
+ export declare function urlSkip(url: string, value: string): string | undefined;
19
+ /**
20
+ * Applies `uritransform=/regex/replacement/flags` to the path, query and hash
21
+ * of a URL. Undefined when the value can't be read or nothing changes.
22
+ */
23
+ export declare function uriTransform(url: string, value: string): string | undefined;
@@ -0,0 +1,192 @@
1
+ import { tryRegex } from "../util.js";
2
+ // What a document's response gets for `inline-script` and `inline-font`: a
3
+ // policy that leaves out `'unsafe-inline'` and `data:`.
4
+ export const INLINE_SCRIPT_CSP = "script-src 'unsafe-eval' * blob: data:";
5
+ export const INLINE_FONT_CSP = "font-src *";
6
+ const REGEX_VALUE = /^\/(.+)\/([a-z]*)$/;
7
+ /** Whether one `removeparam=` value removes a `name=value` parameter. */
8
+ function removesParam(value, param) {
9
+ if (value === "")
10
+ return true;
11
+ const negated = value.startsWith("~");
12
+ if (negated)
13
+ value = value.slice(1);
14
+ const regex = REGEX_VALUE.exec(value);
15
+ let matches;
16
+ if (regex === null) {
17
+ const equals = param.indexOf("=");
18
+ matches = (equals === -1 ? param : param.slice(0, equals)) === value;
19
+ }
20
+ else {
21
+ matches = tryRegex(regex[1], regex[2])?.test(param) === true;
22
+ }
23
+ return matches !== negated;
24
+ }
25
+ /**
26
+ * Applies `removeparam=` values to a URL: a name removes that parameter, a
27
+ * `/regex/` the parameters whose `name=value` it matches, `~` what doesn't
28
+ * match, and an empty value all of them. Returns the URL as given when
29
+ * nothing was removed.
30
+ */
31
+ export function removeParams(url, values) {
32
+ const hash = url.indexOf("#");
33
+ const base = hash === -1 ? url : url.slice(0, hash);
34
+ const query = base.indexOf("?");
35
+ if (query === -1 || values.length === 0)
36
+ return url;
37
+ const params = base.slice(query + 1).split("&");
38
+ const kept = params.filter((param) => !values.some((value) => removesParam(value, param)));
39
+ if (kept.length === params.length)
40
+ return url;
41
+ return (base.slice(0, query) +
42
+ (kept.length === 0 ? "" : `?${kept.join("&")}`) +
43
+ (hash === -1 ? "" : url.slice(hash)));
44
+ }
45
+ /** The names of a URL's query parameters, as written. */
46
+ export function paramNames(url) {
47
+ const hash = url.indexOf("#");
48
+ const query = url.indexOf("?");
49
+ if (query === -1 || (hash !== -1 && hash < query))
50
+ return [];
51
+ return url
52
+ .slice(query + 1, hash === -1 ? undefined : hash)
53
+ .split("&")
54
+ .map((param) => {
55
+ const equals = param.indexOf("=");
56
+ return equals === -1 ? param : param.slice(0, equals);
57
+ });
58
+ }
59
+ function queryOf(text) {
60
+ const query = text.indexOf("?");
61
+ if (query === -1)
62
+ return undefined;
63
+ const hash = text.indexOf("#", query);
64
+ return new URLSearchParams(text.slice(query + 1, hash === -1 ? undefined : hash));
65
+ }
66
+ function decodeBase64(text) {
67
+ try {
68
+ return Buffer.from(atob(text), "latin1").toString("utf8");
69
+ }
70
+ catch {
71
+ return undefined;
72
+ }
73
+ }
74
+ /** Splits `urlskip=` steps at spaces, keeping a `/regex/` step whole. */
75
+ function skipSteps(value) {
76
+ const steps = [];
77
+ for (let i = 0; i < value.length;) {
78
+ if (value[i] === " ") {
79
+ i++;
80
+ continue;
81
+ }
82
+ let end = value.indexOf(" ", i);
83
+ if (value[i] === "/") {
84
+ // To the slash that a space or the end follows.
85
+ for (let k = i + 1; k < value.length; k++) {
86
+ if (value[k] === "\\")
87
+ k++;
88
+ else if (value[k] === "/" &&
89
+ (k + 1 === value.length || value[k + 1] === " ")) {
90
+ end = k + 1;
91
+ break;
92
+ }
93
+ }
94
+ }
95
+ if (end === -1)
96
+ end = value.length;
97
+ steps.push(value.slice(i, end));
98
+ i = end;
99
+ }
100
+ return steps;
101
+ }
102
+ /** Whether `urlskip=` steps also apply to a request another filter blocks. */
103
+ export function skipsBlocked(value) {
104
+ return skipSteps(value).includes("-blocked");
105
+ }
106
+ /**
107
+ * Applies `urlskip=` steps: extracts the URL a tracking link wraps. Undefined
108
+ * when a step fails or the result isn't an http(s) URL.
109
+ */
110
+ export function urlSkip(url, value) {
111
+ let current = url;
112
+ for (const step of skipSteps(value)) {
113
+ if (step === "-blocked")
114
+ continue;
115
+ if (step.startsWith("?")) {
116
+ current = queryOf(current)?.get(step.slice(1)) ?? undefined;
117
+ }
118
+ else if (step.startsWith("&")) {
119
+ const keys = [...(queryOf(current)?.keys() ?? [])];
120
+ current = keys[Number(step.slice(1)) - 1];
121
+ }
122
+ else if (step === "#") {
123
+ const hash = current.indexOf("#");
124
+ current = hash === -1 ? undefined : current.slice(hash + 1);
125
+ }
126
+ else if (step.length > 2 && step.startsWith("/") && step.endsWith("/")) {
127
+ current = tryRegex(step.slice(1, -1))?.exec(current)?.[1];
128
+ }
129
+ else if (step === "+https") {
130
+ current = /^https?:\/\//i.test(current)
131
+ ? current.replace(/^http:/i, "https:")
132
+ : `https://${current}`;
133
+ }
134
+ else if (step === "-base64") {
135
+ current = decodeBase64(current);
136
+ }
137
+ else if (step === "-safebase64") {
138
+ current = decodeBase64(current.replaceAll("-", "+").replaceAll("_", "/"));
139
+ }
140
+ else if (step === "-uricomponent") {
141
+ try {
142
+ current = decodeURIComponent(current);
143
+ }
144
+ catch {
145
+ current = undefined;
146
+ }
147
+ }
148
+ else {
149
+ return undefined;
150
+ }
151
+ if (current === undefined || current === "")
152
+ return undefined;
153
+ }
154
+ return current !== url &&
155
+ /^https?:\/\//i.test(current) &&
156
+ URL.canParse(current)
157
+ ? current
158
+ : undefined;
159
+ }
160
+ /**
161
+ * Applies `uritransform=/regex/replacement/flags` to the path, query and hash
162
+ * of a URL. Undefined when the value can't be read or nothing changes.
163
+ */
164
+ export function uriTransform(url, value) {
165
+ if (!value.startsWith("/") || !URL.canParse(url))
166
+ return undefined;
167
+ let end = -1;
168
+ let inClass = false;
169
+ for (let i = 1; i < value.length && end === -1; i++) {
170
+ const c = value[i];
171
+ if (c === "\\")
172
+ i++;
173
+ else if (c === "[")
174
+ inClass = true;
175
+ else if (c === "]")
176
+ inClass = false;
177
+ else if (c === "/" && !inClass)
178
+ end = i;
179
+ }
180
+ const last = value.lastIndexOf("/");
181
+ if (end === -1 || last <= end)
182
+ return undefined;
183
+ const regex = tryRegex(value.slice(1, end), value.slice(last + 1));
184
+ if (regex === undefined)
185
+ return undefined;
186
+ const { origin, pathname, search, hash } = new URL(url);
187
+ const before = pathname + search + hash;
188
+ const after = before.replace(regex, value.slice(end + 1, last));
189
+ return after === before || !after.startsWith("/")
190
+ ? undefined
191
+ : origin + after;
192
+ }
@@ -0,0 +1,7 @@
1
+ import { type NetworkRule } from "../parser/types.js";
2
+ import type { Request } from "./request.js";
3
+ /**
4
+ * Whether a rule applies to a request: its type, party, pattern and hostname
5
+ * options. `header=` isn't checked here: it needs the response.
6
+ */
7
+ export declare function ruleMatches(rule: NetworkRule, request: Request): boolean;