@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
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
|
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IP pipeline alignment tests.
|
|
3
|
+
*
|
|
4
|
+
* Runs the same pattern matrix through processIpRule(), Validate, and
|
|
5
|
+
* ValidateAllowIp to ensure all three agree — and to catch any future
|
|
6
|
+
* divergence caused by editing one module without updating the others.
|
|
7
|
+
*/
|
|
8
|
+
const { ACTION, processIpRule } = require('../../src/transformations/ip-normalize');
|
|
9
|
+
const { validate } = require('../../src/transformations/validate');
|
|
10
|
+
const { validateAllowIp } = require('../../src/transformations/validate-allow-ip');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Each row: [ input, expectedNormalized, survivesValidate, survivesValidateAllowIp ]
|
|
14
|
+
*
|
|
15
|
+
* expectedNormalized: if processIpRule rewrites the rule, this is the canonical
|
|
16
|
+
* form; null means processIpRule leaves the input unchanged (keep/allow).
|
|
17
|
+
* survives*: whether the canonical form is kept by that transformation.
|
|
18
|
+
*
|
|
19
|
+
* Note: Validate (without AllowIp) rejects all IP-like patterns regardless of
|
|
20
|
+
* canonical form — IPs are only permitted in the ValidateAllowIp path.
|
|
21
|
+
*/
|
|
22
|
+
const CASES = [
|
|
23
|
+
// Full 4-octet IPs — already canonical; dropped by Validate, kept by ValidateAllowIp
|
|
24
|
+
['||1.2.3.4^', null, false, true],
|
|
25
|
+
// Full 4-octet IPs — normalizer rewrites to canonical form first
|
|
26
|
+
['1.2.3.4', '||1.2.3.4^', false, true],
|
|
27
|
+
['1.2.3.4^', '||1.2.3.4^', false, true],
|
|
28
|
+
['1.2.3.4^|', '||1.2.3.4^', false, true],
|
|
29
|
+
['|1.2.3.4^|', '||1.2.3.4^', false, true],
|
|
30
|
+
['||1.2.3.4^|', '||1.2.3.4^', false, true],
|
|
31
|
+
// 3-octet subnets with || — normalizer keeps them as-is
|
|
32
|
+
['||192.168.1.', null, false, true],
|
|
33
|
+
['||192.168.1.*', null, false, true],
|
|
34
|
+
// 3-octet subnets without || — normalizer adds the prefix
|
|
35
|
+
['192.168.1.', '||192.168.1.', false, true],
|
|
36
|
+
['192.168.1.*', '||192.168.1.*', false, true],
|
|
37
|
+
['||192.168.1.^|', null, false, false],
|
|
38
|
+
// Patterns rejected by all validators
|
|
39
|
+
['||1.2.3^', null, false, false],
|
|
40
|
+
['||1.2^', null, false, false],
|
|
41
|
+
['192.168.1', null, false, false],
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
describe('IP pipeline alignment: processIpRule / Validate / ValidateAllowIp', () => {
|
|
45
|
+
it.each(CASES)(
|
|
46
|
+
'input=%s, normalized=%s, validate=%s, allowIp=%s',
|
|
47
|
+
(input, expectedNorm, expectsValidate, expectsAllowIp) => {
|
|
48
|
+
// 1. Normalization step
|
|
49
|
+
const result = processIpRule(input);
|
|
50
|
+
const canonical = result.action === ACTION.NORMALIZE ? result.normalized : input;
|
|
51
|
+
|
|
52
|
+
if (expectedNorm !== null) {
|
|
53
|
+
expect(canonical).toBe(expectedNorm);
|
|
54
|
+
} else {
|
|
55
|
+
expect(canonical).toBe(input);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 2. Validate — receives the canonical form (no IP normalization in this path)
|
|
59
|
+
expect(validate([canonical]).includes(canonical)).toBe(expectsValidate);
|
|
60
|
+
|
|
61
|
+
// 3. ValidateAllowIp — receives the raw input (it runs normalization internally)
|
|
62
|
+
expect(validateAllowIp([input]).includes(canonical)).toBe(expectsAllowIp);
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
});
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
const {
|
|
2
|
+
ACTION,
|
|
3
|
+
parseIpPattern,
|
|
4
|
+
normalizeFullIp,
|
|
5
|
+
check3OctetSubnet,
|
|
6
|
+
processIpRule,
|
|
7
|
+
normalizeIpRules,
|
|
8
|
+
} = require('../../src/transformations/ip-normalize');
|
|
9
|
+
|
|
10
|
+
describe('ip-normalize', () => {
|
|
11
|
+
describe('parseIpPattern', () => {
|
|
12
|
+
it('parses full IP without separators', () => {
|
|
13
|
+
const result = parseIpPattern('1.2.3.4');
|
|
14
|
+
expect(result).toEqual({
|
|
15
|
+
prefix: '',
|
|
16
|
+
octets: ['1', '2', '3', '4'],
|
|
17
|
+
hasTrailingDot: false,
|
|
18
|
+
hasTrailingWildcard: false,
|
|
19
|
+
hasCaret: false,
|
|
20
|
+
hasCaretPipe: false,
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('parses full IP with || and ^', () => {
|
|
25
|
+
const result = parseIpPattern('||1.2.3.4^');
|
|
26
|
+
expect(result).toEqual({
|
|
27
|
+
prefix: '||',
|
|
28
|
+
octets: ['1', '2', '3', '4'],
|
|
29
|
+
hasTrailingDot: false,
|
|
30
|
+
hasTrailingWildcard: false,
|
|
31
|
+
hasCaret: true,
|
|
32
|
+
hasCaretPipe: false,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('parses full IP with | and ^', () => {
|
|
37
|
+
const result = parseIpPattern('|1.2.3.4^');
|
|
38
|
+
expect(result).toEqual({
|
|
39
|
+
prefix: '|',
|
|
40
|
+
octets: ['1', '2', '3', '4'],
|
|
41
|
+
hasTrailingDot: false,
|
|
42
|
+
hasTrailingWildcard: false,
|
|
43
|
+
hasCaret: true,
|
|
44
|
+
hasCaretPipe: false,
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('parses 3-octet subnet with trailing dot', () => {
|
|
49
|
+
const result = parseIpPattern('192.168.1.');
|
|
50
|
+
expect(result).toEqual({
|
|
51
|
+
prefix: '',
|
|
52
|
+
octets: ['192', '168', '1'],
|
|
53
|
+
hasTrailingDot: true,
|
|
54
|
+
hasTrailingWildcard: false,
|
|
55
|
+
hasCaret: false,
|
|
56
|
+
hasCaretPipe: false,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('parses 3-octet subnet with trailing wildcard', () => {
|
|
61
|
+
const result = parseIpPattern('||192.168.1.*');
|
|
62
|
+
expect(result).toEqual({
|
|
63
|
+
prefix: '||',
|
|
64
|
+
octets: ['192', '168', '1'],
|
|
65
|
+
hasTrailingDot: false,
|
|
66
|
+
hasTrailingWildcard: true,
|
|
67
|
+
hasCaret: false,
|
|
68
|
+
hasCaretPipe: false,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('parses pattern with ^|', () => {
|
|
73
|
+
const result = parseIpPattern('||1.2.3.4^|');
|
|
74
|
+
expect(result).toEqual({
|
|
75
|
+
prefix: '||',
|
|
76
|
+
octets: ['1', '2', '3', '4'],
|
|
77
|
+
hasTrailingDot: false,
|
|
78
|
+
hasTrailingWildcard: false,
|
|
79
|
+
hasCaret: true,
|
|
80
|
+
hasCaretPipe: true,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('returns null for non-IP patterns', () => {
|
|
85
|
+
expect(parseIpPattern('example.com')).toBeNull();
|
|
86
|
+
expect(parseIpPattern('||example.com^')).toBeNull();
|
|
87
|
+
expect(parseIpPattern('1.2.3.4.5')).toBeNull();
|
|
88
|
+
expect(parseIpPattern('1.2.3.256')).toBeNull();
|
|
89
|
+
expect(parseIpPattern('1.2.3.abc')).toBeNull();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('normalizeFullIp', () => {
|
|
94
|
+
it('normalizes IP without separators', () => {
|
|
95
|
+
expect(normalizeFullIp('1.2.3.4')).toBe('||1.2.3.4^');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('normalizes IP with only ^', () => {
|
|
99
|
+
expect(normalizeFullIp('1.2.3.4^')).toBe('||1.2.3.4^');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('normalizes IP with only |', () => {
|
|
103
|
+
expect(normalizeFullIp('|1.2.3.4')).toBe('||1.2.3.4^');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('normalizes IP with | and ^', () => {
|
|
107
|
+
expect(normalizeFullIp('|1.2.3.4^')).toBe('||1.2.3.4^');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('normalizes IP with ^|', () => {
|
|
111
|
+
expect(normalizeFullIp('1.2.3.4^|')).toBe('||1.2.3.4^');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('normalizes IP with | and ^|', () => {
|
|
115
|
+
expect(normalizeFullIp('|1.2.3.4^|')).toBe('||1.2.3.4^');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('normalizes IP with only ||', () => {
|
|
119
|
+
expect(normalizeFullIp('||1.2.3.4')).toBe('||1.2.3.4^');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('returns null for already correct format', () => {
|
|
123
|
+
expect(normalizeFullIp('||1.2.3.4^')).toBeNull();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('returns null for 3-octet patterns', () => {
|
|
127
|
+
expect(normalizeFullIp('192.168.1.')).toBeNull();
|
|
128
|
+
expect(normalizeFullIp('||192.168.1.*')).toBeNull();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('returns null for non-IP patterns', () => {
|
|
132
|
+
expect(normalizeFullIp('example.com')).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('check3OctetSubnet', () => {
|
|
137
|
+
it('allows 3-octet with trailing dot', () => {
|
|
138
|
+
expect(check3OctetSubnet('192.168.1.')).toEqual({
|
|
139
|
+
action: ACTION.NORMALIZE,
|
|
140
|
+
normalized: '||192.168.1.',
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('allows 3-octet with trailing wildcard', () => {
|
|
145
|
+
expect(check3OctetSubnet('192.168.1.*')).toEqual({
|
|
146
|
+
action: ACTION.NORMALIZE,
|
|
147
|
+
normalized: '||192.168.1.*',
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('normalizes 3-octet with single | and trailing dot', () => {
|
|
152
|
+
expect(check3OctetSubnet('|192.168.1.')).toEqual({
|
|
153
|
+
action: ACTION.NORMALIZE,
|
|
154
|
+
normalized: '||192.168.1.',
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('normalizes 3-octet with single | and trailing wildcard', () => {
|
|
159
|
+
expect(check3OctetSubnet('|192.168.1.*')).toEqual({
|
|
160
|
+
action: ACTION.NORMALIZE,
|
|
161
|
+
normalized: '||192.168.1.*',
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('allows 3-octet with || and trailing dot', () => {
|
|
166
|
+
expect(check3OctetSubnet('||192.168.1.')).toEqual({
|
|
167
|
+
action: ACTION.ALLOW,
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('allows 3-octet with || and trailing wildcard', () => {
|
|
172
|
+
expect(check3OctetSubnet('||192.168.1.*')).toEqual({
|
|
173
|
+
action: ACTION.ALLOW,
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('does not apply to 4-octet patterns', () => {
|
|
178
|
+
expect(check3OctetSubnet('1.2.3.4')).toBeNull();
|
|
179
|
+
expect(check3OctetSubnet('||1.2.3.4^')).toBeNull();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('does not apply to non-3-octet patterns', () => {
|
|
183
|
+
expect(check3OctetSubnet('192.168.')).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// processIpRule — only tests logic unique to this function:
|
|
188
|
+
// @@-prefix handling, $modifier extraction, comments/empty lines, domain passthrough.
|
|
189
|
+
// Pattern-level normalize/reject/allow decisions are covered by helper tests above.
|
|
190
|
+
describe('processIpRule', () => {
|
|
191
|
+
it('keeps comments and empty lines', () => {
|
|
192
|
+
expect(processIpRule('! comment')).toEqual({ action: ACTION.KEEP });
|
|
193
|
+
expect(processIpRule('# comment')).toEqual({ action: ACTION.KEEP });
|
|
194
|
+
expect(processIpRule('')).toEqual({ action: ACTION.KEEP });
|
|
195
|
+
expect(processIpRule(' ')).toEqual({ action: ACTION.KEEP });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('keeps domain rules unchanged', () => {
|
|
199
|
+
expect(processIpRule('||example.com^')).toEqual({ action: ACTION.KEEP });
|
|
200
|
+
expect(processIpRule('example.com')).toEqual({ action: ACTION.KEEP });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('preserves modifiers during normalization', () => {
|
|
204
|
+
expect(processIpRule('1.2.3.4$important')).toEqual({
|
|
205
|
+
action: ACTION.NORMALIZE,
|
|
206
|
+
normalized: '||1.2.3.4^$important',
|
|
207
|
+
});
|
|
208
|
+
expect(processIpRule('@@1.2.3.4$important')).toEqual({
|
|
209
|
+
action: ACTION.NORMALIZE,
|
|
210
|
+
normalized: '@@||1.2.3.4^$important',
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('normalizes single-pipe 3-octet subnet to ||', () => {
|
|
215
|
+
expect(processIpRule('|192.168.1.')).toEqual({
|
|
216
|
+
action: ACTION.NORMALIZE,
|
|
217
|
+
normalized: '||192.168.1.',
|
|
218
|
+
});
|
|
219
|
+
expect(processIpRule('|192.168.1.*')).toEqual({
|
|
220
|
+
action: ACTION.NORMALIZE,
|
|
221
|
+
normalized: '||192.168.1.*',
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('normalizes IP rules with ^|', () => {
|
|
226
|
+
expect(processIpRule('1.2.3.4^|')).toEqual({
|
|
227
|
+
action: ACTION.NORMALIZE,
|
|
228
|
+
normalized: '||1.2.3.4^',
|
|
229
|
+
});
|
|
230
|
+
expect(processIpRule('|1.2.3.4^|')).toEqual({
|
|
231
|
+
action: ACTION.NORMALIZE,
|
|
232
|
+
normalized: '||1.2.3.4^',
|
|
233
|
+
});
|
|
234
|
+
expect(processIpRule('||1.2.3.4^|')).toEqual({
|
|
235
|
+
action: ACTION.NORMALIZE,
|
|
236
|
+
normalized: '||1.2.3.4^',
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('normalizes exception rules (@@)', () => {
|
|
241
|
+
expect(processIpRule('@@1.2.3.4')).toEqual({
|
|
242
|
+
action: ACTION.NORMALIZE,
|
|
243
|
+
normalized: '@@||1.2.3.4^',
|
|
244
|
+
});
|
|
245
|
+
expect(processIpRule('@@|1.2.3.4^')).toEqual({
|
|
246
|
+
action: ACTION.NORMALIZE,
|
|
247
|
+
normalized: '@@||1.2.3.4^',
|
|
248
|
+
});
|
|
249
|
+
expect(processIpRule('@@||1.2.3.4')).toEqual({
|
|
250
|
+
action: ACTION.NORMALIZE,
|
|
251
|
+
normalized: '@@||1.2.3.4^',
|
|
252
|
+
});
|
|
253
|
+
expect(processIpRule('@@192.168.1.')).toEqual({
|
|
254
|
+
action: ACTION.NORMALIZE,
|
|
255
|
+
normalized: '@@||192.168.1.',
|
|
256
|
+
});
|
|
257
|
+
// already correct — keep as-is
|
|
258
|
+
expect(processIpRule('@@||1.2.3.4^')).toEqual({ action: ACTION.KEEP });
|
|
259
|
+
expect(processIpRule('@@||192.168.1.')).toEqual({ action: ACTION.KEEP });
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('does not reject invalid @@-patterns — normalization-only, validateAllowIp will reject', () => {
|
|
263
|
+
// processIpRule cannot normalize these, so it keeps them unchanged.
|
|
264
|
+
// Rejection is the sole responsibility of the validator (validate.js),
|
|
265
|
+
// not of the normalizer — that eliminates double-rejection.
|
|
266
|
+
expect(processIpRule('@@192.168.1').action).toBe(ACTION.KEEP);
|
|
267
|
+
expect(processIpRule('@@||192.168.1^').action).toBe(ACTION.KEEP);
|
|
268
|
+
expect(processIpRule('@@1.2.').action).toBe(ACTION.KEEP);
|
|
269
|
+
expect(processIpRule('@@||10^').action).toBe(ACTION.KEEP);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// normalizeIpRules — one integration test with a mixed list covering
|
|
274
|
+
// normalize, reject, keep, @@, and modifiers in a single scenario.
|
|
275
|
+
describe('normalizeIpRules', () => {
|
|
276
|
+
it('processes a mixed list of rules', () => {
|
|
277
|
+
const rules = [
|
|
278
|
+
// keep
|
|
279
|
+
'! comment',
|
|
280
|
+
'||5.6.7.8^',
|
|
281
|
+
'||example.com^',
|
|
282
|
+
// normalize
|
|
283
|
+
'1.2.3.4',
|
|
284
|
+
'1.2.3.4^|',
|
|
285
|
+
'192.168.1.',
|
|
286
|
+
'1.2.3.4$important',
|
|
287
|
+
'5.6.7.8$client=192.168.1.1',
|
|
288
|
+
'|1.2.3.4^|',
|
|
289
|
+
// normalize @@
|
|
290
|
+
'@@1.2.3.4',
|
|
291
|
+
'@@1.2.3.4^|',
|
|
292
|
+
'@@|5.6.7.8^',
|
|
293
|
+
'@@192.168.1.',
|
|
294
|
+
// normalizeIpRules cannot normalize these — passes them through unchanged.
|
|
295
|
+
// validateAllowIp's validator is the single place responsible for rejecting
|
|
296
|
+
// invalid patterns (separation of concerns, no double-rejection).
|
|
297
|
+
'192.168.1',
|
|
298
|
+
'||1.2.3.4^',
|
|
299
|
+
'||192.168^',
|
|
300
|
+
'1.2.',
|
|
301
|
+
'@@192.168.1',
|
|
302
|
+
'@@1.2.',
|
|
303
|
+
'@@||10.0.1^',
|
|
304
|
+
];
|
|
305
|
+
const result = normalizeIpRules(rules);
|
|
306
|
+
expect(result).toEqual([
|
|
307
|
+
'! comment',
|
|
308
|
+
'||5.6.7.8^',
|
|
309
|
+
'||example.com^',
|
|
310
|
+
'||1.2.3.4^',
|
|
311
|
+
'||1.2.3.4^',
|
|
312
|
+
'||192.168.1.',
|
|
313
|
+
'||1.2.3.4^$important',
|
|
314
|
+
'||5.6.7.8^$client=192.168.1.1',
|
|
315
|
+
'||1.2.3.4^',
|
|
316
|
+
'@@||1.2.3.4^',
|
|
317
|
+
'@@||1.2.3.4^',
|
|
318
|
+
'@@||5.6.7.8^',
|
|
319
|
+
'@@||192.168.1.',
|
|
320
|
+
// normalizer passes invalid patterns through; validator rejects them
|
|
321
|
+
'192.168.1',
|
|
322
|
+
'||1.2.3.4^',
|
|
323
|
+
'||192.168^',
|
|
324
|
+
'1.2.',
|
|
325
|
+
'@@192.168.1',
|
|
326
|
+
'@@1.2.',
|
|
327
|
+
'@@||10.0.1^',
|
|
328
|
+
]);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
});
|
|
@@ -56,6 +56,31 @@ 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
|
+
['Validate + ValidateAllowIpAndPublicSuffix', ['Validate', 'ValidateAllowIpAndPublicSuffix']],
|
|
63
|
+
['ValidateAllowIp + ValidateAllowPublicSuffix', ['ValidateAllowIp', 'ValidateAllowPublicSuffix']],
|
|
64
|
+
['ValidateAllowIp + ValidateAllowIpAndPublicSuffix', ['ValidateAllowIp', 'ValidateAllowIpAndPublicSuffix']],
|
|
65
|
+
[
|
|
66
|
+
'ValidateAllowPublicSuffix + ValidateAllowIpAndPublicSuffix',
|
|
67
|
+
['ValidateAllowPublicSuffix', 'ValidateAllowIpAndPublicSuffix'],
|
|
68
|
+
],
|
|
69
|
+
[
|
|
70
|
+
'Validate + ValidateAllowIp + ValidateAllowPublicSuffix',
|
|
71
|
+
['Validate', 'ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
72
|
+
],
|
|
73
|
+
])('throws when incompatible validation transformations are combined: %s', async (_name, transformations) => {
|
|
74
|
+
const rules = [
|
|
75
|
+
'||185.149.120.173^',
|
|
76
|
+
'||hl.cn^',
|
|
77
|
+
'||example.com^',
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
await expect(transform(rules, {}, transformations))
|
|
81
|
+
.rejects.toThrow(`Validation transformations cannot be combined: ${transformations.join(', ')}.`);
|
|
82
|
+
});
|
|
83
|
+
|
|
59
84
|
it('simple transformations with removeModifiers', async () => {
|
|
60
85
|
const rules = `! test comment
|
|
61
86
|
rule1
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
const { validateAllowIpAndPublicSuffix } = require('../../src/transformations/validate-allow-ip-and-public-suffix');
|
|
2
|
+
|
|
3
|
+
describe('ValidateAllowIpAndPublicSuffix', () => {
|
|
4
|
+
it('keeps valid domain rules', () => {
|
|
5
|
+
const rules = [
|
|
6
|
+
'||example.org^',
|
|
7
|
+
'||example.com^',
|
|
8
|
+
];
|
|
9
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
10
|
+
|
|
11
|
+
expect(filtered).toEqual([
|
|
12
|
+
'||example.org^',
|
|
13
|
+
'||example.com^',
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('keeps IP address rules (like ValidateAllowIp)', () => {
|
|
18
|
+
const rules = [
|
|
19
|
+
'||185.149.120.173^',
|
|
20
|
+
'0.0.0.0 example.org',
|
|
21
|
+
];
|
|
22
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
23
|
+
|
|
24
|
+
expect(filtered).toEqual([
|
|
25
|
+
'||185.149.120.173^',
|
|
26
|
+
'0.0.0.0 example.org',
|
|
27
|
+
]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('keeps public suffix rules (like ValidateAllowPublicSuffix)', () => {
|
|
31
|
+
const rules = [
|
|
32
|
+
'||hl.cn^',
|
|
33
|
+
'||org^',
|
|
34
|
+
'||*.org^',
|
|
35
|
+
'.org^',
|
|
36
|
+
];
|
|
37
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
38
|
+
|
|
39
|
+
expect(filtered).toEqual([
|
|
40
|
+
'||hl.cn^',
|
|
41
|
+
'||org^',
|
|
42
|
+
'||*.org^',
|
|
43
|
+
'.org^',
|
|
44
|
+
]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('keeps both IP and public suffix rules together', () => {
|
|
48
|
+
const rules = [
|
|
49
|
+
'||185.149.120.173^',
|
|
50
|
+
'||org^',
|
|
51
|
+
'||example.org^',
|
|
52
|
+
'||hl.cn^',
|
|
53
|
+
'0.0.0.0 example.com',
|
|
54
|
+
];
|
|
55
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
56
|
+
|
|
57
|
+
expect(filtered).toEqual([
|
|
58
|
+
'||185.149.120.173^',
|
|
59
|
+
'||org^',
|
|
60
|
+
'||example.org^',
|
|
61
|
+
'||hl.cn^',
|
|
62
|
+
'0.0.0.0 example.com',
|
|
63
|
+
]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('normalizes IP patterns (like ValidateAllowIp)', () => {
|
|
67
|
+
const rules = [
|
|
68
|
+
'1.1.1.1^',
|
|
69
|
+
'192.168.1.1^',
|
|
70
|
+
'|1.1.1.1^',
|
|
71
|
+
'||192.168.1.',
|
|
72
|
+
'||192.168.1.*',
|
|
73
|
+
'||example.org^',
|
|
74
|
+
];
|
|
75
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
76
|
+
|
|
77
|
+
expect(filtered).toEqual([
|
|
78
|
+
'||1.1.1.1^',
|
|
79
|
+
'||192.168.1.1^',
|
|
80
|
+
'||1.1.1.1^',
|
|
81
|
+
'||192.168.1.',
|
|
82
|
+
'||192.168.1.*',
|
|
83
|
+
'||example.org^',
|
|
84
|
+
]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('rejects invalid rules', () => {
|
|
88
|
+
const rules = [
|
|
89
|
+
'||invalid/rule',
|
|
90
|
+
'||example.org^$domain=example.com',
|
|
91
|
+
'||example.org^',
|
|
92
|
+
];
|
|
93
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
94
|
+
|
|
95
|
+
expect(filtered).toEqual([
|
|
96
|
+
'||example.org^',
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('rejects invalid IP patterns', () => {
|
|
101
|
+
const rules = [
|
|
102
|
+
// 3-octet without trailing dot — rejected (ambiguous)
|
|
103
|
+
'192.168.1',
|
|
104
|
+
'||192.168.1',
|
|
105
|
+
// 3-octet with ^ — rejected (doesn't work)
|
|
106
|
+
'||192.168.1^',
|
|
107
|
+
// 2-octet — rejected (too wide)
|
|
108
|
+
'||1.1.',
|
|
109
|
+
'1.1.',
|
|
110
|
+
// valid entries — kept
|
|
111
|
+
'||example.org^',
|
|
112
|
+
'||1.2.3.4^',
|
|
113
|
+
];
|
|
114
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
115
|
+
|
|
116
|
+
expect(filtered).toEqual([
|
|
117
|
+
'||example.org^',
|
|
118
|
+
'||1.2.3.4^',
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('rejects terminated IDN TLDs (like ValidateAllowPublicSuffix)', () => {
|
|
123
|
+
const rules = [
|
|
124
|
+
'||*.xn--jlq61u9w7b^',
|
|
125
|
+
'||xn--jlq61u9w7b^',
|
|
126
|
+
'.xn--p1acf^',
|
|
127
|
+
'||*.xn--p1acf^',
|
|
128
|
+
];
|
|
129
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
130
|
+
|
|
131
|
+
expect(filtered).toEqual([
|
|
132
|
+
'.xn--p1acf^',
|
|
133
|
+
'||*.xn--p1acf^',
|
|
134
|
+
]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('keeps known ICANN TLDs but rejects unknown ones', () => {
|
|
138
|
+
const rules = [
|
|
139
|
+
'||aaa^',
|
|
140
|
+
'||aaaa^',
|
|
141
|
+
'||foo^',
|
|
142
|
+
'||zzzz^',
|
|
143
|
+
'||example.org^',
|
|
144
|
+
];
|
|
145
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
146
|
+
|
|
147
|
+
expect(filtered).toEqual([
|
|
148
|
+
'||aaa^',
|
|
149
|
+
'||foo^',
|
|
150
|
+
'||example.org^',
|
|
151
|
+
]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('removes preceding comments for invalid rules', () => {
|
|
155
|
+
const rules = `! rule comment
|
|
156
|
+
|
|
157
|
+
||invalid/rule
|
|
158
|
+
! comment
|
|
159
|
+
||valid.com^`.split(/\r?\n/);
|
|
160
|
+
const filtered = validateAllowIpAndPublicSuffix(rules);
|
|
161
|
+
|
|
162
|
+
expect(filtered).toEqual([
|
|
163
|
+
'! comment',
|
|
164
|
+
'||valid.com^',
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
});
|