@adguard/hostlist-compiler 1.0.39 → 2.0.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/AGENTS.md +175 -0
- package/CHANGELOG.md +15 -0
- package/DEVELOPMENT.md +172 -0
- package/README.md +107 -94
- package/bamboo-specs/build.yaml +2 -10
- package/bamboo-specs/deploy.yaml +0 -7
- package/bamboo-specs/increment.yaml +1 -1
- package/bamboo-specs/test.yaml +0 -8
- package/package.json +1 -1
- package/src/index.d.ts +5 -2
- package/src/index.js +43 -1
- package/src/schemas/configuration.schema.json +102 -0
- package/src/transformations/transform.js +35 -1
- package/src/transformations/validate-allow-ip.js +3 -2
- package/src/transformations/validate-allow-public-suffix.js +20 -0
- package/src/transformations/validate.js +183 -19
- package/test/configuration.test.js +64 -0
- package/test/index.test.js +64 -0
- package/test/transformations/transform.test.js +19 -0
- package/test/transformations/validate-allow-ip.test.js +44 -1
- package/test/transformations/validate-allow-public-suffix.test.js +264 -0
- package/test/transformations/validate.test.js +194 -0
|
@@ -1,3 +1,65 @@
|
|
|
1
|
+
/**
|
|
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.
|
|
6
|
+
* @param {string} s - The pattern string to check
|
|
7
|
+
* @returns {boolean} True if the pattern is an IP subnet pattern, false otherwise
|
|
8
|
+
*/
|
|
9
|
+
function isIpSubnetPattern(s) {
|
|
10
|
+
// Must have | or || prefix to be a subnet pattern (left-anchored)
|
|
11
|
+
if (!s.startsWith('|')) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
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;
|
|
31
|
+
}
|
|
32
|
+
// All parts must be numbers 0-255
|
|
33
|
+
return parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) >= 0 && Number(p) <= 255);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a pattern is an IP-suffix (no | or || prefix): 2, 3, or 4 octets.
|
|
38
|
+
* Examples: 1.1^, 1.1.1^, 1.1.1.1^, 192.168^
|
|
39
|
+
* These patterns match string endings and block unpredictably (e.g., 1.1^ blocks 1.1.1.1, 1.1.111.1, example1.1).
|
|
40
|
+
* They should ALWAYS be rejected (in both Validate and ValidateAllowIp).
|
|
41
|
+
* @param {string} s - The pattern string to check
|
|
42
|
+
* @returns {boolean} True if the pattern is an IP suffix pattern, false otherwise
|
|
43
|
+
*/
|
|
44
|
+
function isIpSuffixPattern(s) {
|
|
45
|
+
// Must NOT have | or || prefix (otherwise it's a subnet pattern, not a suffix)
|
|
46
|
+
if (s.startsWith('|')) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
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) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// All parts must be numbers 0-255
|
|
61
|
+
return parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) >= 0 && Number(p) <= 255);
|
|
62
|
+
}
|
|
1
63
|
const _ = require('lodash');
|
|
2
64
|
const consola = require('consola');
|
|
3
65
|
const tldts = require('tldts');
|
|
@@ -8,6 +70,30 @@ const DOMAIN_PREFIX = '||';
|
|
|
8
70
|
const DOMAIN_SEPARATOR = '^';
|
|
9
71
|
const WILDCARD = '*';
|
|
10
72
|
const WILDCARD_DOMAIN_PART = '*.';
|
|
73
|
+
// Matches exact domain-style adblock patterns: ||example.org^, *.org^, .org^, ||org^
|
|
74
|
+
// Each part explained:
|
|
75
|
+
const EXACT_DOMAIN_PATTERN = new RegExp(
|
|
76
|
+
// optional || prefix: matches ||example.org^, does not require it for .org^ or *.org^
|
|
77
|
+
'^(?:\\|\\|)?'
|
|
78
|
+
// optional leading *. or .: matches *.org^, .org^; absent for plain ||example.org^
|
|
79
|
+
+ '(?:\\*\\.|\\.)?'
|
|
80
|
+
// first label: alnum start/end, hyphens only inside
|
|
81
|
+
// valid: example, foo-bar, xn--d1acufc
|
|
82
|
+
// invalid: -example, example-, (empty)
|
|
83
|
+
+ '([a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?'
|
|
84
|
+
// additional dot-separated labels with same constraints
|
|
85
|
+
// valid: example.org, sub.example.org, foo-bar.baz
|
|
86
|
+
// invalid: example..org, example.-org, example.org-
|
|
87
|
+
+ '(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*'
|
|
88
|
+
// optional trailing dot: FQDN notation, normalized away before tldts
|
|
89
|
+
// valid: foo.bar. (treated as foo.bar)
|
|
90
|
+
// invalid: foo.. (two dots — not matched)
|
|
91
|
+
+ '\\.?)'
|
|
92
|
+
// required ^ separator; optional trailing | for exception-end anchors
|
|
93
|
+
// valid: ||example.org^, ||example.org^|
|
|
94
|
+
// invalid: ||example.org, ||example.org^abc
|
|
95
|
+
+ '\\^\\|?$',
|
|
96
|
+
);
|
|
11
97
|
|
|
12
98
|
// TODO: Remove lodash from the project if possible
|
|
13
99
|
|
|
@@ -27,7 +113,6 @@ const ANY_PATTERN_MODIFIER = [
|
|
|
27
113
|
const SUPPORTED_MODIFIERS = [
|
|
28
114
|
'important',
|
|
29
115
|
'~important',
|
|
30
|
-
'ctag',
|
|
31
116
|
// DNS-related modifiers.
|
|
32
117
|
'dnstype',
|
|
33
118
|
'dnsrewrite',
|
|
@@ -48,9 +133,10 @@ const MAX_PATTERN_LENGTH = 5;
|
|
|
48
133
|
* @param {string} ruleText - original rule text (for logging)
|
|
49
134
|
* @param {boolean} allowedIP - flag to determine if IP validation is allowed
|
|
50
135
|
* @param {boolean} hasLimitModifier - flag to determine if the rule has a limit modifier, e.g. denyallow
|
|
136
|
+
* @param {boolean} allowPublicSuffix - flag to determine if matching whole public suffixes is allowed
|
|
51
137
|
* @returns {boolean} true if the hostname is okay to be in the blocklist.
|
|
52
138
|
*/
|
|
53
|
-
function validHostname(hostname, ruleText, allowedIP, hasLimitModifier) {
|
|
139
|
+
function validHostname(hostname, ruleText, allowedIP, hasLimitModifier, allowPublicSuffix) {
|
|
54
140
|
const result = tldts.parse(hostname);
|
|
55
141
|
|
|
56
142
|
if (!result.hostname) {
|
|
@@ -64,13 +150,40 @@ function validHostname(hostname, ruleText, allowedIP, hasLimitModifier) {
|
|
|
64
150
|
}
|
|
65
151
|
|
|
66
152
|
if (result.hostname === result.publicSuffix && !hasLimitModifier) {
|
|
67
|
-
|
|
68
|
-
|
|
153
|
+
if (!allowPublicSuffix) {
|
|
154
|
+
consola.debug(`matching the whole public suffix ${hostname} is not allowed: ${ruleText}`);
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
// Even with allowPublicSuffix, reject hostnames that tldts does not recognise
|
|
158
|
+
// as real ICANN or private public suffixes. Without this check, any single-label
|
|
159
|
+
// garbage like "a", "aa", "1" passes because tldts defaults hostname === publicSuffix
|
|
160
|
+
// for unknown labels.
|
|
161
|
+
if (!result.isIcann && !result.isPrivate) {
|
|
162
|
+
consola.debug(`hostname ${hostname} is not a known public suffix: ${ruleText}`);
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
69
165
|
}
|
|
70
166
|
|
|
71
167
|
return true;
|
|
72
168
|
}
|
|
73
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Extracts a domain-like pattern from rules such as ||org^, ||*.org^, .org^ or *.org^.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} pattern - adblock rule pattern without modifiers
|
|
174
|
+
* @returns {string|null} normalized hostname to validate or null if the pattern does not match
|
|
175
|
+
*/
|
|
176
|
+
function extractDomainPattern(pattern) {
|
|
177
|
+
const match = pattern.match(EXACT_DOMAIN_PATTERN);
|
|
178
|
+
if (!match) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const hostname = match[1];
|
|
183
|
+
// Allow trailing dot as absolute FQDN marker by normalizing it away for validation.
|
|
184
|
+
return hostname.endsWith('.') ? hostname.slice(0, -1) : hostname;
|
|
185
|
+
}
|
|
186
|
+
|
|
74
187
|
/**
|
|
75
188
|
* Validates an /etc/hosts rule.
|
|
76
189
|
*
|
|
@@ -81,9 +194,10 @@ function validHostname(hostname, ruleText, allowedIP, hasLimitModifier) {
|
|
|
81
194
|
*
|
|
82
195
|
* @param {string} ruleText - rule text
|
|
83
196
|
* @param {boolean} allowIP - flag to determine if IP validation is allowed
|
|
197
|
+
* @param {boolean} allowPublicSuffix - flag to determine if matching whole public suffixes is allowed
|
|
84
198
|
* @returns {boolean} true if the rule is a valid /etc/hosts rule
|
|
85
199
|
*/
|
|
86
|
-
function validEtcHostsRule(ruleText, allowIP) {
|
|
200
|
+
function validEtcHostsRule(ruleText, allowIP, allowPublicSuffix) {
|
|
87
201
|
let props;
|
|
88
202
|
try {
|
|
89
203
|
props = ruleUtils.loadEtcHostsRuleProperties(ruleText);
|
|
@@ -96,7 +210,7 @@ function validEtcHostsRule(ruleText, allowIP) {
|
|
|
96
210
|
return false;
|
|
97
211
|
}
|
|
98
212
|
|
|
99
|
-
if (props.hostnames.some((h) => !validHostname(h, ruleText, allowIP))) {
|
|
213
|
+
if (props.hostnames.some((h) => !validHostname(h, ruleText, allowIP, false, allowPublicSuffix))) {
|
|
100
214
|
return false;
|
|
101
215
|
}
|
|
102
216
|
|
|
@@ -114,9 +228,10 @@ function validEtcHostsRule(ruleText, allowIP) {
|
|
|
114
228
|
*
|
|
115
229
|
* @param {string} ruleText - rule text
|
|
116
230
|
* @param {boolean} allowedIP - flag to determine if IP validation is allowed
|
|
231
|
+
* @param {boolean} allowPublicSuffix - flag to determine if matching whole public suffixes is allowed
|
|
117
232
|
* @returns {boolean} - adblock-style rule
|
|
118
233
|
*/
|
|
119
|
-
function validAdblockRule(ruleText, allowedIP) {
|
|
234
|
+
function validAdblockRule(ruleText, allowedIP, allowPublicSuffix) {
|
|
120
235
|
let props;
|
|
121
236
|
try {
|
|
122
237
|
props = ruleUtils.loadAdblockRuleProperties(ruleText);
|
|
@@ -144,7 +259,10 @@ function validAdblockRule(ruleText, allowedIP) {
|
|
|
144
259
|
}
|
|
145
260
|
|
|
146
261
|
// 2. It checks whether the pattern is not too wide (should be at least 5 characters).
|
|
147
|
-
|
|
262
|
+
// Exception: exact domain-style patterns (||uk^, .uk^, etc.) bypass this check even if shorter,
|
|
263
|
+
// because they must reach step 4 to be validated as public suffixes by tldts.
|
|
264
|
+
const exactDomainPattern = extractDomainPattern(props.pattern);
|
|
265
|
+
if (props.pattern.length < MAX_PATTERN_LENGTH && !exactDomainPattern) {
|
|
148
266
|
consola.debug(`The rule is too short: ${ruleText}`);
|
|
149
267
|
return false;
|
|
150
268
|
}
|
|
@@ -173,6 +291,39 @@ function validAdblockRule(ruleText, allowedIP) {
|
|
|
173
291
|
return false;
|
|
174
292
|
}
|
|
175
293
|
|
|
294
|
+
// 3.5. Check if the base pattern is an IP address or IP-like pattern.
|
|
295
|
+
|
|
296
|
+
// Reject IP-suffix patterns (1.1^, 1.1.1^, 1.1.1.1^) ALWAYS — they match string endings unpredictably
|
|
297
|
+
// Must check BEFORE tldts.isIp because 4-octet suffixes would be recognized as valid IPs
|
|
298
|
+
if (isIpSuffixPattern(props.pattern)) {
|
|
299
|
+
consola.debug(`IP-suffix pattern is not allowed (blocks unpredictably): ${ruleText}`);
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
|
|
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}`);
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Check for full 4-octet IP addresses (||1.1.1.1^, |1.1.1.1^)
|
|
310
|
+
let ipCandidate = props.pattern;
|
|
311
|
+
if (_.startsWith(ipCandidate, DOMAIN_PREFIX)) {
|
|
312
|
+
ipCandidate = ipCandidate.slice(DOMAIN_PREFIX.length);
|
|
313
|
+
} else if (_.startsWith(ipCandidate, '|')) {
|
|
314
|
+
ipCandidate = ipCandidate.slice(1);
|
|
315
|
+
}
|
|
316
|
+
ipCandidate = ipCandidate.replace(/\^\|?$/, '');
|
|
317
|
+
|
|
318
|
+
if (tldts.parse(ipCandidate).isIp) {
|
|
319
|
+
if (!allowedIP) {
|
|
320
|
+
consola.debug(`IP address is not allowed: ${ruleText}`);
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
// IP is allowed — no further domain validation needed
|
|
324
|
+
return true;
|
|
325
|
+
}
|
|
326
|
+
|
|
176
327
|
// 4. Validate domain name
|
|
177
328
|
// Note that we don't check rules that contain wildcard characters
|
|
178
329
|
const sepIdx = props.pattern.indexOf(DOMAIN_SEPARATOR);
|
|
@@ -182,13 +333,18 @@ function validAdblockRule(ruleText, allowedIP) {
|
|
|
182
333
|
return false;
|
|
183
334
|
}
|
|
184
335
|
|
|
336
|
+
if (exactDomainPattern) {
|
|
337
|
+
return validHostname(exactDomainPattern, ruleText, allowedIP, hasLimitModifier, allowPublicSuffix);
|
|
338
|
+
}
|
|
339
|
+
|
|
185
340
|
// Check if the pattern does not start with the domain prefix and does not contain a domain separator
|
|
186
341
|
if (!_.startsWith(props.pattern, DOMAIN_PREFIX) || sepIdx === -1) {
|
|
187
342
|
return true;
|
|
188
343
|
}
|
|
189
344
|
|
|
190
|
-
// Extract the domain to check from the
|
|
191
|
-
|
|
345
|
+
// Extract the domain to check from the parsed pattern (not ruleText) to stay consistent
|
|
346
|
+
// with sepIdx/wildcardIdx which are also computed from props.pattern.
|
|
347
|
+
const domainToCheck = utils.substringBetween(props.pattern, DOMAIN_PREFIX, DOMAIN_SEPARATOR);
|
|
192
348
|
|
|
193
349
|
// If there are wildcard characters in the pattern
|
|
194
350
|
if (wildcardIdx !== -1) {
|
|
@@ -201,14 +357,14 @@ function validAdblockRule(ruleText, allowedIP) {
|
|
|
201
357
|
// If it's a wildcard with TLD, validate the cleaned TLD
|
|
202
358
|
if (startsWithWildcard && isOnlyTLD) {
|
|
203
359
|
const cleanedDomain = domainToCheck.replace(WILDCARD_DOMAIN_PART, '');
|
|
204
|
-
return validHostname(cleanedDomain, ruleText, allowedIP, hasLimitModifier);
|
|
360
|
+
return validHostname(cleanedDomain, ruleText, allowedIP, hasLimitModifier, allowPublicSuffix);
|
|
205
361
|
}
|
|
206
362
|
// If the rule has wildcard characters but is not a TLD (e.g., ||*.example.org^)
|
|
207
363
|
return true;
|
|
208
364
|
}
|
|
209
365
|
|
|
210
366
|
// Validate the domain
|
|
211
|
-
if (!validHostname(domainToCheck, ruleText, allowedIP, hasLimitModifier)) {
|
|
367
|
+
if (!validHostname(domainToCheck, ruleText, allowedIP, hasLimitModifier, allowPublicSuffix)) {
|
|
212
368
|
return false;
|
|
213
369
|
}
|
|
214
370
|
|
|
@@ -230,18 +386,19 @@ function validAdblockRule(ruleText, allowedIP) {
|
|
|
230
386
|
*
|
|
231
387
|
* @param {string} ruleText - rule to check
|
|
232
388
|
* @param {boolean} allowedIP - flag to determine if IP validation is allowed
|
|
389
|
+
* @param {boolean} allowPublicSuffix - flag to determine if matching whole public suffixes is allowed
|
|
233
390
|
* @returns {boolean} - true if the rule is valid, false otherwise
|
|
234
391
|
*/
|
|
235
|
-
function valid(ruleText, allowedIP) {
|
|
392
|
+
function valid(ruleText, allowedIP, allowPublicSuffix) {
|
|
236
393
|
if (ruleUtils.isComment(ruleText) || _.isEmpty(_.trim(ruleText))) {
|
|
237
394
|
return true;
|
|
238
395
|
}
|
|
239
396
|
|
|
240
397
|
if (ruleUtils.isEtcHostsRule(ruleText)) {
|
|
241
|
-
return validEtcHostsRule(ruleText, allowedIP);
|
|
398
|
+
return validEtcHostsRule(ruleText, allowedIP, allowPublicSuffix);
|
|
242
399
|
}
|
|
243
400
|
|
|
244
|
-
return validAdblockRule(ruleText, allowedIP);
|
|
401
|
+
return validAdblockRule(ruleText, allowedIP, allowPublicSuffix);
|
|
245
402
|
}
|
|
246
403
|
|
|
247
404
|
/**
|
|
@@ -251,8 +408,9 @@ class Validator {
|
|
|
251
408
|
/**
|
|
252
409
|
* Creates a new rule validator.
|
|
253
410
|
* @param {boolean} allowedIP - Flag indicating whether IP addresses should be considered valid.
|
|
411
|
+
* @param {boolean} allowPublicSuffix - Flag indicating whether rules matching whole public suffix are allowed.
|
|
254
412
|
*/
|
|
255
|
-
constructor(allowedIP) {
|
|
413
|
+
constructor(allowedIP, allowPublicSuffix) {
|
|
256
414
|
/**
|
|
257
415
|
* Indicates that a rule was previously removed (the iteration processed an invalid rule).
|
|
258
416
|
* Used to remove preceding comments or empty lines.
|
|
@@ -265,6 +423,12 @@ class Validator {
|
|
|
265
423
|
* @type {boolean}
|
|
266
424
|
*/
|
|
267
425
|
this.allowedIP = allowedIP;
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Flag to allow or disallow validation when hostname equals public suffix.
|
|
429
|
+
* @type {boolean}
|
|
430
|
+
*/
|
|
431
|
+
this.allowPublicSuffix = allowPublicSuffix;
|
|
268
432
|
}
|
|
269
433
|
|
|
270
434
|
/**
|
|
@@ -279,7 +443,7 @@ class Validator {
|
|
|
279
443
|
// Iterate from the end to the beginning so we can remove
|
|
280
444
|
// preceding comments/empty lines if needed
|
|
281
445
|
for (let i = filtered.length - 1; i >= 0; i -= 1) {
|
|
282
|
-
const isValidRule = valid(filtered[i], this.allowedIP);
|
|
446
|
+
const isValidRule = valid(filtered[i], this.allowedIP, this.allowPublicSuffix);
|
|
283
447
|
const isCommentOrEmptyLine = ruleUtils.isComment(filtered[i]) || _.isEmpty(filtered[i]);
|
|
284
448
|
|
|
285
449
|
if (!isValidRule) {
|
|
@@ -304,12 +468,12 @@ class Validator {
|
|
|
304
468
|
* Validates a list of rules.
|
|
305
469
|
*
|
|
306
470
|
* @param {Array<string>} rules - The array of rules to validate.
|
|
307
|
-
* @param {boolean} allowedIP - flag to allow IP validation.
|
|
308
471
|
* @returns {Array<string>} The filtered list of valid rules.
|
|
309
472
|
*/
|
|
310
473
|
function validate(rules) {
|
|
311
|
-
const validator = new Validator(false);
|
|
474
|
+
const validator = new Validator(false, false);
|
|
312
475
|
// ip is not allowed for default validation
|
|
476
|
+
// public suffix matching is not allowed for default validation
|
|
313
477
|
return validator.validate(rules);
|
|
314
478
|
}
|
|
315
479
|
|
|
@@ -71,4 +71,68 @@ describe('Configuration', () => {
|
|
|
71
71
|
expect(ret.valid).toBe(false);
|
|
72
72
|
expect(ret.errorsText).toBeTruthy();
|
|
73
73
|
});
|
|
74
|
+
|
|
75
|
+
it('test ValidateAllowPublicSuffix transformation in configuration', () => {
|
|
76
|
+
const ret = config.validateConfiguration({
|
|
77
|
+
name: 'test',
|
|
78
|
+
sources: [
|
|
79
|
+
{
|
|
80
|
+
source: 'test.txt',
|
|
81
|
+
transformations: [
|
|
82
|
+
'ValidateAllowPublicSuffix',
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
transformations: [
|
|
87
|
+
'ValidateAllowPublicSuffix',
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
expect(ret.valid).toBe(true);
|
|
92
|
+
expect(ret.errorsText).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it.each([
|
|
96
|
+
['Validate', 'ValidateAllowIp'],
|
|
97
|
+
['Validate', 'ValidateAllowPublicSuffix'],
|
|
98
|
+
['ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
99
|
+
])('test incompatible top-level transformations configuration: %s + %s', (first, second) => {
|
|
100
|
+
const ret = config.validateConfiguration({
|
|
101
|
+
name: 'test',
|
|
102
|
+
sources: [
|
|
103
|
+
{
|
|
104
|
+
source: 'test.txt',
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
transformations: [
|
|
108
|
+
first,
|
|
109
|
+
second,
|
|
110
|
+
],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(ret.valid).toBe(false);
|
|
114
|
+
expect(ret.errorsText).toBeTruthy();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it.each([
|
|
118
|
+
['Validate', 'ValidateAllowIp'],
|
|
119
|
+
['Validate', 'ValidateAllowPublicSuffix'],
|
|
120
|
+
['ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
121
|
+
])('test incompatible source transformations configuration: %s + %s', (first, second) => {
|
|
122
|
+
const ret = config.validateConfiguration({
|
|
123
|
+
name: 'test',
|
|
124
|
+
sources: [
|
|
125
|
+
{
|
|
126
|
+
source: 'test.txt',
|
|
127
|
+
transformations: [
|
|
128
|
+
first,
|
|
129
|
+
second,
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(ret.valid).toBe(false);
|
|
136
|
+
expect(ret.errorsText).toBeTruthy();
|
|
137
|
+
});
|
|
74
138
|
});
|
package/test/index.test.js
CHANGED
|
@@ -45,4 +45,68 @@ describe('Hostlist compiler', () => {
|
|
|
45
45
|
|
|
46
46
|
scope.done();
|
|
47
47
|
});
|
|
48
|
+
|
|
49
|
+
it('throws error when validation transformations are used at both source and top level', async () => {
|
|
50
|
+
// No need to mock HTTP request - error is thrown before download
|
|
51
|
+
const configuration = {
|
|
52
|
+
name: 'Test filter',
|
|
53
|
+
transformations: ['Validate'],
|
|
54
|
+
sources: [
|
|
55
|
+
{
|
|
56
|
+
source: 'https://example.org/source.txt',
|
|
57
|
+
transformations: ['ValidateAllowPublicSuffix'],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
await expect(compile(configuration)).rejects.toThrow(
|
|
63
|
+
/Validation transformations cannot be used at both source and top level/,
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('allows validation transformation at source level only', async () => {
|
|
68
|
+
const scope = nock('https://example.org')
|
|
69
|
+
.get('/source.txt')
|
|
70
|
+
.reply(200, '||org^', {
|
|
71
|
+
'Content-Type': 'text/plain',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const configuration = {
|
|
75
|
+
name: 'Test filter',
|
|
76
|
+
sources: [
|
|
77
|
+
{
|
|
78
|
+
source: 'https://example.org/source.txt',
|
|
79
|
+
transformations: ['ValidateAllowPublicSuffix'],
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const list = await compile(configuration);
|
|
85
|
+
expect(list.join('\n')).toContain('||org^');
|
|
86
|
+
|
|
87
|
+
scope.done();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('allows validation transformation at top level only', async () => {
|
|
91
|
+
const scope = nock('https://example.org')
|
|
92
|
+
.get('/source.txt')
|
|
93
|
+
.reply(200, '||org^', {
|
|
94
|
+
'Content-Type': 'text/plain',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const configuration = {
|
|
98
|
+
name: 'Test filter',
|
|
99
|
+
transformations: ['Validate'],
|
|
100
|
+
sources: [
|
|
101
|
+
{
|
|
102
|
+
source: 'https://example.org/source.txt',
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const list = await compile(configuration);
|
|
108
|
+
expect(list.join('\n')).not.toContain('||org^');
|
|
109
|
+
|
|
110
|
+
scope.done();
|
|
111
|
+
});
|
|
48
112
|
});
|
|
@@ -56,6 +56,25 @@ dupl1
|
|
|
56
56
|
expect(filtered).toEqual(['rule1', 'dupl1', '||185.149.120.173^']);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
it.each([
|
|
60
|
+
['Validate + ValidateAllowIp', ['Validate', 'ValidateAllowIp']],
|
|
61
|
+
['Validate + ValidateAllowPublicSuffix', ['Validate', 'ValidateAllowPublicSuffix']],
|
|
62
|
+
['ValidateAllowIp + ValidateAllowPublicSuffix', ['ValidateAllowIp', 'ValidateAllowPublicSuffix']],
|
|
63
|
+
[
|
|
64
|
+
'Validate + ValidateAllowIp + ValidateAllowPublicSuffix',
|
|
65
|
+
['Validate', 'ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
66
|
+
],
|
|
67
|
+
])('throws when incompatible validation transformations are combined: %s', async (_name, transformations) => {
|
|
68
|
+
const rules = [
|
|
69
|
+
'||185.149.120.173^',
|
|
70
|
+
'||hl.cn^',
|
|
71
|
+
'||example.com^',
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
await expect(transform(rules, {}, transformations))
|
|
75
|
+
.rejects.toThrow(`Validation transformations cannot be combined: ${transformations.join(', ')}.`);
|
|
76
|
+
});
|
|
77
|
+
|
|
59
78
|
it('simple transformations with removeModifiers', async () => {
|
|
60
79
|
const rules = `! test comment
|
|
61
80
|
rule1
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { validateAllowIp } = require('../../src/transformations/validate-allow-ip');
|
|
2
2
|
|
|
3
|
-
describe('
|
|
3
|
+
describe('ValidateAllowIp', () => {
|
|
4
4
|
it('simple /etc/hosts rule', () => {
|
|
5
5
|
const rules = '0.0.0.0 example.org'.split(/\r?\n/);
|
|
6
6
|
const filtered = validateAllowIp(rules);
|
|
@@ -68,4 +68,47 @@ describe('validateAllowIp', () => {
|
|
|
68
68
|
'@@||example.org^|$important',
|
|
69
69
|
]);
|
|
70
70
|
});
|
|
71
|
+
|
|
72
|
+
it('allows IP-subnet patterns but rejects IP-suffix patterns', () => {
|
|
73
|
+
// ValidateAllowIp allows IP-subnet patterns (with | or || prefix) because they block specific subnets.
|
|
74
|
+
// But IP-suffix patterns (no prefix) are ALWAYS rejected because they block unpredictably
|
|
75
|
+
// (e.g., 1.1^ blocks 1.1.1.1, 1.1.111.1, example1.1).
|
|
76
|
+
const rules = [
|
|
77
|
+
// IP-suffix (no prefix) — rejected ALWAYS (blocks unpredictably)
|
|
78
|
+
'1.1^',
|
|
79
|
+
'1.1.1^',
|
|
80
|
+
'1.1.1.1^',
|
|
81
|
+
'192.168^',
|
|
82
|
+
'10.0.1^',
|
|
83
|
+
'192.168.1.1^',
|
|
84
|
+
// IP-subnet (with prefix) — allowed in ValidateAllowIp
|
|
85
|
+
'||1.1^',
|
|
86
|
+
'|1.1^',
|
|
87
|
+
'||1.1.1^',
|
|
88
|
+
'|1.1.1^',
|
|
89
|
+
'||1.1.1.1^',
|
|
90
|
+
'|1.1.1.1^',
|
|
91
|
+
'||192.168^',
|
|
92
|
+
'||10.0.1^',
|
|
93
|
+
'||192.168.1.1^',
|
|
94
|
+
// valid domain — kept
|
|
95
|
+
'||example.org^',
|
|
96
|
+
];
|
|
97
|
+
const filtered = validateAllowIp(rules);
|
|
98
|
+
|
|
99
|
+
expect(filtered).toEqual([
|
|
100
|
+
// IP-subnet patterns are allowed
|
|
101
|
+
'||1.1^',
|
|
102
|
+
'|1.1^',
|
|
103
|
+
'||1.1.1^',
|
|
104
|
+
'|1.1.1^',
|
|
105
|
+
'||1.1.1.1^',
|
|
106
|
+
'|1.1.1.1^',
|
|
107
|
+
'||192.168^',
|
|
108
|
+
'||10.0.1^',
|
|
109
|
+
'||192.168.1.1^',
|
|
110
|
+
// valid domain
|
|
111
|
+
'||example.org^',
|
|
112
|
+
]);
|
|
113
|
+
});
|
|
71
114
|
});
|