@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,455 @@
|
|
|
1
|
+
import { NEGATION_MARKER } from '@adguard/agtree';
|
|
2
|
+
import { MASK_REGEX_RULE } from '../constants.js';
|
|
3
|
+
import { UnsupportedModifierError } from '../errors/conversion-errors/unsupported-modifier-error.js';
|
|
4
|
+
import { OPTION_NAMES } from './option-names.js';
|
|
5
|
+
import { REMOVEHEADER_COMPATIBLE_MODIFIERS } from './rule.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Class for validating network rules against DNR (declarative network request) constraints.
|
|
9
|
+
* Ported from tsurlfilter's `RuleDeclarativeValidator`.
|
|
10
|
+
*/ class RuleDeclarativeValidator {
|
|
11
|
+
/**
|
|
12
|
+
* Checks if the $removeparam value in the provided network rule
|
|
13
|
+
* is supported for conversion to MV3.
|
|
14
|
+
*
|
|
15
|
+
* @param rule Network rule.
|
|
16
|
+
* @param name Modifier's name.
|
|
17
|
+
*
|
|
18
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
19
|
+
*/ static checkRemoveParamModifierFn(rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
20
|
+
name) {
|
|
21
|
+
const value = rule.advancedModifierValue ?? '';
|
|
22
|
+
// $removeparam with negation (~) or regex (/) is not MV3-compatible
|
|
23
|
+
if (value.startsWith(NEGATION_MARKER) || value.startsWith(MASK_REGEX_RULE)) {
|
|
24
|
+
return new UnsupportedModifierError('Network rule with $removeparam modifier with negation or regexp is not supported', rule);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the provided rule is an allowlist rule.
|
|
30
|
+
*
|
|
31
|
+
* @param rule Network rule.
|
|
32
|
+
* @param name Modifier's name.
|
|
33
|
+
*
|
|
34
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
35
|
+
*/ static checkAllowRulesFn(rule, name) {
|
|
36
|
+
if (rule.allowlist) {
|
|
37
|
+
return new UnsupportedModifierError(`Network allowlist rule with ${name} modifier is not supported`, rule);
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks if the specified modifier is included in rule explicitly.
|
|
43
|
+
*
|
|
44
|
+
* @param rule Network rule.
|
|
45
|
+
* @param name Modifier's name.
|
|
46
|
+
*
|
|
47
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
48
|
+
*/ static checkHasModifierExplicitlyFn(rule, name) {
|
|
49
|
+
let nameToCheck = name;
|
|
50
|
+
// Remove leading dollar sign, if any
|
|
51
|
+
if (nameToCheck.startsWith('$')) {
|
|
52
|
+
nameToCheck = nameToCheck.slice(1);
|
|
53
|
+
}
|
|
54
|
+
if (rule.node.modifiers?.children.some((m)=>m.name.value === nameToCheck)) {
|
|
55
|
+
return new UnsupportedModifierError(`Network rule with explicitly enabled ${name} modifier is not supported`, rule);
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Checks if the $removeheader value in the provided network rule
|
|
61
|
+
* is supported for conversion to MV3.
|
|
62
|
+
*
|
|
63
|
+
* @param rule Network rule.
|
|
64
|
+
* @param name Modifier's name.
|
|
65
|
+
*
|
|
66
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
67
|
+
*/ static checkRemoveHeaderModifierFn(rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
|
+
name) {
|
|
69
|
+
const value = rule.advancedModifierValue;
|
|
70
|
+
// $removeheader without a value is valid
|
|
71
|
+
if (!value) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
// Check if the header resolves to a valid (non-forbidden) name
|
|
75
|
+
const hasValidResponseHeader = rule.getApplicableHeaderName(false) !== null;
|
|
76
|
+
const hasValidRequestHeader = rule.getApplicableHeaderName(true) !== null;
|
|
77
|
+
if (!hasValidResponseHeader && !hasValidRequestHeader) {
|
|
78
|
+
return new UnsupportedModifierError('Network rule with $removeheader modifier contains some of the unsupported headers', rule);
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks if the $removeheader modifier is combined only with modifiers
|
|
84
|
+
* that are compatible with it, aligned with tsurlfilter's
|
|
85
|
+
* `RemoveHeaderCompatibleOptions` and `validateRemoveHeaderRule`.
|
|
86
|
+
*
|
|
87
|
+
* Because dnr-converter stores modifiers differently from tsurlfilter, two
|
|
88
|
+
* kinds of incompatible modifiers must be detected:
|
|
89
|
+
*
|
|
90
|
+
* First, loop-reachable modifiers present in `rule.enabledModifiers`
|
|
91
|
+
* (e.g. `$method`, `$csp`, `$redirect`, `$cookie`, `$removeparam`,
|
|
92
|
+
* `$app`, `$network`, DNS modifiers). Any enabled modifier that is not
|
|
93
|
+
* in {@link REMOVEHEADER_COMPATIBLE_MODIFIERS} makes the rule
|
|
94
|
+
* incompatible. This mirrors tsurlfilter's
|
|
95
|
+
* `enabledOptions ⊆ RemoveHeaderCompatibleOptions`.
|
|
96
|
+
*
|
|
97
|
+
* Second, the `$to` modifier, which is parsed into `permittedToDomains` /
|
|
98
|
+
* `restrictedToDomains` and therefore never enters `enabledModifiers`,
|
|
99
|
+
* so it is invisible to the loop. Tsurlfilter still rejects it (it sets
|
|
100
|
+
* the `$to` flag, which is absent from
|
|
101
|
+
* `RemoveHeaderCompatibleOptions`), so it is rejected here explicitly
|
|
102
|
+
* for parity.
|
|
103
|
+
*
|
|
104
|
+
* `$domain` and `$denyallow` set no flag in tsurlfilter and are compatible
|
|
105
|
+
* with `$removeheader`, so they are intentionally NOT checked here.
|
|
106
|
+
*
|
|
107
|
+
* Parity relies on `rule.ts` keeping `$to` and `$domain` in separate fields
|
|
108
|
+
* — only `$to` (`permittedToDomains`/`restrictedToDomains`) is field-only
|
|
109
|
+
* and thus invisible to the `enabledModifiers` loop.
|
|
110
|
+
*
|
|
111
|
+
* @param rule Network rule.
|
|
112
|
+
* @param name Modifier's name.
|
|
113
|
+
*
|
|
114
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
115
|
+
*/ static checkRemoveHeaderCompatibleModifiersFn(rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
116
|
+
name) {
|
|
117
|
+
// Registered as a `customChecks` entry under `$removeheader`, so this is
|
|
118
|
+
// invoked exactly once per rule — when `$removeheader` is the currently
|
|
119
|
+
// iterated modifier — even though the body re-scans all
|
|
120
|
+
// `enabledModifiers`. `name` (the iterated modifier name) is unused
|
|
121
|
+
// here but required by the `customChecks` callback signature.
|
|
122
|
+
// 1. Loop-reachable incompatible modifiers.
|
|
123
|
+
for (const modifier of rule.enabledModifiers){
|
|
124
|
+
if (!REMOVEHEADER_COMPATIBLE_MODIFIERS.has(modifier)) {
|
|
125
|
+
return new UnsupportedModifierError(`$removeheader is not compatible with $${modifier}`, rule);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// 2. $to is field-only (invisible to the loop) but incompatible per tsurlfilter.
|
|
129
|
+
if (rule.permittedToDomains !== null || rule.restrictedToDomains !== null) {
|
|
130
|
+
return new UnsupportedModifierError('$removeheader is not compatible with $to', rule);
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Checks if the $method values in the provided network rule
|
|
136
|
+
* are supported for conversion to MV3.
|
|
137
|
+
*
|
|
138
|
+
* @param rule Network rule.
|
|
139
|
+
* @param name Modifier's name.
|
|
140
|
+
*
|
|
141
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
142
|
+
*/ static checkMethodModifierFn(rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
143
|
+
name) {
|
|
144
|
+
const { permittedMethods, restrictedMethods } = rule;
|
|
145
|
+
if (permittedMethods?.some((method)=>method === 'trace') || restrictedMethods?.some((method)=>method === 'trace')) {
|
|
146
|
+
return new UnsupportedModifierError("Network rule with $method modifier containing 'trace' method is not supported", rule);
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Checks if a network rule can be converted to a declarative format or not.
|
|
152
|
+
* We skip the following modifiers:
|
|
153
|
+
*
|
|
154
|
+
* All specific exceptions:
|
|
155
|
+
* $genericblock;
|
|
156
|
+
* $jsinject;
|
|
157
|
+
* $urlblock;
|
|
158
|
+
* $content;
|
|
159
|
+
* $extension;
|
|
160
|
+
* $stealth;
|
|
161
|
+
*
|
|
162
|
+
* Following specific exceptions are not required to be converted, but they
|
|
163
|
+
* are used in the cosmetic filtering:
|
|
164
|
+
* $elemhide;
|
|
165
|
+
* $generichide;
|
|
166
|
+
* $specifichide;
|
|
167
|
+
*
|
|
168
|
+
* Other:
|
|
169
|
+
* $header;
|
|
170
|
+
* $popup;
|
|
171
|
+
* $csp;
|
|
172
|
+
* $replace;
|
|
173
|
+
* $cookie - if modifier is not empty and contains any parameters;
|
|
174
|
+
* $redirect - if the rule is allowlist;
|
|
175
|
+
* $removeparam - if it contains a negation, or regexp,
|
|
176
|
+
* or the rule is allowlist;
|
|
177
|
+
* $removeheader - if it contains a title from a prohibited list
|
|
178
|
+
* (see FORBIDDEN_HEADERS in Rule);
|
|
179
|
+
* $jsonprune;
|
|
180
|
+
* $method - if the modifier contains 'trace' method;
|
|
181
|
+
* $hls;
|
|
182
|
+
* $permissions.
|
|
183
|
+
*
|
|
184
|
+
* @param rule Network rule.
|
|
185
|
+
*
|
|
186
|
+
* @returns Boolean flag - `false` if the rule does not require conversion
|
|
187
|
+
* and `true` if the rule is convertible.
|
|
188
|
+
*
|
|
189
|
+
* @throws Error with type {@link UnsupportedModifierError} if the rule is not convertible.
|
|
190
|
+
*/ static shouldConvertRule(rule) {
|
|
191
|
+
for (const modifier of rule.enabledModifiers){
|
|
192
|
+
const validator = this.optionsValidators[modifier];
|
|
193
|
+
if (!validator) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const { name, customChecks, skipConversion, notSupported } = validator;
|
|
197
|
+
if (notSupported) {
|
|
198
|
+
throw new UnsupportedModifierError(`Unsupported option "${name}"`, rule);
|
|
199
|
+
}
|
|
200
|
+
if (skipConversion) {
|
|
201
|
+
if (rule.isSingleModifierEnabled(modifier)) {
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (customChecks) {
|
|
207
|
+
for(let j = 0; j < customChecks.length; j += 1){
|
|
208
|
+
const err = customChecks[j](rule, name);
|
|
209
|
+
if (err !== null) {
|
|
210
|
+
throw err;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Checks if the $cookie values in the provided network rule
|
|
220
|
+
* are supported for conversion to MV3.
|
|
221
|
+
* Only $cookie without parameters is supported.
|
|
222
|
+
*
|
|
223
|
+
* @param rule Network rule.
|
|
224
|
+
* @param name Modifier's name.
|
|
225
|
+
*
|
|
226
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
227
|
+
*/ RuleDeclarativeValidator.checkCookieModifierFn = (rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
228
|
+
name)=>{
|
|
229
|
+
const value = rule.advancedModifierValue;
|
|
230
|
+
// $cookie with additional parameters is not supported
|
|
231
|
+
if (value !== null && value !== '') {
|
|
232
|
+
const msg = 'The use of additional parameters in $cookie (apart from $cookie itself) is not supported';
|
|
233
|
+
return new UnsupportedModifierError(msg, rule);
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* Checks if rule is a "document"-allowlist and contains all these
|
|
239
|
+
* `$elemhide,content,urlblock,jsinject` modifiers at the same time.
|
|
240
|
+
* If it is - we allow partial conversion of this rule, because `$content`
|
|
241
|
+
* is not supported in MV3 at all and `$jsinject` and `$urlblock`
|
|
242
|
+
* are not implemented yet, but we can support at least allowlist-rule
|
|
243
|
+
* with `$elemhide` modifier (not in the DNR, but with tsurlfilter engine).
|
|
244
|
+
*
|
|
245
|
+
* @param rule Network rule.
|
|
246
|
+
* @param name Modifier's name.
|
|
247
|
+
*
|
|
248
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
249
|
+
*/ // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
250
|
+
RuleDeclarativeValidator.checkDocumentAllowlistFn = (rule, name)=>{
|
|
251
|
+
if (rule.isFilteringDisabled()) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
return new UnsupportedModifierError(`Network rule with "${name}" modifier is not supported`, rule);
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* Checks if the $header values in the provided network rule
|
|
258
|
+
* are supported for conversion to MV3.
|
|
259
|
+
* DNR does not support regex patterns in HeaderInfo.values field.
|
|
260
|
+
*
|
|
261
|
+
* @param rule Network rule.
|
|
262
|
+
* @param name Modifier's name.
|
|
263
|
+
*
|
|
264
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
265
|
+
*/ RuleDeclarativeValidator.checkHeaderModifierFn = (rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
266
|
+
name)=>{
|
|
267
|
+
const { headerMatcher } = rule;
|
|
268
|
+
if (!headerMatcher) {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
// Check if value is a RegExp - not supported in DNR HeaderInfo
|
|
272
|
+
if (headerMatcher.isRegExp) {
|
|
273
|
+
return new UnsupportedModifierError('Declarative network rules with $header modifier cannot contain regex values', rule);
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* The $redirect-rule support will be possible to implement after browsers add this feature:
|
|
279
|
+
* https://github.com/w3c/webextensions/issues/493.
|
|
280
|
+
*
|
|
281
|
+
* @param rule Network rule.
|
|
282
|
+
* @param name Modifier's name.
|
|
283
|
+
*
|
|
284
|
+
* @returns Error {@link UnsupportedModifierError} or null if rule is supported.
|
|
285
|
+
*/ RuleDeclarativeValidator.checkRedirectModifierFn = (rule, // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
286
|
+
name)=>{
|
|
287
|
+
const isRedirectRule = rule.isModifierEnabled(OPTION_NAMES.REDIRECT) && rule.isRedirectRuleModifier;
|
|
288
|
+
if (!isRedirectRule) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
return new UnsupportedModifierError('Network rule with $redirect-rule modifier is not supported', rule);
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Maps each canonical modifier name to its validation configuration.
|
|
295
|
+
*/ RuleDeclarativeValidator.optionsValidators = {
|
|
296
|
+
// Supported
|
|
297
|
+
[OPTION_NAMES.THIRD_PARTY]: {
|
|
298
|
+
name: '$third-party'
|
|
299
|
+
},
|
|
300
|
+
[OPTION_NAMES.MATCH_CASE]: {
|
|
301
|
+
name: '$match-case'
|
|
302
|
+
},
|
|
303
|
+
[OPTION_NAMES.IMPORTANT]: {
|
|
304
|
+
name: '$important'
|
|
305
|
+
},
|
|
306
|
+
[OPTION_NAMES.TO]: {
|
|
307
|
+
name: '$to'
|
|
308
|
+
},
|
|
309
|
+
[OPTION_NAMES.BADFILTER]: {
|
|
310
|
+
name: '$badfilter'
|
|
311
|
+
},
|
|
312
|
+
[OPTION_NAMES.PERMISSIONS]: {
|
|
313
|
+
name: '$permissions'
|
|
314
|
+
},
|
|
315
|
+
// Supported without conversion.
|
|
316
|
+
[OPTION_NAMES.ELEMHIDE]: {
|
|
317
|
+
name: '$elemhide',
|
|
318
|
+
skipConversion: true
|
|
319
|
+
},
|
|
320
|
+
[OPTION_NAMES.GENERICHIDE]: {
|
|
321
|
+
name: '$generichide',
|
|
322
|
+
skipConversion: true
|
|
323
|
+
},
|
|
324
|
+
[OPTION_NAMES.SPECIFICHIDE]: {
|
|
325
|
+
name: '$specifichide',
|
|
326
|
+
skipConversion: true
|
|
327
|
+
},
|
|
328
|
+
// Partially supported.
|
|
329
|
+
[OPTION_NAMES.JSINJECT]: {
|
|
330
|
+
name: '$jsinject',
|
|
331
|
+
customChecks: [
|
|
332
|
+
RuleDeclarativeValidator.checkDocumentAllowlistFn
|
|
333
|
+
]
|
|
334
|
+
},
|
|
335
|
+
[OPTION_NAMES.URLBLOCK]: {
|
|
336
|
+
name: '$urlblock',
|
|
337
|
+
customChecks: [
|
|
338
|
+
RuleDeclarativeValidator.checkDocumentAllowlistFn
|
|
339
|
+
]
|
|
340
|
+
},
|
|
341
|
+
[OPTION_NAMES.CONTENT]: {
|
|
342
|
+
name: '$content',
|
|
343
|
+
customChecks: [
|
|
344
|
+
RuleDeclarativeValidator.checkDocumentAllowlistFn
|
|
345
|
+
]
|
|
346
|
+
},
|
|
347
|
+
// $popup is not supported in MV3, but rule with $all modifier includes $popup, so we should skip it.
|
|
348
|
+
[OPTION_NAMES.POPUP]: {
|
|
349
|
+
name: '$popup',
|
|
350
|
+
customChecks: [
|
|
351
|
+
RuleDeclarativeValidator.checkHasModifierExplicitlyFn
|
|
352
|
+
]
|
|
353
|
+
},
|
|
354
|
+
[OPTION_NAMES.CSP]: {
|
|
355
|
+
name: '$csp',
|
|
356
|
+
customChecks: [
|
|
357
|
+
RuleDeclarativeValidator.checkAllowRulesFn
|
|
358
|
+
]
|
|
359
|
+
},
|
|
360
|
+
[OPTION_NAMES.REDIRECT]: {
|
|
361
|
+
// $redirect and $redirect-rule modifiers are falling under this option
|
|
362
|
+
name: '$redirect',
|
|
363
|
+
customChecks: [
|
|
364
|
+
RuleDeclarativeValidator.checkAllowRulesFn,
|
|
365
|
+
RuleDeclarativeValidator.checkRedirectModifierFn
|
|
366
|
+
]
|
|
367
|
+
},
|
|
368
|
+
[OPTION_NAMES.REMOVEPARAM]: {
|
|
369
|
+
name: '$removeparam',
|
|
370
|
+
customChecks: [
|
|
371
|
+
RuleDeclarativeValidator.checkAllowRulesFn,
|
|
372
|
+
RuleDeclarativeValidator.checkRemoveParamModifierFn
|
|
373
|
+
]
|
|
374
|
+
},
|
|
375
|
+
[OPTION_NAMES.REMOVEHEADER]: {
|
|
376
|
+
name: '$removeheader',
|
|
377
|
+
customChecks: [
|
|
378
|
+
RuleDeclarativeValidator.checkAllowRulesFn,
|
|
379
|
+
RuleDeclarativeValidator.checkRemoveHeaderModifierFn,
|
|
380
|
+
RuleDeclarativeValidator.checkRemoveHeaderCompatibleModifiersFn
|
|
381
|
+
]
|
|
382
|
+
},
|
|
383
|
+
[OPTION_NAMES.COOKIE]: {
|
|
384
|
+
name: '$cookie',
|
|
385
|
+
customChecks: [
|
|
386
|
+
RuleDeclarativeValidator.checkAllowRulesFn,
|
|
387
|
+
RuleDeclarativeValidator.checkCookieModifierFn
|
|
388
|
+
]
|
|
389
|
+
},
|
|
390
|
+
[OPTION_NAMES.METHOD]: {
|
|
391
|
+
name: '$method',
|
|
392
|
+
customChecks: [
|
|
393
|
+
RuleDeclarativeValidator.checkMethodModifierFn
|
|
394
|
+
]
|
|
395
|
+
},
|
|
396
|
+
[OPTION_NAMES.HEADER]: {
|
|
397
|
+
name: '$header',
|
|
398
|
+
customChecks: [
|
|
399
|
+
RuleDeclarativeValidator.checkHeaderModifierFn
|
|
400
|
+
]
|
|
401
|
+
},
|
|
402
|
+
[OPTION_NAMES.URLTRANSFORM]: {
|
|
403
|
+
name: '$urltransform'
|
|
404
|
+
},
|
|
405
|
+
// Not supported yet.
|
|
406
|
+
[OPTION_NAMES.GENERICBLOCK]: {
|
|
407
|
+
name: '$genericblock',
|
|
408
|
+
notSupported: true
|
|
409
|
+
},
|
|
410
|
+
[OPTION_NAMES.STEALTH]: {
|
|
411
|
+
name: '$stealth',
|
|
412
|
+
notSupported: true
|
|
413
|
+
},
|
|
414
|
+
// Will not be supported.
|
|
415
|
+
[OPTION_NAMES.REPLACE]: {
|
|
416
|
+
name: '$replace',
|
|
417
|
+
notSupported: true
|
|
418
|
+
},
|
|
419
|
+
[OPTION_NAMES.JSONPRUNE]: {
|
|
420
|
+
name: '$jsonprune',
|
|
421
|
+
notSupported: true
|
|
422
|
+
},
|
|
423
|
+
[OPTION_NAMES.HLS]: {
|
|
424
|
+
name: '$hls',
|
|
425
|
+
notSupported: true
|
|
426
|
+
},
|
|
427
|
+
// DNS modifiers.
|
|
428
|
+
[OPTION_NAMES.CLIENT]: {
|
|
429
|
+
name: '$client',
|
|
430
|
+
notSupported: true
|
|
431
|
+
},
|
|
432
|
+
[OPTION_NAMES.DNSREWRITE]: {
|
|
433
|
+
name: '$dnsrewrite',
|
|
434
|
+
notSupported: true
|
|
435
|
+
},
|
|
436
|
+
[OPTION_NAMES.DNSTYPE]: {
|
|
437
|
+
name: '$dnstype',
|
|
438
|
+
notSupported: true
|
|
439
|
+
},
|
|
440
|
+
[OPTION_NAMES.CTAG]: {
|
|
441
|
+
name: '$ctag',
|
|
442
|
+
notSupported: true
|
|
443
|
+
},
|
|
444
|
+
// Desktop modifiers only.
|
|
445
|
+
[OPTION_NAMES.NETWORK]: {
|
|
446
|
+
name: '$network',
|
|
447
|
+
notSupported: true
|
|
448
|
+
},
|
|
449
|
+
[OPTION_NAMES.EXTENSION]: {
|
|
450
|
+
name: '$extension',
|
|
451
|
+
notSupported: true
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
export { RuleDeclarativeValidator };
|