@adguard/dnr-converter 2.0.0 → 2.1.1
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 +62 -3
- package/dist/cli.cjs +52148 -47533
- package/dist/constants.js +4 -1
- package/dist/errors/conversion-errors/index.js +3 -2
- package/dist/errors/conversion-errors/unexpected-conversion-error.js +23 -0
- package/dist/errors/scan-errors.js +39 -0
- package/dist/errors/unavailable-sources-errors/unavailable-filter-source-error.js +10 -1
- package/dist/filter-converter/filter-converter.js +151 -6
- package/dist/index.js +5 -1
- package/dist/package.json.js +1 -1
- package/dist/rule/rule-badfilter.js +144 -55
- package/dist/rule/rule-validator.js +56 -6
- package/dist/rule/rule.js +95 -9
- package/dist/rule-converters/action-kind.js +121 -0
- package/dist/rule-converters/csp-condition-coverage.js +268 -0
- package/dist/rule-converters/csp-converter.js +132 -11
- package/dist/rule-converters/csp-rules-resolver.js +417 -0
- package/dist/rule-converters/regular-rule-converter.js +489 -129
- package/dist/rule-converters/rules-converter.js +75 -7
- package/dist/rule-converters/rules-grouper.js +5 -3
- package/dist/rules-scanner/rules-scanner.js +124 -56
- package/dist/ruleset/ruleset-with-source-map.js +5 -1
- package/dist/types/src/errors/conversion-errors/index.d.ts +7 -2
- package/dist/types/src/errors/conversion-errors/unexpected-conversion-error.d.ts +21 -0
- package/dist/types/src/errors/scan-errors.d.ts +38 -0
- package/dist/types/src/errors/unavailable-sources-errors/unavailable-filter-source-error.d.ts +9 -1
- package/dist/types/src/filter-converter/filter-converter-options.d.ts +18 -1
- package/dist/types/src/filter-converter/filter-converter.d.ts +83 -6
- package/dist/types/src/index.d.ts +7 -3
- package/dist/types/src/rule/rule-validator.d.ts +21 -0
- package/dist/types/src/rule/rule.d.ts +52 -2
- package/dist/types/src/rule-converters/csp-converter.d.ts +12 -0
- package/dist/types/src/ruleset/ruleset-with-source-map.d.ts +14 -0
- package/dist/utils/string.js +27 -1
- package/package.json +6 -6
- package/dist/rule/string-utils.js +0 -49
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RuleGenerator } from '@adguard/agtree/generator';
|
|
2
|
+
import { ADBLOCK_URL_SEPARATOR, ADBLOCK_URL_START, RegExpUtils, ADBLOCK_URL_SEPARATOR_REGEX, ADBLOCK_URL_START_REGEX } from '@adguard/agtree/utils';
|
|
2
3
|
import { getRedirectFilename } from '@adguard/scriptlets/redirects';
|
|
3
4
|
import { MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, CSP_HEADER_NAME, PERMISSIONS_POLICY_HEADER_NAME, DOT } from '../constants.js';
|
|
4
5
|
import { POPULAR_TLDS } from '../constants/popular-tlds.js';
|
|
@@ -8,14 +9,16 @@ import { re2Validator } from '../re2-regexp/re2-validator.js';
|
|
|
8
9
|
import { OPTION_NAMES } from '../rule/option-names.js';
|
|
9
10
|
import { RuleDeclarativeValidator } from '../rule/rule-validator.js';
|
|
10
11
|
import { getErrorMessage } from '../utils/error.js';
|
|
11
|
-
import { isRegexPattern, toASCII,
|
|
12
|
+
import { encodeNonAscii, prepareASCII, isRegexPattern, toASCII, removeSlashes } from '../utils/string.js';
|
|
13
|
+
import { selectActionKinds, ActionKind } from './action-kind.js';
|
|
12
14
|
import { UnsupportedModifierError } from '../errors/conversion-errors/unsupported-modifier-error.js';
|
|
13
15
|
import { HeaderOperation } from '../declarative-rule/modify-header-info.js';
|
|
14
16
|
import { RuleActionType } from '../declarative-rule/rule-action.js';
|
|
15
17
|
import { DomainType, ResourceType } from '../declarative-rule/rule-condition.js';
|
|
18
|
+
import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-regexp-error.js';
|
|
16
19
|
import { EmptyResourcesError } from '../errors/conversion-errors/empty-resources-error.js';
|
|
17
20
|
import { EmptyDomainsError } from '../errors/conversion-errors/empty-domains-error.js';
|
|
18
|
-
import {
|
|
21
|
+
import { UnexpectedConversionError } from '../errors/conversion-errors/unexpected-conversion-error.js';
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* @typedef {import('../errors/conversion-errors').UnsupportedModifierError} UnsupportedModifierError
|
|
@@ -38,6 +41,64 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
38
41
|
*/ static isWildcardTldDomain(domain) {
|
|
39
42
|
return domain.length > RegularRuleConverter.WILDCARD_TLD.length && domain.endsWith(RegularRuleConverter.WILDCARD_TLD);
|
|
40
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Converts a URL filter to an ASCII-only string.
|
|
46
|
+
*
|
|
47
|
+
* The hostname is converted to punycode, while the rest of the pattern is
|
|
48
|
+
* percent-encoded: this is how DNR matches it against a request URL. An
|
|
49
|
+
* explicit port belongs to the rest of the pattern, not to the hostname.
|
|
50
|
+
*
|
|
51
|
+
* The hostname is the leading part of the pattern after an optional domain
|
|
52
|
+
* anchor, scheme and `*`/`*.` wildcard. A leading part without a dot is
|
|
53
|
+
* percent-encoded as a path fragment, unless the pattern is anchored to a
|
|
54
|
+
* domain or a scheme, where the hostname is guaranteed.
|
|
55
|
+
*
|
|
56
|
+
* @param pattern URL filter pattern.
|
|
57
|
+
* @param isCaseSensitive Whether the pattern is matched case-sensitively.
|
|
58
|
+
*
|
|
59
|
+
* @returns URL filter pattern with non-ASCII characters converted.
|
|
60
|
+
*/ static prepareUrlFilter(pattern, isCaseSensitive) {
|
|
61
|
+
const domainAnchor = pattern.startsWith('||') ? '||' : '';
|
|
62
|
+
let rest = pattern.substring(domainAnchor.length);
|
|
63
|
+
const schemeMatch = /^\|?[a-z][a-z0-9+.-]*:\/\//i.exec(rest);
|
|
64
|
+
const scheme = schemeMatch ? schemeMatch[0] : '';
|
|
65
|
+
rest = rest.substring(scheme.length);
|
|
66
|
+
const wildcardMatch = /^\*\.?/.exec(rest);
|
|
67
|
+
const wildcard = wildcardMatch ? wildcardMatch[0] : '';
|
|
68
|
+
rest = rest.substring(wildcard.length);
|
|
69
|
+
// A `*` inside an anchored hostname is a wildcard, not a separator, so
|
|
70
|
+
// a pattern like `||foo*.пример.рф^` keeps the whole hostname. An
|
|
71
|
+
// unanchored pattern has no hostname before the `*`: the leading part
|
|
72
|
+
// is a path fragment there.
|
|
73
|
+
const hasAnchor = domainAnchor !== '' || scheme !== '';
|
|
74
|
+
const hostnameEndIndex = rest.search(hasAnchor ? /[\^/|?:]/ : /[*^/|?:]/);
|
|
75
|
+
const hostname = hostnameEndIndex === -1 ? rest : rest.substring(0, hostnameEndIndex);
|
|
76
|
+
const suffix = hostnameEndIndex === -1 ? '' : rest.substring(hostnameEndIndex);
|
|
77
|
+
const isHostname = hasAnchor || hostname.includes('.');
|
|
78
|
+
const preparedHostname = isHostname ? RegularRuleConverter.prepareHostname(hostname, isCaseSensitive) : encodeNonAscii(hostname);
|
|
79
|
+
return `${domainAnchor}${scheme}${wildcard}${preparedHostname}${encodeNonAscii(suffix)}`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Converts a hostname, possibly containing `*` wildcards, to ASCII.
|
|
83
|
+
*
|
|
84
|
+
* The parts between the wildcards are converted separately: punycode
|
|
85
|
+
* cannot encode a `*`, so converting the whole string would fold it into a
|
|
86
|
+
* label and the rule would never match.
|
|
87
|
+
*
|
|
88
|
+
* The hostname is lowercased, because a host in a URL is always
|
|
89
|
+
* lowercased and punycode keeps the case of non-ASCII letters, so
|
|
90
|
+
* `ПРИМЕР.рф` would otherwise be converted to a label which never matches.
|
|
91
|
+
* The case is kept for a `$match-case` rule, which asks for a
|
|
92
|
+
* case-sensitive comparison.
|
|
93
|
+
*
|
|
94
|
+
* @param hostname Hostname with optional `*` wildcards.
|
|
95
|
+
* @param isCaseSensitive Whether the pattern is matched case-sensitively.
|
|
96
|
+
*
|
|
97
|
+
* @returns ASCII hostname.
|
|
98
|
+
*/ static prepareHostname(hostname, isCaseSensitive) {
|
|
99
|
+
const prepared = isCaseSensitive ? hostname : hostname.toLowerCase();
|
|
100
|
+
return prepared.split('*').map((part)=>part === '' ? part : prepareASCII(part)).join('*');
|
|
101
|
+
}
|
|
41
102
|
/**
|
|
42
103
|
* Expands wildcard TLD domain into concrete popular TLD domains.
|
|
43
104
|
*
|
|
@@ -48,6 +109,16 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
48
109
|
const domainPrefix = domain.slice(0, -RegularRuleConverter.WILDCARD_TLD.length);
|
|
49
110
|
return POPULAR_TLDS.map((tld)=>`${domainPrefix}.${tld}`);
|
|
50
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether a domain from a network rule modifier can be represented
|
|
114
|
+
* in a DNR condition.
|
|
115
|
+
*
|
|
116
|
+
* @param domain Domain to check.
|
|
117
|
+
*
|
|
118
|
+
* @returns `true` if DNR supports the domain.
|
|
119
|
+
*/ static isRepresentableDomain(domain) {
|
|
120
|
+
return !isRegexPattern(domain) && (!domain.includes(MASK_ANY_CHARACTER) || RegularRuleConverter.isWildcardTldDomain(domain));
|
|
121
|
+
}
|
|
51
122
|
/**
|
|
52
123
|
* Prepares domains for DNR condition arrays.
|
|
53
124
|
*
|
|
@@ -55,27 +126,79 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
55
126
|
* dropped; wildcard TLD domains (e.g. `example.*`) are expanded to the
|
|
56
127
|
* popular TLD list.
|
|
57
128
|
*
|
|
129
|
+
* Domains are lowercased before the punycode conversion: punycode keeps the
|
|
130
|
+
* case of non-ASCII letters, so `ПРИМЕР.рф` and `пример.рф` would otherwise
|
|
131
|
+
* produce different `xn--` labels. A host in a URL is always lowercased, so
|
|
132
|
+
* the case written in a modifier carries no meaning, while the condition
|
|
133
|
+
* arrays are also compared with each other by the CSP resolver.
|
|
134
|
+
*
|
|
58
135
|
* @param domains Domains from network rule modifier.
|
|
59
136
|
*
|
|
60
|
-
* @returns ASCII-only unique domains supported by DNR.
|
|
137
|
+
* @returns Lowercased ASCII-only unique domains supported by DNR.
|
|
61
138
|
*/ static prepareDeclarativeDomains(domains) {
|
|
62
139
|
const expandedDomains = domains?.flatMap((domain)=>{
|
|
63
|
-
if (
|
|
140
|
+
if (!RegularRuleConverter.isRepresentableDomain(domain)) {
|
|
64
141
|
return [];
|
|
65
142
|
}
|
|
66
143
|
if (RegularRuleConverter.isWildcardTldDomain(domain)) {
|
|
67
144
|
return RegularRuleConverter.expandWildcardTldDomain(domain);
|
|
68
145
|
}
|
|
69
|
-
if (domain.includes(MASK_ANY_CHARACTER)) {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
146
|
return [
|
|
73
147
|
domain
|
|
74
148
|
];
|
|
75
149
|
}) ?? [];
|
|
76
150
|
return [
|
|
77
|
-
...new Set(toASCII(expandedDomains))
|
|
151
|
+
...new Set(toASCII(expandedDomains.map((domain)=>domain.toLowerCase())))
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Checks whether all domain conditions can be represented in DNR.
|
|
156
|
+
*
|
|
157
|
+
* A port cannot appear in a DNR domain list, so a `$domain` entry written
|
|
158
|
+
* as `example.com:8080` is not a domain which drops a part of the
|
|
159
|
+
* condition: it has no valid declarative form at all.
|
|
160
|
+
*
|
|
161
|
+
* @param rule Source rule.
|
|
162
|
+
*
|
|
163
|
+
* @returns `true` if conversion does not drop any domain condition.
|
|
164
|
+
*/ static areDomainConditionsRepresentable(rule) {
|
|
165
|
+
const domainLists = [
|
|
166
|
+
rule.permittedDomains,
|
|
167
|
+
rule.restrictedDomains,
|
|
168
|
+
rule.permittedToDomains,
|
|
169
|
+
rule.restrictedToDomains,
|
|
170
|
+
rule.denyAllowDomains
|
|
78
171
|
];
|
|
172
|
+
return domainLists.every((domains)=>domains?.every((domain)=>!isRegexPattern(domain) && !domain.includes(RegularRuleConverter.PORT_SEPARATOR) && (!domain.includes(MASK_ANY_CHARACTER) || RegularRuleConverter.isWildcardTldDomain(domain))) ?? true);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Checks whether a condition keeps every restriction of the rule.
|
|
176
|
+
*
|
|
177
|
+
* {@link RegularRuleConverter.getCondition} stores one inclusion list per
|
|
178
|
+
* field, so a rule which combines an inclusion with an exclusion in the
|
|
179
|
+
* same field loses the inclusion: `$csp,script,~image` keeps only `~image`,
|
|
180
|
+
* and `$to=~a.com,denyallow=b.com` keeps only `$denyallow`. A CSP exception
|
|
181
|
+
* would then cancel more than written, so it must not be applied.
|
|
182
|
+
*
|
|
183
|
+
* @param rule Source rule.
|
|
184
|
+
*
|
|
185
|
+
* @returns `true` if the condition keeps all restrictions of the rule.
|
|
186
|
+
*/ static areConditionRestrictionsRepresentable(rule) {
|
|
187
|
+
const hasPermittedResourceTypes = rule.permittedResourceTypes.length !== 0;
|
|
188
|
+
const hasRestrictedResourceTypes = rule.restrictedResourceTypes.length !== 0;
|
|
189
|
+
const hasRestrictedToDomains = (rule.restrictedToDomains?.length ?? 0) !== 0;
|
|
190
|
+
const hasDenyAllowDomains = (rule.denyAllowDomains?.length ?? 0) !== 0;
|
|
191
|
+
return !(hasPermittedResourceTypes && hasRestrictedResourceTypes) && !(hasRestrictedToDomains && hasDenyAllowDomains);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Checks whether every domain from a network rule modifier can be
|
|
195
|
+
* represented in a DNR condition.
|
|
196
|
+
*
|
|
197
|
+
* @param domains Domains from network rule modifier.
|
|
198
|
+
*
|
|
199
|
+
* @returns `true` if every domain is supported by DNR.
|
|
200
|
+
*/ static areDomainsRepresentable(domains) {
|
|
201
|
+
return domains?.every((domain)=>RegularRuleConverter.isRepresentableDomain(domain)) ?? true;
|
|
79
202
|
}
|
|
80
203
|
/**
|
|
81
204
|
* Checks if {@link Rule} can be converted to {@link RuleActionType.AllowAllRequests}.
|
|
@@ -185,6 +308,9 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
185
308
|
* - value cannot be URI-decoded.
|
|
186
309
|
* - param name contains urlFilter special characters (`|`, `*`, `^`).
|
|
187
310
|
*
|
|
311
|
+
* Non-ASCII characters in the param name are percent-encoded, because
|
|
312
|
+
* the token is matched against a url where they are encoded as well.
|
|
313
|
+
*
|
|
188
314
|
* @param rule Rule with $removeparam modifier enabled.
|
|
189
315
|
*
|
|
190
316
|
* @returns A urlFilter token string, or null if augmentation should be skipped.
|
|
@@ -209,7 +335,8 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
209
335
|
if (RegularRuleConverter.URL_FILTER_SPECIAL_CHARS.test(decoded)) {
|
|
210
336
|
return null;
|
|
211
337
|
}
|
|
212
|
-
|
|
338
|
+
// The token is matched against the url, where non-ASCII characters are percent-encoded.
|
|
339
|
+
return `^${encodeNonAscii(decoded)}=`;
|
|
213
340
|
}
|
|
214
341
|
/**
|
|
215
342
|
* Returns rule modify headers action.
|
|
@@ -281,7 +408,7 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
281
408
|
if (!rule.isModifierEnabled(OPTION_NAMES.CSP)) {
|
|
282
409
|
return null;
|
|
283
410
|
}
|
|
284
|
-
const cspHeaderValue = rule.
|
|
411
|
+
const cspHeaderValue = rule.cspDirective;
|
|
285
412
|
if (!cspHeaderValue) {
|
|
286
413
|
return null;
|
|
287
414
|
}
|
|
@@ -321,93 +448,127 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
321
448
|
* @throws Error {@link ResourcesPathError} when specified an empty path to the web accessible resources.
|
|
322
449
|
* @throws Error {@link UnsupportedModifierError} when a `$removeparam` value cannot be URI-decoded.
|
|
323
450
|
*/ getAction(rule) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
return {
|
|
331
|
-
type: RuleActionType.Allow
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
if (rule.isModifierEnabled(OPTION_NAMES.REDIRECT)) {
|
|
335
|
-
const redirectAction = this.getRedirectAction(rule);
|
|
336
|
-
if (redirectAction) {
|
|
337
|
-
return {
|
|
338
|
-
type: RuleActionType.Redirect,
|
|
339
|
-
redirect: redirectAction
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
if (rule.isModifierEnabled(OPTION_NAMES.REMOVEPARAM)) {
|
|
344
|
-
const removeParamRedirectAction = RegularRuleConverter.getRemoveParamRedirectAction(rule);
|
|
345
|
-
if (removeParamRedirectAction) {
|
|
346
|
-
return {
|
|
347
|
-
type: RuleActionType.Redirect,
|
|
348
|
-
redirect: removeParamRedirectAction
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
if (rule.isModifierEnabled(OPTION_NAMES.REMOVEHEADER)) {
|
|
353
|
-
const modifyHeadersAction = RegularRuleConverter.getModifyHeadersAction(rule);
|
|
354
|
-
if (modifyHeadersAction?.requestHeaders) {
|
|
355
|
-
return {
|
|
356
|
-
type: RuleActionType.ModifyHeaders,
|
|
357
|
-
requestHeaders: modifyHeadersAction.requestHeaders
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
if (modifyHeadersAction?.responseHeaders) {
|
|
361
|
-
return {
|
|
362
|
-
type: RuleActionType.ModifyHeaders,
|
|
363
|
-
responseHeaders: modifyHeadersAction.responseHeaders
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
if (rule.isModifierEnabled(OPTION_NAMES.CSP)) {
|
|
368
|
-
const headersAction = RegularRuleConverter.getAddingCspHeadersAction(rule);
|
|
369
|
-
if (headersAction) {
|
|
370
|
-
return {
|
|
371
|
-
type: RuleActionType.ModifyHeaders,
|
|
372
|
-
responseHeaders: [
|
|
373
|
-
headersAction
|
|
374
|
-
]
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
if (rule.isModifierEnabled(OPTION_NAMES.PERMISSIONS)) {
|
|
379
|
-
const headersAction = RegularRuleConverter.getAddingPermissionsHeadersAction(rule);
|
|
380
|
-
if (headersAction) {
|
|
381
|
-
return {
|
|
382
|
-
type: RuleActionType.ModifyHeaders,
|
|
383
|
-
responseHeaders: [
|
|
384
|
-
headersAction
|
|
385
|
-
]
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
if (rule.isModifierEnabled(OPTION_NAMES.COOKIE)) {
|
|
390
|
-
const removeCookieHeaders = RegularRuleConverter.getRemovingCookieHeadersAction(rule);
|
|
391
|
-
if (removeCookieHeaders) {
|
|
392
|
-
const { responseHeaders, requestHeaders } = removeCookieHeaders;
|
|
393
|
-
return {
|
|
394
|
-
type: RuleActionType.ModifyHeaders,
|
|
395
|
-
responseHeaders,
|
|
396
|
-
requestHeaders
|
|
397
|
-
};
|
|
451
|
+
const kinds = selectActionKinds(rule);
|
|
452
|
+
for(let i = 0; i < kinds.length; i += 1){
|
|
453
|
+
const action = this.buildAction(rule, kinds[i]);
|
|
454
|
+
if (action) {
|
|
455
|
+
return action;
|
|
398
456
|
}
|
|
399
457
|
}
|
|
400
458
|
return {
|
|
401
459
|
type: RuleActionType.Block
|
|
402
460
|
};
|
|
403
461
|
}
|
|
462
|
+
/**
|
|
463
|
+
* Builds the action of the given kind for a rule.
|
|
464
|
+
*
|
|
465
|
+
* @param rule {@link Rule} to build an action for.
|
|
466
|
+
* @param kind Action kind to build.
|
|
467
|
+
*
|
|
468
|
+
* @returns Action or `null` when the modifier cannot produce it, so the
|
|
469
|
+
* next kind in {@link selectActionKinds} is used.
|
|
470
|
+
*/ buildAction(rule, kind) {
|
|
471
|
+
switch(kind){
|
|
472
|
+
case ActionKind.Allow:
|
|
473
|
+
{
|
|
474
|
+
if (rule.isFilteringDisabled() && RegularRuleConverter.isCompatibleWithAllowAllRequests(rule)) {
|
|
475
|
+
return {
|
|
476
|
+
type: RuleActionType.AllowAllRequests
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
type: RuleActionType.Allow
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
case ActionKind.Redirect:
|
|
484
|
+
{
|
|
485
|
+
const redirectAction = this.getRedirectAction(rule);
|
|
486
|
+
return redirectAction ? {
|
|
487
|
+
type: RuleActionType.Redirect,
|
|
488
|
+
redirect: redirectAction
|
|
489
|
+
} : null;
|
|
490
|
+
}
|
|
491
|
+
case ActionKind.RemoveParamRedirect:
|
|
492
|
+
{
|
|
493
|
+
const removeParamRedirectAction = RegularRuleConverter.getRemoveParamRedirectAction(rule);
|
|
494
|
+
return removeParamRedirectAction ? {
|
|
495
|
+
type: RuleActionType.Redirect,
|
|
496
|
+
redirect: removeParamRedirectAction
|
|
497
|
+
} : null;
|
|
498
|
+
}
|
|
499
|
+
case ActionKind.RemoveHeader:
|
|
500
|
+
{
|
|
501
|
+
const modifyHeadersAction = RegularRuleConverter.getModifyHeadersAction(rule);
|
|
502
|
+
if (modifyHeadersAction?.requestHeaders) {
|
|
503
|
+
return {
|
|
504
|
+
type: RuleActionType.ModifyHeaders,
|
|
505
|
+
requestHeaders: modifyHeadersAction.requestHeaders
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
if (modifyHeadersAction?.responseHeaders) {
|
|
509
|
+
return {
|
|
510
|
+
type: RuleActionType.ModifyHeaders,
|
|
511
|
+
responseHeaders: modifyHeadersAction.responseHeaders
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
case ActionKind.Csp:
|
|
517
|
+
{
|
|
518
|
+
const headersAction = RegularRuleConverter.getAddingCspHeadersAction(rule);
|
|
519
|
+
return headersAction ? {
|
|
520
|
+
type: RuleActionType.ModifyHeaders,
|
|
521
|
+
responseHeaders: [
|
|
522
|
+
headersAction
|
|
523
|
+
]
|
|
524
|
+
} : null;
|
|
525
|
+
}
|
|
526
|
+
case ActionKind.Permissions:
|
|
527
|
+
{
|
|
528
|
+
const headersAction = RegularRuleConverter.getAddingPermissionsHeadersAction(rule);
|
|
529
|
+
return headersAction ? {
|
|
530
|
+
type: RuleActionType.ModifyHeaders,
|
|
531
|
+
responseHeaders: [
|
|
532
|
+
headersAction
|
|
533
|
+
]
|
|
534
|
+
} : null;
|
|
535
|
+
}
|
|
536
|
+
case ActionKind.Cookie:
|
|
537
|
+
{
|
|
538
|
+
const removeCookieHeaders = RegularRuleConverter.getRemovingCookieHeadersAction(rule);
|
|
539
|
+
if (!removeCookieHeaders) {
|
|
540
|
+
return null;
|
|
541
|
+
}
|
|
542
|
+
const { responseHeaders, requestHeaders } = removeCookieHeaders;
|
|
543
|
+
return {
|
|
544
|
+
type: RuleActionType.ModifyHeaders,
|
|
545
|
+
responseHeaders,
|
|
546
|
+
requestHeaders
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
case ActionKind.Block:
|
|
550
|
+
{
|
|
551
|
+
return {
|
|
552
|
+
type: RuleActionType.Block
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
default:
|
|
556
|
+
{
|
|
557
|
+
// A new action kind must be handled explicitly instead of
|
|
558
|
+
// silently becoming a blocking rule.
|
|
559
|
+
const unknownKind = kind;
|
|
560
|
+
throw new Error(`Unhandled action kind: ${unknownKind}`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
404
564
|
/**
|
|
405
565
|
* Retrieves the condition for the provided {@link Rule}.
|
|
406
566
|
*
|
|
407
567
|
* @param rule {@link Rule} to get condition for.
|
|
568
|
+
* @param useRemoveParamRegex Whether to use a regexp for eligible named parameter removal.
|
|
408
569
|
*
|
|
409
570
|
* @returns A rule condition that describes to which request the declarative rule should be applied.
|
|
410
|
-
*/ static getCondition(rule) {
|
|
571
|
+
*/ static getCondition(rule, useRemoveParamRegex = true) {
|
|
411
572
|
const condition = {};
|
|
412
573
|
// set `urlFilter` or `regexFilter` depending on the pattern type
|
|
413
574
|
const { pattern } = rule;
|
|
@@ -417,19 +578,46 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
417
578
|
} else {
|
|
418
579
|
// A pattern beginning with ||* is not allowed. Use * instead.
|
|
419
580
|
const patternWithoutVerticals = pattern.startsWith('||*') ? pattern.substring(2) : pattern;
|
|
420
|
-
condition.urlFilter =
|
|
581
|
+
condition.urlFilter = RegularRuleConverter.prepareUrlFilter(patternWithoutVerticals, rule.isModifierEnabled(OPTION_NAMES.MATCH_CASE));
|
|
421
582
|
}
|
|
422
583
|
}
|
|
423
|
-
// For $removeparam rules with a specific named parameter,
|
|
424
|
-
// a
|
|
425
|
-
// when the target parameter is present in the URL query string.
|
|
584
|
+
// For $removeparam rules with a specific named parameter, add
|
|
585
|
+
// a parameter-aware URL condition that stops matching after removal.
|
|
426
586
|
// This enables Chrome DNR to chain multiple redirect hops,
|
|
427
587
|
// stripping one parameter per hop until all are removed.
|
|
428
588
|
if (rule.isModifierEnabled(OPTION_NAMES.REMOVEPARAM)) {
|
|
429
589
|
const paramToken = RegularRuleConverter.getRemoveParamToken(rule);
|
|
430
590
|
if (paramToken !== null) {
|
|
431
591
|
if (condition.urlFilter) {
|
|
432
|
-
condition.urlFilter
|
|
592
|
+
const prefix = condition.urlFilter.slice(0, -ADBLOCK_URL_SEPARATOR.length);
|
|
593
|
+
const hasDomainAnchor = prefix.startsWith(ADBLOCK_URL_START);
|
|
594
|
+
const authority = hasDomainAnchor ? prefix.slice(ADBLOCK_URL_START.length).split('/')[0] : '';
|
|
595
|
+
// The trailing separator can take the query delimiter only when the pattern ends
|
|
596
|
+
// inside the path. After a bare host it matches the `/` or the port colon,
|
|
597
|
+
// so the native URL filter already finds the first parameter there.
|
|
598
|
+
const hasPath = prefix.replace(RegularRuleConverter.ANCHORED_PATTERN_START, '').includes('/');
|
|
599
|
+
if (useRemoveParamRegex && condition.urlFilter.endsWith(ADBLOCK_URL_SEPARATOR) && prefix.startsWith('|') && hasPath && !/[*?#^]/.test(prefix) && (!hasDomainAnchor || authority.length > 0 && !authority.includes('@'))) {
|
|
600
|
+
// Share the trailing separator with the query delimiter when the parameter is first.
|
|
601
|
+
// Anchoring keeps this delimiter at the actual query boundary.
|
|
602
|
+
// Unanchored patterns can match inside the query and retain native URL-filter matching.
|
|
603
|
+
// Patterns without a path keep it too: a filter list has thousands of them,
|
|
604
|
+
// and DNR allows only 1000 regexp rules.
|
|
605
|
+
const patternRegexp = RegExpUtils.patternToRegexp(condition.urlFilter);
|
|
606
|
+
let prefixRegexp = patternRegexp.slice(0, -ADBLOCK_URL_SEPARATOR_REGEX.length);
|
|
607
|
+
if (hasDomainAnchor) {
|
|
608
|
+
// Domain anchors start at the parsed host, after any credentials, on any URL scheme.
|
|
609
|
+
prefixRegexp = prefixRegexp.replace(ADBLOCK_URL_START_REGEX, '^[a-z][a-z0-9+.-]*://(?:[^[:^ascii:]/?#@]*@)?(?:[^[:^ascii:]/?#@]*\\.)?');
|
|
610
|
+
}
|
|
611
|
+
const paramRegexp = RegExpUtils.patternToRegexp(paramToken);
|
|
612
|
+
const paramNameRegexp = paramRegexp.slice(ADBLOCK_URL_SEPARATOR_REGEX.length);
|
|
613
|
+
// DNR matches ASCII-serialized URLs. Bound classes to ASCII to keep RE2 compilation small.
|
|
614
|
+
const pathSeparator = '[^[:^ascii:] a-zA-Z0-9.%_?#-]';
|
|
615
|
+
const pathTail = `(?:${pathSeparator}[^[:^ascii:]?#]*)?`;
|
|
616
|
+
condition.regexFilter = `${prefixRegexp}${pathTail}` + `\\?(?:[^[:^ascii:]#]*&)?${paramNameRegexp}`;
|
|
617
|
+
delete condition.urlFilter;
|
|
618
|
+
} else {
|
|
619
|
+
condition.urlFilter += `*${paramToken}`;
|
|
620
|
+
}
|
|
433
621
|
} else if (!condition.regexFilter) {
|
|
434
622
|
condition.urlFilter = paramToken;
|
|
435
623
|
}
|
|
@@ -595,12 +783,137 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
595
783
|
// Set calculated priority
|
|
596
784
|
declarativeRule.priority = rule.priority;
|
|
597
785
|
// Validate created declarative rule and throw error if not valid
|
|
598
|
-
|
|
786
|
+
let conversionErr = await RegularRuleConverter.checkRuleApplication(rule, declarativeRule);
|
|
787
|
+
if (conversionErr instanceof UnsupportedRegexpError && !rule.isRegexRule() && rule.isModifierEnabled(OPTION_NAMES.REMOVEPARAM)) {
|
|
788
|
+
// Preserve native URL-filter matching when the generated parameter-aware regexp is unsupported.
|
|
789
|
+
declarativeRule.condition = RegularRuleConverter.getCondition(rule, false);
|
|
790
|
+
conversionErr = await RegularRuleConverter.checkRuleApplication(rule, declarativeRule);
|
|
791
|
+
}
|
|
599
792
|
if (conversionErr) {
|
|
600
793
|
throw conversionErr;
|
|
601
794
|
}
|
|
602
795
|
return declarativeRule;
|
|
603
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* Checks whether {@link convertRules} builds the additional `main_frame`
|
|
799
|
+
* rule for a rule, see {@link buildTargetDomainRule}.
|
|
800
|
+
*
|
|
801
|
+
* @param rule Source {@link Rule}.
|
|
802
|
+
*
|
|
803
|
+
* @returns `true` if the additional rule is built.
|
|
804
|
+
*/ static hasTargetDomainRule(rule) {
|
|
805
|
+
return RegularRuleConverter.getTargetDomainCondition(rule, {
|
|
806
|
+
id: 0,
|
|
807
|
+
action: {
|
|
808
|
+
type: RuleActionType.Block
|
|
809
|
+
},
|
|
810
|
+
condition: RegularRuleConverter.getCondition(rule)
|
|
811
|
+
}) !== null;
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* Builds the additional `main_frame` rule when the `$domain` modifier
|
|
815
|
+
* must also match the target hostname of a document request.
|
|
816
|
+
*
|
|
817
|
+
* Chrome matches `initiatorDomains` against the request initiator only,
|
|
818
|
+
* and a document request started by the browser (new tab page, address
|
|
819
|
+
* bar, bookmark, reload) has no initiator. AdGuard matches `$domain`
|
|
820
|
+
* against the target hostname for document requests when the pattern is
|
|
821
|
+
* not domain-specific, or when the `$domain` list contains only excluded
|
|
822
|
+
* domains, so the additional rule keeps the same behavior.
|
|
823
|
+
*
|
|
824
|
+
* @param rule Source {@link Rule}.
|
|
825
|
+
* @param declarativeRule Converted {@link DeclarativeRule}.
|
|
826
|
+
* @param usedIds Set with already used IDs to exclude duplications in IDs.
|
|
827
|
+
*
|
|
828
|
+
* @returns Additional {@link DeclarativeRule}, or `null` when the
|
|
829
|
+
* additional rule is not needed.
|
|
830
|
+
*/ static buildTargetDomainRule(rule, declarativeRule, usedIds) {
|
|
831
|
+
const targetDomainCondition = RegularRuleConverter.getTargetDomainCondition(rule, declarativeRule);
|
|
832
|
+
if (!targetDomainCondition) {
|
|
833
|
+
return null;
|
|
834
|
+
}
|
|
835
|
+
return {
|
|
836
|
+
id: RegularRuleConverter.generateId(rule, usedIds),
|
|
837
|
+
action: JSON.parse(JSON.stringify(declarativeRule.action)),
|
|
838
|
+
condition: targetDomainCondition,
|
|
839
|
+
priority: declarativeRule.priority
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
/**
|
|
843
|
+
* Builds the condition of the additional `main_frame` rule that matches
|
|
844
|
+
* the `$domain` modifier against the request URL hostname.
|
|
845
|
+
*
|
|
846
|
+
* Note: for a `$domain` list with only excluded domains, the original
|
|
847
|
+
* rule still matches a browser-initiated document request to an excluded
|
|
848
|
+
* domain, because DNR has no condition for a missing initiator. AdGuard
|
|
849
|
+
* does not match that request.
|
|
850
|
+
*
|
|
851
|
+
* @param rule Source {@link Rule}.
|
|
852
|
+
* @param declarativeRule Converted {@link DeclarativeRule}.
|
|
853
|
+
*
|
|
854
|
+
* @returns Condition for the additional rule, or `null` when the
|
|
855
|
+
* additional rule is not needed.
|
|
856
|
+
*/ static getTargetDomainCondition(rule, declarativeRule) {
|
|
857
|
+
/**
|
|
858
|
+
* DNR cannot express "first party or no initiator". Chrome treats a
|
|
859
|
+
* request without an initiator as third-party, while MV2 treats it as
|
|
860
|
+
* first-party, so the additional rule would widen a `$third-party`
|
|
861
|
+
* or a `$~third-party` rule. Skip it in this case.
|
|
862
|
+
*/ if (declarativeRule.condition.domainType) {
|
|
863
|
+
return null;
|
|
864
|
+
}
|
|
865
|
+
const permittedDomains = declarativeRule.condition.initiatorDomains ?? [];
|
|
866
|
+
const excludedDomains = declarativeRule.condition.excludedInitiatorDomains ?? [];
|
|
867
|
+
if (permittedDomains.length === 0 && excludedDomains.length === 0) {
|
|
868
|
+
return null;
|
|
869
|
+
}
|
|
870
|
+
/**
|
|
871
|
+
* Every domain condition must be fully representable in the
|
|
872
|
+
* additional rule. Otherwise it would drop a constraint that the
|
|
873
|
+
* source rule has and match requests the source rule does not.
|
|
874
|
+
*/ if (!RegularRuleConverter.areDomainsRepresentable(rule.permittedDomains) || !RegularRuleConverter.areDomainsRepresentable(rule.restrictedDomains) || !RegularRuleConverter.areDomainsRepresentable(rule.permittedToDomains) || !RegularRuleConverter.areDomainsRepresentable(rule.restrictedToDomains) || !RegularRuleConverter.areDomainsRepresentable(rule.denyAllowDomains)) {
|
|
875
|
+
return null;
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* A permitted `$to` also sets `requestDomains`, and the additional
|
|
879
|
+
* rule would replace them with the `$domain` domains. `$denyallow`
|
|
880
|
+
* and a negated `$to` only add `excludedRequestDomains`, which the
|
|
881
|
+
* additional rule merges.
|
|
882
|
+
*/ if ((rule.permittedToDomains?.length ?? 0) > 0 && permittedDomains.length > 0) {
|
|
883
|
+
return null;
|
|
884
|
+
}
|
|
885
|
+
if (!declarativeRule.condition.resourceTypes?.includes(ResourceType.MainFrame)) {
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* AdGuard matches the target hostname for a document request when
|
|
890
|
+
* the pattern is not domain-specific or when the `$domain` list
|
|
891
|
+
* contains only excluded domains.
|
|
892
|
+
*/ const hasOnlyExcludedDomains = permittedDomains.length === 0 && excludedDomains.length > 0;
|
|
893
|
+
if (!hasOnlyExcludedDomains && (rule.isRegexRule() || RegularRuleConverter.DOMAIN_SPECIFIC_PATTERN_PREFIXES.some((prefix)=>rule.pattern.startsWith(prefix)))) {
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
const condition = {
|
|
897
|
+
...declarativeRule.condition,
|
|
898
|
+
resourceTypes: [
|
|
899
|
+
ResourceType.MainFrame
|
|
900
|
+
]
|
|
901
|
+
};
|
|
902
|
+
delete condition.initiatorDomains;
|
|
903
|
+
delete condition.excludedInitiatorDomains;
|
|
904
|
+
if (permittedDomains.length > 0) {
|
|
905
|
+
condition.requestDomains = [
|
|
906
|
+
...permittedDomains
|
|
907
|
+
];
|
|
908
|
+
}
|
|
909
|
+
if (excludedDomains.length > 0) {
|
|
910
|
+
condition.excludedRequestDomains = Array.from(new Set([
|
|
911
|
+
...condition.excludedRequestDomains ?? [],
|
|
912
|
+
...excludedDomains
|
|
913
|
+
]));
|
|
914
|
+
}
|
|
915
|
+
return condition;
|
|
916
|
+
}
|
|
604
917
|
/**
|
|
605
918
|
* Verifies whether the converted {@link DeclarativeRule}:
|
|
606
919
|
* - has non-empty resource types,
|
|
@@ -651,22 +964,20 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
651
964
|
}
|
|
652
965
|
/**
|
|
653
966
|
* Checks the captured conversion error, if it is one of the expected
|
|
654
|
-
* conversion errors - returns it, otherwise
|
|
655
|
-
* the original rule
|
|
967
|
+
* conversion errors - returns it, otherwise packages it into a new error
|
|
968
|
+
* which keeps the original rule and the captured error as a cause.
|
|
656
969
|
*
|
|
657
|
-
* @param
|
|
970
|
+
* @param rule {@link Rule} which could not be converted.
|
|
658
971
|
* @param id Identifier of the desired declarative rule.
|
|
659
972
|
* @param error Captured error.
|
|
660
973
|
*
|
|
661
974
|
* @returns Initial error or new packaged error.
|
|
662
|
-
*/ static catchConversionError(
|
|
975
|
+
*/ static catchConversionError(rule, id, error) {
|
|
663
976
|
if (isConversionError(error)) {
|
|
664
977
|
return error;
|
|
665
978
|
}
|
|
666
|
-
const message = `Non-categorized error during a conversion rule (index - ${index}, id - ${id})`;
|
|
667
|
-
return
|
|
668
|
-
cause: error
|
|
669
|
-
}) : new Error(message);
|
|
979
|
+
const message = `Non-categorized error during a conversion rule (index - ${rule.index}, id - ${id})`;
|
|
980
|
+
return new UnexpectedConversionError(message, rule, error);
|
|
670
981
|
}
|
|
671
982
|
/**
|
|
672
983
|
* Converts the provided list of {@link Rule} into {@link DeclarativeRule},
|
|
@@ -697,16 +1008,25 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
697
1008
|
// Rule doesn't require conversion (e.g. $elemhide-only rules)
|
|
698
1009
|
return;
|
|
699
1010
|
}
|
|
700
|
-
// Convert rule and collect source map
|
|
701
|
-
const
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
1011
|
+
// Convert rule and collect source map values
|
|
1012
|
+
const declarativeRule = await this.convertRule(id, rule);
|
|
1013
|
+
const convertedRules = [
|
|
1014
|
+
declarativeRule
|
|
1015
|
+
];
|
|
1016
|
+
const targetDomainRule = RegularRuleConverter.buildTargetDomainRule(rule, declarativeRule, usedIds);
|
|
1017
|
+
if (targetDomainRule) {
|
|
1018
|
+
convertedRules.push(targetDomainRule);
|
|
1019
|
+
}
|
|
1020
|
+
convertedRules.forEach((converted)=>{
|
|
1021
|
+
res.sourceMapValues.push({
|
|
1022
|
+
declarativeRuleId: converted.id,
|
|
1023
|
+
sourceRuleIndex: index,
|
|
1024
|
+
filterId: filterListId
|
|
1025
|
+
});
|
|
1026
|
+
res.declarativeRules.push(converted);
|
|
706
1027
|
});
|
|
707
|
-
res.declarativeRules.push(converted);
|
|
708
1028
|
} catch (e) {
|
|
709
|
-
const err = RegularRuleConverter.catchConversionError(
|
|
1029
|
+
const err = RegularRuleConverter.catchConversionError(rule, id, e);
|
|
710
1030
|
res.errors.push(err);
|
|
711
1031
|
}
|
|
712
1032
|
}));
|
|
@@ -724,15 +1044,17 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
724
1044
|
* a declarative rule. This template is used to compare different
|
|
725
1045
|
* declarative rules.
|
|
726
1046
|
* @param combineRulePair A function that combines two similar
|
|
727
|
-
* declarative rules into one by merging their specific properties
|
|
1047
|
+
* declarative rules into one by merging their specific properties, or
|
|
1048
|
+
* returns `null` when the two rules cannot be combined and must stay
|
|
1049
|
+
* separate.
|
|
728
1050
|
*
|
|
729
1051
|
* @returns Object with grouped similar declarative rules.
|
|
730
1052
|
*/ static groupConvertedRules(converted, createRuleTemplate, combineRulePair) {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
1053
|
+
/**
|
|
1054
|
+
* Rules of one template, in order. A template describes the matching
|
|
1055
|
+
* condition, so the rules of one bucket may still be incompatible with
|
|
1056
|
+
* each other, e.g. two `$csp` rules with conflicting directives.
|
|
1057
|
+
*/ const rulesTemplates = new Map();
|
|
736
1058
|
const result = {
|
|
737
1059
|
declarativeRules: [],
|
|
738
1060
|
sourceMapValues: [],
|
|
@@ -742,7 +1064,7 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
742
1064
|
declarativeRules.forEach((declarativeRule)=>{
|
|
743
1065
|
// Try to find a sibling declarative rule
|
|
744
1066
|
const template = createRuleTemplate(declarativeRule);
|
|
745
|
-
const
|
|
1067
|
+
const siblings = rulesTemplates.get(template) ?? [];
|
|
746
1068
|
// Try to find the source mapping for the current declarative rule
|
|
747
1069
|
const source = sourceMapValues.find((s)=>s.declarativeRuleId === declarativeRule.id);
|
|
748
1070
|
if (source === undefined) {
|
|
@@ -750,25 +1072,39 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
750
1072
|
return;
|
|
751
1073
|
}
|
|
752
1074
|
/**
|
|
753
|
-
* If a
|
|
754
|
-
*
|
|
755
|
-
*
|
|
1075
|
+
* If a sibling can be combined with the current rule, replace it
|
|
1076
|
+
* with the combined rule and update the source mapping to reflect
|
|
1077
|
+
* the combined rule ID.
|
|
756
1078
|
*
|
|
757
|
-
* If no
|
|
758
|
-
* rule
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
1079
|
+
* If no sibling accepts the rule, it is added to the bucket as is:
|
|
1080
|
+
* a rule which cannot be merged must not be merged with a rule it
|
|
1081
|
+
* contradicts.
|
|
1082
|
+
*/ let siblingIndex = -1;
|
|
1083
|
+
let combinedRule = null;
|
|
1084
|
+
for(let index = 0; index < siblings.length; index += 1){
|
|
1085
|
+
const candidate = combineRulePair(siblings[index], declarativeRule);
|
|
1086
|
+
if (candidate !== null) {
|
|
1087
|
+
siblingIndex = index;
|
|
1088
|
+
combinedRule = candidate;
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
if (siblingIndex === -1 || combinedRule === null) {
|
|
1093
|
+
rulesTemplates.set(template, [
|
|
1094
|
+
...siblings,
|
|
1095
|
+
declarativeRule
|
|
1096
|
+
]);
|
|
768
1097
|
result.sourceMapValues.push(source);
|
|
1098
|
+
return;
|
|
769
1099
|
}
|
|
1100
|
+
const mergedRule = combinedRule;
|
|
1101
|
+
rulesTemplates.set(template, siblings.map((rule, index)=>index === siblingIndex ? mergedRule : rule));
|
|
1102
|
+
result.sourceMapValues.push({
|
|
1103
|
+
...source,
|
|
1104
|
+
declarativeRuleId: mergedRule.id
|
|
1105
|
+
});
|
|
770
1106
|
});
|
|
771
|
-
result.declarativeRules = Array.from(rulesTemplates.values());
|
|
1107
|
+
result.declarativeRules = Array.from(rulesTemplates.values()).flat();
|
|
772
1108
|
return result;
|
|
773
1109
|
}
|
|
774
1110
|
/**
|
|
@@ -846,5 +1182,29 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
|
|
|
846
1182
|
/**
|
|
847
1183
|
* Wildcard TLD suffix (`.*`) used in domain modifiers, e.g. `example.*`.
|
|
848
1184
|
*/ RegularRuleConverter.WILDCARD_TLD = `${DOT}${MASK_ANY_CHARACTER}`;
|
|
1185
|
+
/**
|
|
1186
|
+
* Separator between a host and a port in a URL.
|
|
1187
|
+
*/ RegularRuleConverter.PORT_SEPARATOR = ':';
|
|
1188
|
+
/**
|
|
1189
|
+
* Pattern prefixes that target a specific domain.
|
|
1190
|
+
*
|
|
1191
|
+
* AdGuard matches the `$domain` modifier against the target hostname of
|
|
1192
|
+
* a document request when the pattern is not domain-specific, or when the
|
|
1193
|
+
* `$domain` list contains only excluded domains. This mirrors
|
|
1194
|
+
* `NetworkRule.matchDomainModifier()` and
|
|
1195
|
+
* `Pattern.isPatternDomainSpecific()` in `@adguard/tsurlfilter`.
|
|
1196
|
+
*
|
|
1197
|
+
* @see {@link https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/tsurlfilter/src/rules/network-rule.ts | NetworkRule.matchDomainModifier()}
|
|
1198
|
+
* @see {@link https://github.com/AdguardTeam/tsurlfilter/blob/master/packages/tsurlfilter/src/rules/pattern.ts | Pattern.isPatternDomainSpecific()}
|
|
1199
|
+
*/ RegularRuleConverter.DOMAIN_SPECIFIC_PATTERN_PREFIXES = [
|
|
1200
|
+
ADBLOCK_URL_START,
|
|
1201
|
+
'http://',
|
|
1202
|
+
'https:/',
|
|
1203
|
+
'://'
|
|
1204
|
+
];
|
|
1205
|
+
/**
|
|
1206
|
+
* Start of an anchored pattern: the `|` or `||` anchor and an optional
|
|
1207
|
+
* scheme. What follows it is the host and, after the first `/`, the path.
|
|
1208
|
+
*/ RegularRuleConverter.ANCHORED_PATTERN_START = /^\|{1,2}(?:[a-z][a-z0-9+.-]*:\/\/)?/i;
|
|
849
1209
|
|
|
850
1210
|
export { RegularRuleConverter };
|