@adguard/hostlist-compiler 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.
Files changed (35) hide show
  1. package/.dockerignore +29 -0
  2. package/.github/workflows/ci.yml +22 -0
  3. package/.github/workflows/mirror.yml +24 -0
  4. package/.github/workflows/prepare-release.yml +26 -0
  5. package/.github/workflows/publish-release.yml +44 -0
  6. package/AGENTS.md +15 -16
  7. package/CHANGELOG.md +44 -0
  8. package/DEVELOPMENT.md +16 -17
  9. package/README.md +42 -3
  10. package/package.json +10 -11
  11. package/src/cli.js +6 -2
  12. package/src/index.d.ts +1 -0
  13. package/src/index.js +6 -1
  14. package/src/schemas/configuration.schema.json +98 -0
  15. package/src/transformations/ip-normalize.js +182 -0
  16. package/src/transformations/transform.js +6 -4
  17. package/src/transformations/validate-allow-ip-and-public-suffix.js +33 -0
  18. package/src/transformations/validate-allow-ip.js +16 -3
  19. package/src/transformations/validate.js +118 -53
  20. package/src/utils.js +137 -0
  21. package/test/configuration.test.js +6 -0
  22. package/test/transformations/ip-alignment.test.js +65 -0
  23. package/test/transformations/ip-normalize.test.js +331 -0
  24. package/test/transformations/transform.test.js +6 -0
  25. package/test/transformations/validate-allow-ip-and-public-suffix.test.js +167 -0
  26. package/test/transformations/validate-allow-ip.test.js +46 -18
  27. package/test/transformations/validate-allow-public-suffix.test.js +20 -0
  28. package/test/transformations/validate.test.js +57 -0
  29. package/bamboo-specs/bamboo.yaml +0 -14
  30. package/bamboo-specs/build.yaml +0 -92
  31. package/bamboo-specs/deploy.yaml +0 -38
  32. package/bamboo-specs/increment.yaml +0 -54
  33. package/bamboo-specs/permissions.yaml +0 -16
  34. package/bamboo-specs/test.yaml +0 -56
  35. package/tools/build-txt.js +0 -19
@@ -85,6 +85,54 @@
85
85
  }
86
86
  ]
87
87
  }
88
+ },
89
+ {
90
+ "not": {
91
+ "allOf": [
92
+ {
93
+ "contains": {
94
+ "const": "Validate"
95
+ }
96
+ },
97
+ {
98
+ "contains": {
99
+ "const": "ValidateAllowIpAndPublicSuffix"
100
+ }
101
+ }
102
+ ]
103
+ }
104
+ },
105
+ {
106
+ "not": {
107
+ "allOf": [
108
+ {
109
+ "contains": {
110
+ "const": "ValidateAllowIp"
111
+ }
112
+ },
113
+ {
114
+ "contains": {
115
+ "const": "ValidateAllowIpAndPublicSuffix"
116
+ }
117
+ }
118
+ ]
119
+ }
120
+ },
121
+ {
122
+ "not": {
123
+ "allOf": [
124
+ {
125
+ "contains": {
126
+ "const": "ValidateAllowPublicSuffix"
127
+ }
128
+ },
129
+ {
130
+ "contains": {
131
+ "const": "ValidateAllowIpAndPublicSuffix"
132
+ }
133
+ }
134
+ ]
135
+ }
88
136
  }
89
137
  ],
90
138
  "items": {
@@ -96,6 +144,7 @@
96
144
  "Validate",
97
145
  "ValidateAllowIp",
98
146
  "ValidateAllowPublicSuffix",
147
+ "ValidateAllowIpAndPublicSuffix",
99
148
  "Deduplicate",
100
149
  "InvertAllow",
101
150
  "RemoveEmptyLines",
@@ -213,6 +262,54 @@
213
262
  }
214
263
  ]
215
264
  }
