@adguard/dnr-converter 1.0.0-alpha.2 → 1.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.
@@ -0,0 +1,190 @@
1
+ /**
2
+ * Popular TLD suffixes used to expand wildcard TLD domains for DNR.
3
+ *
4
+ * Source: manually curated from the IANA Root Zone Database
5
+ * (https://www.iana.org/domains/root/db). "Popular" means broad practical
6
+ * coverage for filter rules: common generic TLDs, commonly registered ccTLDs,
7
+ * common new gTLDs, and TLDs observed in AdGuard filter domain modifiers. This
8
+ * is not a ranked list or a full public suffix list; update it manually when
9
+ * wildcard TLD coverage needs to change.
10
+ *
11
+ * Keep values lowercase, ASCII, and without a leading dot.
12
+ */ const POPULAR_TLDS = [
13
+ 'ac',
14
+ 'ae',
15
+ 'agency',
16
+ 'ai',
17
+ 'am',
18
+ 'app',
19
+ 'ar',
20
+ 'art',
21
+ 'at',
22
+ 'au',
23
+ 'az',
24
+ 'bd',
25
+ 'be',
26
+ 'bg',
27
+ 'biz',
28
+ 'blog',
29
+ 'br',
30
+ 'by',
31
+ 'ca',
32
+ 'cam',
33
+ 'cc',
34
+ 'center',
35
+ 'cfd',
36
+ 'ch',
37
+ 'cl',
38
+ 'click',
39
+ 'cloud',
40
+ 'club',
41
+ 'cn',
42
+ 'co',
43
+ 'co.in',
44
+ 'co.jp',
45
+ 'co.kr',
46
+ 'co.nz',
47
+ 'co.uk',
48
+ 'com',
49
+ 'com.au',
50
+ 'com.br',
51
+ 'com.cn',
52
+ 'com.mx',
53
+ 'com.pl',
54
+ 'com.tr',
55
+ 'com.ua',
56
+ 'company',
57
+ 'cz',
58
+ 'de',
59
+ 'dev',
60
+ 'digital',
61
+ 'dk',
62
+ 'edu',
63
+ 'ee',
64
+ 'eg',
65
+ 'email',
66
+ 'es',
67
+ 'eu',
68
+ 'fi',
69
+ 'fm',
70
+ 'fr',
71
+ 'fun',
72
+ 'ge',
73
+ 'gg',
74
+ 'gov',
75
+ 'gr',
76
+ 'group',
77
+ 'hk',
78
+ 'hr',
79
+ 'hu',
80
+ 'icu',
81
+ 'id',
82
+ 'ie',
83
+ 'il',
84
+ 'im',
85
+ 'in',
86
+ 'info',
87
+ 'ink',
88
+ 'int',
89
+ 'io',
90
+ 'ir',
91
+ 'is',
92
+ 'it',
93
+ 'jobs',
94
+ 'jp',
95
+ 'ke',
96
+ 'kr',
97
+ 'kz',
98
+ 'la',
99
+ 'li',
100
+ 'life',
101
+ 'link',
102
+ 'live',
103
+ 'lk',
104
+ 'lol',
105
+ 'lt',
106
+ 'lu',
107
+ 'lv',
108
+ 'ma',
109
+ 'md',
110
+ 'me',
111
+ 'media',
112
+ 'mil',
113
+ 'mobi',
114
+ 'mx',
115
+ 'my',
116
+ 'name',
117
+ 'net',
118
+ 'net.au',
119
+ 'network',
120
+ 'news',
121
+ 'ng',
122
+ 'nl',
123
+ 'no',
124
+ 'np',
125
+ 'nu',
126
+ 'nz',
127
+ 'one',
128
+ 'onion',
129
+ 'online',
130
+ 'org',
131
+ 'org.uk',
132
+ 'pe',
133
+ 'ph',
134
+ 'pk',
135
+ 'pl',
136
+ 'plus',
137
+ 'porn',
138
+ 'pro',
139
+ 'pt',
140
+ 'pub',
141
+ 'pw',
142
+ 'ro',
143
+ 'rs',
144
+ 'ru',
145
+ 'sa',
146
+ 'sbs',
147
+ 'se',
148
+ 'sg',
149
+ 'shop',
150
+ 'si',
151
+ 'site',
152
+ 'sk',
153
+ 'software',
154
+ 'solutions',
155
+ 'space',
156
+ 'store',
157
+ 'su',
158
+ 'sx',
159
+ 'systems',
160
+ 'tech',
161
+ 'th',
162
+ 'to',
163
+ 'today',
164
+ 'tokyo',
165
+ 'top',
166
+ 'tr',
167
+ 'travel',
168
+ 'tube',
169
+ 'tv',
170
+ 'tw',
171
+ 'ua',
172
+ 'uk',
173
+ 'us',
174
+ 'video',
175
+ 'vip',
176
+ 'vn',
177
+ 'website',
178
+ 'wiki',
179
+ 'win',
180
+ 'work',
181
+ 'world',
182
+ 'ws',
183
+ 'xn--p1ai',
184
+ 'xxx',
185
+ 'xyz',
186
+ 'za',
187
+ 'zone'
188
+ ];
189
+
190
+ export { POPULAR_TLDS };
package/dist/constants.js CHANGED
@@ -16,8 +16,11 @@
16
16
  * This is a wildcard character. It is used to represent "any set of characters".
