@adguard/dnr-converter 1.0.0-alpha.2
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/README.md +476 -0
- package/dist/cli.cjs +58912 -0
- package/dist/constants.js +23 -0
- package/dist/declarative-rule/declarative-rule.js +15 -0
- package/dist/declarative-rule/header-info.js +12 -0
- package/dist/declarative-rule/modify-header-info.js +31 -0
- package/dist/declarative-rule/query-key-value.js +12 -0
- package/dist/declarative-rule/query-transform.js +12 -0
- package/dist/declarative-rule/redirect.js +14 -0
- package/dist/declarative-rule/rule-action.js +44 -0
- package/dist/declarative-rule/rule-condition.js +77 -0
- package/dist/declarative-rule/url-transform.js +30 -0
- package/dist/errors/conversion-errors/empty-domains-error.js +24 -0
- package/dist/errors/conversion-errors/empty-resources-error.js +24 -0
- package/dist/errors/conversion-errors/index.js +17 -0
- package/dist/errors/conversion-errors/invalid-declarative-rule-error.js +22 -0
- package/dist/errors/conversion-errors/unsupported-modifier-error.js +20 -0
- package/dist/errors/conversion-errors/unsupported-regexp-error.js +27 -0
- package/dist/errors/converter-options-errors/empty-or-negative-number-of-rules-error.js +18 -0
- package/dist/errors/converter-options-errors/index.js +15 -0
- package/dist/errors/converter-options-errors/negative-number-of-rules-error.js +18 -0
- package/dist/errors/converter-options-errors/resources-path-error.js +18 -0
- package/dist/errors/limitation-errors/index.js +17 -0
- package/dist/errors/limitation-errors/max-scanned-rules-error.js +20 -0
- package/dist/errors/limitation-errors/too-many-error.js +28 -0
- package/dist/errors/limitation-errors/too-many-regexp-rules-error.js +25 -0
- package/dist/errors/limitation-errors/too-many-rules-error.js +25 -0
- package/dist/errors/limitation-errors/too-many-unsafe-rules-error.js +25 -0
- package/dist/errors/unavailable-sources-errors/unavailable-filter-source-error.js +21 -0
- package/dist/errors/unavailable-sources-errors/unavailable-ruleset-source-error.js +21 -0
- package/dist/filter/filter.js +92 -0
- package/dist/filter-converter/filter-converter.js +385 -0
- package/dist/grouped-rules-converters/url-transform-converter.js +340 -0
- package/dist/index.js +30 -0
- package/dist/package.json.js +3 -0
- package/dist/re2-regexp/re2-validator.js +37 -0
- package/dist/re2-regexp/regex-validator-extension.js +30 -0
- package/dist/re2.wasm +0 -0
- package/dist/rule/option-names.js +65 -0
- package/dist/rule/rule-badfilter.js +81 -0
- package/dist/rule/rule-priority.js +88 -0
- package/dist/rule/rule-validator.js +455 -0
- package/dist/rule/rule.js +853 -0
- package/dist/rule/string-utils.js +49 -0
- package/dist/rule-converters/csp-converter.js +105 -0
- package/dist/rule-converters/regular-rule-converter.js +789 -0
- package/dist/rule-converters/remove-header-converter.js +97 -0
- package/dist/rule-converters/rules-converter.js +365 -0
- package/dist/rule-converters/rules-grouper.js +75 -0
- package/dist/rules-scanner/rules-scanner.js +136 -0
- package/dist/ruleset/metadata-rule.js +35 -0
- package/dist/ruleset/metadata-ruleset.js +162 -0
- package/dist/ruleset/rules-hash-map.js +71 -0
- package/dist/ruleset/ruleset-with-source-map.js +406 -0
- package/dist/ruleset/ruleset.js +54 -0
- package/dist/ruleset/source-map.js +283 -0
- package/dist/types/cli/convert-filters.d.ts +32 -0
- package/dist/types/cli/index.d.ts +7 -0
- package/dist/types/cli/utils.d.ts +19 -0
- package/dist/types/src/declarative-rule/declarative-rule.d.ts +32 -0
- package/dist/types/src/declarative-rule/header-info.d.ts +31 -0
- package/dist/types/src/declarative-rule/modify-header-info.d.ts +46 -0
- package/dist/types/src/declarative-rule/query-key-value.d.ts +24 -0
- package/dist/types/src/declarative-rule/query-transform.d.ts +21 -0
- package/dist/types/src/declarative-rule/redirect.d.ts +32 -0
- package/dist/types/src/declarative-rule/rule-action.d.ts +68 -0
- package/dist/types/src/declarative-rule/rule-condition.d.ts +262 -0
- package/dist/types/src/declarative-rule/url-transform.d.ts +69 -0
- package/dist/types/src/errors/conversion-errors/empty-domains-error.d.ts +21 -0
- package/dist/types/src/errors/conversion-errors/empty-resources-error.d.ts +21 -0
- package/dist/types/src/errors/conversion-errors/index.d.ts +27 -0
- package/dist/types/src/errors/conversion-errors/invalid-declarative-rule-error.d.ts +30 -0
- package/dist/types/src/errors/conversion-errors/unsupported-modifier-error.d.ts +20 -0
- package/dist/types/src/errors/conversion-errors/unsupported-regexp-error.d.ts +23 -0
- package/dist/types/src/errors/converter-options-errors/empty-or-negative-number-of-rules-error.d.ts +13 -0
- package/dist/types/src/errors/converter-options-errors/index.d.ts +23 -0
- package/dist/types/src/errors/converter-options-errors/negative-number-of-rules-error.d.ts +13 -0
- package/dist/types/src/errors/converter-options-errors/resources-path-error.d.ts +13 -0
- package/dist/types/src/errors/limitation-errors/index.d.ts +27 -0
- package/dist/types/src/errors/limitation-errors/max-scanned-rules-error.d.ts +18 -0
- package/dist/types/src/errors/limitation-errors/too-many-error.d.ts +33 -0
- package/dist/types/src/errors/limitation-errors/too-many-regexp-rules-error.d.ts +21 -0
- package/dist/types/src/errors/limitation-errors/too-many-rules-error.d.ts +21 -0
- package/dist/types/src/errors/limitation-errors/too-many-unsafe-rules-error.d.ts +21 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-filter-source-error.d.ts +19 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-ruleset-source-error.d.ts +19 -0
- package/dist/types/src/filter/filter.d.ts +74 -0
- package/dist/types/src/filter/types.d.ts +45 -0
- package/dist/types/src/filter-converter/conversion-result.d.ts +35 -0
- package/dist/types/src/filter-converter/filter-converter-options.d.ts +81 -0
- package/dist/types/src/filter-converter/filter-converter.d.ts +197 -0
- package/dist/types/src/index.d.ts +37 -0
- package/dist/types/src/rule/rule-types.d.ts +33 -0
- package/dist/types/src/rule/rule-validator.d.ts +185 -0
- package/dist/types/src/rule/rule.d.ts +269 -0
- package/dist/types/src/ruleset/metadata-ruleset.d.ts +128 -0
- package/dist/types/src/ruleset/rules-hash-map.d.ts +70 -0
- package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +318 -0
- package/dist/types/src/ruleset/ruleset.d.ts +86 -0
- package/dist/types/src/ruleset/source-map.d.ts +45 -0
- package/dist/types/src/ruleset/types.d.ts +68 -0
- package/dist/types/src/utils/is-safe-rule.d.ts +20 -0
- package/dist/types/src/utils/ruleset-utils.d.ts +45 -0
- package/dist/types/src/version.d.ts +6 -0
- package/dist/url-transform-converter.js +338 -0
- package/dist/utils/error.js +22 -0
- package/dist/utils/is-safe-rule.js +25 -0
- package/dist/utils/lazy-loader.js +116 -0
- package/dist/utils/lazy.js +50 -0
- package/dist/utils/ruleset-utils.js +76 -0
- package/dist/utils/string.js +180 -0
- package/dist/utils/valibot.js +74 -0
- package/dist/version.js +7 -0
- package/package.json +122 -0
|
@@ -0,0 +1,853 @@
|
|
|
1
|
+
import { RuleParser, RuleCategory, NetworkRuleType } from '@adguard/agtree';
|
|
2
|
+
import { RuleConverter } from '@adguard/agtree/converter';
|
|
3
|
+
import { RuleGenerator } from '@adguard/agtree/generator';
|
|
4
|
+
import { MASK_REGEX_RULE } from '../constants.js';
|
|
5
|
+
import { ResourceType } from '../declarative-rule/rule-condition.js';
|
|
6
|
+
import { getErrorMessage } from '../utils/error.js';
|
|
7
|
+
import { fastHash31, hasSpaces, fastHash } from '../utils/string.js';
|
|
8
|
+
import { OPTION_NAMES } from './option-names.js';
|
|
9
|
+
import { RuleBadfilter } from './rule-badfilter.js';
|
|
10
|
+
import { RulePriority } from './rule-priority.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Maps canonical content-type modifier names to their DNR `ResourceType`
|
|
14
|
+
* equivalents. Only modifier names that correspond to adblock filter syntax
|
|
15
|
+
* content-type options are included here (excluded `webtransport` and `webbundle`).
|
|
16
|
+
*/ const CONTENT_TYPE_RESOURCE_TYPE_MAP = {
|
|
17
|
+
[OPTION_NAMES.DOCUMENT]: ResourceType.MainFrame,
|
|
18
|
+
[OPTION_NAMES.SUBDOCUMENT]: ResourceType.SubFrame,
|
|
19
|
+
[OPTION_NAMES.SCRIPT]: ResourceType.Script,
|
|
20
|
+
[OPTION_NAMES.STYLESHEET]: ResourceType.Stylesheet,
|
|
21
|
+
[OPTION_NAMES.OBJECT]: ResourceType.Object,
|
|
22
|
+
[OPTION_NAMES.IMAGE]: ResourceType.Image,
|
|
23
|
+
[OPTION_NAMES.XMLHTTPREQUEST]: ResourceType.XmlHttpRequest,
|
|
24
|
+
[OPTION_NAMES.MEDIA]: ResourceType.Media,
|
|
25
|
+
[OPTION_NAMES.FONT]: ResourceType.Font,
|
|
26
|
+
[OPTION_NAMES.WEBSOCKET]: ResourceType.WebSocket,
|
|
27
|
+
[OPTION_NAMES.OTHER]: ResourceType.Other,
|
|
28
|
+
[OPTION_NAMES.PING]: ResourceType.Ping
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Modifier names that can be negated with `~`.
|
|
32
|
+
*
|
|
33
|
+
* Any modifier not in this set will cause a {@link SyntaxError} when negated.
|
|
34
|
+
*/ const NEGATABLE_MODIFIERS = new Set([
|
|
35
|
+
OPTION_NAMES.FIRST_PARTY,
|
|
36
|
+
OPTION_NAMES.THIRD_PARTY,
|
|
37
|
+
OPTION_NAMES.MATCH_CASE,
|
|
38
|
+
OPTION_NAMES.DOCUMENT,
|
|
39
|
+
OPTION_NAMES.DOC,
|
|
40
|
+
OPTION_NAMES.SCRIPT,
|
|
41
|
+
OPTION_NAMES.STYLESHEET,
|
|
42
|
+
OPTION_NAMES.SUBDOCUMENT,
|
|
43
|
+
OPTION_NAMES.OBJECT,
|
|
44
|
+
OPTION_NAMES.IMAGE,
|
|
45
|
+
OPTION_NAMES.XMLHTTPREQUEST,
|
|
46
|
+
OPTION_NAMES.MEDIA,
|
|
47
|
+
OPTION_NAMES.FONT,
|
|
48
|
+
OPTION_NAMES.WEBSOCKET,
|
|
49
|
+
OPTION_NAMES.OTHER,
|
|
50
|
+
OPTION_NAMES.PING,
|
|
51
|
+
OPTION_NAMES.EXTENSION
|
|
52
|
+
]);
|
|
53
|
+
/**
|
|
54
|
+
* Request prefix used in `$removeheader` modifier values.
|
|
55
|
+
*/ const REMOVE_HEADER_REQUEST_PREFIX = 'request:';
|
|
56
|
+
/**
|
|
57
|
+
* Headers that may not be removed via the `$removeheader` modifier.
|
|
58
|
+
*
|
|
59
|
+
* @see {@link https://kb.adguard.com/en/general/how-to-create-your-own-ad-filters#removeheader-modifier}
|
|
60
|
+
*/ const FORBIDDEN_REMOVE_HEADERS = new Set([
|
|
61
|
+
'access-control-allow-origin',
|
|
62
|
+
'access-control-allow-credentials',
|
|
63
|
+
'access-control-allow-headers',
|
|
64
|
+
'access-control-allow-methods',
|
|
65
|
+
'access-control-expose-headers',
|
|
66
|
+
'access-control-max-age',
|
|
67
|
+
'access-control-request-headers',
|
|
68
|
+
'access-control-request-method',
|
|
69
|
+
'origin',
|
|
70
|
+
'timing-allow-origin',
|
|
71
|
+
'allow',
|
|
72
|
+
'cross-origin-embedder-policy',
|
|
73
|
+
'cross-origin-opener-policy',
|
|
74
|
+
'cross-origin-resource-policy',
|
|
75
|
+
'content-security-policy',
|
|
76
|
+
'content-security-policy-report-only',
|
|
77
|
+
'expect-ct',
|
|
78
|
+
'feature-policy',
|
|
79
|
+
'origin-isolation',
|
|
80
|
+
'strict-transport-security',
|
|
81
|
+
'upgrade-insecure-requests',
|
|
82
|
+
'x-content-type-options',
|
|
83
|
+
'x-download-options',
|
|
84
|
+
'x-frame-options',
|
|
85
|
+
'x-permitted-cross-domain-policies',
|
|
86
|
+
'x-powered-by',
|
|
87
|
+
'x-xss-protection',
|
|
88
|
+
'public-key-pins',
|
|
89
|
+
'public-key-pins-report-only',
|
|
90
|
+
'sec-websocket-key',
|
|
91
|
+
'sec-websocket-extensions',
|
|
92
|
+
'sec-websocket-accept',
|
|
93
|
+
'sec-websocket-protocol',
|
|
94
|
+
'sec-websocket-version',
|
|
95
|
+
'p3p',
|
|
96
|
+
'sec-fetch-mode',
|
|
97
|
+
'sec-fetch-dest',
|
|
98
|
+
'sec-fetch-site',
|
|
99
|
+
'sec-fetch-user',
|
|
100
|
+
'referrer-policy',
|
|
101
|
+
'content-type',
|
|
102
|
+
'content-length',
|
|
103
|
+
'accept',
|
|
104
|
+
'accept-encoding',
|
|
105
|
+
'host',
|
|
106
|
+
'connection',
|
|
107
|
+
'transfer-encoding',
|
|
108
|
+
'upgrade'
|
|
109
|
+
]);
|
|
110
|
+
/**
|
|
111
|
+
* Modifier names that are compatible with `$removeparam`.
|
|
112
|
+
* Ported from tsurlfilter's `RemoveParamCompatibleOptions`.
|
|
113
|
+
*/ const REMOVEPARAM_COMPATIBLE_MODIFIERS = new Set([
|
|
114
|
+
OPTION_NAMES.REMOVEPARAM,
|
|
115
|
+
OPTION_NAMES.THIRD_PARTY,
|
|
116
|
+
OPTION_NAMES.IMPORTANT,
|
|
117
|
+
OPTION_NAMES.MATCH_CASE,
|
|
118
|
+
OPTION_NAMES.BADFILTER,
|
|
119
|
+
OPTION_NAMES.SCRIPT,
|
|
120
|
+
OPTION_NAMES.STYLESHEET,
|
|
121
|
+
OPTION_NAMES.SUBDOCUMENT,
|
|
122
|
+
OPTION_NAMES.OBJECT,
|
|
123
|
+
OPTION_NAMES.IMAGE,
|
|
124
|
+
OPTION_NAMES.XMLHTTPREQUEST,
|
|
125
|
+
OPTION_NAMES.MEDIA,
|
|
126
|
+
OPTION_NAMES.FONT,
|
|
127
|
+
OPTION_NAMES.WEBSOCKET,
|
|
128
|
+
OPTION_NAMES.OTHER,
|
|
129
|
+
OPTION_NAMES.PING
|
|
130
|
+
]);
|
|
131
|
+
/**
|
|
132
|
+
* Modifier names that are compatible with `$permissions`.
|
|
133
|
+
* Ported from tsurlfilter's `PermissionsCompatibleOptions`.
|
|
134
|
+
*
|
|
135
|
+
* `$permissions` is compatible with `$important`, `$badfilter`, `$domain`,
|
|
136
|
+
* and all content-type modifiers (`$subdocument`, `$script`, `$stylesheet`,
|
|
137
|
+
* etc.). This mirrors the runtime behavior of tsurlfilter's `NetworkRule`,
|
|
138
|
+
* which tracks content-type and `$domain` modifiers separately from the
|
|
139
|
+
* option bitmask used by `validatePermissionsRule`.
|
|
140
|
+
*/ const PERMISSIONS_COMPATIBLE_MODIFIERS = new Set([
|
|
141
|
+
OPTION_NAMES.PERMISSIONS,
|
|
142
|
+
OPTION_NAMES.IMPORTANT,
|
|
143
|
+
OPTION_NAMES.BADFILTER,
|
|
144
|
+
OPTION_NAMES.DOMAIN,
|
|
145
|
+
OPTION_NAMES.SUBDOCUMENT,
|
|
146
|
+
OPTION_NAMES.SCRIPT,
|
|
147
|
+
OPTION_NAMES.IMAGE,
|
|
148
|
+
OPTION_NAMES.STYLESHEET,
|
|
149
|
+
OPTION_NAMES.OBJECT,
|
|
150
|
+
OPTION_NAMES.XMLHTTPREQUEST,
|
|
151
|
+
OPTION_NAMES.MEDIA,
|
|
152
|
+
OPTION_NAMES.FONT,
|
|
153
|
+
OPTION_NAMES.WEBSOCKET,
|
|
154
|
+
OPTION_NAMES.OTHER,
|
|
155
|
+
OPTION_NAMES.PING
|
|
156
|
+
]);
|
|
157
|
+
/**
|
|
158
|
+
* Modifier names that are compatible with `$header`.
|
|
159
|
+
* Ported from tsurlfilter's `HeaderCompatibleOptions`.
|
|
160
|
+
*/ const HEADER_COMPATIBLE_MODIFIERS = new Set([
|
|
161
|
+
OPTION_NAMES.HEADER,
|
|
162
|
+
OPTION_NAMES.IMPORTANT,
|
|
163
|
+
OPTION_NAMES.CSP,
|
|
164
|
+
OPTION_NAMES.REMOVEHEADER,
|
|
165
|
+
OPTION_NAMES.THIRD_PARTY,
|
|
166
|
+
OPTION_NAMES.MATCH_CASE,
|
|
167
|
+
OPTION_NAMES.BADFILTER,
|
|
168
|
+
OPTION_NAMES.SCRIPT,
|
|
169
|
+
OPTION_NAMES.STYLESHEET,
|
|
170
|
+
OPTION_NAMES.SUBDOCUMENT,
|
|
171
|
+
OPTION_NAMES.OBJECT,
|
|
172
|
+
OPTION_NAMES.IMAGE,
|
|
173
|
+
OPTION_NAMES.XMLHTTPREQUEST,
|
|
174
|
+
OPTION_NAMES.MEDIA,
|
|
175
|
+
OPTION_NAMES.FONT,
|
|
176
|
+
OPTION_NAMES.WEBSOCKET,
|
|
177
|
+
OPTION_NAMES.OTHER,
|
|
178
|
+
OPTION_NAMES.PING
|
|
179
|
+
]);
|
|
180
|
+
/**
|
|
181
|
+
* Modifier names that are compatible with `$removeheader`.
|
|
182
|
+
* Ported from tsurlfilter's `RemoveHeaderCompatibleOptions`.
|
|
183
|
+
*/ const REMOVEHEADER_COMPATIBLE_MODIFIERS = new Set([
|
|
184
|
+
OPTION_NAMES.REMOVEHEADER,
|
|
185
|
+
OPTION_NAMES.HEADER,
|
|
186
|
+
OPTION_NAMES.THIRD_PARTY,
|
|
187
|
+
OPTION_NAMES.IMPORTANT,
|
|
188
|
+
OPTION_NAMES.MATCH_CASE,
|
|
189
|
+
OPTION_NAMES.BADFILTER,
|
|
190
|
+
OPTION_NAMES.SCRIPT,
|
|
191
|
+
OPTION_NAMES.STYLESHEET,
|
|
192
|
+
OPTION_NAMES.SUBDOCUMENT,
|
|
193
|
+
OPTION_NAMES.OBJECT,
|
|
194
|
+
OPTION_NAMES.IMAGE,
|
|
195
|
+
OPTION_NAMES.XMLHTTPREQUEST,
|
|
196
|
+
OPTION_NAMES.MEDIA,
|
|
197
|
+
OPTION_NAMES.FONT,
|
|
198
|
+
OPTION_NAMES.WEBSOCKET,
|
|
199
|
+
OPTION_NAMES.OTHER,
|
|
200
|
+
OPTION_NAMES.PING
|
|
201
|
+
]);
|
|
202
|
+
/**
|
|
203
|
+
* Parsed and normalized network filtering rule.
|
|
204
|
+
*
|
|
205
|
+
* Instances are created exclusively via the static factory methods
|
|
206
|
+
* {@link Rule.createFromText} and {@link Rule.parseFromNode}.
|
|
207
|
+
*/ class Rule {
|
|
208
|
+
/**
|
|
209
|
+
* Checks if the specified modifier is enabled on the rule.
|
|
210
|
+
*
|
|
211
|
+
* @param modifier Canonical modifier name (e.g. `"redirect"`, `"script"`).
|
|
212
|
+
*
|
|
213
|
+
* @returns `true` if the modifier is enabled.
|
|
214
|
+
*/ isModifierEnabled(modifier) {
|
|
215
|
+
return this.enabledModifiers.has(modifier);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Checks if the specified modifier is disabled (negated) on the rule.
|
|
219
|
+
*
|
|
220
|
+
* @param modifier Canonical modifier name.
|
|
221
|
+
*
|
|
222
|
+
* @returns `true` if the modifier is disabled.
|
|
223
|
+
*/ isModifierDisabled(modifier) {
|
|
224
|
+
return this.disabledModifiers.has(modifier);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Returns `true` if one and only the specified modifier is enabled.
|
|
228
|
+
*
|
|
229
|
+
* @param modifier Canonical modifier name.
|
|
230
|
+
*
|
|
231
|
+
* @returns `true` if the modifier is the only enabled modifier.
|
|
232
|
+
*/ isSingleModifierEnabled(modifier) {
|
|
233
|
+
return this.enabledModifiers.size === 1 && this.enabledModifiers.has(modifier);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Checks if the rule completely disables filtering (elemhide + content +
|
|
237
|
+
* urlblock + jsinject all enabled on an allowlist rule).
|
|
238
|
+
*
|
|
239
|
+
* @returns `true` if the rule disables all filtering.
|
|
240
|
+
*/ isFilteringDisabled() {
|
|
241
|
+
if (!this.allowlist) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
return this.enabledModifiers.has(OPTION_NAMES.ELEMHIDE) && this.enabledModifiers.has(OPTION_NAMES.CONTENT) && this.enabledModifiers.has(OPTION_NAMES.URLBLOCK) && this.enabledModifiers.has(OPTION_NAMES.JSINJECT);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Checks if the rule pattern is a regular expression.
|
|
248
|
+
*
|
|
249
|
+
* @returns `true` if the rule pattern is a regex.
|
|
250
|
+
*/ isRegexRule() {
|
|
251
|
+
return this.pattern.startsWith(MASK_REGEX_RULE) && this.pattern.endsWith(MASK_REGEX_RULE);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Returns the rule text generated from the AST node.
|
|
255
|
+
*
|
|
256
|
+
* @returns Rule text string.
|
|
257
|
+
*/ getText() {
|
|
258
|
+
return RuleGenerator.generate(this.node);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Gets a hash for the full text of the rule. Needed to keep the ID of the
|
|
262
|
+
* rule in the filter stable between runs (for CWS "skip review").
|
|
263
|
+
*
|
|
264
|
+
* @param salt Optional numeric salt for uniqueness.
|
|
265
|
+
*
|
|
266
|
+
* @returns Hash value in range [0, 2^31-1].
|
|
267
|
+
*/ getTextHash(salt) {
|
|
268
|
+
const text = this.getText();
|
|
269
|
+
const trialText = salt === undefined ? text : `${text}\0${salt}`;
|
|
270
|
+
return fastHash31(trialText);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Returns the applicable header name for `$removeheader`, or `null`
|
|
274
|
+
* if the direction doesn't match or no valid header is set.
|
|
275
|
+
*
|
|
276
|
+
* @param isRequestHeaders `true` for request headers, `false` for response.
|
|
277
|
+
*
|
|
278
|
+
* @returns Header name or `null`.
|
|
279
|
+
*/ getApplicableHeaderName(isRequestHeaders) {
|
|
280
|
+
if (!this.removeHeaderName) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
if (isRequestHeaders !== this.removeHeaderIsRequestType) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
return this.removeHeaderName;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Checks whether this rule (which must carry `$badfilter`) negates
|
|
290
|
+
* `targetRule`. Delegates to {@link RuleBadfilter.negates}.
|
|
291
|
+
*
|
|
292
|
+
* @param targetRule The rule to test for negation.
|
|
293
|
+
*
|
|
294
|
+
* @returns `true` if this rule negates `targetRule`.
|
|
295
|
+
*/ negatesBadfilter(targetRule) {
|
|
296
|
+
return RuleBadfilter.negates(this, targetRule);
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Determines the effective header name and direction for a
|
|
300
|
+
* `$removeheader` value.
|
|
301
|
+
*
|
|
302
|
+
* @param value Raw modifier value (may be empty for allowlist rules).
|
|
303
|
+
*
|
|
304
|
+
* @returns `name` — effective header name, or `null` when
|
|
305
|
+
* forbidden/unsupported; `isRequest` — `true` when targeting request
|
|
306
|
+
* headers.
|
|
307
|
+
*/ static computeRemoveHeaderInfo(value) {
|
|
308
|
+
const lowerValue = value.toLowerCase();
|
|
309
|
+
const isRequest = lowerValue.startsWith(REMOVE_HEADER_REQUEST_PREFIX);
|
|
310
|
+
const rawHeaderName = isRequest ? lowerValue.slice(REMOVE_HEADER_REQUEST_PREFIX.length) : lowerValue;
|
|
311
|
+
const headerName = rawHeaderName.trim();
|
|
312
|
+
const isValid = headerName.length > 0 && !FORBIDDEN_REMOVE_HEADERS.has(headerName) && !headerName.includes(':');
|
|
313
|
+
return {
|
|
314
|
+
name: isValid ? headerName : null,
|
|
315
|
+
isRequest
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Parses the `$header` modifier value into an {@link HttpHeaderMatcher}.
|
|
320
|
+
*
|
|
321
|
+
* Expected format: `<header-name>` or `<header-name>:<value-or-/regex/>`.
|
|
322
|
+
*
|
|
323
|
+
* @param value Raw modifier value string.
|
|
324
|
+
*
|
|
325
|
+
* @returns Parsed header matcher.
|
|
326
|
+
*/ static parseHeaderModifier(value) {
|
|
327
|
+
const colonIdx = value.indexOf(':');
|
|
328
|
+
if (colonIdx === -1) {
|
|
329
|
+
return {
|
|
330
|
+
header: value,
|
|
331
|
+
value: null,
|
|
332
|
+
isRegExp: false
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const header = value.slice(0, colonIdx);
|
|
336
|
+
const rawValue = value.slice(colonIdx + 1);
|
|
337
|
+
if (rawValue.startsWith('/') && rawValue.endsWith('/')) {
|
|
338
|
+
return {
|
|
339
|
+
header,
|
|
340
|
+
value: rawValue.slice(1, -1),
|
|
341
|
+
isRegExp: true
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
header,
|
|
346
|
+
value: rawValue,
|
|
347
|
+
isRegExp: false
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Checks if a network rule node is too general to be useful (no modifiers
|
|
352
|
+
* and pattern shorter than 4 characters).
|
|
353
|
+
*
|
|
354
|
+
* @param node Network rule AST node.
|
|
355
|
+
*
|
|
356
|
+
* @returns `true` if the rule is too general.
|
|
357
|
+
*/ static isTooGeneral(node) {
|
|
358
|
+
return !node.modifiers?.children?.length && node.pattern.value.length < 4;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Parses and converts rule text to AG syntax, then creates
|
|
362
|
+
* {@link Rule} instances from all resulting network-rule nodes.
|
|
363
|
+
*
|
|
364
|
+
* @param filterId Filter list ID.
|
|
365
|
+
* @param ruleIndex Rule index within the filter list.
|
|
366
|
+
* @param text Raw rule text.
|
|
367
|
+
*
|
|
368
|
+
* @returns Array of {@link Rule} parsed from `text`.
|
|
369
|
+
*
|
|
370
|
+
* @throws `Error` when parsing or conversion fails.
|
|
371
|
+
*/ static createFromText(filterId, ruleIndex, text) {
|
|
372
|
+
let rulesConvertedToAGSyntax;
|
|
373
|
+
try {
|
|
374
|
+
const node = RuleParser.parse(text);
|
|
375
|
+
const conversionResult = RuleConverter.convertToAdg(node);
|
|
376
|
+
if (conversionResult.isConverted) {
|
|
377
|
+
rulesConvertedToAGSyntax = conversionResult.result;
|
|
378
|
+
} else {
|
|
379
|
+
rulesConvertedToAGSyntax = [
|
|
380
|
+
node
|
|
381
|
+
];
|
|
382
|
+
}
|
|
383
|
+
} catch (e) {
|
|
384
|
+
throw new Error(// eslint-disable-next-line max-len
|
|
385
|
+
`Unknown error during conversion rule to AG syntax: ${getErrorMessage(e)}`);
|
|
386
|
+
}
|
|
387
|
+
const rules = [];
|
|
388
|
+
for(let i = 0; i < rulesConvertedToAGSyntax.length; i += 1){
|
|
389
|
+
const ruleNode = rulesConvertedToAGSyntax[i];
|
|
390
|
+
if (ruleNode.category !== RuleCategory.Network || ruleNode.type !== NetworkRuleType.NetworkRule) {
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
rules.push(new Rule(filterId, ruleIndex, ruleNode));
|
|
395
|
+
} catch (e) {
|
|
396
|
+
throw new Error(// eslint-disable-next-line max-len
|
|
397
|
+
`Cannot create Rule from filter "${filterId}" and rule "${text}": ${getErrorMessage(e)}`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return rules;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Converts an already-parsed rule node to AG syntax and creates
|
|
404
|
+
* {@link Rule} instances from all resulting network-rule nodes.
|
|
405
|
+
*
|
|
406
|
+
* @param filterListId Filter list ID.
|
|
407
|
+
* @param index Rule index within the filter list.
|
|
408
|
+
* @param node Rule AST node.
|
|
409
|
+
*
|
|
410
|
+
* @returns Array of {@link Rule} parsed from `node`.
|
|
411
|
+
*
|
|
412
|
+
* @throws `Error` when conversion to AG syntax fails.
|
|
413
|
+
* @throws `SyntaxError` when the rule has spaces in pattern or is too
|
|
414
|
+
* general.
|
|
415
|
+
*/ static parseFromNode(filterListId, index, node) {
|
|
416
|
+
let rulesConvertedToAG;
|
|
417
|
+
try {
|
|
418
|
+
const conversionResult = RuleConverter.convertToAdg(node);
|
|
419
|
+
if (conversionResult.isConverted) {
|
|
420
|
+
rulesConvertedToAG = conversionResult.result;
|
|
421
|
+
} else {
|
|
422
|
+
rulesConvertedToAG = [
|
|
423
|
+
node
|
|
424
|
+
];
|
|
425
|
+
}
|
|
426
|
+
} catch (e) {
|
|
427
|
+
throw new Error(// eslint-disable-next-line max-len
|
|
428
|
+
`Unknown error during conversion rule to AG syntax: ${getErrorMessage(e)}`);
|
|
429
|
+
}
|
|
430
|
+
const rules = [];
|
|
431
|
+
for(let i = 0; i < rulesConvertedToAG.length; i += 1){
|
|
432
|
+
const ruleNode = rulesConvertedToAG[i];
|
|
433
|
+
if (ruleNode.category !== RuleCategory.Network || ruleNode.type !== NetworkRuleType.NetworkRule) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
rules.push(new Rule(filterListId, index, ruleNode));
|
|
438
|
+
} catch (e) {
|
|
439
|
+
let msg = `"${getErrorMessage(e)}" in the rule: `;
|
|
440
|
+
try {
|
|
441
|
+
msg += `"${RuleGenerator.generate(node)}"`;
|
|
442
|
+
} catch (generateError) {
|
|
443
|
+
msg += `"${JSON.stringify(node)}" `;
|
|
444
|
+
msg += `(generate error: ${getErrorMessage(generateError)})`;
|
|
445
|
+
}
|
|
446
|
+
throw new Error(msg);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return rules;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Creates a new {@link Rule} from filter list metadata and an already-converted
|
|
453
|
+
* network rule AST node.
|
|
454
|
+
*
|
|
455
|
+
* @param filterListId Filter list ID.
|
|
456
|
+
* @param index Rule index within the filter list.
|
|
457
|
+
* @param node Network rule AST node (must already be in AG syntax).
|
|
458
|
+
*
|
|
459
|
+
* @throws `SyntaxError` when the pattern contains spaces, the rule is too
|
|
460
|
+
* general, or any modifier is invalid.
|
|
461
|
+
*/ constructor(filterListId, index, node){
|
|
462
|
+
/**
|
|
463
|
+
* Set of canonical modifier names that are enabled on this rule.
|
|
464
|
+
*
|
|
465
|
+
* Content-type modifiers (script, image, etc.) are tracked here
|
|
466
|
+
* **in addition to** being mapped to {@link permittedResourceTypes}.
|
|
467
|
+
*/ this.enabledModifiers = new Set();
|
|
468
|
+
/**
|
|
469
|
+
* Set of canonical modifier names that are disabled (negated) on
|
|
470
|
+
* this rule. E.g. `~third-party` → `"third-party"` in this set.
|
|
471
|
+
*/ this.disabledModifiers = new Set();
|
|
472
|
+
/**
|
|
473
|
+
* Permitted (non-negated) domains from the `$domain` modifier.
|
|
474
|
+
*/ this.permittedDomains = null;
|
|
475
|
+
/**
|
|
476
|
+
* Restricted (negated) domains from the `$domain` modifier.
|
|
477
|
+
*/ this.restrictedDomains = null;
|
|
478
|
+
/**
|
|
479
|
+
* Permitted destination domains from the `$to` modifier.
|
|
480
|
+
*/ this.permittedToDomains = null;
|
|
481
|
+
/**
|
|
482
|
+
* Restricted destination domains from the `$to` modifier.
|
|
483
|
+
*/ this.restrictedToDomains = null;
|
|
484
|
+
/**
|
|
485
|
+
* Exception domains from the `$denyallow` modifier.
|
|
486
|
+
*/ this.denyAllowDomains = null;
|
|
487
|
+
/**
|
|
488
|
+
* Permitted HTTP methods from the `$method` modifier.
|
|
489
|
+
*/ this.permittedMethods = null;
|
|
490
|
+
/**
|
|
491
|
+
* Restricted HTTP methods from the `$method` modifier.
|
|
492
|
+
*/ this.restrictedMethods = null;
|
|
493
|
+
/**
|
|
494
|
+
* Name of the advanced modifier (e.g. `"csp"`, `"redirect"`,
|
|
495
|
+
* `"removeparam"`, `"removeheader"`, `"cookie"`, `"permissions"`).
|
|
496
|
+
*/ this.advancedModifierName = null;
|
|
497
|
+
/**
|
|
498
|
+
* Raw string value of the advanced modifier.
|
|
499
|
+
*/ this.advancedModifierValue = null;
|
|
500
|
+
/**
|
|
501
|
+
* Parsed data for the `$header` modifier.
|
|
502
|
+
*/ this.headerMatcher = null;
|
|
503
|
+
/**
|
|
504
|
+
* Effective header name to be removed by `$removeheader`.
|
|
505
|
+
* `null` when no valid `$removeheader` modifier is present.
|
|
506
|
+
*/ this.removeHeaderName = null;
|
|
507
|
+
/**
|
|
508
|
+
* Whether the `$removeheader` modifier targets request headers
|
|
509
|
+
* (true) or response headers (false).
|
|
510
|
+
*/ this.removeHeaderIsRequestType = false;
|
|
511
|
+
/**
|
|
512
|
+
* Whether the rule uses `$redirect-rule` (only redirects
|
|
513
|
+
* already-blocked requests) vs plain `$redirect`.
|
|
514
|
+
*/ this.isRedirectRuleModifier = false;
|
|
515
|
+
const pattern = node.pattern.value;
|
|
516
|
+
if (pattern && hasSpaces(pattern)) {
|
|
517
|
+
throw new SyntaxError('Rule has spaces, seems to be a host rule');
|
|
518
|
+
}
|
|
519
|
+
if (Rule.isTooGeneral(node)) {
|
|
520
|
+
throw new SyntaxError(`Rule is too general: ${RuleGenerator.generate(node)}`);
|
|
521
|
+
}
|
|
522
|
+
const isAllowlist = node.exception;
|
|
523
|
+
const permittedResourceTypesSet = new Set();
|
|
524
|
+
const restrictedResourceTypesSet = new Set();
|
|
525
|
+
const meta = {
|
|
526
|
+
baseModifierCount: 0,
|
|
527
|
+
permittedContentTypeCount: 0,
|
|
528
|
+
permittedMethodCount: 0,
|
|
529
|
+
hasHeader: false,
|
|
530
|
+
permittedDomainCount: 0,
|
|
531
|
+
hasRedirect: false,
|
|
532
|
+
specificExclusionCount: 0,
|
|
533
|
+
hasImportant: false
|
|
534
|
+
};
|
|
535
|
+
let hasRestrictedContentType = false;
|
|
536
|
+
for (const modifier of node.modifiers?.children ?? []){
|
|
537
|
+
const rawName = modifier.name.value;
|
|
538
|
+
const exception = modifier.exception ?? false;
|
|
539
|
+
const value = modifier.value?.value ?? '';
|
|
540
|
+
const name = rawName;
|
|
541
|
+
if (exception && !NEGATABLE_MODIFIERS.has(name)) {
|
|
542
|
+
throw new SyntaxError(`Modifier $${name} cannot be negated`);
|
|
543
|
+
}
|
|
544
|
+
switch(name){
|
|
545
|
+
case OPTION_NAMES.FIRST_PARTY:
|
|
546
|
+
// $first-party is an alias for ~$third-party.
|
|
547
|
+
// Normalize it into the same internal representation so that
|
|
548
|
+
// RegularRuleConverter.getCondition() can derive DomainType.FirstParty
|
|
549
|
+
// from isModifierDisabled(THIRD_PARTY) uniformly.
|
|
550
|
+
if (exception) {
|
|
551
|
+
// ~$first-party ≡ $third-party
|
|
552
|
+
this.enabledModifiers.add(OPTION_NAMES.THIRD_PARTY);
|
|
553
|
+
} else {
|
|
554
|
+
// $first-party ≡ ~$third-party
|
|
555
|
+
this.disabledModifiers.add(OPTION_NAMES.THIRD_PARTY);
|
|
556
|
+
}
|
|
557
|
+
meta.baseModifierCount += 1;
|
|
558
|
+
break;
|
|
559
|
+
case OPTION_NAMES.THIRD_PARTY:
|
|
560
|
+
if (exception) {
|
|
561
|
+
this.disabledModifiers.add(name);
|
|
562
|
+
} else {
|
|
563
|
+
this.enabledModifiers.add(name);
|
|
564
|
+
}
|
|
565
|
+
meta.baseModifierCount += 1;
|
|
566
|
+
break;
|
|
567
|
+
case OPTION_NAMES.MATCH_CASE:
|
|
568
|
+
if (exception) {
|
|
569
|
+
this.disabledModifiers.add(name);
|
|
570
|
+
} else {
|
|
571
|
+
this.enabledModifiers.add(name);
|
|
572
|
+
}
|
|
573
|
+
meta.baseModifierCount += 1;
|
|
574
|
+
break;
|
|
575
|
+
case OPTION_NAMES.IMPORTANT:
|
|
576
|
+
this.enabledModifiers.add(name);
|
|
577
|
+
meta.hasImportant = true;
|
|
578
|
+
break;
|
|
579
|
+
case OPTION_NAMES.BADFILTER:
|
|
580
|
+
case OPTION_NAMES.POPUP:
|
|
581
|
+
this.enabledModifiers.add(name);
|
|
582
|
+
break;
|
|
583
|
+
case OPTION_NAMES.STEALTH:
|
|
584
|
+
if (!isAllowlist) {
|
|
585
|
+
throw new SyntaxError('Modifier $stealth is only allowed on allowlist rules');
|
|
586
|
+
}
|
|
587
|
+
this.enabledModifiers.add(name);
|
|
588
|
+
break;
|
|
589
|
+
case OPTION_NAMES.DOCUMENT:
|
|
590
|
+
if (exception) {
|
|
591
|
+
this.disabledModifiers.add(name);
|
|
592
|
+
restrictedResourceTypesSet.add(ResourceType.MainFrame);
|
|
593
|
+
hasRestrictedContentType = true;
|
|
594
|
+
} else {
|
|
595
|
+
this.enabledModifiers.add(name);
|
|
596
|
+
permittedResourceTypesSet.add(ResourceType.MainFrame);
|
|
597
|
+
if (isAllowlist) {
|
|
598
|
+
this.enabledModifiers.add(OPTION_NAMES.ELEMHIDE);
|
|
599
|
+
this.enabledModifiers.add(OPTION_NAMES.JSINJECT);
|
|
600
|
+
this.enabledModifiers.add(OPTION_NAMES.URLBLOCK);
|
|
601
|
+
this.enabledModifiers.add(OPTION_NAMES.CONTENT);
|
|
602
|
+
meta.specificExclusionCount += 4;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
break;
|
|
606
|
+
case OPTION_NAMES.ELEMHIDE:
|
|
607
|
+
case OPTION_NAMES.GENERICHIDE:
|
|
608
|
+
case OPTION_NAMES.SPECIFICHIDE:
|
|
609
|
+
case OPTION_NAMES.CONTENT:
|
|
610
|
+
case OPTION_NAMES.URLBLOCK:
|
|
611
|
+
case OPTION_NAMES.GENERICBLOCK:
|
|
612
|
+
case OPTION_NAMES.JSINJECT:
|
|
613
|
+
if (exception) {
|
|
614
|
+
this.disabledModifiers.add(name);
|
|
615
|
+
} else {
|
|
616
|
+
if (!isAllowlist) {
|
|
617
|
+
throw new SyntaxError(`Modifier $${name} is only allowed on allowlist rules`);
|
|
618
|
+
}
|
|
619
|
+
this.enabledModifiers.add(name);
|
|
620
|
+
permittedResourceTypesSet.add(ResourceType.MainFrame);
|
|
621
|
+
permittedResourceTypesSet.add(ResourceType.SubFrame);
|
|
622
|
+
meta.specificExclusionCount += 1;
|
|
623
|
+
}
|
|
624
|
+
break;
|
|
625
|
+
case OPTION_NAMES.EXTENSION:
|
|
626
|
+
if (exception) {
|
|
627
|
+
this.disabledModifiers.add(name);
|
|
628
|
+
} else {
|
|
629
|
+
if (!isAllowlist) {
|
|
630
|
+
throw new SyntaxError(`Modifier $${name} is only allowed on allowlist rules`);
|
|
631
|
+
}
|
|
632
|
+
this.enabledModifiers.add(name);
|
|
633
|
+
meta.specificExclusionCount += 1;
|
|
634
|
+
}
|
|
635
|
+
break;
|
|
636
|
+
case OPTION_NAMES.SCRIPT:
|
|
637
|
+
case OPTION_NAMES.STYLESHEET:
|
|
638
|
+
case OPTION_NAMES.SUBDOCUMENT:
|
|
639
|
+
case OPTION_NAMES.OBJECT:
|
|
640
|
+
case OPTION_NAMES.IMAGE:
|
|
641
|
+
case OPTION_NAMES.XMLHTTPREQUEST:
|
|
642
|
+
case OPTION_NAMES.MEDIA:
|
|
643
|
+
case OPTION_NAMES.FONT:
|
|
644
|
+
case OPTION_NAMES.WEBSOCKET:
|
|
645
|
+
case OPTION_NAMES.OTHER:
|
|
646
|
+
case OPTION_NAMES.PING:
|
|
647
|
+
{
|
|
648
|
+
const rt = CONTENT_TYPE_RESOURCE_TYPE_MAP[name];
|
|
649
|
+
if (exception) {
|
|
650
|
+
this.disabledModifiers.add(name);
|
|
651
|
+
if (rt !== undefined) {
|
|
652
|
+
restrictedResourceTypesSet.add(rt);
|
|
653
|
+
hasRestrictedContentType = true;
|
|
654
|
+
}
|
|
655
|
+
} else {
|
|
656
|
+
this.enabledModifiers.add(name);
|
|
657
|
+
if (rt !== undefined) {
|
|
658
|
+
permittedResourceTypesSet.add(rt);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
case OPTION_NAMES.DOMAIN:
|
|
664
|
+
{
|
|
665
|
+
const parts = value.split('|').filter(Boolean);
|
|
666
|
+
const permitted = [];
|
|
667
|
+
const restricted = [];
|
|
668
|
+
for (const part of parts){
|
|
669
|
+
if (part.startsWith('~')) {
|
|
670
|
+
restricted.push(part.slice(1));
|
|
671
|
+
} else {
|
|
672
|
+
permitted.push(part);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (permitted.length > 0) {
|
|
676
|
+
this.permittedDomains = permitted;
|
|
677
|
+
meta.permittedDomainCount = permitted.length;
|
|
678
|
+
}
|
|
679
|
+
if (restricted.length > 0) {
|
|
680
|
+
this.restrictedDomains = restricted;
|
|
681
|
+
meta.baseModifierCount += 1;
|
|
682
|
+
}
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
case OPTION_NAMES.DENYALLOW:
|
|
686
|
+
{
|
|
687
|
+
const parts = value.split('|').filter(Boolean);
|
|
688
|
+
if (parts.some((p)=>p.startsWith('~'))) {
|
|
689
|
+
throw new SyntaxError('Modifier $denyallow domains cannot be negated');
|
|
690
|
+
}
|
|
691
|
+
if (parts.length > 0) {
|
|
692
|
+
this.denyAllowDomains = parts;
|
|
693
|
+
meta.baseModifierCount += 1;
|
|
694
|
+
}
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
case OPTION_NAMES.TO:
|
|
698
|
+
{
|
|
699
|
+
const parts = value.split('|').filter(Boolean);
|
|
700
|
+
const toPermitted = [];
|
|
701
|
+
const toRestricted = [];
|
|
702
|
+
for (const part of parts){
|
|
703
|
+
if (part.startsWith('~')) {
|
|
704
|
+
toRestricted.push(part.slice(1));
|
|
705
|
+
} else {
|
|
706
|
+
toPermitted.push(part);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (toPermitted.length > 0) {
|
|
710
|
+
this.permittedToDomains = toPermitted;
|
|
711
|
+
}
|
|
712
|
+
if (toRestricted.length > 0) {
|
|
713
|
+
this.restrictedToDomains = toRestricted;
|
|
714
|
+
}
|
|
715
|
+
meta.baseModifierCount += 1;
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
case OPTION_NAMES.METHOD:
|
|
719
|
+
{
|
|
720
|
+
const parts = value.split('|').filter(Boolean);
|
|
721
|
+
const methodPermitted = [];
|
|
722
|
+
const methodRestricted = [];
|
|
723
|
+
for (const part of parts){
|
|
724
|
+
const isNegated = part.startsWith('~');
|
|
725
|
+
const methodName = (isNegated ? part.slice(1) : part).toLowerCase();
|
|
726
|
+
// Unknown methods (e.g. 'trace') are kept and rejected later by the validator.
|
|
727
|
+
if (isNegated) {
|
|
728
|
+
methodRestricted.push(methodName);
|
|
729
|
+
} else {
|
|
730
|
+
methodPermitted.push(methodName);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
this.enabledModifiers.add(name);
|
|
734
|
+
if (methodPermitted.length > 0) {
|
|
735
|
+
this.permittedMethods = methodPermitted;
|
|
736
|
+
meta.permittedMethodCount = methodPermitted.length;
|
|
737
|
+
}
|
|
738
|
+
if (methodRestricted.length > 0) {
|
|
739
|
+
this.restrictedMethods = methodRestricted;
|
|
740
|
+
meta.baseModifierCount += 1;
|
|
741
|
+
}
|
|
742
|
+
break;
|
|
743
|
+
}
|
|
744
|
+
case OPTION_NAMES.HEADER:
|
|
745
|
+
this.enabledModifiers.add(name);
|
|
746
|
+
this.headerMatcher = Rule.parseHeaderModifier(value);
|
|
747
|
+
meta.hasHeader = true;
|
|
748
|
+
break;
|
|
749
|
+
case OPTION_NAMES.CSP:
|
|
750
|
+
case OPTION_NAMES.REMOVEPARAM:
|
|
751
|
+
case OPTION_NAMES.COOKIE:
|
|
752
|
+
case OPTION_NAMES.REPLACE:
|
|
753
|
+
case OPTION_NAMES.JSONPRUNE:
|
|
754
|
+
case OPTION_NAMES.HLS:
|
|
755
|
+
case OPTION_NAMES.REFERRERPOLICY:
|
|
756
|
+
case OPTION_NAMES.PERMISSIONS:
|
|
757
|
+
this.enabledModifiers.add(name);
|
|
758
|
+
this.advancedModifierName = rawName;
|
|
759
|
+
this.advancedModifierValue = value || null;
|
|
760
|
+
break;
|
|
761
|
+
case OPTION_NAMES.REDIRECTRULE:
|
|
762
|
+
case OPTION_NAMES.REDIRECT:
|
|
763
|
+
this.enabledModifiers.add(OPTION_NAMES.REDIRECT);
|
|
764
|
+
this.advancedModifierName = name;
|
|
765
|
+
this.advancedModifierValue = value || null;
|
|
766
|
+
this.isRedirectRuleModifier = name === OPTION_NAMES.REDIRECTRULE;
|
|
767
|
+
meta.hasRedirect = true;
|
|
768
|
+
break;
|
|
769
|
+
case OPTION_NAMES.REMOVEHEADER:
|
|
770
|
+
{
|
|
771
|
+
this.enabledModifiers.add(name);
|
|
772
|
+
this.advancedModifierName = rawName;
|
|
773
|
+
this.advancedModifierValue = value || null;
|
|
774
|
+
const info = Rule.computeRemoveHeaderInfo(value);
|
|
775
|
+
this.removeHeaderName = info.name;
|
|
776
|
+
this.removeHeaderIsRequestType = info.isRequest;
|
|
777
|
+
break;
|
|
778
|
+
}
|
|
779
|
+
case OPTION_NAMES.DNSREWRITE:
|
|
780
|
+
this.enabledModifiers.add(name);
|
|
781
|
+
this.advancedModifierName = rawName;
|
|
782
|
+
this.advancedModifierValue = value || null;
|
|
783
|
+
meta.baseModifierCount += 1;
|
|
784
|
+
break;
|
|
785
|
+
// DNS/network-level modifiers — no DNR equivalent.
|
|
786
|
+
// Still added to enabledModifiers so the validator can reject them.
|
|
787
|
+
case OPTION_NAMES.DNSTYPE:
|
|
788
|
+
case OPTION_NAMES.CTAG:
|
|
789
|
+
case OPTION_NAMES.CLIENT:
|
|
790
|
+
case OPTION_NAMES.APP:
|
|
791
|
+
case OPTION_NAMES.NETWORK:
|
|
792
|
+
this.enabledModifiers.add(name);
|
|
793
|
+
break;
|
|
794
|
+
case OPTION_NAMES.ALL:
|
|
795
|
+
if (isAllowlist) {
|
|
796
|
+
throw new SyntaxError('Modifier $all is not allowed on allowlist rules');
|
|
797
|
+
}
|
|
798
|
+
for (const rt of Object.values(ResourceType)){
|
|
799
|
+
permittedResourceTypesSet.add(rt);
|
|
800
|
+
}
|
|
801
|
+
this.enabledModifiers.add(OPTION_NAMES.POPUP);
|
|
802
|
+
break;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (this.denyAllowDomains !== null && this.permittedToDomains !== null) {
|
|
806
|
+
throw new SyntaxError('Modifiers $denyallow and $to cannot be used together');
|
|
807
|
+
}
|
|
808
|
+
// Validate modifier combinations (ported from tsurlfilter).
|
|
809
|
+
if (this.enabledModifiers.has(OPTION_NAMES.REMOVEPARAM)) {
|
|
810
|
+
for (const modifier of this.enabledModifiers){
|
|
811
|
+
if (!REMOVEPARAM_COMPATIBLE_MODIFIERS.has(modifier)) {
|
|
812
|
+
throw new SyntaxError(`$removeparam is not compatible with $${modifier}`);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
if (this.enabledModifiers.has(OPTION_NAMES.PERMISSIONS)) {
|
|
817
|
+
for (const modifier of this.enabledModifiers){
|
|
818
|
+
if (!PERMISSIONS_COMPATIBLE_MODIFIERS.has(modifier)) {
|
|
819
|
+
throw new SyntaxError(`$permissions is not compatible with $${modifier}`);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (this.enabledModifiers.has(OPTION_NAMES.HEADER)) {
|
|
824
|
+
for (const modifier of this.enabledModifiers){
|
|
825
|
+
if (!HEADER_COMPATIBLE_MODIFIERS.has(modifier)) {
|
|
826
|
+
throw new SyntaxError(`$header is not compatible with $${modifier}`);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
if (this.removeHeaderIsRequestType) {
|
|
830
|
+
throw new SyntaxError('Request-side $removeheader is not compatible with $header');
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
if (hasRestrictedContentType || restrictedResourceTypesSet.size > 0) {
|
|
834
|
+
meta.baseModifierCount += 1;
|
|
835
|
+
}
|
|
836
|
+
meta.permittedContentTypeCount = permittedResourceTypesSet.size;
|
|
837
|
+
this.filterListId = filterListId;
|
|
838
|
+
this.index = index;
|
|
839
|
+
this.node = node;
|
|
840
|
+
this.pattern = pattern;
|
|
841
|
+
this.hash = fastHash(pattern);
|
|
842
|
+
this.allowlist = isAllowlist;
|
|
843
|
+
this.priority = RulePriority.calculate(meta, isAllowlist);
|
|
844
|
+
this.permittedResourceTypes = [
|
|
845
|
+
...permittedResourceTypesSet
|
|
846
|
+
];
|
|
847
|
+
this.restrictedResourceTypes = [
|
|
848
|
+
...restrictedResourceTypesSet
|
|
849
|
+
];
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
export { MASK_REGEX_RULE, REMOVEHEADER_COMPATIBLE_MODIFIERS, Rule };
|