265
+ },
266
+ {
267
+ "not": {
268
+ "allOf": [
269
+ {
270
+ "contains": {
271
+ "const": "Validate"
272
+ }
273
+ },
274
+ {
275
+ "contains": {
276
+ "const": "ValidateAllowIpAndPublicSuffix"
277
+ }
278
+ }
279
+ ]
280
+ }
281
+ },
282
+ {
283
+ "not": {
284
+ "allOf": [
285
+ {
286
+ "contains": {
287
+ "const": "ValidateAllowIp"
288
+ }
289
+ },
290
+ {
291
+ "contains": {
292
+ "const": "ValidateAllowIpAndPublicSuffix"
293
+ }
294
+ }
295
+ ]
296
+ }
297
+ },
298
+ {
299
+ "not": {
300
+ "allOf": [
301
+ {
302
+ "contains": {
303
+ "const": "ValidateAllowPublicSuffix"
304
+ }
305
+ },
306
+ {
307
+ "contains": {
308
+ "const": "ValidateAllowIpAndPublicSuffix"
309
+ }
310
+ }
311
+ ]
312
+ }
216
313
  }
217
314
  ],
218
315
  "items": {
@@ -224,6 +321,7 @@
224
321
  "Validate",
225
322
  "ValidateAllowIp",
226
323
  "ValidateAllowPublicSuffix",
324
+ "ValidateAllowIpAndPublicSuffix",
227
325
  "Deduplicate",
228
326
  "InvertAllow",
229
327
  "RemoveEmptyLines",
@@ -0,0 +1,182 @@
1
+ const consola = require('consola');
2
+ const ruleUtils = require('../rule');
3
+ const { classifyIpPattern, parseIpPattern } = require('../utils');
4
+
5
+ // Prefix for canonical IP/domain patterns
6
+ const IP_PREFIX = '||';
7
+
8
+ /**
9
+ * Action constants for IP rule processing results.
10
+ * Used by check helpers, processIpRule, and normalizeIpRules.
11
+ */
12
+ const ACTION = Object.freeze({
13
+ /**
14
+ * Pattern is valid and already in canonical form — no change needed.
15
+ */
16
+ KEEP: 'keep',
17
+
18
+ /**
19
+ * Pattern needs rewriting to canonical form.
20
+ */
21
+ NORMALIZE: 'normalize',
22
+
23
+ /**
24
+ * Pattern is a valid subnet already in canonical form (||prefix present).
25
+ */
26
+ ALLOW: 'allow',
27
+ });
28
+
29
+ /**
30
+ * Determines if a pattern is a full 4-octet IP address that needs normalization.
31
+ *
32
+ * @param {string} pattern - The adblock pattern.
33
+ * @returns {string|null} The normalized pattern (||ip^) or null if not applicable.
34
+ */
35
+ function normalizeFullIp(pattern) {
36
+ const c = classifyIpPattern(pattern);
37
+ if (!c || !c.isFullIp) {
38
+ return null;
39
+ }
40
+
41
+ const ip = c.octets.join('.');
42
+
43
+ // Already in correct format ||ip^ (||ip^| is not a non-canonical variant)
44
+ if (c.prefix === IP_PREFIX && c.hasCaret && !c.hasCaretPipe) {
45
+ return null;
46
+ }
47
+
48
+ // Normalize to ||ip^
49
+ return `||${ip}^`;
50
+ }
51
+
52
+ /**
53
+ * Determines if a pattern is a 3-octet subnet that should be allowed.
54
+ *
55
+ * Valid 3-octet patterns:
56
+ * - 192.168.1. (trailing dot)
57
+ * - 192.168.1.* (trailing wildcard)
58
+ * - ||192.168.1. (with prefix)
59
+ * - ||192.168.1.* (with prefix)
60
+ *
61
+ * Invalid 3-octet patterns (returned as null — caller treats as pass-through):
62
+ * - 192.168.1 (no trailing dot/wildcard - ambiguous)
63
+ * - ||192.168.1^ (with caret - doesn't work in AdGuard Home)
64
+ *
65
+ * @param {string} pattern - The adblock pattern.
66
+ * @returns {object|null} Result with action and normalized pattern, or null if not applicable.
67
+ * - action: ACTION.ALLOW, ACTION.NORMALIZE
68
+ * - normalized: the normalized pattern (if action is ACTION.NORMALIZE)
69
+ * - null means the pattern is not a 3-octet IP subnet, or is an invalid 3-octet pattern;
70
+ * caller should pass it through unchanged.
71
+ */
72
+ function check3OctetSubnet(pattern) {
73
+ const c = classifyIpPattern(pattern);
74
+ if (!c || c.octetCount !== 3) {
75
+ return null;
76
+ }
77
+
78
+ const ip = c.octets.join('.');
79
+
80
+ // 3 octets with ^ - doesn't work in AdGuard Home; pass through for validator
81
+ if (c.hasCaret) {
82
+ return null;
83
+ }
84
+
85
+ // 3 octets without trailing dot/wildcard - ambiguous; pass through for validator
86
+ if (!c.hasTrailingDot && !c.hasTrailingWildcard) {
87
+ return null;
88
+ }
89
+
90
+ // Valid 3-octet subnet pattern - normalize to add || if missing
91
+ if (c.prefix === IP_PREFIX) {
92
+ return {
93
+ action: ACTION.ALLOW,
94
+ };
95
+ }
96
+
97
+ // Normalize: add || prefix
98
+ const suffix = c.hasTrailingWildcard ? '.*' : '.';
99
+ return {
100
+ action: ACTION.NORMALIZE,
101
+ normalized: `||${ip}${suffix}`,
102
+ };
103
+ }
104
+
105
+ /**
106
+ * Normalizes an IP rule if needed, or keeps it unchanged.
107
+ * Invalid patterns are passed through for the validator to reject.
108
+ *
109
+ * @param {string} ruleText - The full rule text (may include modifiers and @@ prefix).
110
+ * @returns {object} Result object.
111
+ * - action: ACTION.KEEP (no change), ACTION.NORMALIZE
112
+ * - normalized: the normalized rule (if action is ACTION.NORMALIZE)
113
+ */
114
+ function processIpRule(ruleText) {
115
+ // Skip comments and empty lines
116
+ if (!ruleText || ruleText.startsWith('!') || ruleText.startsWith('#') || ruleText.trim() === '') {
117
+ return { action: ACTION.KEEP };
118
+ }
119
+
120
+ const ruleProps = ruleUtils.loadAdblockRuleProperties(ruleText);
121
+ const { pattern } = ruleProps;
122
+
123
+ // Check for 3-octet subnet patterns (normalize or keep)
124
+ const subnet = check3OctetSubnet(pattern);
125
+ if (subnet?.action === ACTION.NORMALIZE) {
126
+ ruleProps.pattern = subnet.normalized;
127
+ return {
128
+ action: ACTION.NORMALIZE,
129
+ normalized: ruleUtils.adblockRuleToString(ruleProps),
130
+ };
131
+ }
132
+ if (subnet?.action === ACTION.ALLOW) {
133
+ // Already in canonical form — no change needed
134
+ return { action: ACTION.KEEP };
135
+ }
136
+
137
+ // Check for full 4-octet IP normalization
138
+ const normalized = normalizeFullIp(pattern);
139
+ if (normalized) {
140
+ ruleProps.pattern = normalized;
141
+ return {
142
+ action: ACTION.NORMALIZE,
143
+ normalized: ruleUtils.adblockRuleToString(ruleProps),
144
+ };
145
+ }
146
+
147
+ return { action: ACTION.KEEP };
148
+ }
149
+
150
+ /**
151
+ * Processes a list of rules, normalizing IP rules and passing invalid ones through unchanged.
152
+ *
153
+ * @param {string[]} rules - Array of rules.
154
+ * @returns {string[]} Processed rules with normalization applied.
155
+ */
156
+ function normalizeIpRules(rules) {
157
+ const result = [];
158
+
159
+ // eslint-disable-next-line no-restricted-syntax
160
+ for (const rule of rules) {
161
+ const processed = processIpRule(rule);
162
+
163
+ if (processed.action === ACTION.NORMALIZE) {
164
+ consola.info(`Normalized IP rule: ${rule} → ${processed.normalized}`);
165
+ result.push(processed.normalized);
166
+ } else {
167
+ // KEEP — pass through unchanged; validator will reject invalid patterns
168
+ result.push(rule);
169
+ }
170
+ }
171
+
172
+ return result;
173
+ }
174
+
175
+ module.exports = {
176
+ ACTION,
177
+ parseIpPattern,
178
+ normalizeFullIp,
179
+ check3OctetSubnet,
180
+ processIpRule,
181
+ normalizeIpRules,
182
+ };
@@ -3,6 +3,7 @@ const removeModifiers = require('./remove-modifiers');
3
3
  const { validate } = require('./validate');
4
4
  const { validateAllowIp } = require('./validate-allow-ip');
5
5
  const { validateAllowPublicSuffix } = require('./validate-allow-public-suffix');
6
+ const { validateAllowIpAndPublicSuffix } = require('./validate-allow-ip-and-public-suffix');
6
7
  const exclude = require('./exclude');
7
8
  const include = require('./include');
8
9
  const deduplicate = require('./deduplicate');
@@ -23,6 +24,7 @@ const TRANSFORMATIONS = Object.freeze({
23
24
  Validate: 'Validate',
24
25
  ValidateAllowIp: 'ValidateAllowIp',
25
26
  ValidateAllowPublicSuffix: 'ValidateAllowPublicSuffix',
27
+ ValidateAllowIpAndPublicSuffix: 'ValidateAllowIpAndPublicSuffix',
26
28
  Deduplicate: 'Deduplicate',
27
29
  InvertAllow: 'InvertAllow',
28
30
  RemoveEmptyLines: 'RemoveEmptyLines',
@@ -35,6 +37,7 @@ const VALIDATION_TRANSFORMATIONS = [
35
37
  TRANSFORMATIONS.Validate,
36
38
  TRANSFORMATIONS.ValidateAllowIp,
37
39
  TRANSFORMATIONS.ValidateAllowPublicSuffix,
40
+ TRANSFORMATIONS.ValidateAllowIpAndPublicSuffix,
38
41
  ];
39
42
 
40
43
  /**
@@ -42,10 +45,6 @@ const VALIDATION_TRANSFORMATIONS = [
42
45
  * Combining them causes silent data loss: each validator runs on the already-filtered
43
46
  * output of the previous one, so allow-modes (AllowIp, AllowPublicSuffix) become ineffective.
44
47
  *
45
- * TODO: When a combined mode for ValidateAllowIp + ValidateAllowPublicSuffix is implemented
46
- * (see https://github.com/AdguardTeam/HostlistCompiler/issues/126),
47
- * update this check to allow that specific pair and run Validator(true, true) instead.
48
- *
49
48
  * @param {Array<string>} transformations - configured transformations.
50
49
  */
51
50
  function checkIncompatibleValidationTransformations(transformations) {
@@ -115,6 +114,9 @@ async function transform(rules, configuration, transformations) {
115
114
  if (transformations.indexOf(TRANSFORMATIONS.ValidateAllowPublicSuffix) !== -1) {
116
115
  transformed = validateAllowPublicSuffix(transformed);
117
116
  }
117
+ if (transformations.indexOf(TRANSFORMATIONS.ValidateAllowIpAndPublicSuffix) !== -1) {
118
+ transformed = validateAllowIpAndPublicSuffix(transformed);
119
+ }
118
120
  if (transformations.indexOf(TRANSFORMATIONS.Deduplicate) !== -1) {
119
121
  transformed = deduplicate(transformed);
120
122
  }
@@ -0,0 +1,33 @@
1
+ const { Validator } = require('./validate');
2
+ const { normalizeIpRules } = require('./ip-normalize');
3
+
4
+ /**
5
+ * Validates a list of rules, allowing both IP addresses and public suffix matching.
6
+ *
7
+ * This function first normalizes incomplete IP rules to the safe format ||ip^,
8
+ * then creates an instance of the `Validator` class, enabling both IP address
9
+ * validation and public suffix matching, and returns a filtered list of valid rules.
10
+ *
11
+ * IP normalization:
12
+ * - 4-octet IPs without proper separators are normalized to ||ip^
13
+ * - 3-octet subnet patterns (192.168.1., 192.168.1.*) are allowed with || prefix
14
+ * - 3-octet patterns without trailing dot/wildcard are rejected (ambiguous)
15
+ * - 3-octet patterns with ^ are rejected (don't work in AdGuard Home)
16
+ * - 1-2 octet patterns are rejected (too wide, use regex instead)
17
+ *
18
+ * @param {Array<string>} rules - The array of rules to validate.
19
+ * @returns {Array<string>} - The filtered array of valid rules, allowing both
20
+ * IP addresses and public suffixes.
21
+ */
22
+ function validateAllowIpAndPublicSuffix(rules) {
23
+ // First, normalize IP rules (convert incomplete patterns to ||ip^)
24
+ const normalizedRules = normalizeIpRules(rules);
25
+
26
+ // Then validate with both IP addresses and public suffixes allowed
27
+ const validator = new Validator(true, true);
28
+ return validator.validate(normalizedRules);
29
+ }
30
+
31
+ module.exports = {
32
+ validateAllowIpAndPublicSuffix,
33
+ };
@@ -1,20 +1,33 @@
1
1
  const { Validator } = require('./validate');
2
+ const { normalizeIpRules } = require('./ip-normalize');
2
3
 
3
4
  /**
4
5
  * Validates a list of rules and allows IP validation.
5
6
  *
6
- * This function creates an instance of the `Validator` class, enabling IP address validation,
7
+ * This function first normalizes incomplete IP rules to the safe format ||ip^,
8
+ * then creates an instance of the `Validator` class, enabling IP address validation,
7
9
  * and returns a filtered list of valid rules.
8
10
  *
11
+ * IP normalization:
12
+ * - 4-octet IPs without proper separators are normalized to ||ip^
13
+ * - 3-octet subnet patterns (192.168.1., 192.168.1.*) are allowed with || prefix
14
+ * - 3-octet patterns without trailing dot/wildcard are rejected (ambiguous)
15
+ * - 3-octet patterns with ^ are rejected (don't work in AdGuard Home)
16
+ * - 1-2 octet patterns are rejected (too wide, use regex instead)
17
+ *
9
18
  * @param {Array<string>} rules - The array of rules to validate.
10
19
  * @returns {Array<string>} - The filtered array of valid rules, allowing IP addresses.
11
20
  */
12
21
  function validateAllowIp(rules) {
13
- // ip is allowed for this transformation
22
+ // First, normalize IP rules (convert incomplete patterns to ||ip^)
23
+ const normalizedRules = normalizeIpRules(rules);
24
+
25
+ // Then validate with IP addresses allowed
14
26
  // tld is not allowed by default here
15
27
  const validator = new Validator(true, false);
16
- return validator.validate(rules);
28
+ return validator.validate(normalizedRules);
17
29
  }
30
+
18
31
  module.exports = {
19
32
  validateAllowIp,
20
33
  };
@@ -1,36 +1,52 @@
1
+ const _ = require('lodash');
2
+ const consola = require('consola');
3
+ const tldts = require('tldts');
4
+ const utils = require('../utils');
5
+ const ruleUtils = require('../rule');
6
+
7
+ // Prefix and separators for domain patterns
8
+ const DOMAIN_PREFIX = '||';
9
+ const DOMAIN_SEPARATOR = '^';
10
+ const WILDCARD = '*';
11
+ const WILDCARD_DOMAIN_PART = '*.';
12
+
13
+ /**
14
+ * Checks if a pattern is a 3-octet subnet with trailing dot or wildcard AND a || prefix.
15
+ * These are the only subnet patterns that work in AdGuard Home for ValidateAllowIp.
16
+ * Examples: ||192.168.1. ||192.168.1.*
17
+ *
18
+ * @param {string} s - The pattern string to check
19
+ * @returns {boolean}
20
+ */
21
+ function is3OctetSubnetWithSuffix(s) {
22
+ const c = utils.classifyIpPattern(s);
23
+ // Valid 3-octet subnet: ||prefix, trailing dot or wildcard, no caret
24
+ return c !== null && c.octetCount === 3 && c.isSubnetWildcard && !c.hasCaret && c.prefix === DOMAIN_PREFIX;
25
+ }
26
+
1
27
  /**
2
- * Checks if a pattern is an IP-like subnet with | or || prefix: 2 or 3 octets.
3
- * Examples: ||1.1^, ||1.1.2^, |1.1^, ||192.168^
4
- * These patterns block IP subnets (left-anchored) and should be rejected in Validate,
5
- * but allowed in ValidateAllowIp.
28
+ * Checks if a pattern is an IP-like subnet with | or || prefix: 1, 2, or 3 octets.
29
+ * These patterns should be rejected in Validate:
30
+ * - With ^ separator (||1^, ||1.1^, ||1.1.2^) — do NOT work
31
+ * - With trailing dot (||1.1., ||1.1.2.) — IP wildcard patterns
32
+ * - With trailing wildcard (||1.1.*, ||1.1.2.*) — IP wildcard patterns
33
+ *
34
+ * In ValidateAllowIp, 3-octet wildcards (||192.168.1., ||192.168.1.*) are allowed,
35
+ * but 1-2 octet patterns are still rejected as too wide.
36
+ *
6
37
  * @param {string} s - The pattern string to check
7
38
  * @returns {boolean} True if the pattern is an IP subnet pattern, false otherwise
8
39
  */
9
40
  function isIpSubnetPattern(s) {
10
- // Must have | or || prefix to be a subnet pattern (left-anchored)
11
- if (!s.startsWith('|')) {
41
+ const c = utils.classifyIpPattern(s);
42
+ if (!c || c.prefix === '') {
12
43
  return false;
13
44
  }
14
- // Remove prefixes (||, |)
15
- let pattern = s;
16
- if (pattern.startsWith('||')) {
17
- pattern = pattern.slice(2);
18
- } else if (pattern.startsWith('|')) {
19
- pattern = pattern.slice(1);
20
- }
21
- // Remove ^ and | at the end
22
- pattern = pattern.replace(/\^\|?$/, '');
23
- // Remove trailing dot if present
24
- if (pattern.endsWith('.')) {
25
- pattern = pattern.slice(0, -1);
26
- }
27
- // Must have 2 or 3 octets (1.1 or 1.1.2)
28
- const parts = pattern.split('.');
29
- if (parts.length !== 2 && parts.length !== 3) {
30
- return false;
45
+ if (c.octetCount < 4) {
46
+ return true;
31
47
  }
32
- // All parts must be numbers 0-255
33
- return parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) >= 0 && Number(p) <= 255);
48
+ // 4-octet with trailing dot or wildcard is a malformed IP pattern (not a complete address)
49
+ return c.hasTrailingDot || c.hasTrailingWildcard;
34
50
  }
35
51
 
36
52
  /**
@@ -38,38 +54,61 @@ function isIpSubnetPattern(s) {
38
54
  * Examples: 1.1^, 1.1.1^, 1.1.1.1^, 192.168^
39
55
  * These patterns match string endings and block unpredictably (e.g., 1.1^ blocks 1.1.1.1, 1.1.111.1, example1.1).
40
56
  * They should ALWAYS be rejected (in both Validate and ValidateAllowIp).
57
+ * Note: In ValidateAllowIp, these patterns are normalized to ||ip^ before validation,
58
+ * so this function mainly catches patterns that slip through normalization.
41
59
  * @param {string} s - The pattern string to check
42
60
  * @returns {boolean} True if the pattern is an IP suffix pattern, false otherwise
43
61
  */
44
62
  function isIpSuffixPattern(s) {
45
- // Must NOT have | or || prefix (otherwise it's a subnet pattern, not a suffix)
46
- if (s.startsWith('|')) {
63
+ const c = utils.classifyIpPattern(s);
64
+ return c !== null && c.prefix === '' && c.hasCaret && c.octetCount >= 2;
65
+ }
66
+
67
+ /**
68
+ * Checks if a pattern is an unsafe/ambiguous IP pattern that should be rejected.
69
+ * These are patterns WITHOUT ^ that look like partial IPs:
70
+ * - 1-2 octet patterns (1.2., 1.2.*, 192.168) — too wide
71
+ * - 3 octet patterns without trailing dot/wildcard (192.168.1) — ambiguous
72
+ *
73
+ * These should be rejected in ALL validators (Validate, ValidateAllowIp, ValidateAllowPublicSuffix).
74
+ *
75
+ * @param {string} s - The pattern string to check
76
+ * @returns {boolean} True if the pattern is unsafe, false otherwise
77
+ */
78
+ function isUnsafeIpPattern(s) {
79
+ // Skip if has ^ (handled by isIpSuffixPattern)
80
+ if (s.includes('^')) {
47
81
  return false;
48
82
  }
49
- // Remove ^ and | at the end
50
- let pattern = s.replace(/\^\|?$/, '');
51
- // Remove trailing dot if present
52
- if (pattern.endsWith('.')) {
53
- pattern = pattern.slice(0, -1);
54
- }
55
- // Must have 2, 3, or 4 octets
56
- const parts = pattern.split('.');
57
- if (parts.length < 2 || parts.length > 4) {
83
+
84
+ const c = utils.classifyIpPattern(s);
85
+ if (!c) {
58
86
  return false;
59
87
  }
60
- // All parts must be numbers 0-255
61
- return parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) >= 0 && Number(p) <= 255);
88
+
89
+ // 1-2 octets are always too wide
90
+ if (c.isTooWide) {
91
+ return true;
92
+ }
93
+
94
+ // 3 octets without trailing dot/wildcard are ambiguous
95
+ if (c.isAmbiguous3Octet) {
96
+ return true;
97
+ }
98
+
99
+ // 3 or 4-octet with trailing dot or wildcard, no prefix (e.g. 10.10.34., 10.10.34.*, 1.2.3.4., 1.2.3.4.*)
100
+ // Patterns with `||` prefix are handled by isIpSubnetPattern; in ValidateAllowIp,
101
+ // prefix-less 3-octet ones are pre-normalized to `||ip.` by ip-normalize.js before reaching here.
102
+ if ((c.octetCount === 3 || c.octetCount === 4)
103
+ && (c.hasTrailingDot || c.hasTrailingWildcard)
104
+ && c.prefix === '') {
105
+ return true;
106
+ }
107
+
108
+ return false;
62
109
  }
63
- const _ = require('lodash');
64
- const consola = require('consola');
65
- const tldts = require('tldts');
66
- const utils = require('../utils');
67
- const ruleUtils = require('../rule');
68
110
 
69
- const DOMAIN_PREFIX = '||';
70
- const DOMAIN_SEPARATOR = '^';
71
- const WILDCARD = '*';
72
- const WILDCARD_DOMAIN_PART = '*.';
111
+ const HOSTNAME_ALNUM_PATTERN = /[a-zA-Z0-9]/;
73
112
  // Matches exact domain-style adblock patterns: ||example.org^, *.org^, .org^, ||org^
74
113
  // Each part explained:
75
114
  const EXACT_DOMAIN_PATTERN = new RegExp(
@@ -139,13 +178,19 @@ const MAX_PATTERN_LENGTH = 5;
139
178
  function validHostname(hostname, ruleText, allowedIP, hasLimitModifier, allowPublicSuffix) {
140
179
  const result = tldts.parse(hostname);
141
180
 
142
- if (!result.hostname) {
143
- consola.debug(`invalid hostname ${hostname} in the rule: ${ruleText}`);
181
+ // tldts v7 may return a non-null hostname for malformed inputs consisting only of dots
182
+ // (".." => hostname: "."). Reject any hostname that does not contain at least one
183
+ // alphanumeric character, since real hostnames always have one.
184
+ // This is separate from the adblock-style `checkChars` filter in validAdblockRule(),
185
+ // which rejects raw Unicode patterns before they reach this function. Raw Unicode
186
+ // adblock rules are expected to pass through ConvertToAscii before validation.
187
+ if (!result.hostname || !HOSTNAME_ALNUM_PATTERN.test(result.hostname)) {
188
+ consola.debug(`malformed hostname ${hostname} in the rule: ${ruleText}`);
144
189
  return false;
145
190
  }
146
191
 
147
192
  if (!allowedIP && result.isIp) {
148
- consola.debug(`invalid hostname ${hostname} in the rule: ${ruleText}`);
193
+ consola.debug(`IP address ${hostname} is not allowed in the rule: ${ruleText}`);
149
194
  return false;
150
195
  }
151
196
 
@@ -291,6 +336,15 @@ function validAdblockRule(ruleText, allowedIP, allowPublicSuffix) {
291
336
  return false;
292
337
  }
293
338
 
339
+ // `denyallow` is only meaningful for domain-based rules.
340
+ // IP-like patterns with `denyallow` do not behave as expected, so reject them.
341
+ const hasDenyallowModifier = props.options
342
+ && props.options.some((option) => option.name === 'denyallow');
343
+ if (hasDenyallowModifier && utils.classifyIpPattern(props.pattern)) {
344
+ consola.debug(`denyallow is not supported for IP patterns: ${ruleText}`);
345
+ return false;
346
+ }
347
+
294
348
  // 3.5. Check if the base pattern is an IP address or IP-like pattern.
295
349
 
296
350
  // Reject IP-suffix patterns (1.1^, 1.1.1^, 1.1.1.1^) ALWAYS — they match string endings unpredictably
@@ -300,12 +354,23 @@ function validAdblockRule(ruleText, allowedIP, allowPublicSuffix) {
300
354
  return false;
301
355
  }
302
356
 
303
- // Reject IP-subnet patterns (||1.1^, ||1.1.2^) only in Validate, allow in ValidateAllowIp
304
- if (!allowedIP && isIpSubnetPattern(props.pattern)) {
305
- consola.debug(`IP-like subnet is not allowed: ${ruleText}`);
357
+ // Reject unsafe IP patterns (1.2., 1.2.*, 192.168.1) ALWAYS — too wide or ambiguous
358
+ // These don't have ^ so they slip through isIpSuffixPattern
359
+ if (isUnsafeIpPattern(props.pattern)) {
360
+ consola.debug(`Unsafe IP pattern is not allowed (too wide or ambiguous): ${ruleText}`);
306
361
  return false;
307
362
  }
308
363
 
364
+ // Reject IP-subnet patterns (||1.1^, ||1.1.2^, ||1.1.) always, except in ValidateAllowIp
365
+ // where only 3-octet subnets with trailing dot/wildcard (||192.168.1., ||192.168.1.*) are valid.
366
+ // 1-2 octet patterns and 3-octet with ^ are rejected even when allowedIP=true.
367
+ if (isIpSubnetPattern(props.pattern)) {
368
+ if (!allowedIP || !is3OctetSubnetWithSuffix(props.pattern)) {
369
+ consola.debug(`IP-like subnet is not allowed: ${ruleText}`);
370
+ return false;
371
+ }
372
+ }
373
+
309
374
  // Check for full 4-octet IP addresses (||1.1.1.1^, |1.1.1.1^)
310
375
  let ipCandidate = props.pattern;
311
376
  if (_.startsWith(ipCandidate, DOMAIN_PREFIX)) {