17
17
  * This can also be an empty string or a string of any length.
18
18
  */ const MASK_ANY_CHARACTER = '*';
19
+ /**
20
+ * Dot character, used as a domain label separator.
21
+ */ const DOT = '.';
19
22
  /**
20
23
  * Space character.
21
24
  */ const SPACE_CHARACTER = ' ';
22
25
 
23
- export { CSP_HEADER_NAME, MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, PERMISSIONS_POLICY_HEADER_NAME, SPACE_CHARACTER };
26
+ export { CSP_HEADER_NAME, DOT, MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, PERMISSIONS_POLICY_HEADER_NAME, SPACE_CHARACTER };
@@ -1,3 +1,3 @@
1
- var version = "1.0.0-alpha.2";
1
+ var version = "1.1.0";
2
2
 
3
3
  export { version };
package/dist/rule/rule.js CHANGED
@@ -1,4 +1,4 @@
1
- import { RuleParser, RuleCategory, NetworkRuleType } from '@adguard/agtree';
1
+ import { DomainListParser, RuleParser, RuleCategory, NetworkRuleType } from '@adguard/agtree';
2
2
  import { RuleConverter } from '@adguard/agtree/converter';
3
3
  import { RuleGenerator } from '@adguard/agtree/generator';
4
4
  import { MASK_REGEX_RULE } from '../constants.js';
@@ -9,6 +9,9 @@ import { OPTION_NAMES } from './option-names.js';
9
9
  import { RuleBadfilter } from './rule-badfilter.js';
10
10
  import { RulePriority } from './rule-priority.js';
11
11
 
