@adguard/dnr-converter 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
- var version = "1.1.0";
1
+ var version = "1.1.2";
2
2
 
3
3
  export { version };
@@ -33,6 +33,7 @@
33
33
  MEDIA: 'media',
34
34
  FONT: 'font',
35
35
  WEBSOCKET: 'websocket',
36
+ WEBRTC: 'webrtc',
36
37
  OTHER: 'other',
37
38
  PING: 'ping',
38
39
  BADFILTER: 'badfilter',
@@ -44,6 +45,7 @@
44
45
  REMOVEPARAM: 'removeparam',
45
46
  REMOVEHEADER: 'removeheader',
46
47
  JSONPRUNE: 'jsonprune',
48
+ XMLPRUNE: 'xmlprune',
47
49
  HLS: 'hls',
48
50
  REFERRERPOLICY: 'referrerpolicy',
49
51
  APP: 'app',
@@ -420,10 +420,18 @@ name)=>{
420
420
  name: '$jsonprune',
421
421
  notSupported: true
422
422
  },
423
+ [OPTION_NAMES.XMLPRUNE]: {
424
+ name: '$xmlprune',
425
+ notSupported: true
426
+ },
423
427
  [OPTION_NAMES.HLS]: {
424
428
  name: '$hls',
425
429
  notSupported: true
426
430
  },
431
+ [OPTION_NAMES.WEBRTC]: {
432
+ name: '$webrtc',
433
+ notSupported: true
434
+ },
427
435
  // DNS modifiers.
