@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,11 +1,39 @@
|
|
|
1
1
|
import { CSP_HEADER_NAME } from '../constants.js';
|
|
2
2
|
import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
3
|
+
import { RuleActionType } from '../declarative-rule/rule-action.js';
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a declarative rule appends a CSP response header.
|
|
7
|
+
*
|
|
8
|
+
* @param rule Declarative rule to check.
|
|
9
|
+
*
|
|
10
|
+
* @returns `true` for CSP declarative rules.
|
|
11
|
+
*/ function isCspDeclarativeRule(rule) {
|
|
12
|
+
return rule.action.type === RuleActionType.ModifyHeaders && rule.action.responseHeaders?.some(({ header })=>header === CSP_HEADER_NAME) === true;
|
|
13
|
+
}
|
|
4
14
|
/**
|
|
5
15
|
* Describes how to convert `$csp` rules.
|
|
6
16
|
*
|
|
7
17
|
* @see {@link RegularRuleConverter} parent class.
|
|
8
18
|
*/ class CspConverter extends RegularRuleConverter {
|
|
19
|
+
/**
|
|
20
|
+
* Converts one CSP rule and applies domains produced by the resolver.
|
|
21
|
+
*
|
|
22
|
+
* @param id Rule ID.
|
|
23
|
+
* @param rule Source CSP rule.
|
|
24
|
+
*
|
|
25
|
+
* @returns Converted DNR rule.
|
|
26
|
+
*/ async convertRule(id, rule) {
|
|
27
|
+
const declarativeRule = await super.convertRule(id, rule);
|
|
28
|
+
const domains = this.excludedRequestDomains.get(rule);
|
|
29
|
+
if (domains && domains.length > 0) {
|
|
30
|
+
declarativeRule.condition.excludedRequestDomains = Array.from(new Set([
|
|
31
|
+
...declarativeRule.condition.excludedRequestDomains ?? [],
|
|
32
|
+
...domains
|
|
33
|
+
]));
|
|
34
|
+
}
|
|
35
|
+
return declarativeRule;
|
|
36
|
+
}
|
|
9
37
|
/**
|
|
10
38
|
* Creates rule template for grouping similar `$csp` rules.
|
|
11
39
|
*
|
|
@@ -39,14 +67,101 @@ import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
|
39
67
|
*/ static isCspHeader(modifyHeaderInfo) {
|
|
40
68
|
return modifyHeaderInfo.header === CSP_HEADER_NAME;
|
|
41
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Splits a CSP header value into directives.
|
|
72
|
+
*
|
|
73
|
+
* @param value CSP header value.
|
|
74
|
+
*
|
|
75
|
+
* @returns Non-empty trimmed directives.
|
|
76
|
+
*/ static splitDirectives(value) {
|
|
77
|
+
return value.split(';').map((directive)=>directive.trim()).filter((directive)=>directive.length > 0);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the lowercased directive name.
|
|
81
|
+
*
|
|
82
|
+
* Directive names are case-insensitive, while directive values are not, so
|
|
83
|
+
* only the name is lowercased.
|
|
84
|
+
*
|
|
85
|
+
* @param directive CSP directive.
|
|
86
|
+
*
|
|
87
|
+
* @returns Lowercased directive name.
|
|
88
|
+
*/ static getDirectiveName(directive) {
|
|
89
|
+
const [name] = directive.split(/\s/, 1);
|
|
90
|
+
return name.toLowerCase();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the arguments of a CSP directive.
|
|
94
|
+
*
|
|
95
|
+
* @param directive CSP directive.
|
|
96
|
+
*
|
|
97
|
+
* @returns Arguments as written, or an empty string for a value-less directive.
|
|
98
|
+
*/ static getDirectiveArguments(directive) {
|
|
99
|
+
const match = /^\S+\s+(.*)$/.exec(directive);
|
|
100
|
+
return match ? match[1] : '';
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Collects the CSP header value of a declarative rule.
|
|
104
|
+
*
|
|
105
|
+
* @param rule Declarative rule.
|
|
106
|
+
*
|
|
107
|
+
* @returns CSP header value or `null` if the rule has no CSP header.
|
|
108
|
+
*/ static getCspValue(rule) {
|
|
109
|
+
const header = rule.action.responseHeaders?.find(CspConverter.isCspHeader);
|
|
110
|
+
return header?.value ?? null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Checks whether two CSP header values express the same policy.
|
|
114
|
+
*
|
|
115
|
+
* Merged directives form a single policy, so merging different ones can
|
|
116
|
+
* change what is blocked through the `default-src` fallback:
|
|
117
|
+
* `default-src 'none'` plus `script-src 'self'` in one header allow
|
|
118
|
+
* same-origin scripts, while the same directives in two headers block
|
|
119
|
+
* them, because a browser applies every header of the response. Two
|
|
120
|
+
* identical policies, on the other hand, are applied once either way, so
|
|
121
|
+
* only such values are merged.
|
|
122
|
+
*
|
|
123
|
+
* @param currentValue Current CSP header value.
|
|
124
|
+
* @param valueToMerge CSP header value to append.
|
|
125
|
+
*
|
|
126
|
+
* @returns `true` if the values express the same policy.
|
|
127
|
+
*/ static canMergeCspValues(currentValue, valueToMerge) {
|
|
128
|
+
const currentArguments = CspConverter.mapDirectiveArguments(currentValue);
|
|
129
|
+
const mergeArguments = CspConverter.mapDirectiveArguments(valueToMerge);
|
|
130
|
+
if (currentArguments.size !== mergeArguments.size) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
for (const [name, args] of mergeArguments){
|
|
134
|
+
if (currentArguments.get(name) !== args) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Lists the directives of a CSP header value with their arguments.
|
|
142
|
+
*
|
|
143
|
+
* @param value CSP header value.
|
|
144
|
+
*
|
|
145
|
+
* @returns Arguments by lowercased directive name.
|
|
146
|
+
*/ static mapDirectiveArguments(value) {
|
|
147
|
+
const argumentsByName = new Map();
|
|
148
|
+
CspConverter.splitDirectives(value).forEach((directive)=>{
|
|
149
|
+
const name = CspConverter.getDirectiveName(directive);
|
|
150
|
+
if (!argumentsByName.has(name)) {
|
|
151
|
+
argumentsByName.set(name, CspConverter.getDirectiveArguments(directive));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return argumentsByName;
|
|
155
|
+
}
|
|
42
156
|
/**
|
|
43
157
|
* Combines two similar `$csp` {@link DeclarativeRule}
|
|
44
|
-
* rules into one
|
|
158
|
+
* rules into one when they carry the same policy.
|
|
45
159
|
*
|
|
46
160
|
* @param sourceRule The source rule to merge into.
|
|
47
161
|
* @param ruleToMerge The rule to merge into the source rule.
|
|
48
162
|
*
|
|
49
|
-
* @returns The combined {@link DeclarativeRule}
|
|
163
|
+
* @returns The combined {@link DeclarativeRule}, or `null` if the two rules
|
|
164
|
+
* carry different policies and must stay separate.
|
|
50
165
|
*/ static combineRulePair(sourceRule, ruleToMerge) {
|
|
51
166
|
// Deep copy to drop references to source rule
|
|
52
167
|
const resultRule = JSON.parse(JSON.stringify(sourceRule));
|
|
@@ -60,6 +175,10 @@ import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
|
60
175
|
if (!cspHeaderInfoToMerge) {
|
|
61
176
|
return resultRule;
|
|
62
177
|
}
|
|
178
|
+
const currentValue = CspConverter.getCspValue(resultRule);
|
|
179
|
+
if (currentValue !== null && cspHeaderInfoToMerge.value !== undefined && !CspConverter.canMergeCspValues(currentValue, cspHeaderInfoToMerge.value)) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
63
182
|
// Deep copy to avoid reference issues
|
|
64
183
|
cspHeaderInfoToMerge = JSON.parse(JSON.stringify(cspHeaderInfoToMerge));
|
|
65
184
|
/**
|
|
@@ -73,14 +192,8 @@ import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
|
73
192
|
if (cspHeaderIndex === -1) {
|
|
74
193
|
return resultRule;
|
|
75
194
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
* - if `true` - append value from the rule to merge;
|
|
79
|
-
* - if `false` - set value from the rule to merge.
|
|
80
|
-
*/ const cspHeaderValue = resultHeaders[cspHeaderIndex].value;
|
|
81
|
-
if (cspHeaderValue) {
|
|
82
|
-
resultHeaders[cspHeaderIndex].value = `${cspHeaderValue}; ${cspHeaderInfoToMerge.value}`;
|
|
83
|
-
} else {
|
|
195
|
+
// Only identical policies reach this point, so a set value is kept.
|
|
196
|
+
if (!resultHeaders[cspHeaderIndex].value) {
|
|
84
197
|
resultHeaders[cspHeaderIndex].value = cspHeaderInfoToMerge.value;
|
|
85
198
|
}
|
|
86
199
|
} else {
|
|
@@ -100,6 +213,14 @@ import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
|
100
213
|
groupConverted(converted) {
|
|
101
214
|
return RegularRuleConverter.groupConvertedRules(converted, CspConverter.createRuleTemplate, CspConverter.combineRulePair);
|
|
102
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates a CSP converter.
|
|
218
|
+
*
|
|
219
|
+
* @param webAccessibleResourcesPath Path to web-accessible resources.
|
|
220
|
+
* @param excludedRequestDomains Request domains excluded from source rules.
|
|
221
|
+
*/ constructor(webAccessibleResourcesPath, excludedRequestDomains = new Map()){
|
|
222
|
+
super(webAccessibleResourcesPath), this.excludedRequestDomains = excludedRequestDomains;
|
|
223
|
+
}
|
|
103
224
|
}
|
|
104
225
|
|
|
105
|
-
export { CspConverter };
|
|
226
|
+
export { CspConverter, isCspDeclarativeRule };
|
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import { DomainUtils } from '@adguard/agtree';
|
|
2
|
+
import { OPTION_NAMES } from '../rule/option-names.js';
|
|
3
|
+
import { RuleDeclarativeValidator } from '../rule/rule-validator.js';
|
|
4
|
+
import { prepareASCII } from '../utils/string.js';
|
|
5
|
+
import { isCspRule } from './action-kind.js';
|
|
6
|
+
import { coversCondition, coversNonUrlConditions, isDomainCovered } from './csp-condition-coverage.js';
|
|
7
|
+
import { RegularRuleConverter } from './regular-rule-converter.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolves CSP exceptions before rules are converted to DNR.
|
|
11
|
+
*/ class CspRulesResolver {
|
|
12
|
+
/**
|
|
13
|
+
* Returns why a CSP exception cannot enter resolution.
|
|
14
|
+
*
|
|
15
|
+
* An exception which would lose a part of its condition or whose condition
|
|
16
|
+
* cannot be built at all is not resolved: it is reported as unsupported
|
|
17
|
+
* instead of silently matching more than written. The reason is returned
|
|
18
|
+
* instead of a boolean, so the caller reports it as is.
|
|
19
|
+
*
|
|
20
|
+
* @param exceptionRule CSP exception rule.
|
|
21
|
+
*
|
|
22
|
+
* @returns Reason of the rejection, or `null` if the exception can enter
|
|
23
|
+
* resolution.
|
|
24
|
+
*/ static getUnprocessableReason(exceptionRule) {
|
|
25
|
+
if (!RegularRuleConverter.areDomainConditionsRepresentable(exceptionRule)) {
|
|
26
|
+
return 'Network allowlist rule with $csp modifier has a domain condition' + ' which cannot be represented in DNR';
|
|
27
|
+
}
|
|
28
|
+
if (!RegularRuleConverter.areConditionRestrictionsRepresentable(exceptionRule)) {
|
|
29
|
+
return 'Network allowlist rule with $csp modifier combines an inclusion' + ' with an exclusion in the same condition, which cannot be represented' + ' in DNR';
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return RuleDeclarativeValidator.shouldProcessCspException(exceptionRule) ? null : CspRulesResolver.UNRESOLVABLE_EXCEPTION_MESSAGE;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return error instanceof Error ? error.message : CspRulesResolver.UNRESOLVABLE_EXCEPTION_MESSAGE;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks whether an exception applies to the blocking rule's CSP value.
|
|
39
|
+
*
|
|
40
|
+
* An exception applies only when it outranks the blocking rule:
|
|
41
|
+
* `$important` must win over a regular allowlist rule. A valued exception
|
|
42
|
+
* additionally applies only when both values are equal, while an exception
|
|
43
|
+
* without a value applies to any value.
|
|
44
|
+
*
|
|
45
|
+
* @param exceptionRule CSP exception rule.
|
|
46
|
+
* @param blockingRule Blocking CSP rule.
|
|
47
|
+
*
|
|
48
|
+
* @returns `true` if the exception applies to the blocking rule's value.
|
|
49
|
+
*/ static hasApplicableValue(exceptionRule, blockingRule) {
|
|
50
|
+
if (exceptionRule.priority <= blockingRule.priority) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const exceptionValue = exceptionRule.cspDirective;
|
|
54
|
+
if (!exceptionValue) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return exceptionValue === blockingRule.cspDirective;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Checks whether an exception covers the whole condition of a blocking CSP
|
|
61
|
+
* rule: the same URL identity and no narrower value in any other field.
|
|
62
|
+
*
|
|
63
|
+
* Conditions of domain and header fields are compared by coverage, not by
|
|
64
|
+
* equality: the exception must not be narrower than the blocking rule in
|
|
65
|
+
* any of them.
|
|
66
|
+
*
|
|
67
|
+
* @param exceptionRule CSP exception rule.
|
|
68
|
+
* @param blockingRule Blocking CSP rule.
|
|
69
|
+
*
|
|
70
|
+
* @returns `true` if the exception covers the blocking rule's condition.
|
|
71
|
+
*/ static hasCoveringCondition(exceptionRule, blockingRule) {
|
|
72
|
+
return coversCondition(RegularRuleConverter.getCondition(exceptionRule), RegularRuleConverter.getCondition(blockingRule));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Checks whether an exception covers a blocking rule's non-URL conditions.
|
|
76
|
+
*
|
|
77
|
+
* Conditions of domain and header fields are compared by coverage: the
|
|
78
|
+
* exception must not be narrower than the blocking rule in any of them.
|
|
79
|
+
*
|
|
80
|
+
* @param exceptionRule CSP exception rule.
|
|
81
|
+
* @param blockingRule Blocking CSP rule.
|
|
82
|
+
*
|
|
83
|
+
* @returns `true` if the exception is not narrower than the blocking rule.
|
|
84
|
+
*/ static hasCoveringNonUrlConditions(exceptionRule, blockingRule) {
|
|
85
|
+
return coversNonUrlConditions(RegularRuleConverter.getCondition(exceptionRule), RegularRuleConverter.getCondition(blockingRule));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Normalizes a domain written in a rule pattern.
|
|
89
|
+
*
|
|
90
|
+
* The host in a URL is always lowercased, so a `$match-case` rule with an
|
|
91
|
+
* uppercase domain can never match a request: such a domain is kept as
|
|
92
|
+
* written instead of being normalized into a matching one.
|
|
93
|
+
*
|
|
94
|
+
* @param rule Rule the domain belongs to.
|
|
95
|
+
* @param domain Domain as written in the pattern.
|
|
96
|
+
*
|
|
97
|
+
* @returns Domain to compare, or `null` if it is not a valid domain.
|
|
98
|
+
*/ static normalizeDomain(rule, domain) {
|
|
99
|
+
// Punycode keeps the case of non-ASCII letters, so the domain is
|
|
100
|
+
// lowercased before the conversion to compare it with a host from a URL.
|
|
101
|
+
const normalizedDomain = prepareASCII(domain.toLowerCase());
|
|
102
|
+
// Validation expects the canonical lowercased form.
|
|
103
|
+
if (!DomainUtils.isValidDomainOrHostname(normalizedDomain)) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return rule.isModifierEnabled(OPTION_NAMES.MATCH_CASE) ? prepareASCII(domain) : normalizedDomain;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Checks whether an exception can match a request host.
|
|
110
|
+
*
|
|
111
|
+
* The host in a URL is always lowercased, so a `$match-case` exception
|
|
112
|
+
* whose pattern domain is written with uppercase letters can never match.
|
|
113
|
+
*
|
|
114
|
+
* @param exceptionRule CSP exception rule.
|
|
115
|
+
*
|
|
116
|
+
* @returns `true` if the exception can match a request host.
|
|
117
|
+
*/ static canMatchRequestHost(exceptionRule) {
|
|
118
|
+
if (!exceptionRule.isModifierEnabled(OPTION_NAMES.MATCH_CASE)) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
// Every pattern `getDomain()` accepts is also matched by
|
|
122
|
+
// `getAnchoredDomain()`, with the same result.
|
|
123
|
+
const domain = CspRulesResolver.getAnchoredDomain(exceptionRule);
|
|
124
|
+
return domain === null || domain === domain.toLowerCase();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Extracts a domain from a clean domain-anchored pattern.
|
|
128
|
+
*
|
|
129
|
+
* @param rule Rule to inspect.
|
|
130
|
+
*
|
|
131
|
+
* @returns ASCII domain or `null` for a non-domain-only pattern.
|
|
132
|
+
*/ static getDomain(rule) {
|
|
133
|
+
const match = /^\|\|([^*/^|]+)\^$/.exec(rule.pattern);
|
|
134
|
+
if (!match) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return CspRulesResolver.normalizeDomain(rule, match[1]);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Extracts the domain from an anchored pattern without broad wildcards.
|
|
141
|
+
*
|
|
142
|
+
* The pattern may end right after the domain:
|
|
143
|
+
* `||example.com` matches the same hosts as `||example.com^` plus the hosts
|
|
144
|
+
* which only start with the domain, so it is at least as wide. The domain
|
|
145
|
+
* of a pattern without a separator is a prefix rather than a host, so the
|
|
146
|
+
* coverage of its subdomains is approximate, as for `||example.com/`.
|
|
147
|
+
*
|
|
148
|
+
* @param rule Rule to inspect.
|
|
149
|
+
*
|
|
150
|
+
* @returns ASCII domain or `null` for an unsupported pattern.
|
|
151
|
+
*/ static getAnchoredDomain(rule) {
|
|
152
|
+
const match = /^\|\|([^*/^|]+)(?:\^|\/|$)/.exec(rule.pattern);
|
|
153
|
+
if (!match) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
return CspRulesResolver.normalizeDomain(rule, match[1]);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Extracts the domain from an anchored pattern which ends with a separator.
|
|
160
|
+
*
|
|
161
|
+
* A pattern without the separator (`||example.com`) also matches the hosts
|
|
162
|
+
* which only start with the domain, so it is wider than the separated form
|
|
163
|
+
* and cannot be treated as a plain domain when a full cancellation is
|
|
164
|
+
* decided.
|
|
165
|
+
*
|
|
166
|
+
* @param rule Rule to inspect.
|
|
167
|
+
*
|
|
168
|
+
* @returns ASCII domain or `null` for an unterminated or unsupported pattern.
|
|
169
|
+
*/ static getTerminatedDomain(rule) {
|
|
170
|
+
const match = /^\|\|([^*/^|]+)(?:\^|\/)/.exec(rule.pattern);
|
|
171
|
+
if (!match) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
return CspRulesResolver.normalizeDomain(rule, match[1]);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Extracts the domain from a clean domain pattern of an exception.
|
|
178
|
+
*
|
|
179
|
+
* Both `||example.com^` and `||example.com` are accepted: the second form
|
|
180
|
+
* matches the same hosts plus the hosts which only start with the domain,
|
|
181
|
+
* so it covers a blocking rule with the separator. A pattern with a path is
|
|
182
|
+
* narrower, so it is not a domain exception and returns `null`.
|
|
183
|
+
*
|
|
184
|
+
* @param rule Rule to inspect.
|
|
185
|
+
*
|
|
186
|
+
* @returns ASCII domain or `null` for a non-domain-only pattern.
|
|
187
|
+
*/ static getExceptionDomain(rule) {
|
|
188
|
+
const match = /^\|\|([^*/^|]+)\^?$/.exec(rule.pattern);
|
|
189
|
+
if (!match) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
return CspRulesResolver.normalizeDomain(rule, match[1]);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Checks whether a domain exception covers a domain blocking rule.
|
|
196
|
+
*
|
|
197
|
+
* @param exceptionRule CSP exception rule.
|
|
198
|
+
* @param blockingRule Blocking CSP rule.
|
|
199
|
+
*
|
|
200
|
+
* @returns `true` if the exception covers the blocking rule.
|
|
201
|
+
*/ static coversBlockingDomain(exceptionRule, blockingRule) {
|
|
202
|
+
const exceptionDomain = CspRulesResolver.getExceptionDomain(exceptionRule);
|
|
203
|
+
const blockingDomain = CspRulesResolver.getTerminatedDomain(blockingRule);
|
|
204
|
+
return exceptionDomain !== null && blockingDomain !== null && isDomainCovered(blockingDomain, exceptionDomain) && CspRulesResolver.hasCoveringNonUrlConditions(exceptionRule, blockingRule);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Checks whether an exception covers a blocking rule entirely.
|
|
208
|
+
*
|
|
209
|
+
* A pattern-less exception applies to every URL, so it covers any blocking
|
|
210
|
+
* rule whose non-URL conditions it is not narrower than. A `*`-only
|
|
211
|
+
* pattern matches every URL as well and is treated the same way. This
|
|
212
|
+
* handles global blockers narrowed by `$denyallow`/`$domain` that a
|
|
213
|
+
* pattern-less exception fully covers.
|
|
214
|
+
*
|
|
215
|
+
* @param exceptionRule CSP exception rule.
|
|
216
|
+
* @param blockingRule Blocking CSP rule.
|
|
217
|
+
*
|
|
218
|
+
* @returns `true` if the exception covers the whole blocking rule.
|
|
219
|
+
*/ static coversWholeRule(exceptionRule, blockingRule) {
|
|
220
|
+
return (!exceptionRule.pattern || exceptionRule.pattern === '*') && CspRulesResolver.hasCoveringNonUrlConditions(exceptionRule, blockingRule);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Checks whether two rules cannot match the same request domain.
|
|
224
|
+
*
|
|
225
|
+
* Only terminated domains are compared: a pattern which ends right after
|
|
226
|
+
* the domain (`||example.com`) matches every host which starts with it, so
|
|
227
|
+
* it is a host prefix rather than a host and cannot be called disjoint
|
|
228
|
+
* from `example.company`.
|
|
229
|
+
*
|
|
230
|
+
* @param exceptionRule CSP exception rule.
|
|
231
|
+
* @param blockingRule Blocking CSP rule.
|
|
232
|
+
*
|
|
233
|
+
* @returns `true` if the domain scopes are disjoint.
|
|
234
|
+
*/ static areDomainsDisjoint(exceptionRule, blockingRule) {
|
|
235
|
+
const exceptionDomain = CspRulesResolver.getTerminatedDomain(exceptionRule);
|
|
236
|
+
const blockingDomain = CspRulesResolver.getTerminatedDomain(blockingRule);
|
|
237
|
+
if (!exceptionDomain || !blockingDomain) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
return !isDomainCovered(exceptionDomain, blockingDomain) && !isDomainCovered(blockingDomain, exceptionDomain);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Checks whether two rules cannot match the same initiator domain.
|
|
244
|
+
*
|
|
245
|
+
* @param exceptionRule CSP exception rule.
|
|
246
|
+
* @param blockingRule Blocking CSP rule.
|
|
247
|
+
*
|
|
248
|
+
* @returns `true` if the initiator domain scopes are disjoint.
|
|
249
|
+
*/ static areInitiatorDomainsDisjoint(exceptionRule, blockingRule) {
|
|
250
|
+
const exceptionDomains = RegularRuleConverter.getCondition(exceptionRule).initiatorDomains;
|
|
251
|
+
const blockingDomains = RegularRuleConverter.getCondition(blockingRule).initiatorDomains;
|
|
252
|
+
if (!exceptionDomains || !blockingDomains) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
return exceptionDomains.every((exceptionDomain)=>blockingDomains.every((blockingDomain)=>!isDomainCovered(exceptionDomain, blockingDomain) && !isDomainCovered(blockingDomain, exceptionDomain)));
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Gets a domain which can be excluded from a global blocking rule.
|
|
259
|
+
*
|
|
260
|
+
* @param exceptionRule CSP exception rule.
|
|
261
|
+
* @param blockingRule Blocking CSP rule.
|
|
262
|
+
*
|
|
263
|
+
* @returns Domain to exclude or `null` if subtraction is unsupported.
|
|
264
|
+
*/ static getExcludedDomain(exceptionRule, blockingRule) {
|
|
265
|
+
if (!CspRulesResolver.hasCoveringNonUrlConditions(exceptionRule, blockingRule)) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
const exceptionDomain = CspRulesResolver.getDomain(exceptionRule);
|
|
269
|
+
if (!exceptionDomain) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
// A domain that can never match a request must not be excluded from a blocker.
|
|
273
|
+
if (!CspRulesResolver.canMatchRequestHost(exceptionRule)) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
if (CspRulesResolver.areDomainsDisjoint(exceptionRule, blockingRule)) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
// The additional `main_frame` rule of the blocker matches by the
|
|
280
|
+
// target host, while an exception with a permitted `$domain` matches
|
|
281
|
+
// by the initiator only, so the exclusion would drop the policy for
|
|
282
|
+
// navigations the exception does not cover. The exclusion is set per
|
|
283
|
+
// source rule, so the base rule does not get it either.
|
|
284
|
+
//
|
|
285
|
+
// The overlap is impossible when the target domains and the excluded
|
|
286
|
+
// domain do not cover each other: the additional rule never matches
|
|
287
|
+
// the excluded host, and the base rule is guarded by the covering
|
|
288
|
+
// check above.
|
|
289
|
+
const targetDomains = RegularRuleConverter.getCondition(blockingRule).initiatorDomains ?? [];
|
|
290
|
+
const intersectsTargetDomains = targetDomains.some((domain)=>isDomainCovered(domain, exceptionDomain) || isDomainCovered(exceptionDomain, domain));
|
|
291
|
+
if ((exceptionRule.permittedDomains?.length ?? 0) > 0 && RegularRuleConverter.hasTargetDomainRule(blockingRule) && intersectsTargetDomains) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
return exceptionDomain;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Classifies how a CSP exception affects a blocking rule.
|
|
298
|
+
*
|
|
299
|
+
* @param exceptionRule CSP exception rule.
|
|
300
|
+
* @param blockingRule Blocking CSP rule.
|
|
301
|
+
*
|
|
302
|
+
* @returns Pair resolution result.
|
|
303
|
+
*/ static classifyPair(exceptionRule, blockingRule) {
|
|
304
|
+
try {
|
|
305
|
+
return CspRulesResolver.classifyProcessablePair(exceptionRule, blockingRule);
|
|
306
|
+
} catch {
|
|
307
|
+
// A condition which cannot be built, for example a domain with a
|
|
308
|
+
// label punycode cannot encode, must not fail the whole
|
|
309
|
+
// conversion: the pair is left untouched and the exception is
|
|
310
|
+
// reported as unsupported by `resolve()`.
|
|
311
|
+
return {
|
|
312
|
+
cancels: false,
|
|
313
|
+
excludedDomain: null,
|
|
314
|
+
safe: false
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Classifies a pair of rules whose conditions can be built.
|
|
320
|
+
*
|
|
321
|
+
* @param exceptionRule CSP exception rule.
|
|
322
|
+
* @param blockingRule Blocking CSP rule.
|
|
323
|
+
*
|
|
324
|
+
* @returns Pair resolution result.
|
|
325
|
+
*/ static classifyProcessablePair(exceptionRule, blockingRule) {
|
|
326
|
+
const hasApplicableValue = CspRulesResolver.hasApplicableValue(exceptionRule, blockingRule);
|
|
327
|
+
const cancels = hasApplicableValue && (CspRulesResolver.hasCoveringCondition(exceptionRule, blockingRule) || CspRulesResolver.coversBlockingDomain(exceptionRule, blockingRule) || CspRulesResolver.coversWholeRule(exceptionRule, blockingRule));
|
|
328
|
+
const excludedDomain = hasApplicableValue && !cancels ? CspRulesResolver.getExcludedDomain(exceptionRule, blockingRule) : null;
|
|
329
|
+
return {
|
|
330
|
+
cancels,
|
|
331
|
+
excludedDomain,
|
|
332
|
+
safe: !hasApplicableValue || cancels || excludedDomain !== null || !CspRulesResolver.canMatchRequestHost(exceptionRule) || CspRulesResolver.areDomainsDisjoint(exceptionRule, blockingRule) || CspRulesResolver.areInitiatorDomainsDisjoint(exceptionRule, blockingRule)
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Creates a cached CSP pair result getter.
|
|
337
|
+
*
|
|
338
|
+
* @returns Pair result getter scoped to one resolution.
|
|
339
|
+
*/ static createPairResultGetter() {
|
|
340
|
+
const pairResults = new Map();
|
|
341
|
+
return (exceptionRule, blockingRule)=>{
|
|
342
|
+
let resultsForException = pairResults.get(exceptionRule);
|
|
343
|
+
if (!resultsForException) {
|
|
344
|
+
resultsForException = new Map();
|
|
345
|
+
pairResults.set(exceptionRule, resultsForException);
|
|
346
|
+
}
|
|
347
|
+
let result = resultsForException.get(blockingRule);
|
|
348
|
+
if (!result) {
|
|
349
|
+
result = CspRulesResolver.classifyPair(exceptionRule, blockingRule);
|
|
350
|
+
resultsForException.set(blockingRule, result);
|
|
351
|
+
}
|
|
352
|
+
return result;
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Resolves CSP exceptions against blocking rules.
|
|
357
|
+
*
|
|
358
|
+
* Each exception is applied per pair with a blocking rule: a pair cancels
|
|
359
|
+
* the blocking rule when the exception fully covers it, or contributes an
|
|
360
|
+
* excluded request domain when the exception is a clean domain. An
|
|
361
|
+
* exception is never required to be safe against every blocking rule; an
|
|
362
|
+
* unsafe pair simply leaves its blocking rule untouched while other pairs
|
|
363
|
+
* of the same exception still apply.
|
|
364
|
+
*
|
|
365
|
+
* @param rules CSP rules from active sources.
|
|
366
|
+
*
|
|
367
|
+
* @returns Remaining rules and exception resolution details.
|
|
368
|
+
*/ static resolve(rules) {
|
|
369
|
+
const cspRules = rules.filter(isCspRule);
|
|
370
|
+
const blockingRules = cspRules.filter((rule)=>!rule.allowlist);
|
|
371
|
+
const exceptionRules = cspRules.filter((rule)=>rule.allowlist);
|
|
372
|
+
const processableExceptions = exceptionRules.filter((exceptionRule)=>CspRulesResolver.getUnprocessableReason(exceptionRule) === null);
|
|
373
|
+
const getPairResult = CspRulesResolver.createPairResultGetter();
|
|
374
|
+
const appliedExceptions = new Set();
|
|
375
|
+
const excludedRequestDomains = new Map();
|
|
376
|
+
const remainingRules = blockingRules.filter((blockingRule)=>{
|
|
377
|
+
let cancelled = false;
|
|
378
|
+
const excludedDomains = [];
|
|
379
|
+
processableExceptions.forEach((exceptionRule)=>{
|
|
380
|
+
const pair = getPairResult(exceptionRule, blockingRule);
|
|
381
|
+
if (pair.cancels) {
|
|
382
|
+
cancelled = true;
|
|
383
|
+
appliedExceptions.add(exceptionRule);
|
|
384
|
+
} else if (pair.excludedDomain !== null) {
|
|
385
|
+
excludedDomains.push(pair.excludedDomain);
|
|
386
|
+
appliedExceptions.add(exceptionRule);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
if (cancelled) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
if (excludedDomains.length > 0) {
|
|
393
|
+
excludedRequestDomains.set(blockingRule, Array.from(new Set(excludedDomains)));
|
|
394
|
+
}
|
|
395
|
+
return true;
|
|
396
|
+
});
|
|
397
|
+
const unsupportedExceptions = exceptionRules.filter((exceptionRule)=>{
|
|
398
|
+
if (!processableExceptions.includes(exceptionRule)) {
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
if (appliedExceptions.has(exceptionRule)) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
return blockingRules.some((blockingRule)=>!getPairResult(exceptionRule, blockingRule).safe);
|
|
405
|
+
});
|
|
406
|
+
return {
|
|
407
|
+
rules: remainingRules,
|
|
408
|
+
excludedRequestDomains,
|
|
409
|
+
unsupportedExceptions
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Error message for an exception which cannot be applied safely.
|
|
415
|
+
*/ CspRulesResolver.UNRESOLVABLE_EXCEPTION_MESSAGE = 'Network allowlist rule with $csp modifier' + ' cannot be resolved safely';
|
|
416
|
+
|
|
417
|
+
export { CspRulesResolver };
|