@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
|
@@ -37,6 +37,104 @@
|
|
|
37
37
|
"transformations": {
|
|
38
38
|
"description": "A list of the transformations that will be applied",
|
|
39
39
|
"type": "array",
|
|
40
|
+
"allOf": [
|
|
41
|
+
{
|
|
42
|
+
"not": {
|
|
43
|
+
"allOf": [
|
|
44
|
+
{
|
|
45
|
+
"contains": {
|
|
46
|
+
"const": "Validate"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"contains": {
|
|
51
|
+
"const": "ValidateAllowIp"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"not": {
|
|
59
|
+
"allOf": [
|
|
60
|
+
{
|
|
61
|
+
"contains": {
|
|
62
|
+
"const": "Validate"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"contains": {
|
|
67
|
+
"const": "ValidateAllowPublicSuffix"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"not": {
|
|
75
|
+
"allOf": [
|
|
76
|
+
{
|
|
77
|
+
"contains": {
|
|
78
|
+
"const": "ValidateAllowIp"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"contains": {
|
|
83
|
+
"const": "ValidateAllowPublicSuffix"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"not": {
|
|
91
|
+
"allOf": [
|
|
92
|
+
{
|
|
93
|
+
"contains": {
|
|
94
|
+
"const": "Validate"
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"contains": {
|
|
99
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"not": {
|
|
107
|
+
"allOf": [
|
|
108
|
+
{
|
|
109
|
+
"contains": {
|
|
110
|
+
"const": "ValidateAllowIp"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"contains": {
|
|
115
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"not": {
|
|
123
|
+
"allOf": [
|
|
124
|
+
{
|
|
125
|
+
"contains": {
|
|
126
|
+
"const": "ValidateAllowPublicSuffix"
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"contains": {
|
|
131
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
],
|
|
40
138
|
"items": {
|
|
41
139
|
"type": "string",
|
|
42
140
|
"enum": [
|
|
@@ -45,6 +143,8 @@
|
|
|
45
143
|
"Compress",
|
|
46
144
|
"Validate",
|
|
47
145
|
"ValidateAllowIp",
|
|
146
|
+
"ValidateAllowPublicSuffix",
|
|
147
|
+
"ValidateAllowIpAndPublicSuffix",
|
|
48
148
|
"Deduplicate",
|
|
49
149
|
"InvertAllow",
|
|
50
150
|
"RemoveEmptyLines",
|
|
@@ -114,6 +214,104 @@
|
|
|
114
214
|
"transformations": {
|
|
115
215
|
"description": "A list of the transformations that will be applied",
|
|
116
216
|
"type": "array",
|
|
217
|
+
"allOf": [
|
|
218
|
+
{
|
|
219
|
+
"not": {
|
|
220
|
+
"allOf": [
|
|
221
|
+
{
|
|
222
|
+
"contains": {
|
|
223
|
+
"const": "Validate"
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
"contains": {
|
|
228
|
+
"const": "ValidateAllowIp"
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"not": {
|
|
236
|
+
"allOf": [
|
|
237
|
+
{
|
|
238
|
+
"contains": {
|
|
239
|
+
"const": "Validate"
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
"contains": {
|
|
244
|
+
"const": "ValidateAllowPublicSuffix"
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
]
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
"not": {
|
|
252
|
+
"allOf": [
|
|
253
|
+
{
|
|
254
|
+
"contains": {
|
|
255
|
+
"const": "ValidateAllowIp"
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
"contains": {
|
|
260
|
+
"const": "ValidateAllowPublicSuffix"
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
]
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
"not": {
|
|
268
|
+
"allOf": [
|
|
269
|
+
{
|
|
270
|
+
"contains": {
|
|
271
|
+
"const": "Validate"
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
"contains": {
|
|
276
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
]
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
"not": {
|
|
284
|
+
"allOf": [
|
|
285
|
+
{
|
|
286
|
+
"contains": {
|
|
287
|
+
"const": "ValidateAllowIp"
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
"contains": {
|
|
292
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
]
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
"not": {
|
|
300
|
+
"allOf": [
|
|
301
|
+
{
|
|
302
|
+
"contains": {
|
|
303
|
+
"const": "ValidateAllowPublicSuffix"
|
|
304
|
+
}
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
"contains": {
|
|
308
|
+
"const": "ValidateAllowIpAndPublicSuffix"
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
],
|
|
117
315
|
"items": {
|
|
118
316
|
"type": "string",
|
|
119
317
|
"enum": [
|
|
@@ -122,6 +320,8 @@
|
|
|
122
320
|
"Compress",
|
|
123
321
|
"Validate",
|
|
124
322
|
"ValidateAllowIp",
|
|
323
|
+
"ValidateAllowPublicSuffix",
|
|
324
|
+
"ValidateAllowIpAndPublicSuffix",
|
|
125
325
|
"Deduplicate",
|
|
126
326
|
"InvertAllow",
|
|
127
327
|
"RemoveEmptyLines",
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
const consola = require('consola');
|
|
2
|
+
const ruleUtils = require('../rule');
|
|
3
|
+
const { classifyIpPattern, parseIpPattern } = require('../utils');
|
|
4
|
+
|
|
5
|
+
// Prefix for canonical IP/domain patterns
|
|
6
|
+
const IP_PREFIX = '||';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Action constants for IP rule processing results.
|
|
10
|
+
* Used by check helpers, processIpRule, and normalizeIpRules.
|
|
11
|
+
*/
|
|
12
|
+
const ACTION = Object.freeze({
|
|
13
|
+
/**
|
|
14
|
+
* Pattern is valid and already in canonical form — no change needed.
|
|
15
|
+
*/
|
|
16
|
+
KEEP: 'keep',
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Pattern needs rewriting to canonical form.
|
|
20
|
+
*/
|
|
21
|
+
NORMALIZE: 'normalize',
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Pattern is a valid subnet already in canonical form (||prefix present).
|
|
25
|
+
*/
|
|
26
|
+
ALLOW: 'allow',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Determines if a pattern is a full 4-octet IP address that needs normalization.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} pattern - The adblock pattern.
|
|
33
|
+
* @returns {string|null} The normalized pattern (||ip^) or null if not applicable.
|
|
34
|
+
*/
|
|
35
|
+
function normalizeFullIp(pattern) {
|
|
36
|
+
const c = classifyIpPattern(pattern);
|
|
37
|
+
if (!c || !c.isFullIp) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const ip = c.octets.join('.');
|
|
42
|
+
|
|
43
|
+
// Already in correct format ||ip^ (||ip^| is not a non-canonical variant)
|
|
44
|
+
if (c.prefix === IP_PREFIX && c.hasCaret && !c.hasCaretPipe) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Normalize to ||ip^
|
|
49
|
+
return `||${ip}^`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Determines if a pattern is a 3-octet subnet that should be allowed.
|
|
54
|
+
*
|
|
55
|
+
* Valid 3-octet patterns:
|
|
56
|
+
* - 192.168.1. (trailing dot)
|
|
57
|
+
* - 192.168.1.* (trailing wildcard)
|
|
58
|
+
* - ||192.168.1. (with prefix)
|
|
59
|
+
* - ||192.168.1.* (with prefix)
|
|
60
|
+
*
|
|
61
|
+
* Invalid 3-octet patterns (returned as null — caller treats as pass-through):
|
|
62
|
+
* - 192.168.1 (no trailing dot/wildcard - ambiguous)
|
|
63
|
+
* - ||192.168.1^ (with caret - doesn't work in AdGuard Home)
|
|
64
|
+
*
|
|
65
|
+
* @param {string} pattern - The adblock pattern.
|
|
66
|
+
* @returns {object|null} Result with action and normalized pattern, or null if not applicable.
|
|
67
|
+
* - action: ACTION.ALLOW, ACTION.NORMALIZE
|
|
68
|
+
* - normalized: the normalized pattern (if action is ACTION.NORMALIZE)
|
|
69
|
+
* - null means the pattern is not a 3-octet IP subnet, or is an invalid 3-octet pattern;
|
|
70
|
+
* caller should pass it through unchanged.
|
|
71
|
+
*/
|
|
72
|
+
function check3OctetSubnet(pattern) {
|
|
73
|
+
const c = classifyIpPattern(pattern);
|
|
74
|
+
if (!c || c.octetCount !== 3) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const ip = c.octets.join('.');
|
|
79
|
+
|
|
80
|
+
// 3 octets with ^ - doesn't work in AdGuard Home; pass through for validator
|
|
81
|
+
if (c.hasCaret) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 3 octets without trailing dot/wildcard - ambiguous; pass through for validator
|
|
86
|
+
if (!c.hasTrailingDot && !c.hasTrailingWildcard) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Valid 3-octet subnet pattern - normalize to add || if missing
|
|
91
|
+
if (c.prefix === IP_PREFIX) {
|
|
92
|
+
return {
|
|
93
|
+
action: ACTION.ALLOW,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Normalize: add || prefix
|
|
98
|
+
const suffix = c.hasTrailingWildcard ? '.*' : '.';
|
|
99
|
+
return {
|
|
100
|
+
action: ACTION.NORMALIZE,
|
|
101
|
+
normalized: `||${ip}${suffix}`,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Normalizes an IP rule if needed, or keeps it unchanged.
|
|
107
|
+
* Invalid patterns are passed through for the validator to reject.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} ruleText - The full rule text (may include modifiers and @@ prefix).
|
|
110
|
+
* @returns {object} Result object.
|
|
111
|
+
* - action: ACTION.KEEP (no change), ACTION.NORMALIZE
|
|
112
|
+
* - normalized: the normalized rule (if action is ACTION.NORMALIZE)
|
|
113
|
+
*/
|
|
114
|
+
function processIpRule(ruleText) {
|
|
115
|
+
// Skip comments and empty lines
|
|
116
|
+
if (!ruleText || ruleText.startsWith('!') || ruleText.startsWith('#') || ruleText.trim() === '') {
|
|
117
|
+
return { action: ACTION.KEEP };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const ruleProps = ruleUtils.loadAdblockRuleProperties(ruleText);
|
|
121
|
+
const { pattern } = ruleProps;
|
|
122
|
+
|
|
123
|
+
// Check for 3-octet subnet patterns (normalize or keep)
|
|
124
|
+
const subnet = check3OctetSubnet(pattern);
|
|
125
|
+
if (subnet?.action === ACTION.NORMALIZE) {
|
|
126
|
+
ruleProps.pattern = subnet.normalized;
|
|
127
|
+
return {
|
|
128
|
+
action: ACTION.NORMALIZE,
|
|
129
|
+
normalized: ruleUtils.adblockRuleToString(ruleProps),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (subnet?.action === ACTION.ALLOW) {
|
|
133
|
+
// Already in canonical form — no change needed
|
|
134
|
+
return { action: ACTION.KEEP };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Check for full 4-octet IP normalization
|
|
138
|
+
const normalized = normalizeFullIp(pattern);
|
|
139
|
+
if (normalized) {
|
|
140
|
+
ruleProps.pattern = normalized;
|
|
141
|
+
return {
|
|
142
|
+
action: ACTION.NORMALIZE,
|
|
143
|
+
normalized: ruleUtils.adblockRuleToString(ruleProps),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { action: ACTION.KEEP };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Processes a list of rules, normalizing IP rules and passing invalid ones through unchanged.
|
|
152
|
+
*
|
|
153
|
+
* @param {string[]} rules - Array of rules.
|
|
154
|
+
* @returns {string[]} Processed rules with normalization applied.
|
|
155
|
+
*/
|
|
156
|
+
function normalizeIpRules(rules) {
|
|
157
|
+
const result = [];
|
|
158
|
+
|
|
159
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
160
|
+
for (const rule of rules) {
|
|
161
|
+
const processed = processIpRule(rule);
|
|
162
|
+
|
|
163
|
+
if (processed.action === ACTION.NORMALIZE) {
|
|
164
|
+
consola.info(`Normalized IP rule: ${rule} → ${processed.normalized}`);
|
|
165
|
+
result.push(processed.normalized);
|
|
166
|
+
} else {
|
|
167
|
+
// KEEP — pass through unchanged; validator will reject invalid patterns
|
|
168
|
+
result.push(rule);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
module.exports = {
|
|
176
|
+
ACTION,
|
|
177
|
+
parseIpPattern,
|
|
178
|
+
normalizeFullIp,
|
|
179
|
+
check3OctetSubnet,
|
|
180
|
+
processIpRule,
|
|
181
|
+
normalizeIpRules,
|
|
182
|
+
};
|
|
@@ -2,6 +2,8 @@ const removeComments = require('./remove-comments');
|
|
|
2
2
|
const removeModifiers = require('./remove-modifiers');
|
|
3
3
|
const { validate } = require('./validate');
|
|
4
4
|
const { validateAllowIp } = require('./validate-allow-ip');
|
|
5
|
+
const { validateAllowPublicSuffix } = require('./validate-allow-public-suffix');
|
|
6
|
+
const { validateAllowIpAndPublicSuffix } = require('./validate-allow-ip-and-public-suffix');
|
|
5
7
|
const exclude = require('./exclude');
|
|
6
8
|
const include = require('./include');
|
|
7
9
|
const deduplicate = require('./deduplicate');
|
|
@@ -21,6 +23,8 @@ const TRANSFORMATIONS = Object.freeze({
|
|
|
21
23
|
RemoveModifiers: 'RemoveModifiers',
|
|
22
24
|
Validate: 'Validate',
|
|
23
25
|
ValidateAllowIp: 'ValidateAllowIp',
|
|
26
|
+
ValidateAllowPublicSuffix: 'ValidateAllowPublicSuffix',
|
|
27
|
+
ValidateAllowIpAndPublicSuffix: 'ValidateAllowIpAndPublicSuffix',
|
|
24
28
|
Deduplicate: 'Deduplicate',
|
|
25
29
|
InvertAllow: 'InvertAllow',
|
|
26
30
|
RemoveEmptyLines: 'RemoveEmptyLines',
|
|
@@ -29,6 +33,29 @@ const TRANSFORMATIONS = Object.freeze({
|
|
|
29
33
|
ConvertToAscii: 'ConvertToAscii',
|
|
30
34
|
});
|
|
31
35
|
|
|
36
|
+
const VALIDATION_TRANSFORMATIONS = [
|
|
37
|
+
TRANSFORMATIONS.Validate,
|
|
38
|
+
TRANSFORMATIONS.ValidateAllowIp,
|
|
39
|
+
TRANSFORMATIONS.ValidateAllowPublicSuffix,
|
|
40
|
+
TRANSFORMATIONS.ValidateAllowIpAndPublicSuffix,
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Throws when multiple validation transformations are selected together.
|
|
45
|
+
* Combining them causes silent data loss: each validator runs on the already-filtered
|
|
46
|
+
* output of the previous one, so allow-modes (AllowIp, AllowPublicSuffix) become ineffective.
|
|
47
|
+
*
|
|
48
|
+
* @param {Array<string>} transformations - configured transformations.
|
|
49
|
+
*/
|
|
50
|
+
function checkIncompatibleValidationTransformations(transformations) {
|
|
51
|
+
const selectedValidations = VALIDATION_TRANSFORMATIONS
|
|
52
|
+
.filter((transformation) => transformations.indexOf(transformation) !== -1);
|
|
53
|
+
|
|
54
|
+
if (selectedValidations.length > 1) {
|
|
55
|
+
throw new Error(`Validation transformations cannot be combined: ${selectedValidations.join(', ')}.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
32
59
|
/**
|
|
33
60
|
* Applies the specified transformations to the list of rules in the proper order.
|
|
34
61
|
*
|
|
@@ -38,12 +65,14 @@ const TRANSFORMATIONS = Object.freeze({
|
|
|
38
65
|
* @returns {Promise<Array<string>>} rules after applying all transformations.
|
|
39
66
|
*/
|
|
40
67
|
async function transform(rules, configuration, transformations) {
|
|
41
|
-
// If none specified -- apply all
|
|
68
|
+
// If none specified -- apply all transformations
|
|
42
69
|
if (!transformations) {
|
|
43
70
|
// eslint-disable-next-line no-param-reassign
|
|
44
71
|
transformations = [];
|
|
45
72
|
}
|
|
46
73
|
|
|
74
|
+
checkIncompatibleValidationTransformations(transformations);
|
|
75
|
+
|
|
47
76
|
let transformed = rules;
|
|
48
77
|
|
|
49
78
|
transformed = await exclude(
|
|
@@ -57,6 +86,7 @@ async function transform(rules, configuration, transformations) {
|
|
|
57
86
|
configuration.inclusions_sources,
|
|
58
87
|
);
|
|
59
88
|
|
|
89
|
+
// When updating the list of transformations, be sure to follow the same order in README.md
|
|
60
90
|
if (transformations.indexOf(TRANSFORMATIONS.ConvertToAscii) !== -1) {
|
|
61
91
|
transformed = convertToAscii(transformed);
|
|
62
92
|
}
|
|
@@ -81,6 +111,12 @@ async function transform(rules, configuration, transformations) {
|
|
|
81
111
|
if (transformations.indexOf(TRANSFORMATIONS.ValidateAllowIp) !== -1) {
|
|
82
112
|
transformed = validateAllowIp(transformed);
|
|
83
113
|
}
|
|
114
|
+
if (transformations.indexOf(TRANSFORMATIONS.ValidateAllowPublicSuffix) !== -1) {
|
|
115
|
+
transformed = validateAllowPublicSuffix(transformed);
|
|
116
|
+
}
|
|
117
|
+
if (transformations.indexOf(TRANSFORMATIONS.ValidateAllowIpAndPublicSuffix) !== -1) {
|
|
118
|
+
transformed = validateAllowIpAndPublicSuffix(transformed);
|
|
119
|
+
}
|
|
84
120
|
if (transformations.indexOf(TRANSFORMATIONS.Deduplicate) !== -1) {
|
|
85
121
|
transformed = deduplicate(transformed);
|
|
86
122
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const { Validator } = require('./validate');
|
|
2
|
+
const { normalizeIpRules } = require('./ip-normalize');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validates a list of rules, allowing both IP addresses and public suffix matching.
|
|
6
|
+
*
|
|
7
|
+
* This function first normalizes incomplete IP rules to the safe format ||ip^,
|
|
8
|
+
* then creates an instance of the `Validator` class, enabling both IP address
|
|
9
|
+
* validation and public suffix matching, and returns a filtered list of valid rules.
|
|
10
|
+
*
|
|
11
|
+
* IP normalization:
|
|
12
|
+
* - 4-octet IPs without proper separators are normalized to ||ip^
|
|
13
|
+
* - 3-octet subnet patterns (192.168.1., 192.168.1.*) are allowed with || prefix
|
|
14
|
+
* - 3-octet patterns without trailing dot/wildcard are rejected (ambiguous)
|
|
15
|
+
* - 3-octet patterns with ^ are rejected (don't work in AdGuard Home)
|
|
16
|
+
* - 1-2 octet patterns are rejected (too wide, use regex instead)
|
|
17
|
+
*
|
|
18
|
+
* @param {Array<string>} rules - The array of rules to validate.
|
|
19
|
+
* @returns {Array<string>} - The filtered array of valid rules, allowing both
|
|
20
|
+
* IP addresses and public suffixes.
|
|
21
|
+
*/
|
|
22
|
+
function validateAllowIpAndPublicSuffix(rules) {
|
|
23
|
+
// First, normalize IP rules (convert incomplete patterns to ||ip^)
|
|
24
|
+
const normalizedRules = normalizeIpRules(rules);
|
|
25
|
+
|
|
26
|
+
// Then validate with both IP addresses and public suffixes allowed
|
|
27
|
+
const validator = new Validator(true, true);
|
|
28
|
+
return validator.validate(normalizedRules);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
validateAllowIpAndPublicSuffix,
|
|
33
|
+
};
|
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
const { Validator } = require('./validate');
|
|
2
|
+
const { normalizeIpRules } = require('./ip-normalize');
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Validates a list of rules and allows IP validation.
|
|
5
6
|
*
|
|
6
|
-
* This function
|
|
7
|
+
* This function first normalizes incomplete IP rules to the safe format ||ip^,
|
|
8
|
+
* then creates an instance of the `Validator` class, enabling IP address validation,
|
|
7
9
|
* and returns a filtered list of valid rules.
|
|
8
10
|
*
|
|
11
|
+
* IP normalization:
|
|
12
|
+
* - 4-octet IPs without proper separators are normalized to ||ip^
|
|
13
|
+
* - 3-octet subnet patterns (192.168.1., 192.168.1.*) are allowed with || prefix
|
|
14
|
+
* - 3-octet patterns without trailing dot/wildcard are rejected (ambiguous)
|
|
15
|
+
* - 3-octet patterns with ^ are rejected (don't work in AdGuard Home)
|
|
16
|
+
* - 1-2 octet patterns are rejected (too wide, use regex instead)
|
|
17
|
+
*
|
|
9
18
|
* @param {Array<string>} rules - The array of rules to validate.
|
|
10
19
|
* @returns {Array<string>} - The filtered array of valid rules, allowing IP addresses.
|
|
11
20
|
*/
|
|
12
21
|
function validateAllowIp(rules) {
|
|
13
|
-
//
|
|
14
|
-
const
|
|
15
|
-
|
|
22
|
+
// First, normalize IP rules (convert incomplete patterns to ||ip^)
|
|
23
|
+
const normalizedRules = normalizeIpRules(rules);
|
|
24
|
+
|
|
25
|
+
// Then validate with IP addresses allowed
|
|
26
|
+
// tld is not allowed by default here
|
|
27
|
+
const validator = new Validator(true, false);
|
|
28
|
+
return validator.validate(normalizedRules);
|
|
16
29
|
}
|
|
30
|
+
|
|
17
31
|
module.exports = {
|
|
18
32
|
validateAllowIp,
|
|
19
33
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const { Validator } = require('./validate');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates a list of rules and allows public suffix matching.
|
|
5
|
+
*
|
|
6
|
+
* This function creates an instance of the `Validator` class, enabling public
|
|
7
|
+
* suffix matching, and returns a filtered list of valid rules.
|
|
8
|
+
*
|
|
9
|
+
* @param {Array<string>} rules - The array of rules to validate.
|
|
10
|
+
* @returns {Array<string>} - The filtered array of valid rules.
|
|
11
|
+
*/
|
|
12
|
+
function validateAllowPublicSuffix(rules) {
|
|
13
|
+
// ip is not allowed by default, but public suffix matching is allowed.
|
|
14
|
+
const validator = new Validator(false, true);
|
|
15
|
+
return validator.validate(rules);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = {
|
|
19
|
+
validateAllowPublicSuffix,
|
|
20
|
+
};
|