@adguard/hostlist-compiler 1.0.39 → 2.1.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 +41 -0
- package/DEVELOPMENT.md +172 -0
- package/README.md +142 -95
- 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 +3 -3
- package/src/index.d.ts +6 -2
- package/src/index.js +44 -1
- package/src/schemas/configuration.schema.json +200 -0
- package/src/transformations/ip-normalize.js +182 -0
- package/src/transformations/transform.js +37 -1
- package/src/transformations/validate-allow-ip-and-public-suffix.js +33 -0
- package/src/transformations/validate-allow-ip.js +18 -4
- package/src/transformations/validate-allow-public-suffix.js +20 -0
- package/src/transformations/validate.js +251 -22
- package/src/utils.js +137 -0
- package/test/configuration.test.js +70 -0
- package/test/index.test.js +64 -0
- package/test/transformations/ip-alignment.test.js +65 -0
- package/test/transformations/ip-normalize.test.js +331 -0
- package/test/transformations/transform.test.js +25 -0
- package/test/transformations/validate-allow-ip-and-public-suffix.test.js +167 -0
- package/test/transformations/validate-allow-ip.test.js +72 -1
- package/test/transformations/validate-allow-public-suffix.test.js +284 -0
- package/test/transformations/validate.test.js +251 -0
|
@@ -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);
|
|
@@ -46,6 +46,8 @@ describe('validateAllowIp', () => {
|
|
|
46
46
|
||example.org^$third-party
|
|
47
47
|
||example.org^$important
|
|
48
48
|
||*.ga^$denyallow=example1.ga|example2.ga
|
|
49
|
+
1.1.1.1$denyallow=001114.cn
|
|
50
|
+
||1.1.1.1^$denyallow=001114.cn
|
|
49
51
|
://ww4.$denyallow=ww4.example.com
|
|
50
52
|
://example.org
|
|
51
53
|
||example.org^|
|
|
@@ -68,4 +70,73 @@ describe('validateAllowIp', () => {
|
|
|
68
70
|
'@@||example.org^|$important',
|
|
69
71
|
]);
|
|
70
72
|
});
|
|
73
|
+
|
|
74
|
+
it('normalizes and validates IP patterns', () => {
|
|
75
|
+
// ValidateAllowIp now normalizes incomplete IP rules to ||ip^ format
|
|
76
|
+
// and rejects patterns that don't work in AdGuard Home.
|
|
77
|
+
const rules = [
|
|
78
|
+
// IP-suffix (no prefix) with ^ — normalized to ||ip^ for 4 octets
|
|
79
|
+
'1.1.1.1^',
|
|
80
|
+
'192.168.1.1^',
|
|
81
|
+
// IP-suffix (no prefix) with ^ — rejected for <4 octets (doesn't work)
|
|
82
|
+
'1.1^',
|
|
83
|
+
'1.1.1^',
|
|
84
|
+
'192.168^',
|
|
85
|
+
'10.0.1^',
|
|
86
|
+
// IP-subnet with ^ and <4 octets — rejected (doesn't work in AdGuard Home)
|
|
87
|
+
'||1.1^',
|
|
88
|
+
'|1.1^',
|
|
89
|
+
'||1.1.1^',
|
|
90
|
+
'|1.1.1^',
|
|
91
|
+
'||192.168^',
|
|
92
|
+
'||10.0.1^',
|
|
93
|
+
// Full 4-octet IPs — normalized to ||ip^
|
|
94
|
+
'||1.1.1.1^',
|
|
95
|
+
'|1.1.1.1^',
|
|
96
|
+
'||192.168.1.1^',
|
|
97
|
+
'1.2.3.4^|',
|
|
98
|
+
'|1.2.3.4^|',
|
|
99
|
+
'||1.2.3.4^|',
|
|
100
|
+
// 3-octet subnet wildcards — allowed (all prefix forms normalize to ||)
|
|
101
|
+
'||192.168.1.',
|
|
102
|
+
'||192.168.1.*',
|
|
103
|
+
'|192.168.1.',
|
|
104
|
+
'|192.168.1.*',
|
|
105
|
+
'192.168.1.',
|
|
106
|
+
// 3-octet without trailing dot — rejected (ambiguous)
|
|
107
|
+
'192.168.1',
|
|
108
|
+
'||192.168.1',
|
|
109
|
+
// 2-octet patterns — rejected (too wide)
|
|
110
|
+
'||1.1.',
|
|
111
|
+
'1.1.',
|
|
112
|
+
// 4-octet with trailing dot or wildcard — rejected (malformed IP, not a complete address)
|
|
113
|
+
'1.2.3.4.',
|
|
114
|
+
'1.2.3.4.*',
|
|
115
|
+
'||1.2.3.4.',
|
|
116
|
+
'||1.2.3.4.*',
|
|
117
|
+
// valid domain — kept
|
|
118
|
+
'||example.org^',
|
|
119
|
+
];
|
|
120
|
+
const filtered = validateAllowIp(rules);
|
|
121
|
+
|
|
122
|
+
expect(filtered).toEqual([
|
|
123
|
+
// 4-octet IPs normalized to ||ip^
|
|
124
|
+
'||1.1.1.1^',
|
|
125
|
+
'||192.168.1.1^',
|
|
126
|
+
'||1.1.1.1^',
|
|
127
|
+
'||1.1.1.1^',
|
|
128
|
+
'||192.168.1.1^',
|
|
129
|
+
'||1.2.3.4^',
|
|
130
|
+
'||1.2.3.4^',
|
|
131
|
+
'||1.2.3.4^',
|
|
132
|
+
// 3-octet subnet wildcards (all prefix forms normalized to ||)
|
|
133
|
+
'||192.168.1.',
|
|
134
|
+
'||192.168.1.*',
|
|
135
|
+
'||192.168.1.',
|
|
136
|
+
'||192.168.1.*',
|
|
137
|
+
'||192.168.1.',
|
|
138
|
+
// valid domain
|
|
139
|
+
'||example.org^',
|
|
140
|
+
]);
|
|
141
|
+
});
|
|
71
142
|
});
|
|
@@ -0,0 +1,284 @@
|
|
|
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
|
+
|
|
265
|
+
it('rejects terminated/unknown IDN TLDs (xn-- prefixed) not in ICANN list', () => {
|
|
266
|
+
// xn--jlq61u9w7b (.蔚来) was terminated by ICANN in 2024.
|
|
267
|
+
// tldts reports it as non-ICANN public suffix, so whole-TLD rules should be rejected
|
|
268
|
+
// just like any other unknown single-label pattern.
|
|
269
|
+
const rules = [
|
|
270
|
+
'||*.xn--jlq61u9w7b^', // terminated IDN TLD — rejected
|
|
271
|
+
'||xn--jlq61u9w7b^', // same, without wildcard — rejected
|
|
272
|
+
'.xn--jlq61u9w7b^', // same, dot-prefix — rejected
|
|
273
|
+
'*.xn--jlq61u9w7b^', // same, wildcard-prefix — rejected
|
|
274
|
+
'.xn--p1acf^', // .рус — real ICANN TLD — kept
|
|
275
|
+
'||*.xn--p1acf^', // .рус — real ICANN TLD — kept
|
|
276
|
+
];
|
|
277
|
+
const filtered = validateAllowPublicSuffix(rules);
|
|
278
|
+
|
|
279
|
+
expect(filtered).toEqual([
|
|
280
|
+
'.xn--p1acf^',
|
|
281
|
+
'||*.xn--p1acf^',
|
|
282
|
+
]);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
@@ -46,6 +46,8 @@ describe('Validate', () => {
|
|
|
46
46
|
||example.org^$third-party
|
|
47
47
|
||example.org^$important
|
|
48
48
|
||*.ga^$denyallow=example1.ga|example2.ga
|
|
49
|
+
1.1.1.1$denyallow=001114.cn
|
|
50
|
+
||1.1.1.1^$denyallow=001114.cn
|
|
49
51
|
://ww4.$denyallow=ww4.example.com
|
|
50
52
|
://example.org
|
|
51
53
|
||example.org^|
|
|
@@ -133,4 +135,253 @@ describe('Validate', () => {
|
|
|
133
135
|
'||*.example.org^',
|
|
134
136
|
]);
|
|
135
137
|
});
|
|
138
|
+
|
|
139
|
+
it('removes whole public suffix variants by default', () => {
|
|
140
|
+
const rules = `||*.org^
|
|
141
|
+
.org^
|
|
142
|
+
*.org^
|
|
143
|
+
||org^
|
|
144
|
+
||example.org^`.split(/\r?\n/);
|
|
145
|
+
const filtered = validate(rules);
|
|
146
|
+
|
|
147
|
+
expect(filtered).toEqual(['||example.org^']);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('removes short ccTLD patterns as public suffixes', () => {
|
|
151
|
+
// ||uk^ and .uk^ are 4 chars — below MAX_PATTERN_LENGTH, but structurally valid
|
|
152
|
+
// domain patterns. The length exception must not bypass the public suffix check.
|
|
153
|
+
const rules = [
|
|
154
|
+
'||uk^',
|
|
155
|
+
'.uk^',
|
|
156
|
+
'@@||uk^',
|
|
157
|
+
'@@.uk^',
|
|
158
|
+
'||example.uk^',
|
|
159
|
+
];
|
|
160
|
+
const filtered = validate(rules);
|
|
161
|
+
|
|
162
|
+
expect(filtered).toEqual(['||example.uk^']);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('removes whole public suffix exception variants by default', () => {
|
|
166
|
+
const rules = `@@||*.org^
|
|
167
|
+
@@.org^
|
|
168
|
+
@@*.org^
|
|
169
|
+
@@||org^
|
|
170
|
+
@@||example.org^`.split(/\r?\n/);
|
|
171
|
+
const filtered = validate(rules);
|
|
172
|
+
|
|
173
|
+
expect(filtered).toEqual(['@@||example.org^']);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('rejects malformed domain patterns but keeps valid ones including trailing-dot FQDN', () => {
|
|
177
|
+
const rules = `
|
|
178
|
+
||-a^
|
|
179
|
+
||a-^
|
|
180
|
+
||a..b^
|
|
181
|
+
||org..^
|
|
182
|
+
||..^
|
|
183
|
+
||.foo^
|
|
184
|
+
||foo.^
|
|
185
|
+
||.org^
|
|
186
|
+
||foo.bar.^
|
|
187
|
+
||example.org^
|
|
188
|
+
||example.org^|
|
|
189
|
+
@@||-a^
|
|
190
|
+
@@||a-^
|
|
191
|
+
@@||a..b^
|
|
192
|
+
@@||org..^
|
|
193
|
+
@@||..^
|
|
194
|
+
@@||.foo^
|
|
195
|
+
@@||foo.^
|
|
196
|
+
@@||.org^
|
|
197
|
+
@@||foo.bar.^
|
|
198
|
+
@@||example.org^
|
|
199
|
+
@@||example.org^|`.split(/\r?\n/);
|
|
200
|
+
|
|
201
|
+
const filtered = validate(rules);
|
|
202
|
+
|
|
203
|
+
expect(filtered).toEqual([
|
|
204
|
+
'||foo.bar.^',
|
|
205
|
+
'||example.org^',
|
|
206
|
+
'||example.org^|',
|
|
207
|
+
'@@||foo.bar.^',
|
|
208
|
+
'@@||example.org^',
|
|
209
|
+
'@@||example.org^|',
|
|
210
|
+
]);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('rejects ^| variants targeting public suffixes, keeps ^| for regular domains', () => {
|
|
214
|
+
const rules = [
|
|
215
|
+
'.com^|',
|
|
216
|
+
'.hl.cn^|',
|
|
217
|
+
'@@.com^|',
|
|
218
|
+
'@@.hl.cn^|',
|
|
219
|
+
'.example.org^|',
|
|
220
|
+
'@@.example.org^|',
|
|
221
|
+
];
|
|
222
|
+
const filtered = validate(rules);
|
|
223
|
+
|
|
224
|
+
expect(filtered).toEqual([
|
|
225
|
+
'.example.org^|',
|
|
226
|
+
'@@.example.org^|',
|
|
227
|
+
]);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('rejects unknown single-label patterns not in PSL, keeps valid domains of same length', () => {
|
|
231
|
+
// Validate rejects all public-suffix-only patterns (known or not),
|
|
232
|
+
// but non-PSL labels are rejected for an additional reason: isIcann=false.
|
|
233
|
+
// Valid multi-label domains of similar length are kept to show contrast.
|
|
234
|
+
const rules = [
|
|
235
|
+
'||a^', // unknown, isIcann=false — rejected
|
|
236
|
+
'||a.org^', // valid domain (2 chars pattern) — kept
|
|
237
|
+
'||aa^', // unknown, isIcann=false — rejected
|
|
238
|
+
'||ab.uk^', // valid domain (2+2 chars) — kept
|
|
239
|
+
'||aaa^', // known gTLD, isIcann=true — still rejected (public suffix)
|
|
240
|
+
'||aaa.com^', // valid domain under real TLD — kept
|
|
241
|
+
'||org^', // known TLD, isIcann=true — still rejected (public suffix)
|
|
242
|
+
'||org.example.com^', // valid subdomain — kept
|
|
243
|
+
'||aaaa^', // unknown, isIcann=false — rejected
|
|
244
|
+
'@@||a^', // unknown exception — broad unblock, rejected
|
|
245
|
+
'@@||a.org^', // valid domain exception — kept
|
|
246
|
+
'@@||org^', // known TLD exception — still rejected
|
|
247
|
+
'@@||org.example.com^', // valid subdomain exception — kept
|
|
248
|
+
];
|
|
249
|
+
const filtered = validate(rules);
|
|
250
|
+
|
|
251
|
+
expect(filtered).toEqual([
|
|
252
|
+
'||a.org^',
|
|
253
|
+
'||ab.uk^',
|
|
254
|
+
'||aaa.com^',
|
|
255
|
+
'||org.example.com^',
|
|
256
|
+
'@@||a.org^',
|
|
257
|
+
'@@||org.example.com^',
|
|
258
|
+
]);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('rejects broad blocking/unblocking for non-PSL fake TLDs, keeps valid counterparts', () => {
|
|
262
|
+
// Fake TLD patterns are rejected.
|
|
263
|
+
// Similar-looking valid domain patterns are kept for contrast.
|
|
264
|
+
const rules = [
|
|
265
|
+
'||xyz123notreal^', // fake TLD — rejected
|
|
266
|
+
'||xyz123notreal.com^', // valid domain under .com — kept
|
|
267
|
+
'.xyz123notreal^', // fake TLD — rejected
|
|
268
|
+
'.xyz123notreal.org^', // valid domain under .org — kept
|
|
269
|
+
'*.xyz123notreal^', // fake TLD — rejected
|
|
270
|
+
'@@||xyz123notreal^', // fake TLD exception — rejected
|
|
271
|
+
'@@||xyz123notreal.com^', // valid domain exception — kept
|
|
272
|
+
'@@.xyz123notreal^', // fake TLD exception — rejected
|
|
273
|
+
'@@*.xyz123notreal^', // fake TLD exception — rejected
|
|
274
|
+
];
|
|
275
|
+
const filtered = validate(rules);
|
|
276
|
+
|
|
277
|
+
expect(filtered).toEqual([
|
|
278
|
+
'||xyz123notreal.com^',
|
|
279
|
+
'.xyz123notreal.org^',
|
|
280
|
+
'@@||xyz123notreal.com^',
|
|
281
|
+
]);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('rejects all IP patterns in Validate (subnets and suffixes)', () => {
|
|
285
|
+
// Validate rejects ALL numeric IP-like patterns:
|
|
286
|
+
// - IP-suffix patterns (no | or || prefix) — block unpredictably (e.g., 1.1^ blocks 1.1.1.1, example1.1)
|
|
287
|
+
// - IP-subnet patterns (with | or || prefix) — block IP subnets
|
|
288
|
+
// - Full 4-octet IPs — rejected via tldts.isIp
|
|
289
|
+
const rules = [
|
|
290
|
+
// 1 octet — rejected (too short or invalid)
|
|
291
|
+
'1^',
|
|
292
|
+
'||1^',
|
|
293
|
+
'|1^',
|
|
294
|
+
// 2 octets — IP-suffix (no prefix) — rejected ALWAYS
|
|
295
|
+
'1.1^',
|
|
296
|
+
'192.168^',
|
|
297
|
+
// 2 octets — IP-subnet (with prefix) — rejected in Validate
|
|
298
|
+
'||1.1^',
|
|
299
|
+
'|1.1^',
|
|
300
|
+
'||192.168^',
|
|
301
|
+
'||1.1.',
|
|
302
|
+
'||1.1.^',
|
|
303
|
+
// 3 octets — IP-suffix (no prefix) — rejected ALWAYS
|
|
304
|
+
'1.1.1^',
|
|
305
|
+
'10.0.1^',
|
|
306
|
+
// 3 octets — IP-subnet (with prefix) — rejected in Validate
|
|
307
|
+
'||1.1.2^',
|
|
308
|
+
'|1.1.2^',
|
|
309
|
+
'||10.0.1^',
|
|
310
|
+
'||1.1.2.',
|
|
311
|
+
'||1.1.2.^',
|
|
312
|
+
// 4 octets — IP-suffix (no prefix) — rejected ALWAYS
|
|
313
|
+
'1.1.1.1^',
|
|
314
|
+
'192.168.1.1^',
|
|
315
|
+
// 4 octets — full IP (with prefix) — rejected via tldts.isIp
|
|
316
|
+
'||1.1.1.1^',
|
|
317
|
+
'|1.1.1.1^',
|
|
318
|
+
'||192.168.1.1^',
|
|
319
|
+
'||10.0.0.1^',
|
|
320
|
+
'||255.255.255.255^',
|
|
321
|
+
// valid domains — kept
|
|
322
|
+
'||example.org^',
|
|
323
|
+
'||a.b.c.d^', // 4 labels (not numeric IP) — kept
|
|
324
|
+
];
|
|
325
|
+
const filtered = validate(rules);
|
|
326
|
+
|
|
327
|
+
expect(filtered).toEqual([
|
|
328
|
+
'||example.org^',
|
|
329
|
+
'||a.b.c.d^',
|
|
330
|
+
]);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('rejects unsafe IP patterns without ^ (too wide or ambiguous)', () => {
|
|
334
|
+
// These patterns don't have ^ but are still dangerous:
|
|
335
|
+
// - 1-2 octet patterns (too wide)
|
|
336
|
+
// - 3 octet patterns without trailing dot/wildcard (ambiguous)
|
|
337
|
+
const rules = [
|
|
338
|
+
// 1-2 octets — too wide
|
|
339
|
+
'1.2.',
|
|
340
|
+
'1.2.*',
|
|
341
|
+
'||1.2.',
|
|
342
|
+
'||1.2.*',
|
|
343
|
+
'192.168',
|
|
344
|
+
'||192.168',
|
|
345
|
+
// 3 octets without trailing dot/wildcard — ambiguous
|
|
346
|
+
'192.168.1',
|
|
347
|
+
'||192.168.1',
|
|
348
|
+
'10.0.1',
|
|
349
|
+
// 3 octets WITH trailing dot/wildcard, no prefix — unsafe (no || means subnet is unanchored)
|
|
350
|
+
'192.168.1.',
|
|
351
|
+
'192.168.1.*',
|
|
352
|
+
'10.10.34.',
|
|
353
|
+
'10.10.34.*',
|
|
354
|
+
// 3 octets WITH trailing dot/wildcard, with || prefix — rejected in Validate (IP subnet)
|
|
355
|
+
'||192.168.1.',
|
|
356
|
+
'||192.168.1.*',
|
|
357
|
+
// 4 octets WITH trailing dot or wildcard — malformed IP, rejected in all validators
|
|
358
|
+
'1.2.3.4.',
|
|
359
|
+
'1.2.3.4.*',
|
|
360
|
+
'|1.2.3.4.',
|
|
361
|
+
'|1.2.3.4.*',
|
|
362
|
+
'||1.2.3.4.',
|
|
363
|
+
'||1.2.3.4.*',
|
|
364
|
+
// valid domains — kept
|
|
365
|
+
'||example.org^',
|
|
366
|
+
];
|
|
367
|
+
const filtered = validate(rules);
|
|
368
|
+
|
|
369
|
+
expect(filtered).toEqual([
|
|
370
|
+
'||example.org^',
|
|
371
|
+
]);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it('allows domains with IP-like subdomains', () => {
|
|
375
|
+
// Patterns like ||1.2.3.4.example.com^ are valid domain rules, not IP patterns.
|
|
376
|
+
// parseIpPattern returns null for them (> 4 parts or non-numeric labels),
|
|
377
|
+
// so they pass through all IP checks and are validated as regular domain rules.
|
|
378
|
+
const rules = [
|
|
379
|
+
'||1-2-3-4.example.com^',
|
|
380
|
+
'||111.22.33.44.host.example.com^',
|
|
381
|
+
'||111.22.33.44.ll.host.example.com^',
|
|
382
|
+
];
|
|
383
|
+
const filtered = validate(rules);
|
|
384
|
+
|
|
385
|
+
expect(filtered).toEqual(rules);
|
|
386
|
+
});
|
|
136
387
|
});
|