@libreflare/worker-runtime 0.6.0 → 0.8.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/README.md +342 -22
- package/dist/rule-evaluator/accept-encoding.d.ts +3 -2
- package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
- package/dist/rule-evaluator/accept-encoding.js +80 -35
- package/dist/rule-evaluator/accept-encoding.js.map +1 -1
- package/dist/rule-evaluator/access-values.d.ts +84 -1
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +165 -13
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/analysis.d.ts +44 -13
- package/dist/rule-evaluator/analysis.d.ts.map +1 -1
- package/dist/rule-evaluator/analysis.js +82 -65
- package/dist/rule-evaluator/analysis.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +165 -11
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js +106 -3
- package/dist/rule-evaluator/ast.js.map +1 -1
- package/dist/rule-evaluator/byte-strings.d.ts +23 -0
- package/dist/rule-evaluator/byte-strings.d.ts.map +1 -0
- package/dist/rule-evaluator/byte-strings.js +150 -0
- package/dist/rule-evaluator/byte-strings.js.map +1 -0
- package/dist/rule-evaluator/constants.d.ts +7 -3
- package/dist/rule-evaluator/constants.d.ts.map +1 -1
- package/dist/rule-evaluator/constants.js +8 -1
- package/dist/rule-evaluator/constants.js.map +1 -1
- package/dist/rule-evaluator/country-codes.generated.js +1 -1
- package/dist/rule-evaluator/custom-lists.d.ts +15 -0
- package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
- package/dist/rule-evaluator/custom-lists.js +64 -1
- package/dist/rule-evaluator/custom-lists.js.map +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts +10 -9
- package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
- package/dist/rule-evaluator/diagnostics.js +32 -52
- package/dist/rule-evaluator/diagnostics.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.d.ts +37 -0
- package/dist/rule-evaluator/engine-observations.d.ts.map +1 -0
- package/dist/rule-evaluator/engine-observations.generated.d.ts +991 -0
- package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -0
- package/dist/rule-evaluator/engine-observations.generated.js +292 -0
- package/dist/rule-evaluator/engine-observations.generated.js.map +1 -0
- package/dist/rule-evaluator/engine-observations.js +13 -0
- package/dist/rule-evaluator/engine-observations.js.map +1 -0
- package/dist/rule-evaluator/evaluator.d.ts +10 -3
- package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator/evaluator.js +91 -83
- package/dist/rule-evaluator/evaluator.js.map +1 -1
- package/dist/rule-evaluator/explain.d.ts +11 -2
- package/dist/rule-evaluator/explain.d.ts.map +1 -1
- package/dist/rule-evaluator/explain.js +234 -123
- package/dist/rule-evaluator/explain.js.map +1 -1
- package/dist/rule-evaluator/field-availability.d.ts +11 -10
- package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
- package/dist/rule-evaluator/field-availability.js +136 -97
- package/dist/rule-evaluator/field-availability.js.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts +114 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +261 -1
- package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/field-value-domains.d.ts.map +1 -1
- package/dist/rule-evaluator/field-value-domains.js +2 -4
- package/dist/rule-evaluator/field-value-domains.js.map +1 -1
- package/dist/rule-evaluator/fields.d.ts +37 -4
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +387 -87
- package/dist/rule-evaluator/fields.js.map +1 -1
- package/dist/rule-evaluator/function-availability.d.ts +16 -0
- package/dist/rule-evaluator/function-availability.d.ts.map +1 -0
- package/dist/rule-evaluator/function-availability.js +28 -0
- package/dist/rule-evaluator/function-availability.js.map +1 -0
- package/dist/rule-evaluator/function-descriptions.d.ts +15 -1
- package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.d.ts +63 -7
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +122 -12
- package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.js +11 -2
- package/dist/rule-evaluator/function-descriptions.js.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.d.ts +27 -3
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +351 -170
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +38 -6
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +549 -279
- package/dist/rule-evaluator/functions.js.map +1 -1
- package/dist/rule-evaluator/hash-in-range.d.ts +33 -0
- package/dist/rule-evaluator/hash-in-range.d.ts.map +1 -0
- package/dist/rule-evaluator/hash-in-range.js +72 -0
- package/dist/rule-evaluator/hash-in-range.js.map +1 -0
- package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
- package/dist/rule-evaluator/log-sample.js +14 -6
- package/dist/rule-evaluator/log-sample.js.map +1 -1
- package/dist/rule-evaluator/match-chain.d.ts +3 -3
- package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
- package/dist/rule-evaluator/match-chain.js +19 -13
- package/dist/rule-evaluator/match-chain.js.map +1 -1
- package/dist/rule-evaluator/minifier.d.ts +2 -6
- package/dist/rule-evaluator/minifier.d.ts.map +1 -1
- package/dist/rule-evaluator/minifier.js +24 -28
- package/dist/rule-evaluator/minifier.js.map +1 -1
- package/dist/rule-evaluator/normalizer.d.ts +2 -6
- package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
- package/dist/rule-evaluator/normalizer.js +13 -13
- package/dist/rule-evaluator/normalizer.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +30 -4
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +217 -132
- package/dist/rule-evaluator/operators.js.map +1 -1
- package/dist/rule-evaluator/outcome.d.ts +65 -0
- package/dist/rule-evaluator/outcome.d.ts.map +1 -0
- package/dist/rule-evaluator/outcome.js +82 -0
- package/dist/rule-evaluator/outcome.js.map +1 -0
- package/dist/rule-evaluator/parse-cache.d.ts +3 -3
- package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
- package/dist/rule-evaluator/parse-cache.js +5 -13
- package/dist/rule-evaluator/parse-cache.js.map +1 -1
- package/dist/rule-evaluator/parser.d.ts +325 -12
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +1351 -340
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +2 -2
- package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
- package/dist/rule-evaluator/quantifier.js +1 -16
- package/dist/rule-evaluator/quantifier.js.map +1 -1
- package/dist/rule-evaluator/request-headers.d.ts +101 -0
- package/dist/rule-evaluator/request-headers.d.ts.map +1 -0
- package/dist/rule-evaluator/request-headers.js +344 -0
- package/dist/rule-evaluator/request-headers.js.map +1 -0
- package/dist/rule-evaluator/rule-language-types.d.ts +84 -23
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +266 -76
- package/dist/rule-evaluator/rule-language-types.js.map +1 -1
- package/dist/rule-evaluator/tokenizer.d.ts +14 -4
- package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
- package/dist/rule-evaluator/tokenizer.js +49 -11
- package/dist/rule-evaluator/tokenizer.js.map +1 -1
- package/dist/rule-evaluator/transformation.d.ts.map +1 -1
- package/dist/rule-evaluator/transformation.js +10 -6
- package/dist/rule-evaluator/transformation.js.map +1 -1
- package/dist/rule-evaluator/url-decode.d.ts +8 -0
- package/dist/rule-evaluator/url-decode.d.ts.map +1 -0
- package/dist/rule-evaluator/url-decode.js +126 -0
- package/dist/rule-evaluator/url-decode.js.map +1 -0
- package/dist/rule-evaluator/validation.d.ts +8 -9
- package/dist/rule-evaluator/validation.d.ts.map +1 -1
- package/dist/rule-evaluator/validation.js +544 -253
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator.d.ts +5 -5
- package/dist/rule-evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator.js +1 -1
- package/dist/rule-evaluator.js.map +1 -1
- package/package.json +6 -3
- package/dist/rule-evaluator/custom-security-rule-analysis.d.ts +0 -36
- package/dist/rule-evaluator/custom-security-rule-analysis.d.ts.map +0 -1
- package/dist/rule-evaluator/custom-security-rule-analysis.js +0 -382
- package/dist/rule-evaluator/custom-security-rule-analysis.js.map +0 -1
- package/dist/rule-evaluator/warnings.d.ts +0 -2
- package/dist/rule-evaluator/warnings.d.ts.map +0 -1
- package/dist/rule-evaluator/warnings.js +0 -5
- package/dist/rule-evaluator/warnings.js.map +0 -1
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ruleExpressionDiagnostic, } from './diagnostics.js';
|
|
2
|
+
import { EQUALITY_ORDERING_OPERATORS, isBooleanLiteralName, isConditionArgument, isDynamicValueNode, isEmptyArgument, isExpressionNode, mayHaveFewerArguments, unhandledNode, validSetElements, } from './ast.js';
|
|
3
|
+
import { RULE_EXPRESSION_MATCH_ALL_EXPRESSION } from './constants.js';
|
|
4
|
+
import { isRuleExpressionFunctionAvailable, isRuleExpressionOperatorAvailable } from './function-availability.js';
|
|
3
5
|
import { getUnreliableRuleExpressionFunctionReason } from './functions.js';
|
|
4
|
-
import { validateFunctionSignature } from './function-signature-validation.js';
|
|
6
|
+
import { bindArguments, validateFunctionSignature, } from './function-signature-validation.js';
|
|
5
7
|
import { minifyRuleExpression } from './minifier.js';
|
|
6
8
|
import { parseExpression } from './parse-cache.js';
|
|
9
|
+
import { hasRuleExpressionSyntaxErrors } from './transformation.js';
|
|
7
10
|
import { isKnownRuleExpressionFieldName, isRuleExpressionFieldAvailable, resolveRuleExpressionFieldAvailability, } from './field-availability.js';
|
|
8
|
-
import {
|
|
11
|
+
import { deprecatedFieldReplacement, KNOWN_DEPRECATED_FIELDS, REQUEST_HEADER_ARRAYS, REQUEST_HEADER_MAPS } from './fields.js';
|
|
9
12
|
import { isRuleExpressionCountryCode } from './country-codes.js';
|
|
10
13
|
import { getRuleExpressionFieldValueDomain } from './field-value-domains.js';
|
|
11
|
-
import { isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
|
|
12
|
-
import {
|
|
14
|
+
import { customList, isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
|
|
15
|
+
import { fieldDataType, formatRuleExpressionDataType, isArrayExpandedNode, isBooleanType, isConcreteComparisonFamily, isFieldNode, isFuncNode, inferRuleExpressionArgument, isIpAddressValueType, isLiteralNode, isUnknownRuleExpressionDataType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, singleValueType, } from './rule-language-types.js';
|
|
13
16
|
import { extractQuantifierComparison, extractQuantifierPredicate } from './quantifier.js';
|
|
17
|
+
import { stringLiteralSource } from './tokenizer.js';
|
|
14
18
|
export const MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH = 4096;
|
|
15
19
|
export const RULE_EXPRESSION_VALIDATION_PRESETS = [
|
|
16
20
|
'url-rewrite-filter',
|
|
17
21
|
'url-rewrite-target',
|
|
18
22
|
'logging-source-key',
|
|
19
23
|
];
|
|
20
|
-
|
|
21
|
-
const
|
|
24
|
+
/** Functions Cloudflare accepts at most once in any expression. */
|
|
25
|
+
const SINGLE_USE_FUNCTIONS = new Set(['hash_in_range']);
|
|
26
|
+
/** Functions the URL rewrite phase accepts at most once in a filter or a rewrite target. */
|
|
27
|
+
const URL_REWRITE_SINGLE_USE_FUNCTIONS = new Set(['concat', 'regex_replace', 'wildcard_replace']);
|
|
28
|
+
/** Replacement functions the URL rewrite phase does not accept inside each other. */
|
|
29
|
+
const URL_REWRITE_NON_NESTABLE_REPLACEMENT_FUNCTIONS = new Set(['regex_replace', 'wildcard_replace']);
|
|
22
30
|
const SUPPORTED_LIST_KIND = 'ip';
|
|
23
31
|
export function validateRuleExpressionSyntax(source, config = {}) {
|
|
24
32
|
const minify = config?.minify ?? true;
|
|
@@ -27,7 +35,6 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
27
35
|
const preset = config.preset ?? 'url-rewrite-filter';
|
|
28
36
|
const expression = source.trim();
|
|
29
37
|
const errors = [];
|
|
30
|
-
const warnings = [];
|
|
31
38
|
const diagnostics = [];
|
|
32
39
|
const semanticContext = {
|
|
33
40
|
preset,
|
|
@@ -39,37 +46,33 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
39
46
|
if (!expression) {
|
|
40
47
|
const message = 'rule expression is required';
|
|
41
48
|
errors.push(message);
|
|
42
|
-
|
|
49
|
+
diagnostics.push({
|
|
43
50
|
id: 'rule_expression.required',
|
|
44
51
|
severity: 'error',
|
|
45
|
-
message,
|
|
46
52
|
pos: null,
|
|
47
53
|
length: 0,
|
|
48
54
|
source: 'expression',
|
|
49
55
|
});
|
|
50
56
|
}
|
|
51
|
-
const parseWarnings = [];
|
|
52
57
|
const parseDiagnostics = [];
|
|
53
|
-
const parsedExpression = expression ? parseExpression(expression,
|
|
54
|
-
|
|
55
|
-
const
|
|
58
|
+
const parsedExpression = expression ? parseExpression(expression, parseDiagnostics, parseOptions) : null;
|
|
59
|
+
// The minifier keeps an expression with a syntax error as it is, so only one without is minified.
|
|
60
|
+
const minifies = minify && !hasRuleExpressionSyntaxErrors(parseDiagnostics);
|
|
61
|
+
const minifiedExpression = minifies ? minifyRuleExpression(expression, parseOptions).expression : expression;
|
|
56
62
|
const minifiedParseDiagnostics = [];
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const lengthCheckedExpression = minify ? minified.expression : expression;
|
|
62
|
-
collectValidationDiagnostics(errors, warnings, diagnostics, parseWarnings, parseDiagnostics, 'expression');
|
|
63
|
+
if (minifies && minifiedExpression)
|
|
64
|
+
parseExpression(minifiedExpression, minifiedParseDiagnostics);
|
|
65
|
+
const lengthCheckedExpression = minify ? minifiedExpression : expression;
|
|
66
|
+
collectValidationDiagnostics(errors, diagnostics, parseDiagnostics, 'expression');
|
|
63
67
|
if (parsedExpression)
|
|
64
|
-
collectSemanticValidationDiagnostics(errors,
|
|
65
|
-
collectValidationDiagnostics(errors,
|
|
66
|
-
collectValidationDiagnostics(errors, warnings, diagnostics, minifiedParseWarnings, minifiedParseDiagnostics, 'minifiedExpression');
|
|
68
|
+
collectSemanticValidationDiagnostics(errors, diagnostics, parsedExpression, expression, semanticContext);
|
|
69
|
+
collectValidationDiagnostics(errors, diagnostics, minifiedParseDiagnostics, 'minifiedExpression');
|
|
67
70
|
if ((config.enforceMaxLength ?? true) && lengthCheckedExpression.length > maxLength) {
|
|
68
71
|
const expressionLabel = minify ? 'minified rule expression' : 'rule expression';
|
|
69
72
|
const limitLabel = maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'Cloudflare limit' : 'configured limit';
|
|
70
73
|
const message = `${expressionLabel} is ${lengthCheckedExpression.length} characters; ${limitLabel} is ${maxLength}`;
|
|
71
74
|
errors.push(message);
|
|
72
|
-
|
|
75
|
+
diagnostics.push({
|
|
73
76
|
id: 'rule_expression.too_long',
|
|
74
77
|
params: {
|
|
75
78
|
actualLength: lengthCheckedExpression.length,
|
|
@@ -78,27 +81,24 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
78
81
|
limitKind: maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'cloudflare' : 'configured',
|
|
79
82
|
},
|
|
80
83
|
severity: 'error',
|
|
81
|
-
message,
|
|
82
84
|
pos: null,
|
|
83
85
|
length: 0,
|
|
84
86
|
source: minify ? 'minifiedExpression' : 'expression',
|
|
85
87
|
});
|
|
86
88
|
}
|
|
87
89
|
const validation = {
|
|
88
|
-
ok:
|
|
90
|
+
ok: !diagnostics.some(diagnostic => diagnostic.severity === 'error'),
|
|
89
91
|
expression,
|
|
90
92
|
length: expression.length,
|
|
91
93
|
maxLength,
|
|
92
|
-
errors: [...new Set(errors)],
|
|
93
|
-
warnings: [...new Set(warnings)],
|
|
94
94
|
diagnostics,
|
|
95
95
|
};
|
|
96
96
|
if (!minify)
|
|
97
97
|
return validation;
|
|
98
98
|
return {
|
|
99
99
|
...validation,
|
|
100
|
-
minifiedExpression
|
|
101
|
-
minifiedLength:
|
|
100
|
+
minifiedExpression,
|
|
101
|
+
minifiedLength: minifiedExpression.length,
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
104
|
/**
|
|
@@ -114,6 +114,32 @@ export function validateLoggingSourceKeyRuleExpressionSyntax(source) {
|
|
|
114
114
|
rejectListReferences: true,
|
|
115
115
|
});
|
|
116
116
|
}
|
|
117
|
+
const evaluationErrors = new WeakMap();
|
|
118
|
+
/**
|
|
119
|
+
* Returns the errors that make Cloudflare reject a predicate expression in any
|
|
120
|
+
* rule: syntax, operator and type, function signature, and list usage errors.
|
|
121
|
+
* Field availability, length limits, and list contents are not checked. The
|
|
122
|
+
* result is cached with the parsed expression.
|
|
123
|
+
*/
|
|
124
|
+
export function ruleExpressionEvaluationErrors(expression) {
|
|
125
|
+
const parseDiagnostics = [];
|
|
126
|
+
const node = parseExpression(expression, parseDiagnostics);
|
|
127
|
+
const cached = evaluationErrors.get(node);
|
|
128
|
+
if (cached)
|
|
129
|
+
return cached;
|
|
130
|
+
const errors = [];
|
|
131
|
+
const diagnostics = [];
|
|
132
|
+
collectValidationDiagnostics(errors, diagnostics, parseDiagnostics, 'expression');
|
|
133
|
+
collectSemanticValidationDiagnostics(errors, diagnostics, node, expression, {
|
|
134
|
+
preset: null,
|
|
135
|
+
fieldAvailability: null,
|
|
136
|
+
rejectListReferences: false,
|
|
137
|
+
deprecationNotices: false,
|
|
138
|
+
});
|
|
139
|
+
const result = [...new Set(errors)];
|
|
140
|
+
evaluationErrors.set(node, result);
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
117
143
|
function fieldAvailabilityRules(preset, config, errors, diagnostics) {
|
|
118
144
|
return resolveRuleExpressionFieldAvailability(preset, config, {
|
|
119
145
|
onUnknownField: (configPath, field) => {
|
|
@@ -203,120 +229,139 @@ function isKnownFieldName(field) {
|
|
|
203
229
|
}
|
|
204
230
|
function pushValidationConfigError(message, errors, diagnostics, metadata) {
|
|
205
231
|
errors.push(message);
|
|
206
|
-
|
|
232
|
+
diagnostics.push({
|
|
207
233
|
...metadata,
|
|
208
234
|
severity: 'error',
|
|
209
|
-
message,
|
|
210
235
|
pos: null,
|
|
211
236
|
length: 0,
|
|
212
237
|
source: 'expression',
|
|
213
238
|
});
|
|
214
239
|
}
|
|
215
|
-
function collectValidationDiagnostics(errors,
|
|
216
|
-
const detailedMessages = new Set();
|
|
240
|
+
function collectValidationDiagnostics(errors, diagnostics, messageDiagnostics, source) {
|
|
217
241
|
for (const diagnostic of messageDiagnostics) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
...(diagnostic.id ? { id: diagnostic.id } : {}),
|
|
222
|
-
...(diagnostic.params ? { params: diagnostic.params } : {}),
|
|
223
|
-
severity: diagnostic.severity,
|
|
224
|
-
message: diagnostic.message,
|
|
225
|
-
pos: diagnostic.pos ?? null,
|
|
226
|
-
length: diagnostic.length,
|
|
227
|
-
source,
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
for (const message of messages) {
|
|
231
|
-
if (detailedMessages.has(message))
|
|
232
|
-
continue;
|
|
233
|
-
pushValidationMessage(errors, warnings, message, 'warning');
|
|
234
|
-
pushDiagnostic(diagnostics, {
|
|
235
|
-
severity: 'warning',
|
|
236
|
-
message,
|
|
237
|
-
pos: null,
|
|
238
|
-
length: 0,
|
|
239
|
-
source,
|
|
240
|
-
});
|
|
242
|
+
if (diagnostic.severity === 'error')
|
|
243
|
+
errors.push(diagnostic.message);
|
|
244
|
+
diagnostics.push(ruleExpressionDiagnostic(diagnostic, source));
|
|
241
245
|
}
|
|
242
246
|
}
|
|
243
|
-
function
|
|
244
|
-
if (severity === 'error')
|
|
245
|
-
errors.push(message);
|
|
246
|
-
else
|
|
247
|
-
warnings.push(message);
|
|
248
|
-
}
|
|
249
|
-
function collectSemanticValidationDiagnostics(errors, warnings, diagnostics, node, expression, context) {
|
|
247
|
+
function collectSemanticValidationDiagnostics(errors, diagnostics, node, expression, context) {
|
|
250
248
|
const rootPosition = context.preset === 'url-rewrite-target' ? 'value' : 'predicate';
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
249
|
+
validateBooleanLiterals(node, errors, diagnostics);
|
|
250
|
+
validateSemanticNode(node, new Set(), errors, diagnostics, expression, context, rootPosition);
|
|
251
|
+
validateFunctionUsage(node, context, errors, diagnostics);
|
|
254
252
|
}
|
|
255
|
-
function validateSemanticNode(node, guardedHeaders, errors,
|
|
253
|
+
function validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, position) {
|
|
256
254
|
switch (node.kind) {
|
|
257
255
|
case 'And': {
|
|
258
256
|
const andGuards = unionHeaderGuards(guardedHeaders, collectHeaderPresenceGuards(node));
|
|
259
257
|
for (const operand of node.operands) {
|
|
260
|
-
validateSemanticNode(operand, andGuards, errors,
|
|
258
|
+
validateSemanticNode(operand, andGuards, errors, diagnostics, expression, context, 'predicate');
|
|
261
259
|
}
|
|
262
260
|
return;
|
|
263
261
|
}
|
|
264
262
|
case 'Or':
|
|
265
263
|
case 'Xor':
|
|
266
264
|
for (const operand of node.operands) {
|
|
267
|
-
validateSemanticNode(operand, guardedHeaders, errors,
|
|
265
|
+
validateSemanticNode(operand, guardedHeaders, errors, diagnostics, expression, context, 'predicate');
|
|
268
266
|
}
|
|
269
267
|
return;
|
|
270
268
|
case 'Not':
|
|
271
|
-
validateSemanticNode(node.operand, guardedHeaders, errors,
|
|
269
|
+
validateSemanticNode(node.operand, guardedHeaders, errors, diagnostics, expression, context, 'predicate');
|
|
272
270
|
return;
|
|
273
271
|
case 'Comparison':
|
|
274
|
-
validateFieldOrFunc(node.left, guardedHeaders, errors,
|
|
272
|
+
validateFieldOrFunc(node.left, guardedHeaders, errors, diagnostics, expression, context);
|
|
273
|
+
if (isDynamicValueNode(node.right)) {
|
|
274
|
+
validateFieldOrFunc(node.right, guardedHeaders, errors, diagnostics, expression, context);
|
|
275
|
+
}
|
|
275
276
|
validateComparisonRequirements(node, errors, diagnostics, context);
|
|
276
277
|
return;
|
|
277
|
-
case 'Func':
|
|
278
|
+
case 'Func': {
|
|
278
279
|
if (position === 'predicate')
|
|
279
280
|
validateFunctionPredicateResult(node, errors, diagnostics);
|
|
281
|
+
validateFunctionAvailability(node, errors, diagnostics, context);
|
|
280
282
|
validateAccessPath(node, errors, diagnostics);
|
|
281
|
-
validateFunctionSupport(node,
|
|
282
|
-
|
|
283
|
+
validateFunctionSupport(node, diagnostics);
|
|
284
|
+
const binding = bindArguments(node);
|
|
285
|
+
validateFunctionArgRequirements(node, binding, errors, diagnostics);
|
|
286
|
+
if (node.name === 'coalesce')
|
|
287
|
+
validateCoalesceFallbacks(node, binding, diagnostics);
|
|
283
288
|
if (node.name === 'any' || node.name === 'all') {
|
|
284
|
-
validateQuantifierPredicateArgs(node, guardedHeaders, errors,
|
|
289
|
+
validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
285
290
|
}
|
|
286
291
|
else {
|
|
287
|
-
validateFuncArgs(node, guardedHeaders, errors,
|
|
292
|
+
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
288
293
|
}
|
|
289
294
|
return;
|
|
295
|
+
}
|
|
290
296
|
case 'BoolField':
|
|
291
|
-
validateBoolFieldNode(node, errors,
|
|
297
|
+
validateBoolFieldNode(node, errors, diagnostics, expression, context);
|
|
298
|
+
return;
|
|
299
|
+
case 'Invalid':
|
|
300
|
+
// A field or call that recovery cut short gets its own checks, but is not judged as a condition.
|
|
301
|
+
if (node.operand)
|
|
302
|
+
validateFieldOrFunc(node.operand, guardedHeaders, errors, diagnostics, expression, context);
|
|
292
303
|
return;
|
|
304
|
+
default:
|
|
305
|
+
unhandledNode(node);
|
|
293
306
|
}
|
|
294
307
|
}
|
|
295
308
|
function validateFunctionPredicateResult(node, errors, diagnostics) {
|
|
296
|
-
const
|
|
297
|
-
if (
|
|
298
|
-
return;
|
|
299
|
-
if (result.type.kind === 'scalar' && result.type.name === 'Boolean')
|
|
309
|
+
const type = conditionResultType(node);
|
|
310
|
+
if (!type || isUnknownRuleExpressionDataType(type) || isBooleanType(type))
|
|
300
311
|
return;
|
|
301
|
-
const actualType = formatRuleExpressionDataType(
|
|
312
|
+
const actualType = formatRuleExpressionDataType(type);
|
|
302
313
|
pushFuncError(node, `function '${node.name}' result must be Boolean in predicate position; received ${actualType}`, errors, diagnostics, {
|
|
303
314
|
id: 'function.result_type_mismatch',
|
|
304
315
|
params: { functionName: node.name, expectedType: 'Boolean', actualType },
|
|
305
316
|
});
|
|
306
317
|
}
|
|
318
|
+
/**
|
|
319
|
+
* The type a call's result has as a condition, or null when it is invalid.
|
|
320
|
+
* Where only whether the call maps is unknown, the type of one invocation
|
|
321
|
+
* stands for its result, an array of it or not, which is a Boolean only
|
|
322
|
+
* where that type is one, and then may be either; see singleValueType().
|
|
323
|
+
*/
|
|
324
|
+
function conditionResultType(node) {
|
|
325
|
+
const result = inferRuleExpressionArgument(node);
|
|
326
|
+
if (!result.invalid)
|
|
327
|
+
return result.type;
|
|
328
|
+
const type = singleValueType(node);
|
|
329
|
+
return type && !isBooleanType(type) ? type : null;
|
|
330
|
+
}
|
|
307
331
|
function validateComparisonRequirements(node, errors, diagnostics, context) {
|
|
308
332
|
let invalidArrayComparison = false;
|
|
309
333
|
const leftInfo = inferRuleExpressionArgument(node.left);
|
|
310
|
-
|
|
334
|
+
const leftExpanded = !leftInfo.invalid && leftInfo.expanded;
|
|
335
|
+
if (isFieldNode(node.left) && leftExpanded) {
|
|
311
336
|
pushComparisonError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
|
|
312
337
|
invalidArrayComparison = true;
|
|
313
338
|
}
|
|
314
|
-
if (node.left.kind === 'Func' &&
|
|
339
|
+
if (node.left.kind === 'Func' && leftExpanded) {
|
|
315
340
|
pushFuncError(node.left, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
|
|
316
341
|
invalidArrayComparison = true;
|
|
317
342
|
}
|
|
318
|
-
if (
|
|
343
|
+
if (isArrayExpandedNode(node.right)) {
|
|
344
|
+
pushValueError(node.right, '[*] cannot be used directly with comparison operators; wrap the comparison in any() or all()', errors, diagnostics, { id: 'array_expansion.direct_comparison' });
|
|
345
|
+
invalidArrayComparison = true;
|
|
346
|
+
}
|
|
347
|
+
// Invalid syntax already has its error, and nothing is judged by what it might have been. The checks
|
|
348
|
+
// that read only the left side still run.
|
|
349
|
+
if (!node.right || node.right.kind === 'Invalid') {
|
|
350
|
+
if (!invalidArrayComparison)
|
|
351
|
+
validateComparisonTypeCompatibility(node, undefined, errors, diagnostics);
|
|
319
352
|
return;
|
|
353
|
+
}
|
|
354
|
+
// Only equality and ordering operators compare dynamic values; `in` reports its own list requirement.
|
|
355
|
+
if (isDynamicValueNode(node.right) && node.op !== 'in' && !EQUALITY_ORDERING_OPERATORS.has(node.op)) {
|
|
356
|
+
pushValueError(node.right, `operator '${node.op}' requires a literal value on the right side`, errors, diagnostics, { id: 'operator.requires_literal_value', params: { operator: node.op } });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (node.right.kind === 'SetLiteral' && node.op !== 'in') {
|
|
360
|
+
pushValueError(node.right, 'inline lists can only be used with the in operator', errors, diagnostics, {
|
|
361
|
+
id: 'inline_list.requires_in_operator',
|
|
362
|
+
});
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
320
365
|
if (node.right?.kind === 'ListRef') {
|
|
321
366
|
validateListRef(node.right, node.left, node.op, errors, diagnostics, context);
|
|
322
367
|
}
|
|
@@ -327,22 +372,16 @@ function validateComparisonRequirements(node, errors, diagnostics, context) {
|
|
|
327
372
|
validateWildcardPattern(node.right, errors, diagnostics);
|
|
328
373
|
}
|
|
329
374
|
validateComparisonValueDomain(node, errors, diagnostics);
|
|
330
|
-
if (!invalidArrayComparison
|
|
331
|
-
validateComparisonTypeCompatibility(node, errors, diagnostics);
|
|
332
|
-
}
|
|
375
|
+
if (!invalidArrayComparison)
|
|
376
|
+
validateComparisonTypeCompatibility(node, node.right, errors, diagnostics);
|
|
333
377
|
}
|
|
334
378
|
function validateComparisonValueDomain(node, errors, diagnostics) {
|
|
335
379
|
if (!isFieldNode(node.left) || (node.op !== 'eq' && node.op !== 'ne' && node.op !== 'in'))
|
|
336
380
|
return;
|
|
337
381
|
if (getRuleExpressionFieldValueDomain(node.left.name) !== 'country-code')
|
|
338
382
|
return;
|
|
339
|
-
const right = node.right;
|
|
340
|
-
const
|
|
341
|
-
? [right.value]
|
|
342
|
-
: right?.kind === 'SetLiteral'
|
|
343
|
-
? right.values.filter((value) => typeof value === 'string')
|
|
344
|
-
: [];
|
|
345
|
-
for (const value of values) {
|
|
383
|
+
const right = node.right?.kind === 'StringLiteral' || node.right?.kind === 'SetLiteral' ? node.right : undefined;
|
|
384
|
+
for (const value of stringLiteralValues(right)) {
|
|
346
385
|
if (isRuleExpressionCountryCode(value))
|
|
347
386
|
continue;
|
|
348
387
|
const message = `'${value}' is not a valid country code for field '${node.left.name}'`;
|
|
@@ -352,21 +391,32 @@ function validateComparisonValueDomain(node, errors, diagnostics) {
|
|
|
352
391
|
});
|
|
353
392
|
}
|
|
354
393
|
}
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
394
|
+
/** The quoted String values of a literal or inline list. */
|
|
395
|
+
function stringLiteralValues(node) {
|
|
396
|
+
if (node?.kind === 'StringLiteral')
|
|
397
|
+
return [node.value];
|
|
398
|
+
return node?.elements.flatMap(element => element.type === 'string' ? [element.value] : []) ?? [];
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Checks that the comparison's operator accepts the types of its sides. The
|
|
402
|
+
* left side is checked whatever the right side is, unless that is of a type
|
|
403
|
+
* no operator supports. A right side that is missing, invalid, or of a type
|
|
404
|
+
* not known is not judged, as nothing is judged by what it might have been.
|
|
405
|
+
*/
|
|
406
|
+
function validateComparisonTypeCompatibility(node, right, errors, diagnostics) {
|
|
407
|
+
if (node.op === 'in' && right) {
|
|
408
|
+
if (right.kind === 'ListRef')
|
|
358
409
|
return;
|
|
359
|
-
if (
|
|
360
|
-
pushComparisonTypeError(
|
|
410
|
+
if (right.kind !== 'SetLiteral') {
|
|
411
|
+
pushComparisonTypeError(right, "operator 'in' requires an inline list or list reference", errors, diagnostics, { id: 'operator.in_requires_list', params: { operator: 'in' } });
|
|
361
412
|
return;
|
|
362
413
|
}
|
|
363
414
|
}
|
|
364
|
-
|
|
415
|
+
const leftInfo = inferRuleExpressionArgument(node.left);
|
|
416
|
+
if (leftInfo.invalid)
|
|
365
417
|
return;
|
|
366
|
-
const leftType =
|
|
367
|
-
const rightType = inferRuleExpressionArgument(node.right).type;
|
|
418
|
+
const leftType = leftInfo.type;
|
|
368
419
|
const leftFamily = ruleExpressionComparisonTypeFamily(leftType);
|
|
369
|
-
const rightFamily = ruleExpressionComparisonTypeFamily(rightType);
|
|
370
420
|
if (leftFamily === 'unsupported') {
|
|
371
421
|
const leftTypeLabel = formatRuleExpressionDataType(leftType);
|
|
372
422
|
pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftTypeLabel} values`, errors, diagnostics, {
|
|
@@ -375,31 +425,37 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
375
425
|
});
|
|
376
426
|
return;
|
|
377
427
|
}
|
|
378
|
-
|
|
428
|
+
const rightInfo = right && inferRuleExpressionArgument(right);
|
|
429
|
+
const rightType = rightInfo && !rightInfo.invalid ? rightInfo.type : null;
|
|
430
|
+
const rightFamily = rightType ? ruleExpressionComparisonTypeFamily(rightType) : 'unknown';
|
|
431
|
+
if (right && rightType && rightFamily === 'unsupported') {
|
|
432
|
+
const rightTypeLabel = formatRuleExpressionDataType(rightType);
|
|
433
|
+
pushComparisonTypeError(right, `operator '${node.op}' does not support ${rightTypeLabel} values`, errors, diagnostics, {
|
|
434
|
+
id: 'operator.unsupported_type',
|
|
435
|
+
params: { operator: node.op, side: 'right', valueType: rightTypeLabel },
|
|
436
|
+
});
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
if (!isConcreteComparisonFamily(leftFamily) || !validateLeftOperandType(node, leftFamily, errors, diagnostics))
|
|
440
|
+
return;
|
|
441
|
+
if (!right || !isConcreteComparisonFamily(rightFamily))
|
|
379
442
|
return;
|
|
380
443
|
const leftLabel = comparisonFamilyLabel(leftFamily);
|
|
381
444
|
const rightLabel = comparisonFamilyLabel(rightFamily);
|
|
382
|
-
const valueIsInlineSet =
|
|
445
|
+
const valueIsInlineSet = right.kind === 'SetLiteral';
|
|
383
446
|
const rightValueLabel = valueIsInlineSet ? `inline list of ${rightLabel}` : rightLabel;
|
|
384
447
|
switch (node.op) {
|
|
385
448
|
case 'eq':
|
|
386
449
|
case 'ne':
|
|
387
|
-
if (!isEqualityFamily(leftFamily)) {
|
|
388
|
-
pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftLabel} values`, errors, diagnostics, {
|
|
389
|
-
id: 'operator.unsupported_type',
|
|
390
|
-
params: { operator: node.op, side: 'left', valueType: leftLabel },
|
|
391
|
-
});
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
450
|
if (!isEqualityFamily(rightFamily)) {
|
|
395
|
-
pushComparisonTypeError(
|
|
451
|
+
pushComparisonTypeError(right, `operator '${node.op}' does not support ${rightValueLabel}`, errors, diagnostics, {
|
|
396
452
|
id: 'operator.unsupported_type',
|
|
397
453
|
params: { operator: node.op, side: 'right', valueType: rightLabel, valueIsInlineSet },
|
|
398
454
|
});
|
|
399
455
|
return;
|
|
400
456
|
}
|
|
401
457
|
if (leftFamily !== rightFamily) {
|
|
402
|
-
pushComparisonTypeError(
|
|
458
|
+
pushComparisonTypeError(right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
|
|
403
459
|
id: 'operator.type_mismatch',
|
|
404
460
|
params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
|
|
405
461
|
});
|
|
@@ -409,15 +465,10 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
409
465
|
case 'le':
|
|
410
466
|
case 'gt':
|
|
411
467
|
case 'ge':
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
});
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
if (!isOrderingFamily(rightFamily) || leftFamily !== rightFamily) {
|
|
420
|
-
pushComparisonTypeError(node.right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
|
|
468
|
+
case 'in':
|
|
469
|
+
// The left side's family suits the operator, so the same family on the right does too.
|
|
470
|
+
if (leftFamily !== rightFamily) {
|
|
471
|
+
pushComparisonTypeError(right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
|
|
421
472
|
id: 'operator.type_mismatch',
|
|
422
473
|
params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
|
|
423
474
|
});
|
|
@@ -427,35 +478,51 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
427
478
|
case 'matches':
|
|
428
479
|
case 'wildcard':
|
|
429
480
|
case 'strict_wildcard':
|
|
430
|
-
if (leftFamily !== 'string') {
|
|
431
|
-
pushComparisonTypeError(node.left, `operator '${node.op}' requires String values; left side is ${leftLabel}`, errors, diagnostics, {
|
|
432
|
-
id: 'operator.requires_type',
|
|
433
|
-
params: { operator: node.op, side: 'left', requiredTypes: ['String'], actualType: leftLabel },
|
|
434
|
-
});
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
481
|
if (rightFamily !== 'string') {
|
|
438
|
-
pushComparisonTypeError(
|
|
482
|
+
pushComparisonTypeError(right, `operator '${node.op}' requires String values; right side is ${rightValueLabel}`, errors, diagnostics, {
|
|
439
483
|
id: 'operator.requires_type',
|
|
440
484
|
params: { operator: node.op, side: 'right', requiredTypes: ['String'], actualType: rightLabel, valueIsInlineSet },
|
|
441
485
|
});
|
|
442
486
|
}
|
|
443
487
|
return;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/** Checks that the operator accepts the left side's type, whatever the right side is; false after an error. */
|
|
491
|
+
function validateLeftOperandType(node, leftFamily, errors, diagnostics) {
|
|
492
|
+
const leftLabel = comparisonFamilyLabel(leftFamily);
|
|
493
|
+
switch (node.op) {
|
|
494
|
+
case 'eq':
|
|
495
|
+
case 'ne':
|
|
444
496
|
case 'in':
|
|
445
|
-
if (
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
497
|
+
if (isEqualityFamily(leftFamily))
|
|
498
|
+
return true;
|
|
499
|
+
pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftLabel} values`, errors, diagnostics, {
|
|
500
|
+
id: 'operator.unsupported_type',
|
|
501
|
+
params: { operator: node.op, side: 'left', valueType: leftLabel },
|
|
502
|
+
});
|
|
503
|
+
return false;
|
|
504
|
+
case 'lt':
|
|
505
|
+
case 'le':
|
|
506
|
+
case 'gt':
|
|
507
|
+
case 'ge':
|
|
508
|
+
if (isOrderingFamily(leftFamily))
|
|
509
|
+
return true;
|
|
510
|
+
pushComparisonTypeError(node.left, `operator '${node.op}' requires String or Number values; left side is ${leftLabel}`, errors, diagnostics, {
|
|
511
|
+
id: 'operator.requires_type',
|
|
512
|
+
params: { operator: node.op, side: 'left', requiredTypes: ['String', 'Number'], actualType: leftLabel },
|
|
513
|
+
});
|
|
514
|
+
return false;
|
|
515
|
+
case 'contains':
|
|
516
|
+
case 'matches':
|
|
517
|
+
case 'wildcard':
|
|
518
|
+
case 'strict_wildcard':
|
|
519
|
+
if (leftFamily === 'string')
|
|
520
|
+
return true;
|
|
521
|
+
pushComparisonTypeError(node.left, `operator '${node.op}' requires String values; left side is ${leftLabel}`, errors, diagnostics, {
|
|
522
|
+
id: 'operator.requires_type',
|
|
523
|
+
params: { operator: node.op, side: 'left', requiredTypes: ['String'], actualType: leftLabel },
|
|
524
|
+
});
|
|
525
|
+
return false;
|
|
459
526
|
}
|
|
460
527
|
}
|
|
461
528
|
function isEqualityFamily(family) {
|
|
@@ -475,52 +542,106 @@ function comparisonFamilyLabel(family) {
|
|
|
475
542
|
function pushComparisonTypeError(node, message, errors, diagnostics, metadata) {
|
|
476
543
|
pushValueError(node, message, errors, diagnostics, metadata);
|
|
477
544
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
545
|
+
/**
|
|
546
|
+
* Reports functions used more often than Cloudflare allows, nested
|
|
547
|
+
* replacements, and, in rewrite targets, operators.
|
|
548
|
+
*/
|
|
549
|
+
function validateFunctionUsage(node, context, errors, diagnostics) {
|
|
550
|
+
const { preset } = context;
|
|
551
|
+
const rewriteTarget = preset === 'url-rewrite-target';
|
|
552
|
+
const usage = { calls: new Map(), nestedReplacements: [], ...(rewriteTarget ? { operators: [] } : {}) };
|
|
553
|
+
collectFunctionUsage(node, [], usage);
|
|
554
|
+
for (const functionName of SINGLE_USE_FUNCTIONS)
|
|
555
|
+
reportSingleUseFunction(functionName, usage, errors, diagnostics);
|
|
556
|
+
if (preset !== 'url-rewrite-filter' && preset !== 'url-rewrite-target')
|
|
557
|
+
return;
|
|
558
|
+
for (const functionName of URL_REWRITE_SINGLE_USE_FUNCTIONS)
|
|
559
|
+
reportSingleUseFunction(functionName, usage, errors, diagnostics);
|
|
560
|
+
for (const { node: nested, parentFunctionName } of usage.nestedReplacements) {
|
|
561
|
+
pushFuncError(nested, `function '${nested.name}' cannot be nested inside '${parentFunctionName}'`, errors, diagnostics, {
|
|
562
|
+
id: 'function.nested_replacement',
|
|
563
|
+
params: { functionName: nested.name, parentFunctionName },
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
for (const { operator, node: operatorNode } of usage.operators ?? []) {
|
|
567
|
+
if (isRuleExpressionOperatorAvailable(operator, preset))
|
|
568
|
+
continue;
|
|
569
|
+
errors.push(`operator '${operator}' is not available for ${preset} expressions`);
|
|
570
|
+
diagnostics.push({
|
|
571
|
+
id: 'operator.unavailable',
|
|
572
|
+
params: { operator, preset },
|
|
573
|
+
severity: 'error',
|
|
574
|
+
pos: operatorNode.pos ?? null,
|
|
575
|
+
length: operatorNode.length ?? 0,
|
|
576
|
+
source: 'expression',
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
/** Reports every call of a function after its first. */
|
|
581
|
+
function reportSingleUseFunction(functionName, usage, errors, diagnostics) {
|
|
582
|
+
for (const duplicate of (usage.calls.get(functionName) ?? []).slice(1)) {
|
|
583
|
+
pushFuncError(duplicate, `function '${functionName}' can appear only once in an expression`, errors, diagnostics, {
|
|
584
|
+
id: 'function.single_use',
|
|
585
|
+
params: { functionName },
|
|
586
|
+
});
|
|
489
587
|
}
|
|
490
588
|
}
|
|
491
|
-
|
|
589
|
+
/**
|
|
590
|
+
* Whether an argument is a Boolean field or a call used as a condition, such
|
|
591
|
+
* as `(ssl)` or `(has_value(...))`, which Cloudflare evaluates with its IsTrue
|
|
592
|
+
* operator. Other conditions use their own operators, and so do quantifiers.
|
|
593
|
+
* One that recovery cut short may be a comparison instead, and is invalid;
|
|
594
|
+
* see InvalidNode.operand.
|
|
595
|
+
*/
|
|
596
|
+
function isTruthTestArgument(arg) {
|
|
597
|
+
if (arg.kind === 'Func')
|
|
598
|
+
return arg.condition === true && arg.name !== 'any' && arg.name !== 'all';
|
|
599
|
+
return arg.kind === 'BoolField';
|
|
600
|
+
}
|
|
601
|
+
/** Collects every function call, nested replacement, and operator in an expression. */
|
|
602
|
+
function collectFunctionUsage(node, replacementFunctionStack, usage) {
|
|
492
603
|
switch (node.kind) {
|
|
493
604
|
case 'Func': {
|
|
494
605
|
const functionName = node.name;
|
|
495
|
-
|
|
496
|
-
|
|
606
|
+
const calls = usage.calls.get(functionName);
|
|
607
|
+
if (calls)
|
|
608
|
+
calls.push(node);
|
|
609
|
+
else
|
|
610
|
+
usage.calls.set(functionName, [node]);
|
|
611
|
+
if (functionName === 'any' || functionName === 'all')
|
|
612
|
+
usage.operators?.push({ operator: functionName, node });
|
|
613
|
+
const parentFunctionName = replacementFunctionStack.at(-1);
|
|
614
|
+
const replacement = URL_REWRITE_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName);
|
|
615
|
+
if (parentFunctionName && replacement)
|
|
616
|
+
usage.nestedReplacements.push({ node, parentFunctionName });
|
|
617
|
+
const nextReplacementFunctionStack = replacement ? [...replacementFunctionStack, functionName] : replacementFunctionStack;
|
|
618
|
+
for (const arg of node.args) {
|
|
619
|
+
if (isTruthTestArgument(arg))
|
|
620
|
+
usage.operators?.push({ operator: 'is_true', node: arg });
|
|
621
|
+
collectFunctionUsage(arg, nextReplacementFunctionStack, usage);
|
|
497
622
|
}
|
|
498
|
-
const parentReplacementFunction = replacementFunctionStack.at(-1);
|
|
499
|
-
if (parentReplacementFunction && URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)) {
|
|
500
|
-
pushFuncError(node, `function '${functionName}' cannot be nested inside '${parentReplacementFunction}' in url-rewrite-target expressions`, errors, diagnostics, {
|
|
501
|
-
id: 'function.rewrite_target.nested_replacement',
|
|
502
|
-
params: { functionName, parentFunctionName: parentReplacementFunction, preset: 'url-rewrite-target' },
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
const nextReplacementFunctionStack = URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)
|
|
506
|
-
? [...replacementFunctionStack, functionName]
|
|
507
|
-
: replacementFunctionStack;
|
|
508
|
-
for (const arg of node.args)
|
|
509
|
-
collectRewriteTargetFunctions(arg, nextReplacementFunctionStack, functions, errors, diagnostics);
|
|
510
623
|
return;
|
|
511
624
|
}
|
|
512
625
|
case 'And':
|
|
513
626
|
case 'Or':
|
|
514
627
|
case 'Xor':
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
628
|
+
usage.operators?.push({ operator: node.kind.toLowerCase(), node });
|
|
629
|
+
for (const operand of node.operands)
|
|
630
|
+
collectFunctionUsage(operand, replacementFunctionStack, usage);
|
|
518
631
|
return;
|
|
519
632
|
case 'Not':
|
|
520
|
-
|
|
633
|
+
usage.operators?.push({ operator: 'not', node });
|
|
634
|
+
collectFunctionUsage(node.operand, replacementFunctionStack, usage);
|
|
521
635
|
return;
|
|
522
636
|
case 'Comparison':
|
|
523
|
-
|
|
637
|
+
usage.operators?.push({ operator: node.op, node });
|
|
638
|
+
collectFunctionUsage(node.left, replacementFunctionStack, usage);
|
|
639
|
+
if (isDynamicValueNode(node.right))
|
|
640
|
+
collectFunctionUsage(node.right, replacementFunctionStack, usage);
|
|
641
|
+
return;
|
|
642
|
+
case 'Invalid':
|
|
643
|
+
if (node.operand)
|
|
644
|
+
collectFunctionUsage(node.operand, replacementFunctionStack, usage);
|
|
524
645
|
return;
|
|
525
646
|
case 'Field':
|
|
526
647
|
case 'BoolField':
|
|
@@ -531,6 +652,8 @@ function collectRewriteTargetFunctions(node, replacementFunctionStack, functions
|
|
|
531
652
|
case 'SetLiteral':
|
|
532
653
|
case 'ListRef':
|
|
533
654
|
return;
|
|
655
|
+
default:
|
|
656
|
+
unhandledNode(node);
|
|
534
657
|
}
|
|
535
658
|
}
|
|
536
659
|
function validateListRef(node, left, op, errors, diagnostics, context) {
|
|
@@ -583,7 +706,7 @@ function validateFunctionListRefArg(node, arg, errors, diagnostics, context) {
|
|
|
583
706
|
}
|
|
584
707
|
}
|
|
585
708
|
function validateKnownListRefValueType(node, left, errors, diagnostics, context) {
|
|
586
|
-
const listKind = context.customLists
|
|
709
|
+
const listKind = customList(context.customLists, node.name)?.kind ?? SUPPORTED_LIST_KIND;
|
|
587
710
|
const valueTypes = ruleExpressionValueTypes(left);
|
|
588
711
|
if (valueTypes.length === 0 || valueTypes.some(valueType => listKindAcceptsValueType(listKind, valueType)))
|
|
589
712
|
return;
|
|
@@ -619,7 +742,8 @@ function isRecord(value) {
|
|
|
619
742
|
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
620
743
|
}
|
|
621
744
|
function validateInlineSetLiteral(node, errors, diagnostics) {
|
|
622
|
-
|
|
745
|
+
// An invalid member is not another element type.
|
|
746
|
+
const valueTypes = new Set(validSetElements(node).map(element => element.type));
|
|
623
747
|
if (valueTypes.size <= 1)
|
|
624
748
|
return;
|
|
625
749
|
pushValueError(node, 'inline list elements must all have the same data type', errors, diagnostics, {
|
|
@@ -658,10 +782,9 @@ function hasConsecutiveUnescapedWildcards(pattern) {
|
|
|
658
782
|
function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
|
|
659
783
|
const message = `${requirement}`;
|
|
660
784
|
errors.push(message);
|
|
661
|
-
|
|
785
|
+
diagnostics.push({
|
|
662
786
|
...metadata,
|
|
663
787
|
severity: 'error',
|
|
664
|
-
message,
|
|
665
788
|
pos: node.pos,
|
|
666
789
|
length: node.length,
|
|
667
790
|
source: 'expression',
|
|
@@ -670,10 +793,9 @@ function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
|
|
|
670
793
|
function pushFuncError(node, requirement, errors, diagnostics, metadata) {
|
|
671
794
|
const message = `${requirement}`;
|
|
672
795
|
errors.push(message);
|
|
673
|
-
|
|
796
|
+
diagnostics.push({
|
|
674
797
|
...metadata,
|
|
675
798
|
severity: 'error',
|
|
676
|
-
message,
|
|
677
799
|
pos: node.pos ?? null,
|
|
678
800
|
length: node.length ?? 0,
|
|
679
801
|
source: 'expression',
|
|
@@ -682,81 +804,130 @@ function pushFuncError(node, requirement, errors, diagnostics, metadata) {
|
|
|
682
804
|
function pushValueError(node, message, errors, diagnostics, metadata) {
|
|
683
805
|
errors.push(message);
|
|
684
806
|
const span = argSpan(node);
|
|
685
|
-
|
|
807
|
+
diagnostics.push({
|
|
686
808
|
...metadata,
|
|
687
809
|
severity: 'error',
|
|
688
|
-
message,
|
|
689
810
|
pos: span.pos,
|
|
690
811
|
length: span.length,
|
|
691
812
|
source: 'expression',
|
|
692
813
|
});
|
|
693
814
|
}
|
|
694
|
-
|
|
815
|
+
/**
|
|
816
|
+
* Reports a function the preset's Cloudflare phase rejects. Quantifiers are
|
|
817
|
+
* operators, which rewrite targets check separately.
|
|
818
|
+
*/
|
|
819
|
+
function validateFunctionAvailability(node, errors, diagnostics, context) {
|
|
820
|
+
const functionName = node.name;
|
|
821
|
+
const { preset } = context;
|
|
822
|
+
if (!preset || functionName === 'any' || functionName === 'all')
|
|
823
|
+
return;
|
|
824
|
+
if (isRuleExpressionFunctionAvailable(functionName, preset))
|
|
825
|
+
return;
|
|
826
|
+
pushFuncError(node, `function '${functionName}' is not available for ${preset} expressions`, errors, diagnostics, {
|
|
827
|
+
id: 'function.unavailable',
|
|
828
|
+
params: { functionName, preset },
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
function validateFunctionSupport(node, diagnostics) {
|
|
695
832
|
const functionName = node.name;
|
|
696
833
|
const reason = getUnreliableRuleExpressionFunctionReason(functionName);
|
|
697
834
|
if (!reason)
|
|
698
835
|
return;
|
|
699
|
-
|
|
700
|
-
pushWarning(warnings, message);
|
|
701
|
-
pushDiagnostic(diagnostics, {
|
|
836
|
+
diagnostics.push({
|
|
702
837
|
id: 'function.unreliable',
|
|
703
838
|
params: { functionName, reason },
|
|
704
839
|
severity: 'warning',
|
|
705
|
-
message,
|
|
706
840
|
pos: null,
|
|
707
841
|
length: 0,
|
|
708
842
|
source: 'expression',
|
|
709
843
|
});
|
|
710
844
|
}
|
|
711
|
-
|
|
845
|
+
/**
|
|
846
|
+
* Warns when a coalesce() argument before the last can never be nil, which
|
|
847
|
+
* makes every argument after it unused. Only literals and fields without
|
|
848
|
+
* access are certain: Cloudflare produces nil from map keys, array indexes,
|
|
849
|
+
* and functions, never from a field itself.
|
|
850
|
+
*/
|
|
851
|
+
function validateCoalesceFallbacks(node, binding, diagnostics) {
|
|
852
|
+
// An argument counts only where it binds to a parameter for certain, which one that recovery from a
|
|
853
|
+
// syntax error left uncertain does not; see ArgumentBinding.positions. Only the values after it are
|
|
854
|
+
// unused, counting those the parser read, and an empty slot holds none. When some may be another
|
|
855
|
+
// call's, none may follow.
|
|
856
|
+
if (mayHaveFewerArguments(node))
|
|
857
|
+
return;
|
|
858
|
+
const { positions } = binding;
|
|
859
|
+
const index = node.args.findIndex((arg, argIndex) => positions[argIndex] !== null && ((isLiteralNode(arg) && arg.kind !== 'SetLiteral' && arg.kind !== 'ListRef') ||
|
|
860
|
+
(isFieldNode(arg) && arg.accessPath.length === 0) ||
|
|
861
|
+
isConditionArgument(arg)));
|
|
862
|
+
if (index < 0)
|
|
863
|
+
return;
|
|
864
|
+
const unusedArgumentCount = node.args.slice(index + 1).filter(arg => !isEmptyArgument(arg)).length;
|
|
865
|
+
if (unusedArgumentCount === 0)
|
|
866
|
+
return;
|
|
867
|
+
// The argument found has a position.
|
|
868
|
+
const parameterName = `value${positions[index] + 1}`;
|
|
869
|
+
const span = argSpan(node.args[index]);
|
|
870
|
+
diagnostics.push({
|
|
871
|
+
id: 'function.coalesce.never_nil_argument',
|
|
872
|
+
params: { functionName: 'coalesce', parameterName, unusedArgumentCount },
|
|
873
|
+
severity: 'warning',
|
|
874
|
+
pos: span.pos,
|
|
875
|
+
length: span.length,
|
|
876
|
+
source: 'expression',
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
function validateFieldOrFunc(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
712
880
|
if (isFieldNode(node)) {
|
|
713
|
-
validateFieldNode(node, guardedHeaders, errors,
|
|
881
|
+
validateFieldNode(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
714
882
|
return;
|
|
715
883
|
}
|
|
716
|
-
validateSemanticNode(node, guardedHeaders, errors,
|
|
884
|
+
validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, 'value');
|
|
717
885
|
}
|
|
718
|
-
function validateFuncArgs(node, guardedHeaders, errors,
|
|
886
|
+
function validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
719
887
|
for (const arg of node.args) {
|
|
720
888
|
if (isFieldNode(arg)) {
|
|
721
|
-
validateFieldNode(arg, guardedHeaders, errors,
|
|
889
|
+
validateFieldNode(arg, guardedHeaders, errors, diagnostics, expression, context);
|
|
722
890
|
}
|
|
723
891
|
else if (isExpressionNode(arg)) {
|
|
724
|
-
|
|
892
|
+
const position = isConditionArgument(arg) ? 'predicate' : 'value';
|
|
893
|
+
validateSemanticNode(arg, guardedHeaders, errors, diagnostics, expression, context, position);
|
|
725
894
|
}
|
|
726
895
|
else if (arg.kind === 'ListRef') {
|
|
727
896
|
validateFunctionListRefArg(node, arg, errors, diagnostics, context);
|
|
728
897
|
}
|
|
729
898
|
}
|
|
730
899
|
}
|
|
731
|
-
function validateQuantifierPredicateArgs(node, guardedHeaders, errors,
|
|
900
|
+
function validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
732
901
|
const quantifier = extractQuantifierPredicate(node);
|
|
733
902
|
if (!quantifier) {
|
|
734
903
|
const comparison = extractQuantifierComparison(node);
|
|
735
904
|
if (comparison && !comparison.comparison.right) {
|
|
736
|
-
validateFieldOrFunc(comparison.source, guardedHeaders, errors,
|
|
905
|
+
validateFieldOrFunc(comparison.source, guardedHeaders, errors, diagnostics, expression, context);
|
|
737
906
|
return;
|
|
738
907
|
}
|
|
739
|
-
validateFuncArgs(node, guardedHeaders, errors,
|
|
908
|
+
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
740
909
|
return;
|
|
741
910
|
}
|
|
742
|
-
validateFieldOrFunc(quantifier.source, guardedHeaders, errors,
|
|
911
|
+
validateFieldOrFunc(quantifier.source, guardedHeaders, errors, diagnostics, expression, context);
|
|
912
|
+
if (isDynamicValueNode(quantifier.value)) {
|
|
913
|
+
validateFieldOrFunc(quantifier.value, guardedHeaders, errors, diagnostics, expression, context);
|
|
914
|
+
}
|
|
743
915
|
if (quantifier.value.kind === 'ListRef') {
|
|
744
916
|
validateFunctionListRefArg(node, quantifier.value, errors, diagnostics, context);
|
|
745
917
|
}
|
|
746
918
|
}
|
|
747
|
-
function validateFunctionArgRequirements(node, errors, diagnostics) {
|
|
748
|
-
for (const signatureError of validateFunctionSignature(node)) {
|
|
919
|
+
function validateFunctionArgRequirements(node, binding, errors, diagnostics) {
|
|
920
|
+
for (const signatureError of validateFunctionSignature(node, binding)) {
|
|
749
921
|
pushFunctionSignatureError(signatureError, errors, diagnostics);
|
|
750
922
|
}
|
|
751
923
|
}
|
|
752
924
|
function pushFunctionSignatureError(signatureError, errors, diagnostics) {
|
|
753
925
|
errors.push(signatureError.message);
|
|
754
926
|
const span = argSpan(signatureError.node);
|
|
755
|
-
|
|
927
|
+
diagnostics.push({
|
|
756
928
|
id: signatureError.id,
|
|
757
929
|
...(signatureError.params ? { params: signatureError.params } : {}),
|
|
758
930
|
severity: 'error',
|
|
759
|
-
message: signatureError.message,
|
|
760
931
|
pos: span.pos,
|
|
761
932
|
length: span.length,
|
|
762
933
|
source: 'expression',
|
|
@@ -773,11 +944,16 @@ function argSpan(arg) {
|
|
|
773
944
|
}
|
|
774
945
|
return { pos: null, length: 0 };
|
|
775
946
|
}
|
|
776
|
-
function validateFieldNode(node, guardedHeaders, errors,
|
|
947
|
+
function validateFieldNode(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
777
948
|
validateAccessPath(node, errors, diagnostics);
|
|
949
|
+
const lowercaseKey = validateMapKeyCase(node, errors, diagnostics);
|
|
778
950
|
validateFieldAvailability(node, errors, diagnostics, expression, context);
|
|
779
|
-
validateDeprecatedField(node,
|
|
780
|
-
|
|
951
|
+
validateDeprecatedField(node, diagnostics, expression, context);
|
|
952
|
+
// A guard for a rejected key would only repeat its error.
|
|
953
|
+
if (!REQUEST_HEADER_MAPS.has(node.name) || !lowercaseKey)
|
|
954
|
+
return;
|
|
955
|
+
// An invalid segment may have been the header's key or index, which leaves the ones after it uncertain.
|
|
956
|
+
if (node.accessPath.some(access => access.kind === 'Invalid'))
|
|
781
957
|
return;
|
|
782
958
|
const mapKey = node.accessPath.find(access => access.kind === 'MapKeyAccess');
|
|
783
959
|
const arrayIndex = node.accessPath.find(access => access.kind === 'ArrayIndexAccess');
|
|
@@ -787,21 +963,19 @@ function validateFieldNode(node, guardedHeaders, errors, warnings, diagnostics,
|
|
|
787
963
|
if (guardedHeaders.has(headerName))
|
|
788
964
|
return;
|
|
789
965
|
const fieldExpression = expression.slice(node.pos, node.pos + node.length);
|
|
790
|
-
const guardExpression = `any(lower(
|
|
791
|
-
|
|
792
|
-
pushWarning(warnings, message);
|
|
793
|
-
pushDiagnostic(diagnostics, {
|
|
966
|
+
const guardExpression = `any(lower(${node.name}.names[*])[*] eq ${stringLiteralSource(headerName)})`;
|
|
967
|
+
diagnostics.push({
|
|
794
968
|
id: 'field.header_index_requires_guard',
|
|
795
969
|
params: { fieldExpression, guardExpression, headerName },
|
|
796
970
|
severity: 'warning',
|
|
797
|
-
message,
|
|
798
971
|
pos: node.pos,
|
|
799
972
|
length: node.length,
|
|
800
973
|
source: 'expression',
|
|
801
974
|
});
|
|
802
975
|
}
|
|
803
976
|
function validateAccessPath(node, errors, diagnostics) {
|
|
804
|
-
|
|
977
|
+
const info = inferRuleExpressionArgument(node);
|
|
978
|
+
for (const issue of info.invalid ? info.accessIssues : []) {
|
|
805
979
|
const accessLabel = issue.accessKind === 'map-key'
|
|
806
980
|
? 'map key'
|
|
807
981
|
: issue.accessKind === 'array-index' ? 'array index' : 'array wildcard';
|
|
@@ -809,7 +983,7 @@ function validateAccessPath(node, errors, diagnostics) {
|
|
|
809
983
|
const expectedArticle = issue.expectedType === 'Array' ? 'an' : 'a';
|
|
810
984
|
const message = `${accessLabel} access requires ${expectedArticle} ${issue.expectedType} value; received ${actualType}`;
|
|
811
985
|
errors.push(message);
|
|
812
|
-
|
|
986
|
+
diagnostics.push({
|
|
813
987
|
id: 'access.type_mismatch',
|
|
814
988
|
params: {
|
|
815
989
|
accessKind: issue.accessKind,
|
|
@@ -817,59 +991,174 @@ function validateAccessPath(node, errors, diagnostics) {
|
|
|
817
991
|
expectedType: issue.expectedType,
|
|
818
992
|
},
|
|
819
993
|
severity: 'error',
|
|
820
|
-
message,
|
|
821
994
|
pos: issue.access.pos ?? null,
|
|
822
995
|
length: issue.access.length ?? 0,
|
|
823
996
|
source: 'expression',
|
|
824
997
|
});
|
|
825
998
|
}
|
|
826
999
|
}
|
|
827
|
-
|
|
828
|
-
|
|
1000
|
+
/**
|
|
1001
|
+
* Map fields with lowercase keys, where Cloudflare rejects a key with an
|
|
1002
|
+
* uppercase letter, ASCII or not (dry runs, 2026-10-09). It accepts one for
|
|
1003
|
+
* raw.http.response.headers, and on a function's result, such as
|
|
1004
|
+
* coalesce(http.request.headers, ...)["X-A"], which reads nil, as the map's
|
|
1005
|
+
* keys are lowercase (2026-10-10).
|
|
1006
|
+
*/
|
|
1007
|
+
const LOWERCASE_KEY_MAP_FIELDS = new Set([
|
|
1008
|
+
'http.request.headers',
|
|
1009
|
+
'http.response.headers',
|
|
1010
|
+
'raw.http.request.headers',
|
|
1011
|
+
]);
|
|
1012
|
+
/** Returns false when it rejects the field's map key. */
|
|
1013
|
+
function validateMapKeyCase(node, errors, diagnostics) {
|
|
1014
|
+
const access = node.accessPath[0];
|
|
1015
|
+
if (!LOWERCASE_KEY_MAP_FIELDS.has(node.name) || access?.kind !== 'MapKeyAccess' || !/\p{Uppercase}/u.test(access.key))
|
|
1016
|
+
return true;
|
|
1017
|
+
errors.push(`map key '${access.key}' of field '${node.name}' must be lowercase`);
|
|
1018
|
+
diagnostics.push({
|
|
1019
|
+
id: 'access.uppercase_map_key',
|
|
1020
|
+
params: { fieldName: node.name, key: access.key },
|
|
1021
|
+
severity: 'error',
|
|
1022
|
+
pos: access.pos ?? null,
|
|
1023
|
+
length: access.length ?? 0,
|
|
1024
|
+
source: 'expression',
|
|
1025
|
+
});
|
|
1026
|
+
return false;
|
|
1027
|
+
}
|
|
1028
|
+
function validateBoolFieldNode(node, errors, diagnostics, expression, context) {
|
|
1029
|
+
// validateBooleanLiterals() reports `true` and `false`.
|
|
1030
|
+
if (isBooleanLiteralName(node.name))
|
|
829
1031
|
return;
|
|
830
1032
|
validateFieldAvailability(node, errors, diagnostics, expression, context);
|
|
831
|
-
validateDeprecatedField(node,
|
|
1033
|
+
validateDeprecatedField(node, diagnostics, expression, context);
|
|
1034
|
+
if (!isKnownFieldName(node.name))
|
|
1035
|
+
return;
|
|
1036
|
+
const type = fieldDataType(node.name);
|
|
1037
|
+
if (isUnknownRuleExpressionDataType(type) || isBooleanType(type))
|
|
1038
|
+
return;
|
|
1039
|
+
const actualType = formatRuleExpressionDataType(type);
|
|
1040
|
+
const message = `field '${node.name}' must be Boolean in predicate position; received ${actualType}`;
|
|
1041
|
+
errors.push(message);
|
|
1042
|
+
diagnostics.push({
|
|
1043
|
+
id: 'field.predicate_type_mismatch',
|
|
1044
|
+
params: { fieldName: node.name, expectedType: 'Boolean', actualType },
|
|
1045
|
+
severity: 'error',
|
|
1046
|
+
pos: typeof node.pos === 'number' ? node.pos : null,
|
|
1047
|
+
length: typeof node.length === 'number' ? node.length : 0,
|
|
1048
|
+
source: 'expression',
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Cloudflare has no Boolean literals. It accepts `true` only as an entire
|
|
1053
|
+
* expression, which matches every request.
|
|
1054
|
+
*/
|
|
1055
|
+
function validateBooleanLiterals(root, errors, diagnostics) {
|
|
1056
|
+
if (root.kind === 'BoolField' && root.name === RULE_EXPRESSION_MATCH_ALL_EXPRESSION)
|
|
1057
|
+
return;
|
|
1058
|
+
const visit = (node) => {
|
|
1059
|
+
switch (node.kind) {
|
|
1060
|
+
case 'BoolField':
|
|
1061
|
+
if (isBooleanLiteralName(node.name))
|
|
1062
|
+
pushBooleanLiteralError(node, node.name, errors, diagnostics);
|
|
1063
|
+
return;
|
|
1064
|
+
case 'BoolLiteral':
|
|
1065
|
+
pushBooleanLiteralError(node, String(node.value), errors, diagnostics);
|
|
1066
|
+
return;
|
|
1067
|
+
case 'Comparison':
|
|
1068
|
+
visit(node.left);
|
|
1069
|
+
if (node.right)
|
|
1070
|
+
visit(node.right);
|
|
1071
|
+
return;
|
|
1072
|
+
case 'Func':
|
|
1073
|
+
node.args.forEach(visit);
|
|
1074
|
+
return;
|
|
1075
|
+
case 'Not':
|
|
1076
|
+
visit(node.operand);
|
|
1077
|
+
return;
|
|
1078
|
+
case 'And':
|
|
1079
|
+
case 'Or':
|
|
1080
|
+
case 'Xor':
|
|
1081
|
+
node.operands.forEach(visit);
|
|
1082
|
+
return;
|
|
1083
|
+
case 'Invalid':
|
|
1084
|
+
if (node.operand)
|
|
1085
|
+
visit(node.operand);
|
|
1086
|
+
return;
|
|
1087
|
+
case 'Field':
|
|
1088
|
+
case 'StringLiteral':
|
|
1089
|
+
case 'NumberLiteral':
|
|
1090
|
+
case 'IpLiteral':
|
|
1091
|
+
case 'SetLiteral':
|
|
1092
|
+
case 'ListRef':
|
|
1093
|
+
return;
|
|
1094
|
+
default:
|
|
1095
|
+
unhandledNode(node);
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
visit(root);
|
|
1099
|
+
}
|
|
1100
|
+
function pushBooleanLiteralError(node, literal, errors, diagnostics) {
|
|
1101
|
+
const message = `'${literal}' is not a value; Cloudflare accepts 'true' only as the entire expression`;
|
|
1102
|
+
errors.push(message);
|
|
1103
|
+
diagnostics.push({
|
|
1104
|
+
id: 'rule_expression.boolean_literal',
|
|
1105
|
+
params: { literal },
|
|
1106
|
+
severity: 'error',
|
|
1107
|
+
pos: typeof node.pos === 'number' ? node.pos : null,
|
|
1108
|
+
length: typeof node.length === 'number' ? node.length : 0,
|
|
1109
|
+
source: 'expression',
|
|
1110
|
+
});
|
|
832
1111
|
}
|
|
833
|
-
function validateDeprecatedField(node,
|
|
1112
|
+
function validateDeprecatedField(node, diagnostics, expression, context) {
|
|
834
1113
|
if (!context.deprecationNotices || !KNOWN_DEPRECATED_FIELDS.has(node.name))
|
|
835
1114
|
return;
|
|
836
|
-
const replacement =
|
|
1115
|
+
const replacement = availableReplacement(node.name, context.fieldAvailability);
|
|
837
1116
|
const fieldExpression = expressionForNode(node, expression);
|
|
838
|
-
|
|
839
|
-
? `field '${fieldExpression}' is deprecated; use '${replacement}' instead`
|
|
840
|
-
: `field '${fieldExpression}' is deprecated`;
|
|
841
|
-
pushWarning(warnings, message);
|
|
842
|
-
pushDiagnostic(diagnostics, {
|
|
1117
|
+
diagnostics.push({
|
|
843
1118
|
id: 'field.deprecated',
|
|
844
1119
|
params: replacement
|
|
845
1120
|
? { fieldExpression, replacement }
|
|
846
1121
|
: { fieldExpression },
|
|
847
1122
|
severity: 'notice',
|
|
848
|
-
message,
|
|
849
1123
|
pos: typeof node.pos === 'number' ? node.pos : null,
|
|
850
1124
|
length: typeof node.length === 'number' ? node.length : 0,
|
|
851
1125
|
source: 'expression',
|
|
852
1126
|
});
|
|
853
1127
|
}
|
|
1128
|
+
/**
|
|
1129
|
+
* The replacement of a deprecated field, where it is available. Cloudflare
|
|
1130
|
+
* judges an alias by its own verdict, so a replacement can be unavailable
|
|
1131
|
+
* where the alias is not.
|
|
1132
|
+
*/
|
|
1133
|
+
function availableReplacement(field, rules) {
|
|
1134
|
+
const replacement = deprecatedFieldReplacement(field);
|
|
1135
|
+
if (!replacement || !rules || isRuleExpressionFieldAvailable({ name: replacement }, rules))
|
|
1136
|
+
return replacement;
|
|
1137
|
+
return undefined;
|
|
1138
|
+
}
|
|
854
1139
|
function validateFieldAvailability(node, errors, diagnostics, expression, context) {
|
|
855
|
-
|
|
856
|
-
if (!rules)
|
|
857
|
-
return;
|
|
1140
|
+
// Unknown fields are invalid in every product, whatever its field availability.
|
|
858
1141
|
if (!isKnownFieldName(node.name)) {
|
|
859
1142
|
pushUnknownFieldError(node, errors, diagnostics, expression);
|
|
860
1143
|
return;
|
|
861
1144
|
}
|
|
862
|
-
const
|
|
1145
|
+
const rules = context.fieldAvailability;
|
|
1146
|
+
if (!rules)
|
|
1147
|
+
return;
|
|
1148
|
+
const accessPath = isFieldNode(node) ? node.accessPath : [];
|
|
1149
|
+
const map = accessPath.some(access => access.kind === 'MapKeyAccess');
|
|
863
1150
|
if (isRuleExpressionFieldAvailable({ name: node.name, map }, rules))
|
|
864
1151
|
return;
|
|
1152
|
+
// An invalid segment may have been a map key, so the field is unavailable only if it is either way.
|
|
1153
|
+
if (accessPath.some(access => access.kind === 'Invalid') && isRuleExpressionFieldAvailable({ name: node.name, map: true }, rules))
|
|
1154
|
+
return;
|
|
865
1155
|
const fieldExpression = expressionForNode(node, expression);
|
|
866
1156
|
const message = `field '${fieldExpression}' is not available for ${rules.preset} expressions`;
|
|
867
1157
|
errors.push(message);
|
|
868
|
-
|
|
1158
|
+
diagnostics.push({
|
|
869
1159
|
id: 'field.unavailable',
|
|
870
1160
|
params: { fieldExpression, fieldName: node.name, preset: rules.preset },
|
|
871
1161
|
severity: 'error',
|
|
872
|
-
message,
|
|
873
1162
|
pos: typeof node.pos === 'number' ? node.pos : null,
|
|
874
1163
|
length: typeof node.length === 'number' ? node.length : 0,
|
|
875
1164
|
source: 'expression',
|
|
@@ -879,11 +1168,10 @@ function pushUnknownFieldError(node, errors, diagnostics, expression) {
|
|
|
879
1168
|
const fieldExpression = expressionForNode(node, expression);
|
|
880
1169
|
const message = `unknown field '${fieldExpression}'`;
|
|
881
1170
|
errors.push(message);
|
|
882
|
-
|
|
1171
|
+
diagnostics.push({
|
|
883
1172
|
id: 'field.unknown',
|
|
884
1173
|
params: { fieldExpression, fieldName: node.name },
|
|
885
1174
|
severity: 'error',
|
|
886
|
-
message,
|
|
887
1175
|
pos: typeof node.pos === 'number' ? node.pos : null,
|
|
888
1176
|
length: typeof node.length === 'number' ? node.length : 0,
|
|
889
1177
|
source: 'expression',
|
|
@@ -894,19 +1182,21 @@ function expressionForNode(node, expression) {
|
|
|
894
1182
|
? expression.slice(node.pos, node.pos + node.length)
|
|
895
1183
|
: node.name;
|
|
896
1184
|
}
|
|
897
|
-
function deprecatedFieldReplacement(field) {
|
|
898
|
-
if (!KNOWN_DEPRECATED_FIELDS.has(field))
|
|
899
|
-
return undefined;
|
|
900
|
-
return DEPRECATED_FIELD_REPLACEMENTS[field];
|
|
901
|
-
}
|
|
902
1185
|
function collectHeaderPresenceGuards(node) {
|
|
903
1186
|
switch (node.kind) {
|
|
904
1187
|
case 'And':
|
|
905
1188
|
return node.operands.reduce((guards, operand) => unionHeaderGuards(guards, collectHeaderPresenceGuards(operand)), new Set());
|
|
906
1189
|
case 'Func':
|
|
907
1190
|
return getHeaderPresenceGuardNames(node);
|
|
908
|
-
|
|
1191
|
+
case 'Comparison':
|
|
1192
|
+
case 'BoolField':
|
|
1193
|
+
case 'Or':
|
|
1194
|
+
case 'Xor':
|
|
1195
|
+
case 'Not':
|
|
1196
|
+
case 'Invalid':
|
|
909
1197
|
return new Set();
|
|
1198
|
+
default:
|
|
1199
|
+
return unhandledNode(node);
|
|
910
1200
|
}
|
|
911
1201
|
}
|
|
912
1202
|
function getHeaderPresenceGuardNames(node) {
|
|
@@ -924,22 +1214,23 @@ function getHeaderPresenceGuardNames(node) {
|
|
|
924
1214
|
names.add(quantifier.value.value.toLowerCase());
|
|
925
1215
|
}
|
|
926
1216
|
if (quantifier.op === 'in' && quantifier.value.kind === 'SetLiteral') {
|
|
927
|
-
for (const
|
|
928
|
-
if (
|
|
929
|
-
names.add(value.toLowerCase());
|
|
1217
|
+
for (const element of quantifier.value.elements) {
|
|
1218
|
+
if (element.type === 'string')
|
|
1219
|
+
names.add(element.value.toLowerCase());
|
|
930
1220
|
}
|
|
931
1221
|
}
|
|
932
1222
|
return names;
|
|
933
1223
|
}
|
|
1224
|
+
/**
|
|
1225
|
+
* Whether a guard reads request header names, plain or through lower(). A
|
|
1226
|
+
* guard on either names field counts for both header maps: Cloudflare
|
|
1227
|
+
* documents only the raw fields as unaffected by transforms, but live
|
|
1228
|
+
* requests showed the plain ones list the headers from before them too
|
|
1229
|
+
* (2026-10-09).
|
|
1230
|
+
*/
|
|
934
1231
|
function isHeaderNamesCollection(node) {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
}
|
|
938
|
-
if (isFuncNode(node) && node.name === 'lower' && isArrayExpandedNode(node)) {
|
|
939
|
-
const arg = node.args[0];
|
|
940
|
-
return isFieldNode(arg) && isArrayExpandedNode(arg) && arg.name === 'http.request.headers.names';
|
|
941
|
-
}
|
|
942
|
-
return false;
|
|
1232
|
+
const names = isFuncNode(node) && node.name === 'lower' && isArrayExpandedNode(node) ? node.args[0] : node;
|
|
1233
|
+
return isFieldNode(names) && isArrayExpandedNode(names) && REQUEST_HEADER_ARRAYS.get(names.name) === 'names';
|
|
943
1234
|
}
|
|
944
1235
|
function unionHeaderGuards(left, right) {
|
|
945
1236
|
return new Set([...left, ...right]);
|