428
436
  [OPTION_NAMES.CLIENT]: {
429
437
  name: '$client',
package/dist/rule/rule.js CHANGED
@@ -525,6 +525,11 @@ import { RulePriority } from './rule-priority.js';
525
525
  */ this.advancedModifierName = null;
526
526
  /**
527
527
  * Raw string value of the advanced modifier.
528
+ *
529
+ * `null` when the modifier has no value. The only exception is
530
+ * `$removeparam`: a value-less `$removeparam` is stored as `''`, because
531
+ * it carries a meaning of its own ("remove all query parameters") that
532
+ * must be distinguishable from "no value".
528
533
  */ this.advancedModifierValue = null;
529
534
  /**
530
535
  * Parsed data for the `$header` modifier.
@@ -567,6 +572,10 @@ import { RulePriority } from './rule-priority.js';
567
572
  const exception = modifier.exception ?? false;
568
573
  const value = modifier.value?.value ?? '';
569
574
  const name = rawName;
575
+ // A noop modifier consists only of underscores and does not affect the rule.
576
+ if (name.startsWith(OPTION_NAMES.NOOP) && !name.split(OPTION_NAMES.NOOP).some((part)=>!!part)) {
577
+ continue;
578
+ }
570
579
  if (exception && !NEGATABLE_MODIFIERS.has(name)) {
571
580
  throw new SyntaxError(`Modifier $${name} cannot be negated`);
572
581
  }
@@ -757,11 +766,22 @@ import { RulePriority } from './rule-priority.js';
757
766
  this.headerMatcher = Rule.parseHeaderModifier(value);
758
767
  meta.hasHeader = true;
759
768
  break;
760
- case OPTION_NAMES.CSP:
761
769
  case OPTION_NAMES.REMOVEPARAM:
770
+ this.enabledModifiers.add(name);
771
+ this.advancedModifierName = rawName;
772
+ // Keep an empty value as `''` instead of collapsing it to
773
+ // `null`: a value-less `$removeparam` means "remove all
774
+ // query parameters", and the converter relies on this
775
+ // distinction — otherwise such a rule silently degrades
776
+ // into a plain blocking rule.
777
+ // See https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3602.
778
+ this.advancedModifierValue = value;
779
+ break;
780
+ case OPTION_NAMES.CSP:
762
781
  case OPTION_NAMES.COOKIE:
763
782
  case OPTION_NAMES.REPLACE:
764
783
  case OPTION_NAMES.JSONPRUNE:
784
+ case OPTION_NAMES.XMLPRUNE:
765
785
  case OPTION_NAMES.HLS:
766
786
  case OPTION_NAMES.REFERRERPOLICY:
767
787
  case OPTION_NAMES.PERMISSIONS:
@@ -794,8 +814,9 @@ import { RulePriority } from './rule-priority.js';
794
814
  this.advancedModifierValue = value || null;
795
815
  meta.baseModifierCount += 1;
796
816
  break;
797
- // DNS/network-level modifiers — no DNR equivalent.
817
+ // Known unsupported modifiers — no DNR equivalent.
798
818
  // Still added to enabledModifiers so the validator can reject them.
819
+ case OPTION_NAMES.WEBRTC:
799
820
  case OPTION_NAMES.DNSTYPE:
800
821
  case OPTION_NAMES.CTAG:
801
822
  case OPTION_NAMES.CLIENT:
@@ -812,6 +833,16 @@ import { RulePriority } from './rule-priority.js';
812
833
  }
813
834
  this.enabledModifiers.add(OPTION_NAMES.POPUP);
814
835
  break;
836
+ default:
837
+ {
838
+ const modifierView = [
839
+ rawName,
840
+ value
841
+ ].filter((part)=>part).join('=');
842
+ // Unknown modifiers must invalidate the entire rule.
843
+ // RulesScanner catches this error and records it in the conversion result.
844
+ throw new SyntaxError(`Unknown modifier: ${modifierView}`);
845
+ }
815
846
  }
816
847
  }
817
848
  if (this.denyAllowDomains !== null && this.permittedToDomains !== null) {
@@ -9,6 +9,7 @@ import { OPTION_NAMES } from '../rule/option-names.js';
9
9
  import { RuleDeclarativeValidator } from '../rule/rule-validator.js';
10
10
  import { getErrorMessage } from '../utils/error.js';
11
11
  import { isRegexPattern, toASCII, prepareASCII, removeSlashes } from '../utils/string.js';
12
+ import { UnsupportedModifierError } from '../errors/conversion-errors/unsupported-modifier-error.js';
12
13
  import { HeaderOperation } from '../declarative-rule/modify-header-info.js';
13
14
  import { RuleActionType } from '../declarative-rule/rule-action.js';
14
15
  import { DomainType, ResourceType } from '../declarative-rule/rule-condition.js';
@@ -118,6 +119,9 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
118
119
  /**
119
120
  * Returns a redirect action for a `$removeparam` rule.
120
121
  *
122
+ * A value-less `$removeparam` (e.g. `||example.org^$removeparam`) removes
123
+ * the whole query string via `transform.query: ''`.
124
+ *
121
125
  * Pipe-separated values (e.g. `utm_source|utm_medium`) are
122
126
  * split into individual `removeParams` entries.
123
127
  *
@@ -127,17 +131,20 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
127
131
  * @param rule {@link Rule} to get action for.
128
132
  *
129
133
  * @returns Redirect action, or `null` if the rule does not have a
130
- * `$removeparam` modifier, its value is `null`, or a param value cannot be
131
- * URI-decoded (in which case augmentation is skipped).
134
+ * `$removeparam` modifier.
135
+ *
136
+ * @throws {UnsupportedModifierError} If a param value cannot be URI-decoded.
132
137
  */ static getRemoveParamRedirectAction(rule) {
133
138
  if (!rule.isModifierEnabled(OPTION_NAMES.REMOVEPARAM)) {
134
139
  return null;
135
140
  }
136
141
  const value = rule.advancedModifierValue;
137
- if (value === null) {
138
- return null;
139
- }
140
- if (value === '') {
142
+ // Value-less `$removeparam` — remove all query parameters.
143
+ // `Rule` stores such a value as `''`; `null` is not expected here,
144
+ // but is handled the same way defensively: a `$removeparam` rule must
145
+ // never silently degrade into a plain blocking rule.
146
+ // See https://github.com/AdguardTeam/AdguardBrowserExtension/issues/3602.
147
+ if (!value) {
141
148
  return {
142
149
  transform: {
143
150
  query: ''
@@ -146,12 +153,13 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
146
153
  }
147
154
  // Split on pipe to support multiple param names in a single rule.
148
155
  // If any segment contains invalid percent-encoding, decoding throws;
149
- // skip augmentation by returning null, consistent with getRemoveParamToken().
156
+ // report it as a conversion error so that the rule is skipped instead
157
+ // of being converted into a blocking rule.
150
158
  let removeParams;
151
159
  try {
152
160
  removeParams = value.split(RegularRuleConverter.PIPE_SEPARATOR).map((p)=>decodeURIComponent(p));
153
161
  } catch {
154
- return null;
162
+ throw new UnsupportedModifierError('Network rule with $removeparam modifier contains value that cannot be decoded', rule);
155
163
  }
156
164
  return {
157
165
  transform: {
@@ -311,6 +319,7 @@ import { UnsupportedRegexpError } from '../errors/conversion-errors/unsupported-
311
319
  * @returns The action of a rule that describes what should be done with the request.
312
320
  *
313
321
  * @throws Error {@link ResourcesPathError} when specified an empty path to the web accessible resources.
322
+ * @throws Error {@link UnsupportedModifierError} when a `$removeparam` value cannot be URI-decoded.
314
323
  */ getAction(rule) {
315
324
  if (rule.allowlist) {
316
325
  if (rule.isFilteringDisabled() && RegularRuleConverter.isCompatibleWithAllowAllRequests(rule)) {
@@ -99,6 +99,11 @@ declare class Rule {
99
99
  readonly advancedModifierName: string | null;
100
100
  /**
101
101
  * Raw string value of the advanced modifier.
102
+ *
103
+ * `null` when the modifier has no value. The only exception is
104
+ * `$removeparam`: a value-less `$removeparam` is stored as `''`, because
105
+ * it carries a meaning of its own ("remove all query parameters") that
106
+ * must be distinguishable from "no value".
102
107
  */
103
108
  readonly advancedModifierValue: string | null;
104
109
  /**
@@ -75,9 +75,9 @@ import { splitByDelimiterWithEscapeCharacter } from './utils/string.js';
75
75
  * Origin-capturing prefix for path-only transforms.
76
76
  */ const ORIGIN_PREFIX = '^(https?://[^/]+)';
77
77
  /**
78
- * Prefix for patterns that can match anywhere in the path.
78
+ * Prefix for patterns that can match anywhere in the path and query.
79
79
  * Uses a non-greedy match to capture everything before the pattern.
80
- */ const FLOATING_PREFIX = '^(https?://[^?#]*?)';
80
+ */ const FLOATING_PREFIX = '^(https?://[^/]+/[^#]*?)';
81
81
  /**
82
82
  * Origin-capturing prefix for query-targeting transforms.
83
83
  */ const ORIGIN_PREFIX_QUERY = '^(https?://[^?]+)';
@@ -195,7 +195,7 @@ import { splitByDelimiterWithEscapeCharacter } from './utils/string.js';
195
195
  } else if (isPathAnchored) {
196
196
  prefix = ORIGIN_PREFIX;
197
197
  } else {
198
- // Pattern can match anywhere in the path (floating match)
198
+ // Pattern can match anywhere in the path and query (floating match)
199
199
  prefix = FLOATING_PREFIX;
200
200
  }
201
201
  // Remove leading escaped slash from path pattern for joining
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adguard/dnr-converter",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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,12 +57,12 @@
57
57
  "@adguard/re2-wasm": "1.2.0"
58
58
  },
59
59
  "dependencies": {
60
- "@adguard/scriptlets": "2.5.0",
60
+ "@adguard/scriptlets": "2.5.1",
61
61
  "commander": "^12.1.0",
62
62
  "punycode": "2.3.1",
63
63
  "valibot": "^1.1.0",
64
- "@adguard/agtree": "^4.2.0",
65
- "@adguard/logger": "^2.0.0"
64
+ "@adguard/logger": "^2.0.0",
65
+ "@adguard/agtree": "^4.2.1"
66
66
  },
67
67
  "devDependencies": {
68
68
  "@rollup/plugin-commonjs": "^28.0.1",
@@ -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.2"
100
+ "@adguard/tsurlfilter": "^6.0.3"
101
101
  },
102
102
  "scripts": {
103
103
  "lint": "pnpm lint:code && pnpm lint:types && pnpm lint:md",