@adguard/hostlist-compiler 1.0.38 → 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.
@@ -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
- consola.debug(`matching the whole public suffix ${hostname} is not allowed: ${ruleText}`);
68
- return false;
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
- if (props.pattern.length < MAX_PATTERN_LENGTH) {
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 rule text
191
- const domainToCheck = utils.substringBetween(ruleText, DOMAIN_PREFIX, DOMAIN_SEPARATOR);
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
 
@@ -170,4 +170,48 @@ describe('Source compiler', () => {
170
170
  ]);
171
171
  scope.done();
172
172
  });
173
+
174
+ it('compile the empty local source', async () => {
175
+ mock({
176
+ 'test/dir': {
177
+ 'empty.txt': '',
178
+ },
179
+ });
180
+
181
+ const source = {
182
+ name: 'test source',
183
+ source: 'test/dir/empty.txt',
184
+ transformations: [
185
+ TRANSFORMATIONS.ConvertToAscii,
186
+ TRANSFORMATIONS.Deduplicate,
187
+ TRANSFORMATIONS.RemoveComments,
188
+ TRANSFORMATIONS.TrimLines,
189
+ ],
190
+ };
191
+ const compiled = await compileSource(source);
192
+ expect(compiled).toEqual(['']);
193
+ });
194
+
195
+ it('compile the empty external source', async () => {
196
+ const scope = nock('https://example.org')
197
+ .get('/empty.txt')
198
+ .reply(200, '', {
199
+ 'Content-Type': 'text/plain',
200
+ });
201
+
202
+ const source = {
203
+ name: 'test source',
204
+ source: 'https://example.org/empty.txt',
205
+ transformations: [
206
+ TRANSFORMATIONS.ConvertToAscii,
207
+ TRANSFORMATIONS.Deduplicate,
208
+ TRANSFORMATIONS.RemoveComments,
209
+ TRANSFORMATIONS.TrimLines,
210
+ TRANSFORMATIONS.Compress,
211
+ ],
212
+ };
213
+ const compiled = await compileSource(source);
214
+ expect(compiled).toEqual(['']);
215
+ scope.done();
216
+ });
173
217
  });
@@ -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
  });
@@ -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
  });
@@ -293,4 +293,102 @@ exclude.com
293
293
 
294
294
  scope.done();
295
295
  });
296
+
297
+ it('should compile source with empty !#include and empty source', async () => {
298
+ // Prepare filters content
299
+ const filterContent = `! this is a source
300
+ ||*.ком^
301
+ ||trackers.exclude.com^
302
+ ||exclude.com^
303
+ !#include https://example.org/source.txt`;
304
+
305
+ // Prepare source
306
+ const scope = nock('https://example.org')
307
+ .get('/source1.txt')
308
+ .reply(200, filterContent, {
309
+ 'Content-Type': 'text/plain',
310
+ })
311
+ .get('/source.txt')
312
+ .reply(200, '', {
313
+ 'Content-Type': 'text/plain',
314
+ })
315
+ .get('/empty.txt')
316
+ .reply(200, '', {
317
+ 'Content-Type': 'text/plain',
318
+ });
319
+
320
+ // compiler configuration
321
+ const configuration = {
322
+ name: 'Test filter',
323
+ description: 'Our test filter',
324
+ version: '1.0.0.9',
325
+ sources: [
326
+ {
327
+ name: 'source 1',
328
+ source: 'https://example.org/source1.txt',
329
+ },
330
+ ],
331
+ transformations: [
332
+ 'RemoveComments',
333
+ 'ConvertToAscii',
334
+ ],
335
+ inclusions_sources: ['https://example.org/empty.txt'],
336
+ };
337
+
338
+ const expectedRules = [
339
+ '||*.xn--j1aef^',
340
+ '||trackers.exclude.com^',
341
+ '||exclude.com^',
342
+ ];
343
+
344
+ // compile the final list
345
+ const list = await compile(configuration);
346
+
347
+ const str = list.join('\n');
348
+ consola.info(str);
349
+
350
+ expectedRules.forEach((rule) => {
351
+ expect(list).toContain(rule);
352
+ });
353
+
354
+ scope.done();
355
+ });
356
+
357
+ it('should compile source with empty source', async () => {
358
+ // Prepare source
359
+ const scope = nock('https://example.org')
360
+ .get('/source.txt')
361
+ .reply(200, '', {
362
+ 'Content-Type': 'text/plain',
363
+ })
364
+ .get('/empty.txt')
365
+ .reply(200, '||example.com^', {
366
+ 'Content-Type': 'text/plain',
367
+ });
368
+
369
+ // compiler configuration
370
+ const configuration = {
371
+ name: 'Test filter',
372
+ description: 'Our test filter',
373
+ version: '1.0.0.9',
374
+ sources: [
375
+ {
376
+ name: 'source 1',
377
+ source: 'https://example.org/empty.txt',
378
+ },
379
+ ],
380
+ transformations: [
381
+ 'RemoveComments',
382
+ 'ConvertToAscii',
383
+ ],
384
+ inclusions_sources: ['https://example.org/source.txt'],
385
+ };
386
+
387
+ const list = await compile(configuration);
388
+ consola.info(list.join('\n'));
389
+
390
+ expect(list).toContain('||example.com^');
391
+
392
+ scope.done();
393
+ });
296
394
  });
@@ -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