@adguard/dnr-converter 2.0.0-beta.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -3
- package/dist/cli.cjs +51902 -47539
- 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 +479 -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 +5 -5
- package/dist/rule/string-utils.js +0 -49
package/dist/constants.js
CHANGED
|
@@ -19,8 +19,11 @@
|
|
|
19
19
|
/**
|
|
20
20
|
* Dot character, used as a domain label separator.
|
|
21
21
|
*/ const DOT = '.';
|
|
22
|
+
/**
|
|
23
|
+
* Delimiter that separates the pattern from the modifier list in a rule.
|
|
24
|
+
*/ const OPTIONS_DELIMITER = '$';
|
|
22
25
|
/**
|
|
23
26
|
* Space character.
|
|
24
27
|
*/ const SPACE_CHARACTER = ' ';
|
|
25
28
|
|
|
26
|
-
export { CSP_HEADER_NAME, DOT, MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, PERMISSIONS_POLICY_HEADER_NAME, SPACE_CHARACTER };
|
|
29
|
+
export { CSP_HEADER_NAME, DOT, MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, OPTIONS_DELIMITER, PERMISSIONS_POLICY_HEADER_NAME, SPACE_CHARACTER };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { EmptyDomainsError } from './empty-domains-error.js';
|
|
2
2
|
import { EmptyResourcesError } from './empty-resources-error.js';
|
|
3
3
|
import { InvalidDeclarativeRuleError } from './invalid-declarative-rule-error.js';
|
|
4
|
+
import { UnexpectedConversionError } from './unexpected-conversion-error.js';
|
|
4
5
|
import { UnsupportedModifierError } from './unsupported-modifier-error.js';
|
|
5
6
|
import { UnsupportedRegexpError } from './unsupported-regexp-error.js';
|
|
6
7
|
|
|
@@ -11,7 +12,7 @@ import { UnsupportedRegexpError } from './unsupported-regexp-error.js';
|
|
|
11
12
|
*
|
|
12
13
|
* @returns `true` if the error is a {@link ConversionError}, `false` otherwise.
|
|
13
14
|
*/ function isConversionError(error) {
|
|
14
|
-
return error instanceof EmptyDomainsError || error instanceof EmptyResourcesError || error instanceof InvalidDeclarativeRuleError || error instanceof UnsupportedModifierError || error instanceof UnsupportedRegexpError;
|
|
15
|
+
return error instanceof EmptyDomainsError || error instanceof EmptyResourcesError || error instanceof InvalidDeclarativeRuleError || error instanceof UnexpectedConversionError || error instanceof UnsupportedModifierError || error instanceof UnsupportedRegexpError;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
export { EmptyDomainsError, EmptyResourcesError, InvalidDeclarativeRuleError, UnsupportedModifierError, UnsupportedRegexpError, isConversionError };
|
|
18
|
+
export { EmptyDomainsError, EmptyResourcesError, InvalidDeclarativeRuleError, UnexpectedConversionError, UnsupportedModifierError, UnsupportedRegexpError, isConversionError };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes an error of a rule which failed with an unexpected error.
|
|
3
|
+
*/ class UnexpectedConversionError extends Error {
|
|
4
|
+
/**
|
|
5
|
+
* Describes an error of a rule which failed with an unexpected error.
|
|
6
|
+
*
|
|
7
|
+
* @param message Message of error.
|
|
8
|
+
* @param rule {@link Rule}.
|
|
9
|
+
* @param cause Captured error.
|
|
10
|
+
*/ constructor(message, rule, cause){
|
|
11
|
+
super(message, {
|
|
12
|
+
cause
|
|
13
|
+
});
|
|
14
|
+
// For proper work of the "instanceof" operator
|
|
15
|
+
Object.setPrototypeOf(this, UnexpectedConversionError.prototype);
|
|
16
|
+
// Set the error name to the class name only after setting the prototype
|
|
17
|
+
// to avoid issues with name being overwritten in some environments
|
|
18
|
+
this.name = this.constructor.name;
|
|
19
|
+
this.rule = rule;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { UnexpectedConversionError };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Describes an error of a rule which could not be parsed or validated while
|
|
3
|
+
* scanning.
|
|
4
|
+
*
|
|
5
|
+
* Unlike a conversion error, such a rule is not available as a parsed rule, so
|
|
6
|
+
* the error keeps the filter id and the line index instead of the rule itself.
|
|
7
|
+
*/ class RuleScanError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Describes an error of a rule which could not be parsed or validated
|
|
10
|
+
* while scanning.
|
|
11
|
+
*
|
|
12
|
+
* @param message Message of error.
|
|
13
|
+
* @param filterId Id of the filter the rule belongs to.
|
|
14
|
+
* @param offset Character offset of the rule in the filter content.
|
|
15
|
+
* @param cause Captured error.
|
|
16
|
+
*/ constructor(message, filterId, offset, cause){
|
|
17
|
+
super(message, cause === undefined ? undefined : {
|
|
18
|
+
cause
|
|
19
|
+
});
|
|
20
|
+
// For proper work of the "instanceof" operator
|
|
21
|
+
Object.setPrototypeOf(this, RuleScanError.prototype);
|
|
22
|
+
// Set the error name to the class name only after setting the prototype
|
|
23
|
+
// to avoid issues with name being overwritten in some environments
|
|
24
|
+
this.name = this.constructor.name;
|
|
25
|
+
this.filterId = filterId;
|
|
26
|
+
this.offset = offset;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Type guard for {@link RuleScanError}.
|
|
31
|
+
*
|
|
32
|
+
* @param error The error to check.
|
|
33
|
+
*
|
|
34
|
+
* @returns `true` if the error is a {@link RuleScanError}, `false` otherwise.
|
|
35
|
+
*/ function isRuleScanError(error) {
|
|
36
|
+
return error instanceof RuleScanError;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { RuleScanError, isRuleScanError };
|
|
@@ -17,5 +17,14 @@
|
|
|
17
17
|
Object.setPrototypeOf(this, UnavailableFilterSourceError.prototype);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Type guard for {@link UnavailableFilterSourceError}.
|
|
22
|
+
*
|
|
23
|
+
* @param error The error to check.
|
|
24
|
+
*
|
|
25
|
+
* @returns `true` if the error is a {@link UnavailableFilterSourceError}.
|
|
26
|
+
*/ function isUnavailableFilterSourceError(error) {
|
|
27
|
+
return error instanceof UnavailableFilterSourceError;
|
|
28
|
+
}
|
|
20
29
|
|
|
21
|
-
export { UnavailableFilterSourceError };
|
|
30
|
+
export { UnavailableFilterSourceError, isUnavailableFilterSourceError };
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { getModifierAliases } from '@adguard/agtree';
|
|
2
|
+
import { RegExpUtils } from '@adguard/agtree/utils';
|
|
1
3
|
import { EmptyOrNegativeNumberOfRulesError } from '../errors/converter-options-errors/empty-or-negative-number-of-rules-error.js';
|
|
2
4
|
import { NegativeNumberOfRulesError } from '../errors/converter-options-errors/negative-number-of-rules-error.js';
|
|
3
5
|
import { ResourcesPathError } from '../errors/converter-options-errors/resources-path-error.js';
|
|
6
|
+
import { OPTION_NAMES } from '../rule/option-names.js';
|
|
7
|
+
import { isCspRule } from '../rule-converters/action-kind.js';
|
|
4
8
|
import { RulesConverter } from '../rule-converters/rules-converter.js';
|
|
5
9
|
import { RulesHashMap } from '../ruleset/rules-hash-map.js';
|
|
6
10
|
import { Ruleset } from '../ruleset/ruleset.js';
|
|
@@ -10,6 +14,7 @@ import { getErrorMessage } from '../utils/error.js';
|
|
|
10
14
|
import { isSafeRule } from '../utils/is-safe-rule.js';
|
|
11
15
|
import { getRulesetId } from '../utils/ruleset-utils.js';
|
|
12
16
|
import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
17
|
+
import { UnavailableFilterSourceError } from '../errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
20
|
* Unified filter-to-rules converter. Converts adblock filter lists into
|
|
@@ -60,6 +65,91 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
60
65
|
}
|
|
61
66
|
return FilterConverter.convertSimple(filters, options);
|
|
62
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Converts CSP rules from all provided filters into one ruleset.
|
|
70
|
+
*
|
|
71
|
+
* Only candidate lines are parsed: a line must contain `csp` or a `csp`
|
|
72
|
+
* alias as a modifier. `$badfilter` lines are not candidates: the regular
|
|
73
|
+
* pass owns them. The whole text is still split into lines, so the cost
|
|
74
|
+
* grows with the list size, while parsing stays proportional to the number
|
|
75
|
+
* of candidate lines.
|
|
76
|
+
*
|
|
77
|
+
* Allowlist `$csp` rules are resolved here only: the regular `convert()`
|
|
78
|
+
* rejects them, because an exception can only be applied when all filters
|
|
79
|
+
* are seen at once.
|
|
80
|
+
*
|
|
81
|
+
* @param filters Source filters.
|
|
82
|
+
* @param resourcesPath Path to web-accessible resources.
|
|
83
|
+
*
|
|
84
|
+
* @returns Combined CSP conversion result with source-map support.
|
|
85
|
+
*
|
|
86
|
+
* @throws An {@link ResourcesPathError} if the resources path does not
|
|
87
|
+
* start with a slash or ends with a slash.
|
|
88
|
+
*/ // eslint-disable-next-line class-methods-use-this
|
|
89
|
+
async convertCspRules(filters, resourcesPath) {
|
|
90
|
+
const options = {
|
|
91
|
+
combine: true,
|
|
92
|
+
withSourceMap: true
|
|
93
|
+
};
|
|
94
|
+
if (resourcesPath !== undefined) {
|
|
95
|
+
options.resourcesPath = resourcesPath;
|
|
96
|
+
}
|
|
97
|
+
FilterConverter.checkConverterOptions(options);
|
|
98
|
+
const relevantFilters = await FilterConverter.filterCspFilters(filters);
|
|
99
|
+
const { errors: scanErrors, filters: scannedFilters } = await RulesScanner.scanFilters(relevantFilters.filters, {
|
|
100
|
+
filterFn: (rule)=>isCspRule(rule) || rule.isModifierEnabled(OPTION_NAMES.BADFILTER),
|
|
101
|
+
lineSelector: FilterConverter.isCspRelatedText
|
|
102
|
+
});
|
|
103
|
+
const convertedRules = await RulesConverter.convertCspRules(scannedFilters, options);
|
|
104
|
+
const badFilterRules = scannedFilters.flatMap(({ badFilterRules: rules })=>rules);
|
|
105
|
+
const result = FilterConverter.collectConvertedResult(FilterConverter.CSP_RULESET_ID, filters, scannedFilters, convertedRules, badFilterRules);
|
|
106
|
+
result.errors = [
|
|
107
|
+
...relevantFilters.errors,
|
|
108
|
+
...scanErrors,
|
|
109
|
+
...result.errors
|
|
110
|
+
];
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Filters out filters whose content cannot contain CSP rules.
|
|
115
|
+
*
|
|
116
|
+
* A filter whose content cannot be read is skipped and reported, so one
|
|
117
|
+
* broken source does not fail the whole configuration.
|
|
118
|
+
*
|
|
119
|
+
* @param filters Source filters.
|
|
120
|
+
*
|
|
121
|
+
* @returns Filters whose content mentions a CSP-related modifier and
|
|
122
|
+
* errors of unreadable filters.
|
|
123
|
+
*/ static async filterCspFilters(filters) {
|
|
124
|
+
const errors = [];
|
|
125
|
+
const filtersWithContent = await Promise.all(filters.map(async (filter)=>{
|
|
126
|
+
try {
|
|
127
|
+
return {
|
|
128
|
+
filter,
|
|
129
|
+
content: await filter.getContent()
|
|
130
|
+
};
|
|
131
|
+
} catch (error) {
|
|
132
|
+
errors.push(new UnavailableFilterSourceError(`Cannot read the content of the filter ${filter.getId()}, the CSP pass skipped it`, filter.getId(), error instanceof Error ? error : undefined));
|
|
133
|
+
return {
|
|
134
|
+
filter,
|
|
135
|
+
content: null
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}));
|
|
139
|
+
return {
|
|
140
|
+
filters: filtersWithContent.filter(({ content })=>content !== null && FilterConverter.isCspRelatedText(content)).map(({ filter })=>filter),
|
|
141
|
+
errors
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Checks whether raw filter text may contain a CSP-related rule.
|
|
146
|
+
*
|
|
147
|
+
* @param text Filter content or a single line of it.
|
|
148
|
+
*
|
|
149
|
+
* @returns `true` if the text should be parsed by the CSP pass.
|
|
150
|
+
*/ static isCspRelatedText(text) {
|
|
151
|
+
return FilterConverter.CSP_RELATED_MODIFIER_REGEXP.test(text);
|
|
152
|
+
}
|
|
63
153
|
/**
|
|
64
154
|
* Applies `$badfilter` rules from dynamic rulesets to static rulesets and
|
|
65
155
|
* returns the list of declarative rule IDs that should be disabled.
|
|
@@ -111,7 +201,9 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
111
201
|
*
|
|
112
202
|
* @returns Conversion result for the given filters.
|
|
113
203
|
*/ static async convertFiltersSimple(filters, rulesetId, options, scannedLimit) {
|
|
114
|
-
const { errors: scanErrors, filters: scannedFilters } = await RulesScanner.scanFilters(filters,
|
|
204
|
+
const { errors: scanErrors, filters: scannedFilters } = await RulesScanner.scanFilters(filters, {
|
|
205
|
+
maxScannedRules: scannedLimit
|
|
206
|
+
});
|
|
115
207
|
const convertedRules = await RulesConverter.convert(scannedFilters, options);
|
|
116
208
|
const { declarativeRules, errors: convertErrors, limitations = [] } = convertedRules;
|
|
117
209
|
const ruleset = new Ruleset(rulesetId, declarativeRules);
|
|
@@ -121,6 +213,26 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
121
213
|
limitations
|
|
122
214
|
};
|
|
123
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates a predicate to filter out rules which must not be converted.
|
|
218
|
+
*
|
|
219
|
+
* When CSP rules are rebuilt globally, the regular conversion must not
|
|
220
|
+
* convert them again, while `$badfilter` rules are kept because they can
|
|
221
|
+
* still disable other rules.
|
|
222
|
+
*
|
|
223
|
+
* @param skipNegatedRulesFn Predicate to skip rules negated by `$badfilter`,
|
|
224
|
+
* if any.
|
|
225
|
+
* @param excludeCspRules Whether CSP rules are rebuilt separately.
|
|
226
|
+
*
|
|
227
|
+
* @returns Predicate to filter out rules.
|
|
228
|
+
*/ static createRuleFilterFn(skipNegatedRulesFn, excludeCspRules) {
|
|
229
|
+
return (rule)=>{
|
|
230
|
+
if (skipNegatedRulesFn && !skipNegatedRulesFn(rule)) {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
return !excludeCspRules || !isCspRule(rule) || rule.isModifierEnabled(OPTION_NAMES.BADFILTER);
|
|
234
|
+
};
|
|
235
|
+
}
|
|
124
236
|
/**
|
|
125
237
|
* Runs the advanced conversion flow: scans with optional `$badfilter`
|
|
126
238
|
* pre-filtering, converts, wraps in {@link RulesetWithSourceMap}.
|
|
@@ -146,9 +258,17 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
146
258
|
return true;
|
|
147
259
|
};
|
|
148
260
|
}
|
|
261
|
+
const filterRule = FilterConverter.createRuleFilterFn(skipNegatedRulesFn, options.excludeCspRules);
|
|
149
262
|
const scannedLimit = options?.maxNumberOfRules ? Math.ceil(options.maxNumberOfRules * FilterConverter.SCANNED_RULES_MULTIPLICATOR) : undefined;
|
|
263
|
+
// CSP rules are dropped here and reported by the CSP pass, so their
|
|
264
|
+
// parse errors must not be duplicated in the regular result.
|
|
265
|
+
const skipErrorLineSelector = options.excludeCspRules ? FilterConverter.isCspRelatedText : undefined;
|
|
150
266
|
if (options?.combine) {
|
|
151
|
-
const { errors: scanErrors, filters: scannedFilters } = await RulesScanner.scanFilters(filters,
|
|
267
|
+
const { errors: scanErrors, filters: scannedFilters } = await RulesScanner.scanFilters(filters, {
|
|
268
|
+
filterFn: filterRule,
|
|
269
|
+
maxScannedRules: scannedLimit,
|
|
270
|
+
skipErrorLineSelector
|
|
271
|
+
});
|
|
152
272
|
const convertedRules = await RulesConverter.convert(scannedFilters, options);
|
|
153
273
|
const badFilterRules = scannedFilters.flatMap(({ badFilterRules: rules })=>rules);
|
|
154
274
|
const result = FilterConverter.collectConvertedResult(FilterConverter.COMBINED_RULESET_ID, filters, scannedFilters, convertedRules, badFilterRules);
|
|
@@ -160,7 +280,11 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
160
280
|
const results = await Promise.all(filters.map(async (filter)=>{
|
|
161
281
|
const { errors: scanErrors, filters: [scannedFilter] } = await RulesScanner.scanFilters([
|
|
162
282
|
filter
|
|
163
|
-
],
|
|
283
|
+
], {
|
|
284
|
+
filterFn: filterRule,
|
|
285
|
+
maxScannedRules: scannedLimit,
|
|
286
|
+
skipErrorLineSelector
|
|
287
|
+
});
|
|
164
288
|
const convertedRules = await RulesConverter.convert([
|
|
165
289
|
scannedFilter
|
|
166
290
|
], options);
|
|
@@ -180,6 +304,9 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
180
304
|
* Creates new {@link RulesetWithSourceMap} and wraps all data for
|
|
181
305
|
* {@link RulesetContentProvider}.
|
|
182
306
|
*
|
|
307
|
+
* Freshly converted rulesets have no metadata-stored unsafe rules yet:
|
|
308
|
+
* unsafe rules are excluded later by the `serializeCompact()` caller.
|
|
309
|
+
*
|
|
183
310
|
* @param rulesetId Rule set id.
|
|
184
311
|
* @param filterList List of raw filters.
|
|
185
312
|
* @param scannedFilters Already scanned filters.
|
|
@@ -207,9 +334,7 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
207
334
|
const unsafeRulesCount = declarativeRules.filter((r)=>!isSafeRule(r)).length;
|
|
208
335
|
const safeRulesCount = declarativeRules.length - unsafeRulesCount;
|
|
209
336
|
const regexRulesCount = declarativeRules.filter((r)=>RulesConverter.isRegexRule(r)).length;
|
|
210
|
-
const ruleset = new RulesetWithSourceMap(rulesetId, safeRulesCount, unsafeRulesCount, regexRulesCount, rulesetContent, badFilterRules, rulesHashMap,
|
|
211
|
-
// unsafe-rule exclusion is performed later by the serializeCompact() caller.
|
|
212
|
-
[]);
|
|
337
|
+
const ruleset = new RulesetWithSourceMap(rulesetId, safeRulesCount, unsafeRulesCount, regexRulesCount, rulesetContent, badFilterRules, rulesHashMap, []);
|
|
213
338
|
return {
|
|
214
339
|
ruleset,
|
|
215
340
|
errors,
|
|
@@ -374,6 +499,26 @@ import { RulesScanner } from '../rules-scanner/rules-scanner.js';
|
|
|
374
499
|
* Same as chrome.declarativeNetRequest.DYNAMIC_RULESET_ID.
|
|
375
500
|
* Used as the combined ruleset ID when `combine: true`.
|
|
376
501
|
*/ FilterConverter.COMBINED_RULESET_ID = '_dynamic';
|
|
502
|
+
/**
|
|
503
|
+
* Runtime ruleset ID used for CSP rules.
|
|
504
|
+
*/ FilterConverter.CSP_RULESET_ID = '_csp';
|
|
505
|
+
/**
|
|
506
|
+
* Raw modifier names which may affect CSP conversion.
|
|
507
|
+
*
|
|
508
|
+
* Aliases of `$csp` are taken from the AGTree conversion map, so they
|
|
509
|
+
* cannot drift from it.
|
|
510
|
+
*/ FilterConverter.CSP_RAW_MODIFIER_NAMES = [
|
|
511
|
+
OPTION_NAMES.CSP,
|
|
512
|
+
...getModifierAliases(OPTION_NAMES.CSP)
|
|
513
|
+
];
|
|
514
|
+
/**
|
|
515
|
+
* Matches a CSP-related modifier anywhere in a modifier list.
|
|
516
|
+
*
|
|
517
|
+
* A modifier name is looked up right after `$` or `,`, spaces are allowed
|
|
518
|
+
* after the separator, and the name must be complete: `$csp` and `,csp=`
|
|
519
|
+
* match, while `$cspreport` does not. The first modifier of a list follows
|
|
520
|
+
* `$`, so a rule where `csp` is not the first modifier is found as well.
|
|
521
|
+
*/ FilterConverter.CSP_RELATED_MODIFIER_REGEXP = new RegExp(`(?:^|[$,])[ \\t]*(?:${FilterConverter.CSP_RAW_MODIFIER_NAMES.map((name)=>RegExpUtils.escapeRegexSpecials(name)).join('|')})(?![\\w-])`);
|
|
377
522
|
/**
|
|
378
523
|
* Number of scanned rules can be limited via converter options. In this
|
|
379
524
|
* case we increase the limit by 10% to scan more rules in case of some
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { DNR_CONVERTER_VERSION } from './version.js';
|
|
2
|
+
export { isCspDeclarativeRule } from './rule-converters/csp-converter.js';
|
|
2
3
|
export { isSafeRule } from './utils/is-safe-rule.js';
|
|
3
4
|
export { Ruleset } from './ruleset/ruleset.js';
|
|
4
5
|
export { RulesetWithSourceMap } from './ruleset/ruleset-with-source-map.js';
|
|
5
6
|
export { Filter } from './filter/filter.js';
|
|
6
7
|
export { RulesHashMap } from './ruleset/rules-hash-map.js';
|
|
8
|
+
export { RuleScanError, isRuleScanError } from './errors/scan-errors.js';
|
|
7
9
|
export { parseCompactRuleset } from './ruleset/compact-ruleset.js';
|
|
8
10
|
export { METADATA_RULESET_ID, MetadataRuleset } from './ruleset/metadata-ruleset.js';
|
|
9
11
|
export { Rule } from './rule/rule.js';
|
|
@@ -13,7 +15,7 @@ export { isConverterOptionsError } from './errors/converter-options-errors/index
|
|
|
13
15
|
export { isLimitationError } from './errors/limitation-errors/index.js';
|
|
14
16
|
export { RULESET_NAME_PREFIX, extractRulesetId, getRulesetId, getRulesetPath } from './utils/ruleset-utils.js';
|
|
15
17
|
export { FilterConverter } from './filter-converter/filter-converter.js';
|
|
16
|
-
export { UnavailableFilterSourceError } from './errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
18
|
+
export { UnavailableFilterSourceError, isUnavailableFilterSourceError } from './errors/unavailable-sources-errors/unavailable-filter-source-error.js';
|
|
17
19
|
export { UnavailableRulesetSourceError } from './errors/unavailable-sources-errors/unavailable-ruleset-source-error.js';
|
|
18
20
|
export { InvalidMetadataChunksError } from './errors/metadata-errors/invalid-metadata-chunks-error.js';
|
|
19
21
|
export { ResourceType } from './declarative-rule/rule-condition.js';
|
|
@@ -23,10 +25,12 @@ export { EmptyResourcesError } from './errors/conversion-errors/empty-resources-
|
|
|
23
25
|
export { UnsupportedModifierError } from './errors/conversion-errors/unsupported-modifier-error.js';
|
|
24
26
|
export { UnsupportedRegexpError } from './errors/conversion-errors/unsupported-regexp-error.js';
|
|
25
27
|
export { InvalidDeclarativeRuleError } from './errors/conversion-errors/invalid-declarative-rule-error.js';
|
|
28
|
+
export { UnexpectedConversionError } from './errors/conversion-errors/unexpected-conversion-error.js';
|
|
26
29
|
export { EmptyOrNegativeNumberOfRulesError } from './errors/converter-options-errors/empty-or-negative-number-of-rules-error.js';
|
|
27
30
|
export { NegativeNumberOfRulesError } from './errors/converter-options-errors/negative-number-of-rules-error.js';
|
|
28
31
|
export { ResourcesPathError } from './errors/converter-options-errors/resources-path-error.js';
|
|
29
32
|
export { MaxScannedRulesError } from './errors/limitation-errors/max-scanned-rules-error.js';
|
|
33
|
+
export { TooManyError } from './errors/limitation-errors/too-many-error.js';
|
|
30
34
|
export { TooManyRegexpRulesError } from './errors/limitation-errors/too-many-regexp-rules-error.js';
|
|
31
35
|
export { TooManyUnsafeRulesError } from './errors/limitation-errors/too-many-unsafe-rules-error.js';
|
|
32
36
|
export { TooManyRulesError } from './errors/limitation-errors/too-many-rules-error.js';
|
package/dist/package.json.js
CHANGED
|
@@ -1,32 +1,99 @@
|
|
|
1
|
+
import { NetworkRuleParser } from '@adguard/agtree/parser';
|
|
2
|
+
import { OPTIONS_DELIMITER } from '../constants.js';
|
|
3
|
+
import { isRegexPattern } from '../utils/string.js';
|
|
1
4
|
import { OPTION_NAMES } from './option-names.js';
|
|
2
|
-
import { stringArraysEqual, stringArraysHaveIntersection } from './string-utils.js';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
|
-
*
|
|
7
|
+
* Separates the entries of a written modifier list.
|
|
6
8
|
*
|
|
7
|
-
*
|
|
9
|
+
* A separator escaped with a backslash belongs to the value, not to the list.
|
|
10
|
+
* The list is scanned character by character, because a lookbehind regexp
|
|
11
|
+
* literal does not parse in older Safari versions.
|
|
8
12
|
*
|
|
9
|
-
* @
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
]
|
|
14
|
-
|
|
13
|
+
* @param writtenModifiers Written modifier list, without the `$` separator.
|
|
14
|
+
*
|
|
15
|
+
* @returns Modifier entries as written, empty entries included.
|
|
16
|
+
*/ const splitWrittenModifiers = (writtenModifiers)=>{
|
|
17
|
+
const entries = [];
|
|
18
|
+
let start = 0;
|
|
19
|
+
for(let i = 0; i < writtenModifiers.length; i += 1){
|
|
20
|
+
if (writtenModifiers.charAt(i) === ',' && writtenModifiers.charAt(i - 1) !== '\\') {
|
|
21
|
+
entries.push(writtenModifiers.slice(start, i));
|
|
22
|
+
start = i + 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
entries.push(writtenModifiers.slice(start));
|
|
26
|
+
return entries;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Name of the `$domain` alias of other syntaxes, as it appears in the written
|
|
30
|
+
* rule text.
|
|
31
|
+
*/ const DOMAIN_ALIAS_OPTION_NAME = 'from';
|
|
32
|
+
/**
|
|
33
|
+
* Returns the name of a written modifier entry, without the negation mark.
|
|
34
|
+
*
|
|
35
|
+
* @param entry Written modifier entry, e.g. `~third-party` or `csp=script-src`.
|
|
36
|
+
*
|
|
37
|
+
* @returns Modifier name.
|
|
38
|
+
*/ const getWrittenModifierName = (entry)=>entry.replace(/^~/, '').split('=')[0] ?? '';
|
|
39
|
+
/**
|
|
40
|
+
* Removes the given modifiers from the rule text as written.
|
|
41
|
+
*
|
|
42
|
+
* `$badfilter` requires the text to match, so the modifiers that take part in
|
|
43
|
+
* the match are dropped from the text instead of being compared separately.
|
|
44
|
+
*
|
|
45
|
+
* The modifier list starts after the separator, found the same way as in the
|
|
46
|
+
* parser: the last unescaped `$` that is not followed by a regexp marker, so an
|
|
47
|
+
* end anchor in a regexp pattern is not taken for it. The text is rebuilt as it
|
|
48
|
+
* was apart from the dropped entries.
|
|
49
|
+
*
|
|
50
|
+
* @param text Rule text as written.
|
|
51
|
+
* @param shouldStrip Predicate selecting the modifier names to remove.
|
|
52
|
+
*
|
|
53
|
+
* @returns Rule text without the selected modifiers.
|
|
54
|
+
*/ const stripWrittenModifiers = (text, shouldStrip)=>{
|
|
55
|
+
const delimiterIndex = NetworkRuleParser.findNetworkRuleSeparatorIndex(text);
|
|
56
|
+
if (delimiterIndex === -1) {
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
59
|
+
const pattern = text.slice(0, delimiterIndex);
|
|
60
|
+
const kept = splitWrittenModifiers(text.slice(delimiterIndex + 1)).filter((entry)=>!shouldStrip(getWrittenModifierName(entry)));
|
|
61
|
+
return kept.length === 0 ? pattern : `${pattern}${OPTIONS_DELIMITER}${kept.join(',')}`;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Checks whether the modifier name is `$badfilter`.
|
|
65
|
+
*
|
|
66
|
+
* @param name Modifier name.
|
|
67
|
+
*
|
|
68
|
+
* @returns True for the `$badfilter` modifier.
|
|
69
|
+
*/ const isBadfilterModifier = (name)=>name === OPTION_NAMES.BADFILTER;
|
|
70
|
+
/**
|
|
71
|
+
* Checks whether the modifier name restricts a rule by domain.
|
|
72
|
+
*
|
|
73
|
+
* `$domain` has the `$from` alias in other syntaxes, and both are dropped when a
|
|
74
|
+
* rule with `$domain` disables the target rule only partially.
|
|
75
|
+
*
|
|
76
|
+
* @param name Modifier name.
|
|
77
|
+
*
|
|
78
|
+
* @returns True for `$domain` and its alias.
|
|
79
|
+
*/ const isDomainModifier = (name)=>name === OPTION_NAMES.DOMAIN || name === DOMAIN_ALIAS_OPTION_NAME;
|
|
15
80
|
/**
|
|
16
81
|
* Helper class for checking whether a `$badfilter` rule negates another rule.
|
|
17
82
|
*/ class RuleBadfilter {
|
|
18
83
|
/**
|
|
19
84
|
* Checks whether `badfilterRule` negates `targetRule`.
|
|
20
85
|
*
|
|
21
|
-
* A `$badfilter` rule negates another rule when
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* -
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
86
|
+
* A `$badfilter` rule negates another rule when the text of the two rules, as
|
|
87
|
+
* written in the rule text, matches: the whole text is taken, `$badfilter` is
|
|
88
|
+
* dropped from it, and the rest must be equal. That covers the pattern, the
|
|
89
|
+
* allowlist flag and every modifier with its value, order and repeats — rules
|
|
90
|
+
* that only look equal after parsing (for example `$removeheader=Set-Cookie`
|
|
91
|
+
* against `set-cookie`, or `$queryprune` against `$removeparam`) do not match.
|
|
92
|
+
*
|
|
93
|
+
* A rule with `$domain` and without negated domains is an exception: it
|
|
94
|
+
* negates the target rule only for the domains present in both lists, so the
|
|
95
|
+
* `$domain` entries are dropped from both texts and the rest is compared,
|
|
96
|
+
* together with an intersection check of the two domain lists.
|
|
30
97
|
*
|
|
31
98
|
* @param badfilterRule The rule that carries the `$badfilter` modifier.
|
|
32
99
|
* @param targetRule The rule to test for negation.
|
|
@@ -36,45 +103,67 @@ import { stringArraysEqual, stringArraysHaveIntersection } from './string-utils.
|
|
|
36
103
|
if (!badfilterRule.enabledModifiers.has(OPTION_NAMES.BADFILTER)) {
|
|
37
104
|
return false;
|
|
38
105
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (badfilterRule.pattern !== targetRule.pattern) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
// Compare resource type arrays (order-independent).
|
|
46
|
-
if (sortedJoin(badfilterRule.permittedResourceTypes) !== sortedJoin(targetRule.permittedResourceTypes)) {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
if (sortedJoin(badfilterRule.restrictedResourceTypes) !== sortedJoin(targetRule.restrictedResourceTypes)) {
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
// Compare enabled modifiers excluding $badfilter itself.
|
|
53
|
-
const badfilterEnabled = new Set(badfilterRule.enabledModifiers);
|
|
54
|
-
badfilterEnabled.delete(OPTION_NAMES.BADFILTER);
|
|
55
|
-
const sameSize = badfilterEnabled.size === targetRule.enabledModifiers.size;
|
|
56
|
-
const sameModifiers = [
|
|
57
|
-
...badfilterEnabled
|
|
58
|
-
].every((m)=>targetRule.enabledModifiers.has(m));
|
|
59
|
-
if (!sameSize || !sameModifiers) {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
// Compare disabled modifiers.
|
|
63
|
-
if (badfilterRule.disabledModifiers.size !== targetRule.disabledModifiers.size || ![
|
|
64
|
-
...badfilterRule.disabledModifiers
|
|
65
|
-
].every((m)=>targetRule.disabledModifiers.has(m))) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
if (!stringArraysEqual(badfilterRule.restrictedDomains, targetRule.restrictedDomains)) {
|
|
69
|
-
return false;
|
|
106
|
+
const withoutBadfilter = stripWrittenModifiers(badfilterRule.originalText, isBadfilterModifier);
|
|
107
|
+
if (!RuleBadfilter.isPartialDomainCase(badfilterRule, targetRule)) {
|
|
108
|
+
return withoutBadfilter === targetRule.originalText;
|
|
70
109
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
110
|
+
return stripWrittenModifiers(withoutBadfilter, isDomainModifier) === stripWrittenModifiers(targetRule.originalText, isDomainModifier) && RuleBadfilter.haveIntersection(badfilterRule.permittedDomains, targetRule.permittedDomains);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether a `$badfilter` rule disables the target rule only partially.
|
|
114
|
+
*
|
|
115
|
+
* This is the case when at least one of the two rules restricts itself by
|
|
116
|
+
* `$domain` and neither rule negates a domain: negated domains cannot be
|
|
117
|
+
* expressed as an intersection, so such rules are compared by the whole text
|
|
118
|
+
* instead. A rule without `$domain` restricts nothing, so it keeps cancelling
|
|
119
|
+
* the other rule everywhere the two rules overlap.
|
|
120
|
+
*
|
|
121
|
+
* @param badfilterRule The rule that carries the `$badfilter` modifier.
|
|
122
|
+
* @param targetRule The rule to test for negation.
|
|
123
|
+
*
|
|
124
|
+
* @returns True when the partial cancellation case applies.
|
|
125
|
+
*/ static isPartialDomainCase(badfilterRule, targetRule) {
|
|
126
|
+
return (targetRule.permittedDomains !== null || badfilterRule.permittedDomains !== null) && targetRule.restrictedDomains === null && badfilterRule.restrictedDomains === null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Checks whether two domain lists can match the same request domain.
|
|
130
|
+
*
|
|
131
|
+
* Entries are compared as written, as `$badfilter` matches the rule text; a
|
|
132
|
+
* plain domain name is compared case-insensitively, because a domain name is
|
|
133
|
+
* case-insensitive, while a regexp pattern keeps its case.
|
|
134
|
+
*
|
|
135
|
+
* The MV2 twin receives values already normalized by `DomainModifier`, which
|
|
136
|
+
* resolves the separator escape, so an escaped separator in a regexp entry
|
|
137
|
+
* compares equal to the unescaped one there — a pre-existing divergence that
|
|
138
|
+
* the `$domain` decision has to settle.
|
|
139
|
+
*
|
|
140
|
+
* A wildcard TLD entry (`example.*`) therefore overlaps only an identically
|
|
141
|
+
* written entry: matching it against the domains it stands for needs the
|
|
142
|
+
* public suffix list, which this package does not have, and a prefix-based
|
|
143
|
+
* approximation cancels rules for domains the wildcard does not cover.
|
|
144
|
+
*
|
|
145
|
+
* @param left First domain list.
|
|
146
|
+
* @param right Second domain list.
|
|
147
|
+
*
|
|
148
|
+
* @returns True when at least one entry of one list equals an entry of the
|
|
149
|
+
* other list. A missing list matches any domain, the same way a
|
|
150
|
+
* `$badfilter` without `$domain` disables a rule everywhere it applies.
|
|
151
|
+
*/ static haveIntersection(left, right) {
|
|
152
|
+
if (left === null || right === null) {
|
|
153
|
+
return true;
|
|
76
154
|
}
|
|
77
|
-
return
|
|
155
|
+
return left.some((leftDomain)=>right.some((rightDomain)=>RuleBadfilter.normalizeForComparison(leftDomain) === RuleBadfilter.normalizeForComparison(rightDomain)));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Brings a domain entry to the form used by the `$badfilter` comparison:
|
|
159
|
+
* domain names are case-insensitive, while regexp patterns are compared as
|
|
160
|
+
* written.
|
|
161
|
+
*
|
|
162
|
+
* @param domain Domain entry.
|
|
163
|
+
*
|
|
164
|
+
* @returns Domain entry to compare.
|
|
165
|
+
*/ static normalizeForComparison(domain) {
|
|
166
|
+
return isRegexPattern(domain) ? domain : domain.toLowerCase();
|
|
78
167
|
}
|
|
79
168
|
}
|
|
80
169
|
|