@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.
@@ -0,0 +1,264 @@
1
+ const { validateAllowPublicSuffix } = require('../../src/transformations/validate-allow-public-suffix');
2
+
3
+ describe('validateAllowPublicSuffix', () => {
4
+ it('keeps rules targeting public suffixes', () => {
5
+ const rules = [
6
+ '||hl.cn^',
7
+ '||org^',
8
+ ];
9
+ const filtered = validateAllowPublicSuffix(rules);
10
+
11
+ expect(filtered).toEqual([
12
+ '||hl.cn^',
13
+ '||org^',
14
+ ]);
15
+ });
16
+
17
+ it('keeps /etc/hosts rules targeting public suffixes', () => {
18
+ const rules = [
19
+ '0.0.0.0 org',
20
+ ];
21
+ const filtered = validateAllowPublicSuffix(rules);
22
+
23
+ expect(filtered).toEqual([
24
+ '0.0.0.0 org',
25
+ ]);
26
+ });
27
+
28
+ it('still removes invalid adblock rule', () => {
29
+ const rules = [
30
+ '||hl.cn^',
31
+ '||invalid/rule',
32
+ ];
33
+ const filtered = validateAllowPublicSuffix(rules);
34
+
35
+ expect(filtered).toEqual([
36
+ '||hl.cn^',
37
+ ]);
38
+ });
39
+
40
+ it('keeps whole public suffix variants', () => {
41
+ const rules = [
42
+ '||*.org^',
43
+ '.org^',
44
+ '*.org^',
45
+ '||org^',
46
+ ];
47
+ const filtered = validateAllowPublicSuffix(rules);
48
+
49
+ expect(filtered).toEqual([
50
+ '||*.org^',
51
+ '.org^',
52
+ '*.org^',
53
+ '||org^',
54
+ ]);
55
+ });
56
+
57
+ it('keeps whole public suffix unblocking variants', () => {
58
+ const rules = [
59
+ '@@||*.org^',
60
+ '@@.org^',
61
+ '@@*.org^',
62
+ '@@||org^',
63
+ ];
64
+ const filtered = validateAllowPublicSuffix(rules);
65
+
66
+ expect(filtered).toEqual([
67
+ '@@||*.org^',
68
+ '@@.org^',
69
+ '@@*.org^',
70
+ '@@||org^',
71
+ ]);
72
+ });
73
+
74
+ it('keeps short ccTLD patterns as public suffixes', () => {
75
+ // ||uk^ and .uk^ are 4 chars — below MAX_PATTERN_LENGTH, but extractDomainPattern
76
+ // recognises them as valid domain-style patterns so the length check is skipped.
77
+ const rules = [
78
+ '||uk^',
79
+ '.uk^',
80
+ '@@||uk^',
81
+ '@@.uk^',
82
+ '||example.uk^',
83
+ ];
84
+ const filtered = validateAllowPublicSuffix(rules);
85
+
86
+ expect(filtered).toEqual([
87
+ '||uk^',
88
+ '.uk^',
89
+ '@@||uk^',
90
+ '@@.uk^',
91
+ '||example.uk^',
92
+ ]);
93
+ });
94
+
95
+ it('rejects structurally malformed domain patterns but keeps public-suffix-only and valid ones', () => {
96
+ // Unlike Validate, validateAllowPublicSuffix keeps public-suffix-only patterns (.foo^, foo.^, ||*.org^, etc.),
97
+ // because foo/org is a public suffix and allowPublicSuffix=true.
98
+ const rules = `
99
+ ||-a^
100
+ ||a-^
101
+ ||a..b^
102
+ ||.foo^
103
+ ||foo.^
104
+ ||*.org^
105
+ .org^
106
+ *.org^
107
+ ||org^
108
+ ||foo.bar.^
109
+ ||example.org^
110
+ @@||-a^
111
+ @@||a-^
112
+ @@||a..b^
113
+ @@||.foo^
114
+ @@||foo.^
115
+ @@||*.org^
116
+ @@.org^
117
+ @@*.org^
118
+ @@||org^
119
+ @@||foo.bar.^
120
+ @@||example.org^`.split(/\r?\n/);
121
+
122
+ const filtered = validateAllowPublicSuffix(rules);
123
+
124
+ expect(filtered).toEqual([
125
+ '||.foo^',
126
+ '||foo.^',
127
+ '||*.org^',
128
+ '.org^',
129
+ '*.org^',
130
+ '||org^',
131
+ '||foo.bar.^',
132
+ '||example.org^',
133
+ '@@||.foo^',
134
+ '@@||foo.^',
135
+ '@@||*.org^',
136
+ '@@.org^',
137
+ '@@*.org^',
138
+ '@@||org^',
139
+ '@@||foo.bar.^',
140
+ '@@||example.org^',
141
+ ]);
142
+ });
143
+
144
+ it('keeps ^| variants targeting public suffixes and regular domains', () => {
145
+ const rules = [
146
+ '.com^|',
147
+ '.hl.cn^|',
148
+ '@@.com^|',
149
+ '@@.hl.cn^|',
150
+ '.example.org^|',
151
+ '@@.example.org^|',
152
+ ];
153
+ const filtered = validateAllowPublicSuffix(rules);
154
+
155
+ expect(filtered).toEqual([
156
+ '.com^|',
157
+ '.hl.cn^|',
158
+ '@@.com^|',
159
+ '@@.hl.cn^|',
160
+ '.example.org^|',
161
+ '@@.example.org^|',
162
+ ]);
163
+ });
164
+
165
+ it('rejects unknown single-label patterns not in PSL, keeps valid domains of same length', () => {
166
+ // Non-ICANN labels are rejected even with allowPublicSuffix=true.
167
+ // Valid multi-label domains of similar length are kept for contrast.
168
+ const rules = [
169
+ '||a^', // unknown — rejected
170
+ '||a.org^', // valid domain — kept
171
+ '.a^', // unknown — rejected
172
+ '.a.uk^', // valid domain — kept
173
+ '||aa^', // unknown — rejected
174
+ '||aa.com^', // valid domain — kept
175
+ '||aaaa^', // unknown — rejected
176
+ '||aaaa.org^', // valid domain — kept
177
+ '||aaaaa^', // unknown — rejected
178
+ '||1^', // unknown — rejected
179
+ '||1.org^', // valid domain — kept
180
+ // Exception variants: equally dangerous — broad unblocking of unknown suffix.
181
+ '@@||a^', // unknown exception — rejected
182
+ '@@||a.org^', // valid domain exception — kept
183
+ '@@.aa^', // unknown exception — rejected
184
+ '@@.aa.com^', // valid domain exception — kept
185
+ '@@||aaaa^', // unknown exception — rejected
186
+ '@@||aaaa.org^', // valid domain exception — kept
187
+ ];
188
+ const filtered = validateAllowPublicSuffix(rules);
189
+
190
+ expect(filtered).toEqual([
191
+ '||a.org^',
192
+ '.a.uk^',
193
+ '||aa.com^',
194
+ '||aaaa.org^',
195
+ '||1.org^',
196
+ '@@||a.org^',
197
+ '@@.aa.com^',
198
+ '@@||aaaa.org^',
199
+ ]);
200
+ });
201
+
202
+ it('keeps known ICANN TLDs but rejects unknown ones, with valid domain counterparts', () => {
203
+ // aaa is a real gTLD (isIcann=true), aaaa is not.
204
+ // foo and bar are real gTLDs, "zzzz" is not.
205
+ // Valid domains under each suffix are always kept regardless.
206
+ const rules = [
207
+ '||aaa^', // real gTLD — kept (VAPS allows known public suffixes)
208
+ '||aaa.com^', // valid domain — kept
209
+ '||aaaa^', // unknown — rejected
210
+ '||aaaa.com^', // valid domain — kept
211
+ '||foo^', // real gTLD — kept
212
+ '||test.foo^', // valid domain under .foo — kept
213
+ '||bar^', // real gTLD — kept
214
+ '||zzzz^', // unknown — rejected
215
+ '||zzzz.org^', // valid domain — kept
216
+ '@@||aaa^', // real gTLD exception — kept
217
+ '@@||aaa.com^', // valid domain exception — kept
218
+ '@@||aaaa^', // unknown exception — rejected
219
+ '@@||aaaa.com^', // valid domain exception — kept
220
+ ];
221
+ const filtered = validateAllowPublicSuffix(rules);
222
+
223
+ expect(filtered).toEqual([
224
+ '||aaa^',
225
+ '||aaa.com^',
226
+ '||aaaa.com^',
227
+ '||foo^',
228
+ '||test.foo^',
229
+ '||bar^',
230
+ '||zzzz.org^',
231
+ '@@||aaa^',
232
+ '@@||aaa.com^',
233
+ '@@||aaaa.com^',
234
+ ]);
235
+ });
236
+
237
+ it('rejects broad blocking/unblocking for non-PSL hostnames, keeps valid counterparts', () => {
238
+ // Fake TLD patterns are rejected.
239
+ // Similar-looking valid domain patterns are kept for contrast.
240
+ const rules = [
241
+ // Blocking: fake TLD — rejected
242
+ '||xyz123notreal^',
243
+ '||xyz123notreal.com^', // valid domain — kept
244
+ '.xyz123notreal^',
245
+ '.xyz123notreal.org^', // valid domain — kept
246
+ '*.xyz123notreal^',
247
+ '||*.xyz123notreal^',
248
+ // Unblocking: fake TLD — rejected
249
+ '@@||xyz123notreal^',
250
+ '@@||xyz123notreal.com^', // valid domain exception — kept
251
+ '@@.xyz123notreal^',
252
+ '@@.xyz123notreal.org^', // valid domain exception — kept
253
+ '@@*.xyz123notreal^',
254
+ ];
255
+ const filtered = validateAllowPublicSuffix(rules);
256
+
257
+ expect(filtered).toEqual([
258
+ '||xyz123notreal.com^',
259
+ '.xyz123notreal.org^',
260
+ '@@||xyz123notreal.com^',
261
+ '@@.xyz123notreal.org^',
262
+ ]);
263
+ });
264
+ });
@@ -133,4 +133,198 @@ describe('Validate', () => {
133
133
  '||*.example.org^',
134
134
  ]);