12
+ /**
13
+ * Separator used in network-rule modifier domain lists (`$domain=a|b`).
14
+ */ const DOMAIN_LIST_PIPE_SEPARATOR = '|';
12
15
  /**
13
16
  * Maps canonical content-type modifier names to their DNR `ResourceType`
14
17
  * equivalents. Only modifier names that correspond to adblock filter syntax
@@ -315,6 +318,32 @@ import { RulePriority } from './rule-priority.js';
315
318
  isRequest
316
319
  };
317
320
  }
321
+ /**
322
+ * Parses a pipe-separated domain list, keeping `/regex/` tokens intact
323
+ * (so `|` inside a regexp is not treated as a domain separator).
324
+ *
325
+ * @param value Raw `$domain` / `$to` / `$denyallow` modifier value.
326
+ *
327
+ * @returns Permitted and restricted domain lists.
328
+ */ static parseDomainList(value) {
329
+ const permitted = [];
330
+ const restricted = [];
331
+ const node = DomainListParser.parse(value, undefined, 0, DOMAIN_LIST_PIPE_SEPARATOR);
332
+ for (const { exception, value: domain } of node.children){
333
+ if (!domain) {
334
+ continue;
335
+ }
336
+ if (exception) {
337
+ restricted.push(domain);
338
+ } else {
339
+ permitted.push(domain);
340
+ }
341
+ }
342
+ return {
343
+ permitted,
344
+ restricted
345
+ };
346
+ }
318
347
  /**
319
348
  * Parses the `$header` modifier value into an {@link HttpHeaderMatcher}.
320
349
  *
@@ -662,16 +691,7 @@ import { RulePriority } from './rule-priority.js';
662
691
  }
663
692
  case OPTION_NAMES.DOMAIN:
664
693
  {
665
- const parts = value.split('|').filter(Boolean);
666
- const permitted = [];
667
- const restricted = [];
668
- for (const part of parts){
669
- if (part.startsWith('~')) {
670
- restricted.push(part.slice(1));
671
- } else {
672
- permitted.push(part);
673
- }
674
- }
694
+ const { permitted, restricted } = Rule.parseDomainList(value);
675
695
  if (permitted.length > 0) {
676
696
  this.permittedDomains = permitted;
677
697
  meta.permittedDomainCount = permitted.length;
@@ -684,33 +704,24 @@ import { RulePriority } from './rule-priority.js';
684
704
  }
685
705
  case OPTION_NAMES.DENYALLOW:
686
706
  {
687
- const parts = value.split('|').filter(Boolean);
688
- if (parts.some((p)=>p.startsWith('~'))) {
689
- throw new SyntaxError('Modifier $denyallow domains cannot be negated');
707
+ const { permitted, restricted } = Rule.parseDomainList(value);
708
+ if (restricted.length > 0) {
709
+ throw new SyntaxError('modifier $denyallow domains cannot be negated');
690
710
  }
691
- if (parts.length > 0) {
692
- this.denyAllowDomains = parts;
711
+ if (permitted.length > 0) {
712
+ this.denyAllowDomains = permitted;
693
713
  meta.baseModifierCount += 1;
694
714
  }
695
715
  break;
696
716
  }
697
717
  case OPTION_NAMES.TO:
698
718
  {
699
- const parts = value.split('|').filter(Boolean);
700
- const toPermitted = [];
701
- const toRestricted = [];
702
- for (const part of parts){
703
- if (part.startsWith('~')) {
704
- toRestricted.push(part.slice(1));
705
- } else {
706
- toPermitted.push(part);
707
- }
708
- }
709
- if (toPermitted.length > 0) {
710
- this.permittedToDomains = toPermitted;
719
+ const { permitted, restricted } = Rule.parseDomainList(value);
720
+ if (permitted.length > 0) {
721
+ this.permittedToDomains = permitted;
711
722
  }
712
- if (toRestricted.length > 0) {
713
- this.restrictedToDomains = toRestricted;
723
+ if (restricted.length > 0) {
724
+ this.restrictedToDomains = restricted;
714
725
  }
715
726
  meta.baseModifierCount += 1;
716
727
  break;
@@ -754,6 +765,7 @@ import { RulePriority } from './rule-priority.js';
754
765
  case OPTION_NAMES.HLS:
755
766
  case OPTION_NAMES.REFERRERPOLICY:
756
767
  case OPTION_NAMES.PERMISSIONS:
768
+ case OPTION_NAMES.URLTRANSFORM:
757
769
  this.enabledModifiers.add(name);
758
770
  this.advancedModifierName = rawName;
759
771
  this.advancedModifierValue = value || null;
@@ -1,13 +1,14 @@
1
1
  import { RuleGenerator } from '@adguard/agtree/generator';
2
2
  import { getRedirectFilename } from '@adguard/scriptlets/redirects';
3
- import { MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, CSP_HEADER_NAME, PERMISSIONS_POLICY_HEADER_NAME, MASK_ANY_CHARACTER } from '../constants.js';
3
+ import { MASK_ANY_CHARACTER, MASK_NEGATE_CHARACTER, MASK_REGEX_RULE, CSP_HEADER_NAME, PERMISSIONS_POLICY_HEADER_NAME, DOT } from '../constants.js';
4
+ import { POPULAR_TLDS } from '../constants/popular-tlds.js';
4
5
  import { isConversionError } from '../errors/conversion-errors/index.js';
5
6
  import { ResourcesPathError } from '../errors/converter-options-errors/resources-path-error.js';
6
7
  import { re2Validator } from '../re2-regexp/re2-validator.js';
7
8
  import { OPTION_NAMES } from '../rule/option-names.js';
8
9
  import { RuleDeclarativeValidator } from '../rule/rule-validator.js';
9
10
  import { getErrorMessage } from '../utils/error.js';
10
- import { prepareASCII, removeSlashes, isRegexPattern, toASCII } from '../utils/string.js';
11
+ import { isRegexPattern, toASCII, prepareASCII, removeSlashes } from '../utils/string.js';
11
12
  import { HeaderOperation } from '../declarative-rule/modify-header-info.js';
12
13
  import { RuleActionType } from '../declarative-rule/rule-action.js';
13
14
  import { DomainType, ResourceType } from '../declarative-rule/rule-condition.js';
@@ -27,6 +28,54 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
27
28
  * {@link RegularRuleConverter.convertRule} and {@link RegularRuleConverter.groupConvertedRules}
28
29
  * methods, which contains the general logic of transformation and grouping of rules.
29
30
  */ class RegularRuleConverter {
31
+ /**
32
+ * Checks whether a domain uses wildcard TLD syntax.
33
+ *
34
+ * @param domain Domain to check.
35
+ *
36
+ * @returns True when domain ends with `.*` and has a non-empty prefix.
37
+ */ static isWildcardTldDomain(domain) {
38
+ return domain.length > RegularRuleConverter.WILDCARD_TLD.length && domain.endsWith(RegularRuleConverter.WILDCARD_TLD);
39
+ }
40
+ /**
41
+ * Expands wildcard TLD domain into concrete popular TLD domains.
42
+ *
43
+ * @param domain Wildcard TLD domain.
44
+ *
45
+ * @returns Expanded domain list.
46
+ */ static expandWildcardTldDomain(domain) {
47
+ const domainPrefix = domain.slice(0, -RegularRuleConverter.WILDCARD_TLD.length);
48
+ return POPULAR_TLDS.map((tld)=>`${domainPrefix}.${tld}`);
49
+ }
50
+ /**
51
+ * Prepares domains for DNR condition arrays.
52
+ *
53
+ * Regex and non-TLD wildcard domains are not representable in DNR and are
54
+ * dropped; wildcard TLD domains (e.g. `example.*`) are expanded to the
55
+ * popular TLD list.
56
+ *
57
+ * @param domains Domains from network rule modifier.
58
+ *
59
+ * @returns ASCII-only unique domains supported by DNR.
60
+ */ static prepareDeclarativeDomains(domains) {
61
+ const expandedDomains = domains?.flatMap((domain)=>{
62
+ if (isRegexPattern(domain)) {
63
+ return [];
64
+ }
65
+ if (RegularRuleConverter.isWildcardTldDomain(domain)) {
66
+ return RegularRuleConverter.expandWildcardTldDomain(domain);
67
+ }
68
+ if (domain.includes(MASK_ANY_CHARACTER)) {
69
+ return [];
70
+ }
71
+ return [
72
+ domain
73
+ ];
74
+ }) ?? [];
75
+ return [
76
+ ...new Set(toASCII(expandedDomains))
77
+ ];
78
+ }
30
79
  /**
31
80
  * Checks if {@link Rule} can be converted to {@link RuleActionType.AllowAllRequests}.
32
81
  *
@@ -384,28 +433,28 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
384
433
  condition.domainType = DomainType.FirstParty;
385
434
  }
386
435
  // set `initiatorDomains`
387
- const permittedDomains = rule.permittedDomains?.filter((domain)=>!domain.includes(MASK_ANY_CHARACTER) && !isRegexPattern(domain));
388
- if (permittedDomains && permittedDomains.length > 0) {
389
- condition.initiatorDomains = toASCII(permittedDomains);
436
+ const permittedDomains = RegularRuleConverter.prepareDeclarativeDomains(rule.permittedDomains);
437
+ if (permittedDomains.length > 0) {
438
+ condition.initiatorDomains = permittedDomains;
390
439
  }
391
440
  // set `excludedInitiatorDomains`
392
- const excludedDomains = rule.restrictedDomains;
393
- if (excludedDomains && excludedDomains.length > 0) {
394
- condition.excludedInitiatorDomains = toASCII(excludedDomains);
441
+ const excludedDomains = RegularRuleConverter.prepareDeclarativeDomains(rule.restrictedDomains);
442
+ if (excludedDomains.length > 0) {
443
+ condition.excludedInitiatorDomains = excludedDomains;
395
444
  }
396
445
  // set `requestDomains`
397
- const { permittedToDomains } = rule;
398
- if (permittedToDomains && permittedToDomains.length > 0) {
399
- condition.requestDomains = toASCII(permittedToDomains);
446
+ const permittedToDomains = RegularRuleConverter.prepareDeclarativeDomains(rule.permittedToDomains);
447
+ if (permittedToDomains.length > 0) {
448
+ condition.requestDomains = permittedToDomains;
400
449
  }
401
450
  // Can be specified `$to` or `$denyallow`, but not together.
402
- const { denyAllowDomains } = rule;
403
- const { restrictedToDomains } = rule;
451
+ const denyAllowDomains = RegularRuleConverter.prepareDeclarativeDomains(rule.denyAllowDomains);
452
+ const restrictedToDomains = RegularRuleConverter.prepareDeclarativeDomains(rule.restrictedToDomains);
404
453
  // set `excludedRequestDomains`
405
- if (denyAllowDomains && denyAllowDomains.length !== 0) {
406
- condition.excludedRequestDomains = toASCII(denyAllowDomains);
407
- } else if (restrictedToDomains && restrictedToDomains.length !== 0) {
408
- condition.excludedRequestDomains = toASCII(restrictedToDomains);
454
+ if (denyAllowDomains.length !== 0) {
455
+ condition.excludedRequestDomains = denyAllowDomains;
456
+ } else if (restrictedToDomains.length !== 0) {
457
+ condition.excludedRequestDomains = restrictedToDomains;
409
458
  }
410
459
  // set `excludedResourceTypes`
411
460
  const { restrictedResourceTypes } = rule;
@@ -462,7 +511,7 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
462
511
  * And if we specify {@link ResourceType.MainFrame}, then we also need to specify all
463
512
  * other types, so that it works not only for document requests, but
464
513
  * also for all other types of requests.
465
- */ const shouldMatchAllResourcesTypes = rule.isModifierEnabled(OPTION_NAMES.REMOVEHEADER) || rule.isModifierEnabled(OPTION_NAMES.CSP) || rule.isModifierEnabled(OPTION_NAMES.COOKIE) || rule.isModifierEnabled(OPTION_NAMES.TO) || rule.isModifierEnabled(OPTION_NAMES.METHOD);
514
+ */ const shouldMatchAllResourcesTypes = rule.isModifierEnabled(OPTION_NAMES.REMOVEHEADER) || rule.isModifierEnabled(OPTION_NAMES.CSP) || rule.isModifierEnabled(OPTION_NAMES.COOKIE) || rule.isModifierEnabled(OPTION_NAMES.TO) || rule.isModifierEnabled(OPTION_NAMES.METHOD) || rule.isModifierEnabled(OPTION_NAMES.URLTRANSFORM);
466
515
  /**
467
516
  * `$permissions` and `$removeparam` modifiers must be applied only to `document` content-type
468
517
  * ({@link ResourceType.MainFrame} and {@link ResourceType.SubFrame}) if they don't have resource types.
@@ -785,5 +834,8 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
785
834
  * Pipe character used as a separator in $removeparam multi-value rules,
786
835
  * e.g. `$removeparam=utm_source|utm_medium`.
787
836
  */ RegularRuleConverter.PIPE_SEPARATOR = '|';
837
+ /**
838
+ * Wildcard TLD suffix (`.*`) used in domain modifiers, e.g. `example.*`.
839
+ */ RegularRuleConverter.WILDCARD_TLD = `${DOT}${MASK_ANY_CHARACTER}`;
788
840
 
789
841
  export { RegularRuleConverter };
@@ -217,6 +217,15 @@ declare class Rule {
217
217
  * headers.
218
218
  */
219
219
  private static computeRemoveHeaderInfo;
220
+ /**
221
+ * Parses a pipe-separated domain list, keeping `/regex/` tokens intact
222
+ * (so `|` inside a regexp is not treated as a domain separator).
223
+ *
224
+ * @param value Raw `$domain` / `$to` / `$denyallow` modifier value.
225
+ *
226
+ * @returns Permitted and restricted domain lists.
227
+ */
228
+ private static parseDomainList;
220
229
  /**
221
230
  * Parses the `$header` modifier value into an {@link HttpHeaderMatcher}.
222
231
  *
@@ -168,13 +168,20 @@ import { splitByDelimiterWithEscapeCharacter } from './utils/string.js';
168
168
  if (workPattern.startsWith('^')) {
169
169
  workPattern = workPattern.substring(1);
170
170
  }
171
- // Check for end-of-string anchor (\$ in filter syntax, which
172
- // becomes $ in regex). When present, we must NOT append the
173
- // remainder-capturing group (.*) after the pattern.
171
+ // Check for end-of-string anchor. It may be written either as `\$`
172
+ // (escaped, because `$` is the modifier separator in AdGuard syntax)
173
+ // or as a bare `$` when the modifier value is the last one in the rule.
174
+ // Both forms mean the same regex end-of-string anchor. When present,
175
+ // we must NOT append the remainder-capturing group `(.*)` after the
176
+ // pattern — otherwise the anchor would end up in the middle of the
177
+ // resulting regex, producing a rule that never matches.
174
178
  let hasEndAnchor = false;
175
179
  if (workPattern.endsWith('\\$')) {
176
180
  hasEndAnchor = true;
177
181
  workPattern = workPattern.substring(0, workPattern.length - 2);
182
+ } else if (workPattern.endsWith('$')) {
183
+ hasEndAnchor = true;
184
+ workPattern = workPattern.substring(0, workPattern.length - 1);
178
185
  }
179
186
  const userGroupCount = countCaptureGroups(workPattern);
180
187
  // Determine if this is a query-targeting pattern
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adguard/dnr-converter",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.1.0",
4
4
  "description": "A converter that transforms adblock-style filtering rules into rules compatible with the Declarative Net Request (DNR) API.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -57,11 +57,11 @@
57
57
  "@adguard/re2-wasm": "1.2.0"
58
58
  },
59
59
  "dependencies": {
60
- "@adguard/scriptlets": "2.4.2",
60
+ "@adguard/scriptlets": "2.5.0",
61
61
  "commander": "^12.1.0",
62
62
  "punycode": "2.3.1",
63
63
  "valibot": "^1.1.0",
64
- "@adguard/agtree": "^4.1.1",
64
+ "@adguard/agtree": "^4.2.0",
65
65
  "@adguard/logger": "^2.0.0"
66
66
  },
67
67
  "devDependencies": {
@@ -97,7 +97,7 @@
97
97
  "typescript": "^5.7.3",
98
98
  "vitest": "^4.1.5",
99
99
  "@adguard/eslint-plugin-logger-context": "^1.0.1",
100
- "@adguard/tsurlfilter": "^6.0.0"
100
+ "@adguard/tsurlfilter": "^6.0.2"
101
101
  },
102
102
  "scripts": {
103
103
  "lint": "pnpm lint:code && pnpm lint:types && pnpm lint:md",