@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,145 @@
|
|
|
1
|
+
import { anyHostMatches, hostListMatches } from "../parser/hosts.js";
|
|
2
|
+
import { F_FIRST_PARTY, F_HOST_ANCHOR, F_LEFT_ANCHOR, F_MATCH_CASE, F_RIGHT_ANCHOR, F_STRICT_FIRST_PARTY, F_STRICT_THIRD_PARTY, F_THIRD_PARTY, P_ALL, P_HOST, P_PATTERN, } from "../parser/types.js";
|
|
3
|
+
import { tryRegex } from "../util.js";
|
|
4
|
+
// Compiled the first time a rule is tested: most rules never are.
|
|
5
|
+
const regexes = new WeakMap();
|
|
6
|
+
const SPECIAL = /[.*+?^${}()|[\]\\]/g;
|
|
7
|
+
/**
|
|
8
|
+
* An Adblock Plus pattern as a regular expression: `*` is anything, `^` a
|
|
9
|
+
* separator (anything but a letter, a digit or one of `_-.%`, or the end of
|
|
10
|
+
* the URL), `||` the start of a hostname label.
|
|
11
|
+
*/
|
|
12
|
+
function patternRegex(rule) {
|
|
13
|
+
const body = rule.pattern
|
|
14
|
+
.replace(SPECIAL, "\\$&")
|
|
15
|
+
.replaceAll("\\*", ".*")
|
|
16
|
+
.replaceAll("\\^", "(?:[^\\w\\-.%]|$)");
|
|
17
|
+
const start = rule.flags & F_HOST_ANCHOR
|
|
18
|
+
? "^[a-z][a-z0-9+.-]*:/+(?:[^/?#]*@)?(?:[^/?#]*\\.)?"
|
|
19
|
+
: rule.flags & F_LEFT_ANCHOR
|
|
20
|
+
? "^"
|
|
21
|
+
: "";
|
|
22
|
+
return (tryRegex(start + body + (rule.flags & F_RIGHT_ANCHOR ? "$" : "")) ?? null);
|
|
23
|
+
}
|
|
24
|
+
function regexFor(rule) {
|
|
25
|
+
let regex = regexes.get(rule);
|
|
26
|
+
if (regex === undefined) {
|
|
27
|
+
regex =
|
|
28
|
+
rule.patternKind === P_PATTERN
|
|
29
|
+
? patternRegex(rule)
|
|
30
|
+
: (tryRegex(rule.pattern, rule.flags & F_MATCH_CASE ? "" : "i") ??
|
|
31
|
+
null);
|
|
32
|
+
regexes.set(rule, regex);
|
|
33
|
+
}
|
|
34
|
+
return regex;
|
|
35
|
+
}
|
|
36
|
+
/** Whether a hostname is `suffix` or a subdomain of it. */
|
|
37
|
+
function hostnameEndsWith(hostname, suffix) {
|
|
38
|
+
return (hostname === suffix ||
|
|
39
|
+
(hostname.endsWith(suffix) &&
|
|
40
|
+
hostname.charCodeAt(hostname.length - suffix.length - 1) === 0x2e) /* . */);
|
|
41
|
+
}
|
|
42
|
+
function patternMatches(rule, request) {
|
|
43
|
+
const { pattern, flags } = rule;
|
|
44
|
+
const { lower } = request;
|
|
45
|
+
if (pattern.includes("*") || pattern.includes("^")) {
|
|
46
|
+
return regexFor(rule)?.test(lower) === true;
|
|
47
|
+
}
|
|
48
|
+
// A literal: no regular expression needed.
|
|
49
|
+
if (flags & F_HOST_ANCHOR) {
|
|
50
|
+
for (let i = lower.indexOf(pattern, request.hostStart); i !== -1 && i < request.hostEnd; i = lower.indexOf(pattern, i + 1)) {
|
|
51
|
+
if ((i === request.hostStart || lower.charCodeAt(i - 1) === 0x2e) /* . */ &&
|
|
52
|
+
(!(flags & F_RIGHT_ANCHOR) || i + pattern.length === lower.length)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
if (flags & F_LEFT_ANCHOR) {
|
|
59
|
+
return flags & F_RIGHT_ANCHOR
|
|
60
|
+
? lower === pattern
|
|
61
|
+
: lower.startsWith(pattern);
|
|
62
|
+
}
|
|
63
|
+
return flags & F_RIGHT_ANCHOR
|
|
64
|
+
? lower.endsWith(pattern)
|
|
65
|
+
: lower.includes(pattern);
|
|
66
|
+
}
|
|
67
|
+
const IPV4_LAN = /^(?:10\.|192\.168\.|169\.254\.|172\.(?:1[6-9]|2\d|3[01])\.)/;
|
|
68
|
+
const IPV6_LAN = /^\[(?:f[cd][0-9a-f]{2}|fe[89ab][0-9a-f]):/;
|
|
69
|
+
/**
|
|
70
|
+
* `ipaddress=`: Chromium doesn't tell which address a request resolved to,
|
|
71
|
+
* so only a URL whose host is an address can match.
|
|
72
|
+
*/
|
|
73
|
+
function ipAddressMatches(value, hostname) {
|
|
74
|
+
const v6 = hostname.startsWith("[");
|
|
75
|
+
if (!v6 && !/^(?:\d{1,3}\.){3}\d{1,3}$/.test(hostname))
|
|
76
|
+
return false;
|
|
77
|
+
const address = v6 ? hostname.slice(1, -1) : hostname;
|
|
78
|
+
if (value === "lan")
|
|
79
|
+
return IPV4_LAN.test(hostname) || IPV6_LAN.test(hostname);
|
|
80
|
+
if (value === "loopback")
|
|
81
|
+
return address.startsWith("127.") || address === "::1";
|
|
82
|
+
if (value.length > 2 && value.startsWith("/") && value.endsWith("/")) {
|
|
83
|
+
return tryRegex(value.slice(1, -1))?.test(address) === true;
|
|
84
|
+
}
|
|
85
|
+
return value.endsWith("*")
|
|
86
|
+
? address.startsWith(value.slice(0, -1))
|
|
87
|
+
: address === value;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Whether a rule applies to a request: its type, party, pattern and hostname
|
|
91
|
+
* options. `header=` isn't checked here: it needs the response.
|
|
92
|
+
*/
|
|
93
|
+
export function ruleMatches(rule, request) {
|
|
94
|
+
if ((rule.types & request.type) === 0)
|
|
95
|
+
return false;
|
|
96
|
+
const { flags } = rule;
|
|
97
|
+
if (flags & F_THIRD_PARTY && !request.thirdParty)
|
|
98
|
+
return false;
|
|
99
|
+
if (flags & F_FIRST_PARTY && request.thirdParty)
|
|
100
|
+
return false;
|
|
101
|
+
if (flags & F_STRICT_THIRD_PARTY && !request.strictThirdParty)
|
|
102
|
+
return false;
|
|
103
|
+
if (flags & F_STRICT_FIRST_PARTY && request.strictThirdParty)
|
|
104
|
+
return false;
|
|
105
|
+
if (rule.patternKind === P_HOST) {
|
|
106
|
+
if (!hostnameEndsWith(request.hostname, rule.pattern))
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
else if (rule.patternKind === P_PATTERN) {
|
|
110
|
+
if (!patternMatches(rule, request))
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
else if (rule.patternKind !== P_ALL) {
|
|
114
|
+
const regex = regexFor(rule);
|
|
115
|
+
if (regex === null || !regex.test(request.url))
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (rule.from !== undefined &&
|
|
119
|
+
!hostListMatches(rule.from, request.sourceHostname, request.sourceEntity)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const { extra } = rule;
|
|
123
|
+
if (extra === undefined)
|
|
124
|
+
return true;
|
|
125
|
+
if (extra.methods !== undefined && (extra.methods & request.method) === 0) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (extra.notMethods !== undefined && extra.notMethods & request.method) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
if (extra.to !== undefined &&
|
|
132
|
+
!hostListMatches(extra.to, request.hostname, request.hostEntity)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
if (extra.top !== undefined &&
|
|
136
|
+
!hostListMatches(extra.top, request.topHostname, request.topEntity)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
if (extra.denyallow !== undefined &&
|
|
140
|
+
anyHostMatches(extra.denyallow, request.hostname, "")) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return (extra.ipaddress === undefined ||
|
|
144
|
+
ipAddressMatches(extra.ipaddress, request.hostname));
|
|
145
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { PublicSuffixList } from "../psl/psl.js";
|
|
2
|
+
/** What a request is for, in the words filters use. */
|
|
3
|
+
export type RequestType = "document" | "subdocument" | "stylesheet" | "script" | "image" | "font" | "object" | "xhr" | "ping" | "media" | "websocket" | "other" | "popup";
|
|
4
|
+
export interface RequestInfo {
|
|
5
|
+
url: string;
|
|
6
|
+
type: RequestType;
|
|
7
|
+
/**
|
|
8
|
+
* The URL of the document that makes the request. Without it the request
|
|
9
|
+
* is taken as first-party, as a page's own navigation is.
|
|
10
|
+
*/
|
|
11
|
+
sourceUrl?: string;
|
|
12
|
+
/** The URL of the top document (`top=`). Defaults to `sourceUrl`. */
|
|
13
|
+
topUrl?: string;
|
|
14
|
+
/** The HTTP method (`method=`). */
|
|
15
|
+
method?: string;
|
|
16
|
+
}
|
|
17
|
+
/** The hostname of a URL, lowercase and without a trailing dot, or empty. */
|
|
18
|
+
export declare function hostnameOf(url: string): string;
|
|
19
|
+
/** A request, with what filters ask about it worked out once. */
|
|
20
|
+
export declare class Request {
|
|
21
|
+
readonly url: string;
|
|
22
|
+
/** The URL lowercased: patterns are case-insensitive. */
|
|
23
|
+
readonly lower: string;
|
|
24
|
+
readonly hostname: string;
|
|
25
|
+
/** Where the hostname starts and ends in `lower`. */
|
|
26
|
+
readonly hostStart: number;
|
|
27
|
+
readonly hostEnd: number;
|
|
28
|
+
/** The request type, as a `T_*` bit. */
|
|
29
|
+
type: number;
|
|
30
|
+
readonly sourceHostname: string;
|
|
31
|
+
readonly topHostname: string;
|
|
32
|
+
/** The request's site isn't the source document's. */
|
|
33
|
+
readonly thirdParty: boolean;
|
|
34
|
+
/** The request's hostname isn't the source document's. */
|
|
35
|
+
readonly strictThirdParty: boolean;
|
|
36
|
+
/** The method as a `METHODS` bit, or 0. */
|
|
37
|
+
readonly method: number;
|
|
38
|
+
private readonly psl;
|
|
39
|
+
private entities;
|
|
40
|
+
private urlTokens;
|
|
41
|
+
constructor(info: RequestInfo, psl: PublicSuffixList);
|
|
42
|
+
private entity;
|
|
43
|
+
/** The tokens of the URL, which rules are filed under. */
|
|
44
|
+
get tokens(): string[];
|
|
45
|
+
/** The hostname with its public suffix as `*`, for `to=example.*`. */
|
|
46
|
+
get hostEntity(): string;
|
|
47
|
+
/** The same for the source document, for `domain=example.*`. */
|
|
48
|
+
get sourceEntity(): string;
|
|
49
|
+
get topEntity(): string;
|
|
50
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { METHODS, T_DOCUMENT, T_FONT, T_IMAGE, T_MEDIA, T_OBJECT, T_OTHER, T_PING, T_POPUP, T_SCRIPT, T_STYLESHEET, T_SUBDOCUMENT, T_WEBSOCKET, T_XHR, } from "../parser/types.js";
|
|
2
|
+
import { urlTokens } from "./tokens.js";
|
|
3
|
+
const REQUEST_TYPES = {
|
|
4
|
+
document: T_DOCUMENT,
|
|
5
|
+
subdocument: T_SUBDOCUMENT,
|
|
6
|
+
stylesheet: T_STYLESHEET,
|
|
7
|
+
script: T_SCRIPT,
|
|
8
|
+
image: T_IMAGE,
|
|
9
|
+
font: T_FONT,
|
|
10
|
+
object: T_OBJECT,
|
|
11
|
+
xhr: T_XHR,
|
|
12
|
+
ping: T_PING,
|
|
13
|
+
media: T_MEDIA,
|
|
14
|
+
websocket: T_WEBSOCKET,
|
|
15
|
+
other: T_OTHER,
|
|
16
|
+
popup: T_POPUP,
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Where the hostname is in a lowercased URL, or undefined when the URL has
|
|
20
|
+
* none (`data:`, `blob:`, `about:`).
|
|
21
|
+
*/
|
|
22
|
+
function hostSpan(lower) {
|
|
23
|
+
const scheme = lower.indexOf("://");
|
|
24
|
+
if (scheme <= 0 || scheme > 16)
|
|
25
|
+
return undefined;
|
|
26
|
+
let start = scheme + 3;
|
|
27
|
+
let end = lower.length;
|
|
28
|
+
for (let i = start; i < lower.length; i++) {
|
|
29
|
+
const c = lower.charCodeAt(i);
|
|
30
|
+
if (c === 0x2f /* / */ || c === 0x3f /* ? */ || c === 0x23 /* # */) {
|
|
31
|
+
end = i;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const at = lower.lastIndexOf("@", end - 1);
|
|
36
|
+
if (at >= start)
|
|
37
|
+
start = at + 1;
|
|
38
|
+
if (lower.charCodeAt(start) === 0x5b /* [ */) {
|
|
39
|
+
const close = lower.indexOf("]", start);
|
|
40
|
+
if (close !== -1 && close < end)
|
|
41
|
+
end = close + 1;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const colon = lower.indexOf(":", start);
|
|
45
|
+
if (colon !== -1 && colon < end)
|
|
46
|
+
end = colon;
|
|
47
|
+
}
|
|
48
|
+
return [start, end];
|
|
49
|
+
}
|
|
50
|
+
// A page's requests come one after the other with the same source URL.
|
|
51
|
+
let lastUrl = "";
|
|
52
|
+
let lastHostname = "";
|
|
53
|
+
/** The hostname of a URL, lowercase and without a trailing dot, or empty. */
|
|
54
|
+
export function hostnameOf(url) {
|
|
55
|
+
if (url === lastUrl)
|
|
56
|
+
return lastHostname;
|
|
57
|
+
lastUrl = url;
|
|
58
|
+
return (lastHostname = parseHostname(url));
|
|
59
|
+
}
|
|
60
|
+
function parseHostname(url) {
|
|
61
|
+
const lower = url.toLowerCase();
|
|
62
|
+
const span = hostSpan(lower);
|
|
63
|
+
if (span === undefined)
|
|
64
|
+
return "";
|
|
65
|
+
const hostname = lower.slice(span[0], span[1]);
|
|
66
|
+
return hostname.endsWith(".") ? hostname.slice(0, -1) : hostname;
|
|
67
|
+
}
|
|
68
|
+
/** A request, with what filters ask about it worked out once. */
|
|
69
|
+
export class Request {
|
|
70
|
+
url;
|
|
71
|
+
/** The URL lowercased: patterns are case-insensitive. */
|
|
72
|
+
lower;
|
|
73
|
+
hostname;
|
|
74
|
+
/** Where the hostname starts and ends in `lower`. */
|
|
75
|
+
hostStart;
|
|
76
|
+
hostEnd;
|
|
77
|
+
/** The request type, as a `T_*` bit. */
|
|
78
|
+
type;
|
|
79
|
+
sourceHostname;
|
|
80
|
+
topHostname;
|
|
81
|
+
/** The request's site isn't the source document's. */
|
|
82
|
+
thirdParty;
|
|
83
|
+
/** The request's hostname isn't the source document's. */
|
|
84
|
+
strictThirdParty;
|
|
85
|
+
/** The method as a `METHODS` bit, or 0. */
|
|
86
|
+
method;
|
|
87
|
+
psl;
|
|
88
|
+
entities;
|
|
89
|
+
urlTokens;
|
|
90
|
+
constructor(info, psl) {
|
|
91
|
+
this.psl = psl;
|
|
92
|
+
this.url = info.url;
|
|
93
|
+
this.lower = info.url.toLowerCase();
|
|
94
|
+
const span = hostSpan(this.lower);
|
|
95
|
+
this.hostStart = span?.[0] ?? 0;
|
|
96
|
+
this.hostEnd = span?.[1] ?? 0;
|
|
97
|
+
const hostname = this.lower.slice(this.hostStart, this.hostEnd);
|
|
98
|
+
this.hostname = hostname.endsWith(".") ? hostname.slice(0, -1) : hostname;
|
|
99
|
+
this.type = REQUEST_TYPES[info.type];
|
|
100
|
+
const source = info.sourceUrl === undefined || info.type === "document"
|
|
101
|
+
? ""
|
|
102
|
+
: hostnameOf(info.sourceUrl);
|
|
103
|
+
this.sourceHostname = source === "" ? this.hostname : source;
|
|
104
|
+
const top = info.topUrl === undefined ? "" : hostnameOf(info.topUrl);
|
|
105
|
+
this.topHostname = top === "" ? this.sourceHostname : top;
|
|
106
|
+
this.strictThirdParty = this.hostname !== this.sourceHostname;
|
|
107
|
+
this.thirdParty =
|
|
108
|
+
this.strictThirdParty &&
|
|
109
|
+
psl.domain(this.hostname) !== psl.domain(this.sourceHostname);
|
|
110
|
+
this.method =
|
|
111
|
+
info.method === undefined ? 0 : (METHODS[info.method.toLowerCase()] ?? 0);
|
|
112
|
+
}
|
|
113
|
+
entity(index) {
|
|
114
|
+
this.entities ??= [
|
|
115
|
+
this.psl.entity(this.hostname),
|
|
116
|
+
this.psl.entity(this.sourceHostname),
|
|
117
|
+
this.psl.entity(this.topHostname),
|
|
118
|
+
];
|
|
119
|
+
return this.entities[index];
|
|
120
|
+
}
|
|
121
|
+
/** The tokens of the URL, which rules are filed under. */
|
|
122
|
+
get tokens() {
|
|
123
|
+
return (this.urlTokens ??= urlTokens(this.lower));
|
|
124
|
+
}
|
|
125
|
+
/** The hostname with its public suffix as `*`, for `to=example.*`. */
|
|
126
|
+
get hostEntity() {
|
|
127
|
+
return this.entity(0);
|
|
128
|
+
}
|
|
129
|
+
/** The same for the source document, for `domain=example.*`. */
|
|
130
|
+
get sourceEntity() {
|
|
131
|
+
return this.entity(1);
|
|
132
|
+
}
|
|
133
|
+
get topEntity() {
|
|
134
|
+
return this.entity(2);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type NetworkRule } from "../parser/types.js";
|
|
2
|
+
/** The tokens of a lowercased URL. */
|
|
3
|
+
export declare function urlTokens(lower: string): string[];
|
|
4
|
+
/** The tokens a rule can be filed under. None when nothing is certain. */
|
|
5
|
+
export declare function ruleTokens(rule: NetworkRule): string[];
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { F_HOST_ANCHOR, F_LEFT_ANCHOR, F_RIGHT_ANCHOR, P_PATTERN, P_REGEX, } from "../parser/types.js";
|
|
2
|
+
// A token is a run of letters, digits and `%`. A rule is filed under a token
|
|
3
|
+
// its pattern can't match without, so a request only looks at the rules filed
|
|
4
|
+
// under the tokens of its URL.
|
|
5
|
+
function isTokenChar(c) {
|
|
6
|
+
return ((c >= 0x61 && c <= 0x7a) || // a-z
|
|
7
|
+
(c >= 0x30 && c <= 0x39) || // 0-9
|
|
8
|
+
c === 0x25 // %
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
// Past this, a URL is a blob of data, not something filters describe.
|
|
12
|
+
const MAX_TOKENIZED = 2048;
|
|
13
|
+
/** The tokens of a lowercased URL. */
|
|
14
|
+
export function urlTokens(lower) {
|
|
15
|
+
const tokens = [];
|
|
16
|
+
const end = Math.min(lower.length, MAX_TOKENIZED);
|
|
17
|
+
let start = -1;
|
|
18
|
+
for (let i = 0; i < end; i++) {
|
|
19
|
+
if (isTokenChar(lower.charCodeAt(i))) {
|
|
20
|
+
if (start === -1)
|
|
21
|
+
start = i;
|
|
22
|
+
}
|
|
23
|
+
else if (start !== -1) {
|
|
24
|
+
tokens.push(lower.slice(start, i));
|
|
25
|
+
start = -1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// A token cut by the limit isn't whole: no rule may rely on it.
|
|
29
|
+
if (start !== -1 && end === lower.length)
|
|
30
|
+
tokens.push(lower.slice(start));
|
|
31
|
+
return tokens;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The tokens every URL a pattern matches holds whole: runs bounded on both
|
|
35
|
+
* sides by a character that isn't part of a token, by `^`, or by an anchor.
|
|
36
|
+
* A run next to `*` or at an unanchored end could be part of a longer token.
|
|
37
|
+
*/
|
|
38
|
+
function patternTokens(pattern, flags) {
|
|
39
|
+
const tokens = [];
|
|
40
|
+
let start = -1;
|
|
41
|
+
for (let i = 0; i <= pattern.length; i++) {
|
|
42
|
+
const c = i < pattern.length ? pattern.charCodeAt(i) : -1;
|
|
43
|
+
if (c !== -1 && isTokenChar(c)) {
|
|
44
|
+
if (start === -1)
|
|
45
|
+
start = i;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (start === -1)
|
|
49
|
+
continue;
|
|
50
|
+
const before = start === 0 ? -1 : pattern.charCodeAt(start - 1);
|
|
51
|
+
const bounded = (edge, anchors) => edge === -1 ? (flags & anchors) !== 0 : edge !== 0x2a; /* * */
|
|
52
|
+
if (bounded(before, F_HOST_ANCHOR | F_LEFT_ANCHOR) &&
|
|
53
|
+
bounded(c, F_RIGHT_ANCHOR)) {
|
|
54
|
+
tokens.push(pattern.slice(start, i));
|
|
55
|
+
}
|
|
56
|
+
start = -1;
|
|
57
|
+
}
|
|
58
|
+
return tokens;
|
|
59
|
+
}
|
|
60
|
+
// In a regular expression, a character that stands for itself and isn't part
|
|
61
|
+
// of a token.
|
|
62
|
+
const LITERAL_BOUNDARY = /[\s!"#&',/:;<=>@_`~-]/;
|
|
63
|
+
/**
|
|
64
|
+
* The tokens every URL a regular expression matches holds whole. Only what
|
|
65
|
+
* is certain counts: literal runs outside groups, between literal characters
|
|
66
|
+
* that aren't part of a token. An alternation at the top level gives none.
|
|
67
|
+
*/
|
|
68
|
+
function regexTokens(source) {
|
|
69
|
+
const tokens = [];
|
|
70
|
+
// Each element: a literal token character, a literal boundary, or anything
|
|
71
|
+
// else (a class, a group, a wildcard: could be part of a token).
|
|
72
|
+
const kinds = [];
|
|
73
|
+
const chars = [];
|
|
74
|
+
let depth = 0;
|
|
75
|
+
for (let i = 0; i < source.length; i++) {
|
|
76
|
+
const c = source[i];
|
|
77
|
+
let kind = "other";
|
|
78
|
+
let char = "";
|
|
79
|
+
if (c === "\\") {
|
|
80
|
+
const next = source[++i] ?? "";
|
|
81
|
+
// An escaped punctuation mark is that mark; `\d`, `\w`, `\x41` aren't.
|
|
82
|
+
if (depth === 0 && /[^a-z0-9]/i.test(next)) {
|
|
83
|
+
kind = next === "%" ? "token" : "boundary";
|
|
84
|
+
char = next;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else if (c === "[") {
|
|
88
|
+
for (i++; i < source.length && source[i] !== "]"; i++) {
|
|
89
|
+
if (source[i] === "\\")
|
|
90
|
+
i++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (c === "(") {
|
|
94
|
+
depth++;
|
|
95
|
+
}
|
|
96
|
+
else if (c === ")") {
|
|
97
|
+
depth--;
|
|
98
|
+
}
|
|
99
|
+
else if (c === "|") {
|
|
100
|
+
if (depth === 0)
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
else if (depth === 0) {
|
|
104
|
+
if (/[a-z0-9%]/i.test(c)) {
|
|
105
|
+
kind = "token";
|
|
106
|
+
char = c.toLowerCase();
|
|
107
|
+
}
|
|
108
|
+
else if (c === "?" || c === "*" || c === "{") {
|
|
109
|
+
// The element before may be absent: it bounds nothing, is no token.
|
|
110
|
+
if (kinds.length > 0)
|
|
111
|
+
kinds[kinds.length - 1] = "other";
|
|
112
|
+
}
|
|
113
|
+
else if (LITERAL_BOUNDARY.test(c)) {
|
|
114
|
+
kind = "boundary";
|
|
115
|
+
char = c;
|
|
116
|
+
}
|
|
117
|
+
else if (c === "^" && i === 0) {
|
|
118
|
+
kind = "boundary";
|
|
119
|
+
}
|
|
120
|
+
else if (c === "$" && i === source.length - 1) {
|
|
121
|
+
kind = "boundary";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
kinds.push(depth === 0 || c === ")" ? kind : "other");
|
|
125
|
+
chars.push(char);
|
|
126
|
+
}
|
|
127
|
+
let start = -1;
|
|
128
|
+
for (let i = 0; i <= kinds.length; i++) {
|
|
129
|
+
if (kinds[i] === "token") {
|
|
130
|
+
if (start === -1)
|
|
131
|
+
start = i;
|
|
132
|
+
}
|
|
133
|
+
else if (start !== -1) {
|
|
134
|
+
if (start > 0 &&
|
|
135
|
+
kinds[start - 1] === "boundary" &&
|
|
136
|
+
kinds[i] === "boundary") {
|
|
137
|
+
tokens.push(chars.slice(start, i).join(""));
|
|
138
|
+
}
|
|
139
|
+
start = -1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return tokens;
|
|
143
|
+
}
|
|
144
|
+
/** The tokens a rule can be filed under. None when nothing is certain. */
|
|
145
|
+
export function ruleTokens(rule) {
|
|
146
|
+
if (rule.patternKind === P_PATTERN) {
|
|
147
|
+
return patternTokens(rule.pattern, rule.flags);
|
|
148
|
+
}
|
|
149
|
+
return rule.patternKind === P_REGEX ? regexTokens(rule.pattern) : [];
|
|
150
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { CosmeticRule, HostList, NotARule, ParseContext, Rule } from "./types.js";
|
|
2
|
+
export interface CosmeticParts {
|
|
3
|
+
hosts: HostList;
|
|
4
|
+
marker: string;
|
|
5
|
+
body: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Splits `hostnames##body`. Undefined when the line isn't an extended filter:
|
|
9
|
+
* no marker, or what precedes it isn't a list of hostnames.
|
|
10
|
+
*/
|
|
11
|
+
export declare function splitCosmetic(line: string): CosmeticParts | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Splits a scriptlet's `name, arg, arg`. A comma inside an argument is
|
|
14
|
+
* escaped (`\,`) or the argument is quoted, with `'`, `"` or a backtick.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseScriptletArgs(text: string): string[];
|
|
17
|
+
/**
|
|
18
|
+
* Parses an extended filter: cosmetic, scriptlet or HTML. A selector list with
|
|
19
|
+
* procedural parts gives several cosmetic rules.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseCosmeticFilter(line: string, parts: CosmeticParts, context: ParseContext): Rule | NotARule | CosmeticRule[];
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { parseHostList } from "./hosts.js";
|
|
2
|
+
import { compileSelector } from "./selector.js";
|
|
3
|
+
// hostnames, then ##, #@#, #?#, #@?#, AdGuard's #$#, #@$#, #$?#, #@$?# (CSS)
|
|
4
|
+
// and #%#, #@%# (JavaScript), then the body.
|
|
5
|
+
const MARKER = /^([^#]*?)(#@?(?:\$\?|\$|\?|%)?#)(.+)$/s;
|
|
6
|
+
// The headers `##^responseheader()` may remove: never one that protects the
|
|
7
|
+
// page, like `content-security-policy`.
|
|
8
|
+
const REMOVABLE_HEADERS = new Set([
|
|
9
|
+
"location",
|
|
10
|
+
"refresh",
|
|
11
|
+
"report-to",
|
|
12
|
+
"set-cookie",
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* Splits `hostnames##body`. Undefined when the line isn't an extended filter:
|
|
16
|
+
* no marker, or what precedes it isn't a list of hostnames.
|
|
17
|
+
*/
|
|
18
|
+
export function splitCosmetic(line) {
|
|
19
|
+
if (!line.includes("#"))
|
|
20
|
+
return undefined;
|
|
21
|
+
const match = MARKER.exec(line);
|
|
22
|
+
if (match === null)
|
|
23
|
+
return undefined;
|
|
24
|
+
const prefix = match[1].trim();
|
|
25
|
+
const hosts = prefix === ""
|
|
26
|
+
? { include: [], exclude: [] }
|
|
27
|
+
: parseHostList(prefix, ",", true);
|
|
28
|
+
if (hosts === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
return { hosts, marker: match[2], body: match[3].trim() };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Splits a scriptlet's `name, arg, arg`. A comma inside an argument is
|
|
34
|
+
* escaped (`\,`) or the argument is quoted, with `'`, `"` or a backtick.
|
|
35
|
+
*/
|
|
36
|
+
export function parseScriptletArgs(text) {
|
|
37
|
+
const args = [];
|
|
38
|
+
let i = 0;
|
|
39
|
+
for (;;) {
|
|
40
|
+
while (text[i] === " ")
|
|
41
|
+
i++;
|
|
42
|
+
const quote = text[i];
|
|
43
|
+
let end = -1;
|
|
44
|
+
let next = -1;
|
|
45
|
+
if (quote === '"' || quote === "'" || quote === "`") {
|
|
46
|
+
for (let k = i + 1; k < text.length && end === -1; k++) {
|
|
47
|
+
if (text[k] === "\\")
|
|
48
|
+
k++;
|
|
49
|
+
else if (text[k] === quote) {
|
|
50
|
+
let after = k + 1;
|
|
51
|
+
while (text[after] === " ")
|
|
52
|
+
after++;
|
|
53
|
+
if (after >= text.length || text[after] === ",") {
|
|
54
|
+
end = k;
|
|
55
|
+
next = after + 1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (end !== -1) {
|
|
61
|
+
args.push(text.slice(i + 1, end).replaceAll(`\\${quote}`, quote));
|
|
62
|
+
if (next > text.length)
|
|
63
|
+
break;
|
|
64
|
+
i = next;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
let arg = "";
|
|
68
|
+
let k = i;
|
|
69
|
+
for (; k < text.length && text[k] !== ","; k++) {
|
|
70
|
+
if (text[k] === "\\" && text[k + 1] === ",")
|
|
71
|
+
k++;
|
|
72
|
+
arg += text[k];
|
|
73
|
+
}
|
|
74
|
+
args.push(arg.trim());
|
|
75
|
+
if (k >= text.length)
|
|
76
|
+
break;
|
|
77
|
+
i = k + 1;
|
|
78
|
+
}
|
|
79
|
+
return args;
|
|
80
|
+
}
|
|
81
|
+
const invalid = (reason) => ({ kind: "invalid", reason });
|
|
82
|
+
const unsupported = (reason) => ({
|
|
83
|
+
kind: "unsupported",
|
|
84
|
+
reason,
|
|
85
|
+
});
|
|
86
|
+
/**
|
|
87
|
+
* Parses an extended filter: cosmetic, scriptlet or HTML. A selector list with
|
|
88
|
+
* procedural parts gives several cosmetic rules.
|
|
89
|
+
*/
|
|
90
|
+
export function parseCosmeticFilter(line, parts, context) {
|
|
91
|
+
const { hosts, marker } = parts;
|
|
92
|
+
let { body } = parts;
|
|
93
|
+
const exception = marker[1] === "@";
|
|
94
|
+
const specific = hosts.include.length > 0;
|
|
95
|
+
if (marker.includes("%")) {
|
|
96
|
+
// AdGuard's way to write a scriptlet: `#%#//scriptlet('name', 'arg')`.
|
|
97
|
+
const scriptlet = /^\/\/scriptlet\((.*)\)$/s.exec(body);
|
|
98
|
+
if (scriptlet === null)
|
|
99
|
+
return unsupported("AdGuard JavaScript rule");
|
|
100
|
+
body = `+js(${scriptlet[1]})`;
|
|
101
|
+
}
|
|
102
|
+
// `selector { declarations }`: AdGuard's CSS rules (`#$#`), which EasyList
|
|
103
|
+
// writes with `##` too.
|
|
104
|
+
const css = /^(.*?)\s*\{\s*(.*?)\s*\}$/s.exec(body);
|
|
105
|
+
if (css !== null) {
|
|
106
|
+
body = /^remove\s*:\s*true\s*;?$/i.test(css[2])
|
|
107
|
+
? `${css[1]}:remove()`
|
|
108
|
+
: `${css[1]}:style(${css[2]})`;
|
|
109
|
+
}
|
|
110
|
+
else if (marker.includes("$")) {
|
|
111
|
+
return unsupported("Adblock Plus snippet");
|
|
112
|
+
}
|
|
113
|
+
if (body.startsWith("+js(") && body.endsWith(")")) {
|
|
114
|
+
const [token = "", ...args] = parseScriptletArgs(body.slice(4, -1));
|
|
115
|
+
const name = token.endsWith(".js") ? token.slice(0, -3) : token;
|
|
116
|
+
if (!exception) {
|
|
117
|
+
if (name === "")
|
|
118
|
+
return invalid("scriptlet without a name");
|
|
119
|
+
if (!specific)
|
|
120
|
+
return invalid("generic scriptlet");
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
kind: "scriptlet",
|
|
124
|
+
raw: line,
|
|
125
|
+
exception,
|
|
126
|
+
hosts,
|
|
127
|
+
name,
|
|
128
|
+
args: name === "" ? [] : args,
|
|
129
|
+
trusted: context.trusted,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (body.startsWith("^")) {
|
|
133
|
+
const header = /^\^responseheader\((.*)\)$/.exec(body);
|
|
134
|
+
if (header === null) {
|
|
135
|
+
return unsupported("HTML filter (Electron can't rewrite response bodies)");
|
|
136
|
+
}
|
|
137
|
+
const name = header[1].trim().toLowerCase();
|
|
138
|
+
if (!REMOVABLE_HEADERS.has(name)) {
|
|
139
|
+
return invalid("responseheader() of a header that can't be removed");
|
|
140
|
+
}
|
|
141
|
+
if (!specific && !exception)
|
|
142
|
+
return invalid("generic responseheader()");
|
|
143
|
+
return {
|
|
144
|
+
kind: "responseheader",
|
|
145
|
+
raw: line,
|
|
146
|
+
exception,
|
|
147
|
+
hosts,
|
|
148
|
+
header: name,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const compiled = compileSelector(body, marker.includes("?"));
|
|
152
|
+
if (typeof compiled === "string")
|
|
153
|
+
return invalid(`selector: ${compiled}`);
|
|
154
|
+
const plain = compiled.every((one) => one.procedural === undefined &&
|
|
155
|
+
one.style === undefined &&
|
|
156
|
+
one.media === undefined);
|
|
157
|
+
if (!plain && !specific && !exception) {
|
|
158
|
+
return invalid("generic procedural or :style() filter");
|
|
159
|
+
}
|
|
160
|
+
return compiled.map((one) => ({
|
|
161
|
+
kind: "cosmetic",
|
|
162
|
+
raw: line,
|
|
163
|
+
exception,
|
|
164
|
+
hosts,
|
|
165
|
+
key: body,
|
|
166
|
+
selector: one.selector,
|
|
167
|
+
style: one.style,
|
|
168
|
+
media: one.media,
|
|
169
|
+
procedural: one.procedural,
|
|
170
|
+
}));
|
|
171
|
+
}
|