135
135
  });
136
+
137
+ it('removes whole public suffix variants by default', () => {
138
+ const rules = `||*.org^
139
+ .org^
140
+ *.org^
141
+ ||org^
142
+ ||example.org^`.split(/\r?\n/);
143
+ const filtered = validate(rules);
144
+
145
+ expect(filtered).toEqual(['||example.org^']);
146
+ });
147
+
148
+ it('removes short ccTLD patterns as public suffixes', () => {
149
+ // ||uk^ and .uk^ are 4 chars — below MAX_PATTERN_LENGTH, but structurally valid
150
+ // domain patterns. The length exception must not bypass the public suffix check.
151
+ const rules = [
152
+ '||uk^',
153
+ '.uk^',
154
+ '@@||uk^',
155
+ '@@.uk^',
156
+ '||example.uk^',
157
+ ];
158
+ const filtered = validate(rules);
159
+
160
+ expect(filtered).toEqual(['||example.uk^']);
161
+ });
162
+
163
+ it('removes whole public suffix exception variants by default', () => {
164
+ const rules = `@@||*.org^
165
+ @@.org^
166
+ @@*.org^
167
+ @@||org^
168
+ @@||example.org^`.split(/\r?\n/);
169
+ const filtered = validate(rules);
170
+
171
+ expect(filtered).toEqual(['@@||example.org^']);
172
+ });
173
+
174
+ it('rejects malformed domain patterns but keeps valid ones including trailing-dot FQDN', () => {
175
+ const rules = `
176
+ ||-a^
177
+ ||a-^
178
+ ||a..b^
179
+ ||org..^
180
+ ||..^
181
+ ||.foo^
182
+ ||foo.^
183
+ ||.org^
184
+ ||foo.bar.^
185
+ ||example.org^
186
+ ||example.org^|
187
+ @@||-a^
188
+ @@||a-^
189
+ @@||a..b^
190
+ @@||org..^
191
+ @@||..^
192
+ @@||.foo^
193
+ @@||foo.^
194
+ @@||.org^
195
+ @@||foo.bar.^
196
+ @@||example.org^
197
+ @@||example.org^|`.split(/\r?\n/);
198
+
199
+ const filtered = validate(rules);
200
+
201
+ expect(filtered).toEqual([
202
+ '||foo.bar.^',
203
+ '||example.org^',
204
+ '||example.org^|',
205
+ '@@||foo.bar.^',
206
+ '@@||example.org^',
207
+ '@@||example.org^|',
208
+ ]);
209
+ });
210
+
211
+ it('rejects ^| variants targeting public suffixes, keeps ^| for regular domains', () => {
212
+ const rules = [
213
+ '.com^|',
214
+ '.hl.cn^|',
215
+ '@@.com^|',
216
+ '@@.hl.cn^|',
217
+ '.example.org^|',
218
+ '@@.example.org^|',
219
+ ];
220
+ const filtered = validate(rules);
221
+
222
+ expect(filtered).toEqual([
223
+ '.example.org^|',
224
+ '@@.example.org^|',
225
+ ]);
226
+ });
227
+
228
+ it('rejects unknown single-label patterns not in PSL, keeps valid domains of same length', () => {
229
+ // Validate rejects all public-suffix-only patterns (known or not),
230
+ // but non-PSL labels are rejected for an additional reason: isIcann=false.
231
+ // Valid multi-label domains of similar length are kept to show contrast.
232
+ const rules = [
233
+ '||a^', // unknown, isIcann=false — rejected
234
+ '||a.org^', // valid domain (2 chars pattern) — kept
235
+ '||aa^', // unknown, isIcann=false — rejected
236
+ '||ab.uk^', // valid domain (2+2 chars) — kept
237
+ '||aaa^', // known gTLD, isIcann=true — still rejected (public suffix)
238
+ '||aaa.com^', // valid domain under real TLD — kept
239
+ '||org^', // known TLD, isIcann=true — still rejected (public suffix)
240
+ '||org.example.com^', // valid subdomain — kept
241
+ '||aaaa^', // unknown, isIcann=false — rejected
242
+ '@@||a^', // unknown exception — broad unblock, rejected
243
+ '@@||a.org^', // valid domain exception — kept
244
+ '@@||org^', // known TLD exception — still rejected
245
+ '@@||org.example.com^', // valid subdomain exception — kept
246
+ ];
247
+ const filtered = validate(rules);
248
+
249
+ expect(filtered).toEqual([
250
+ '||a.org^',
251
+ '||ab.uk^',
252
+ '||aaa.com^',
253
+ '||org.example.com^',
254
+ '@@||a.org^',
255
+ '@@||org.example.com^',
256
+ ]);
257
+ });
258
+
259
+ it('rejects broad blocking/unblocking for non-PSL fake TLDs, keeps valid counterparts', () => {
260
+ // Fake TLD patterns are rejected.
261
+ // Similar-looking valid domain patterns are kept for contrast.
262
+ const rules = [
263
+ '||xyz123notreal^', // fake TLD — rejected
264
+ '||xyz123notreal.com^', // valid domain under .com — kept
265
+ '.xyz123notreal^', // fake TLD — rejected
266
+ '.xyz123notreal.org^', // valid domain under .org — kept
267
+ '*.xyz123notreal^', // fake TLD — rejected
268
+ '@@||xyz123notreal^', // fake TLD exception — rejected
269
+ '@@||xyz123notreal.com^', // valid domain exception — kept
270
+ '@@.xyz123notreal^', // fake TLD exception — rejected
271
+ '@@*.xyz123notreal^', // fake TLD exception — rejected
272
+ ];
273
+ const filtered = validate(rules);
274
+
275
+ expect(filtered).toEqual([
276
+ '||xyz123notreal.com^',
277
+ '.xyz123notreal.org^',
278
+ '@@||xyz123notreal.com^',
279
+ ]);
280
+ });
281
+
282
+ it('rejects all IP patterns in Validate (subnets and suffixes)', () => {
283
+ // Validate rejects ALL numeric IP-like patterns:
284
+ // - IP-suffix patterns (no | or || prefix) — block unpredictably (e.g., 1.1^ blocks 1.1.1.1, example1.1)
285
+ // - IP-subnet patterns (with | or || prefix) — block IP subnets
286
+ // - Full 4-octet IPs — rejected via tldts.isIp
287
+ const rules = [
288
+ // 1 octet — rejected (too short or invalid)
289
+ '1^',
290
+ '||1^',
291
+ '|1^',
292
+ // 2 octets — IP-suffix (no prefix) — rejected ALWAYS
293
+ '1.1^',
294
+ '192.168^',
295
+ // 2 octets — IP-subnet (with prefix) — rejected in Validate
296
+ '||1.1^',
297
+ '|1.1^',
298
+ '||192.168^',
299
+ '||1.1.',
300
+ '||1.1.^',
301
+ // 3 octets — IP-suffix (no prefix) — rejected ALWAYS
302
+ '1.1.1^',
303
+ '10.0.1^',
304
+ // 3 octets — IP-subnet (with prefix) — rejected in Validate
305
+ '||1.1.2^',
306
+ '|1.1.2^',
307
+ '||10.0.1^',
308
+ '||1.1.2.',
309
+ '||1.1.2.^',
310
+ // 4 octets — IP-suffix (no prefix) — rejected ALWAYS
311
+ '1.1.1.1^',
312
+ '192.168.1.1^',
313
+ // 4 octets — full IP (with prefix) — rejected via tldts.isIp
314
+ '||1.1.1.1^',
315
+ '|1.1.1.1^',
316
+ '||192.168.1.1^',
317
+ '||10.0.0.1^',
318
+ '||255.255.255.255^',
319
+ // valid domains — kept
320
+ '||example.org^',
321
+ '||a.b.c.d^', // 4 labels (not numeric IP) — kept
322
+ ];
323
+ const filtered = validate(rules);
324
+
325
+ expect(filtered).toEqual([
326
+ '||example.org^',
327
+ '||a.b.c.d^',
328
+ ]);
329
+ });
136
330
  });