@adguard/hostlist-compiler 2.0.0 → 2.1.1
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/.dockerignore +29 -0
- package/.github/workflows/ci.yml +22 -0
- package/.github/workflows/mirror.yml +24 -0
- package/.github/workflows/prepare-release.yml +26 -0
- package/.github/workflows/publish-release.yml +44 -0
- package/AGENTS.md +15 -16
- package/CHANGELOG.md +44 -0
- package/DEVELOPMENT.md +16 -17
- package/README.md +42 -3
- package/package.json +10 -11
- package/src/cli.js +6 -2
- package/src/index.d.ts +1 -0
- package/src/index.js +6 -1
- package/src/schemas/configuration.schema.json +98 -0
- package/src/transformations/ip-normalize.js +182 -0
- package/src/transformations/transform.js +6 -4
- package/src/transformations/validate-allow-ip-and-public-suffix.js +33 -0
- package/src/transformations/validate-allow-ip.js +16 -3
- package/src/transformations/validate.js +118 -53
- package/src/utils.js +137 -0
- package/test/configuration.test.js +6 -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 +6 -0
- package/test/transformations/validate-allow-ip-and-public-suffix.test.js +167 -0
- package/test/transformations/validate-allow-ip.test.js +46 -18
- package/test/transformations/validate-allow-public-suffix.test.js +20 -0
- package/test/transformations/validate.test.js +57 -0
- package/bamboo-specs/bamboo.yaml +0 -14
- package/bamboo-specs/build.yaml +0 -92
- package/bamboo-specs/deploy.yaml +0 -38
- package/bamboo-specs/increment.yaml +0 -54
- package/bamboo-specs/permissions.yaml +0 -16
- package/bamboo-specs/test.yaml +0 -56
- package/tools/build-txt.js +0 -19
package/src/utils.js
CHANGED
|
@@ -1,5 +1,105 @@
|
|
|
1
1
|
const _ = require('lodash');
|
|
2
2
|
|
|
3
|
+
// Prefix for canonical IP/domain patterns
|
|
4
|
+
const IP_PREFIX = '||';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Regex that matches strings that look like adblock rule modifiers
|
|
8
|
+
* Used to distinguish a real `$modifier` suffix from a `$` that is
|
|
9
|
+
* part of a regex anchor.
|
|
10
|
+
*/
|
|
11
|
+
const MODIFIER_REGEX = /^[a-zA-Z0-9,=~_.-]+$/;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Regex to match a valid IPv4 octet (0-255).
|
|
15
|
+
*/
|
|
16
|
+
const OCTET_REGEX = /^\d{1,3}$/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Checks if a string is a valid IPv4 octet (0-255).
|
|
20
|
+
*
|
|
21
|
+
* @param {string} s - The string to check.
|
|
22
|
+
* @returns {boolean} True if valid octet.
|
|
23
|
+
*/
|
|
24
|
+
function isValidOctet(s) {
|
|
25
|
+
if (!OCTET_REGEX.test(s)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
const num = Number(s);
|
|
29
|
+
return num >= 0 && num <= 255;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Parses an IP-like adblock pattern and extracts its structural components.
|
|
34
|
+
* Expects a pattern already stripped of @@ prefix and $modifier suffix
|
|
35
|
+
* (as returned by parseRuleTokens in rule.js).
|
|
36
|
+
*
|
|
37
|
+
* @param {string} pattern - The adblock pattern.
|
|
38
|
+
* @returns {{ prefix: string, octets: string[],
|
|
39
|
+
* hasTrailingDot: boolean, hasTrailingWildcard: boolean,
|
|
40
|
+
* hasCaret: boolean, hasCaretPipe: boolean }|null}
|
|
41
|
+
* Parsed components, or null if the pattern is not a valid IP pattern.
|
|
42
|
+
*/
|
|
43
|
+
function parseIpPattern(pattern) {
|
|
44
|
+
let remaining = pattern;
|
|
45
|
+
let prefix = '';
|
|
46
|
+
|
|
47
|
+
// Extract prefix
|
|
48
|
+
if (remaining.startsWith(IP_PREFIX)) {
|
|
49
|
+
prefix = IP_PREFIX;
|
|
50
|
+
remaining = remaining.slice(2);
|
|
51
|
+
} else if (remaining.startsWith('|')) {
|
|
52
|
+
prefix = '|';
|
|
53
|
+
remaining = remaining.slice(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Check for ^| or ^ suffix
|
|
57
|
+
let hasCaret = false;
|
|
58
|
+
let hasCaretPipe = false;
|
|
59
|
+
if (remaining.endsWith('^|')) {
|
|
60
|
+
hasCaretPipe = true;
|
|
61
|
+
hasCaret = true;
|
|
62
|
+
remaining = remaining.slice(0, -2);
|
|
63
|
+
} else if (remaining.endsWith('^')) {
|
|
64
|
+
hasCaret = true;
|
|
65
|
+
remaining = remaining.slice(0, -1);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Check for trailing wildcard (.*)
|
|
69
|
+
let hasTrailingWildcard = false;
|
|
70
|
+
if (remaining.endsWith('.*')) {
|
|
71
|
+
hasTrailingWildcard = true;
|
|
72
|
+
remaining = remaining.slice(0, -2);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Check for trailing dot
|
|
76
|
+
let hasTrailingDot = false;
|
|
77
|
+
if (remaining.endsWith('.')) {
|
|
78
|
+
hasTrailingDot = true;
|
|
79
|
+
remaining = remaining.slice(0, -1);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Split into octets
|
|
83
|
+
const parts = remaining.split('.');
|
|
84
|
+
if (parts.length === 0 || parts.length > 4) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// All parts must be valid octets
|
|
89
|
+
if (!parts.every(isValidOctet)) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
prefix,
|
|
95
|
+
octets: parts,
|
|
96
|
+
hasTrailingDot,
|
|
97
|
+
hasTrailingWildcard,
|
|
98
|
+
hasCaret,
|
|
99
|
+
hasCaretPipe,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
3
103
|
/**
|
|
4
104
|
* Extracts a substring between two tags.
|
|
5
105
|
*
|
|
@@ -136,7 +236,44 @@ class Wildcard {
|
|
|
136
236
|
}
|
|
137
237
|
}
|
|
138
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Classifies an IP-like adblock pattern into categories used by validators and normalizers.
|
|
241
|
+
* Returns null when the pattern cannot be parsed as an IP-like pattern.
|
|
242
|
+
*
|
|
243
|
+
* @param {string} pattern - The adblock pattern to classify.
|
|
244
|
+
* @returns {{ prefix: string, octets: string[],
|
|
245
|
+
* hasTrailingDot: boolean, hasTrailingWildcard: boolean,
|
|
246
|
+
* hasCaret: boolean, hasCaretPipe: boolean,
|
|
247
|
+
* octetCount: number, isFullIp: boolean,
|
|
248
|
+
* isSubnetWildcard: boolean, isAmbiguous3Octet: boolean,
|
|
249
|
+
* isTooWide: boolean }|null}
|
|
250
|
+
*/
|
|
251
|
+
function classifyIpPattern(pattern) {
|
|
252
|
+
const parsed = parseIpPattern(pattern);
|
|
253
|
+
if (!parsed) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
...parsed,
|
|
259
|
+
octetCount: parsed.octets.length,
|
|
260
|
+
isFullIp: parsed.octets.length === 4
|
|
261
|
+
&& !parsed.hasTrailingDot
|
|
262
|
+
&& !parsed.hasTrailingWildcard,
|
|
263
|
+
isSubnetWildcard: parsed.octets.length < 4
|
|
264
|
+
&& (parsed.hasTrailingDot || parsed.hasTrailingWildcard),
|
|
265
|
+
isAmbiguous3Octet: parsed.octets.length === 3
|
|
266
|
+
&& !parsed.hasTrailingDot
|
|
267
|
+
&& !parsed.hasTrailingWildcard
|
|
268
|
+
&& !parsed.hasCaret,
|
|
269
|
+
isTooWide: parsed.octets.length <= 2,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
139
273
|
module.exports = {
|
|
274
|
+
MODIFIER_REGEX,
|
|
275
|
+
parseIpPattern,
|
|
276
|
+
classifyIpPattern,
|
|
140
277
|
Wildcard,
|
|
141
278
|
splitByDelimiterWithEscapeCharacter,
|
|
142
279
|
substringBetween,
|
|
@@ -95,7 +95,10 @@ describe('Configuration', () => {
|
|
|
95
95
|
it.each([
|
|
96
96
|
['Validate', 'ValidateAllowIp'],
|
|
97
97
|
['Validate', 'ValidateAllowPublicSuffix'],
|
|
98
|
+
['Validate', 'ValidateAllowIpAndPublicSuffix'],
|
|
98
99
|
['ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
100
|
+
['ValidateAllowIp', 'ValidateAllowIpAndPublicSuffix'],
|
|
101
|
+
['ValidateAllowPublicSuffix', 'ValidateAllowIpAndPublicSuffix'],
|
|
99
102
|
])('test incompatible top-level transformations configuration: %s + %s', (first, second) => {
|
|
100
103
|
const ret = config.validateConfiguration({
|
|
101
104
|
name: 'test',
|
|
@@ -117,7 +120,10 @@ describe('Configuration', () => {
|
|
|
117
120
|
it.each([
|
|
118
121
|
['Validate', 'ValidateAllowIp'],
|
|
119
122
|
['Validate', 'ValidateAllowPublicSuffix'],
|
|
123
|
+
['Validate', 'ValidateAllowIpAndPublicSuffix'],
|
|
120
124
|
['ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|
|
125
|
+
['ValidateAllowIp', 'ValidateAllowIpAndPublicSuffix'],
|
|
126
|
+
['ValidateAllowPublicSuffix', 'ValidateAllowIpAndPublicSuffix'],
|
|
121
127
|
])('test incompatible source transformations configuration: %s + %s', (first, second) => {
|
|
122
128
|
const ret = config.validateConfiguration({
|
|
123
129
|
name: 'test',
|
|
@@ -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
|
+
});
|
|
@@ -59,7 +59,13 @@ dupl1
|
|
|
59
59
|
it.each([
|
|
60
60
|
['Validate + ValidateAllowIp', ['Validate', 'ValidateAllowIp']],
|
|
61
61
|
['Validate + ValidateAllowPublicSuffix', ['Validate', 'ValidateAllowPublicSuffix']],
|
|
62
|
+
['Validate + ValidateAllowIpAndPublicSuffix', ['Validate', 'ValidateAllowIpAndPublicSuffix']],
|
|
62
63
|
['ValidateAllowIp + ValidateAllowPublicSuffix', ['ValidateAllowIp', 'ValidateAllowPublicSuffix']],
|
|
64
|
+
['ValidateAllowIp + ValidateAllowIpAndPublicSuffix', ['ValidateAllowIp', 'ValidateAllowIpAndPublicSuffix']],
|
|
65
|
+
[
|
|
66
|
+
'ValidateAllowPublicSuffix + ValidateAllowIpAndPublicSuffix',
|
|
67
|
+
['ValidateAllowPublicSuffix', 'ValidateAllowIpAndPublicSuffix'],
|
|
68
|
+
],
|
|
63
69
|
[
|
|
64
70
|
'Validate + ValidateAllowIp + ValidateAllowPublicSuffix',
|
|
65
71
|
['Validate', 'ValidateAllowIp', 'ValidateAllowPublicSuffix'],
|