@libreflare/worker-runtime 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +235 -13
- package/dist/rule-evaluator/accept-encoding.d.ts +2 -2
- package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
- package/dist/rule-evaluator/accept-encoding.js +62 -15
- package/dist/rule-evaluator/accept-encoding.js.map +1 -1
- package/dist/rule-evaluator/access-values.d.ts +77 -14
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +149 -17
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/analysis.d.ts +43 -8
- package/dist/rule-evaluator/analysis.d.ts.map +1 -1
- package/dist/rule-evaluator/analysis.js +72 -29
- package/dist/rule-evaluator/analysis.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +145 -11
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js +102 -4
- 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 +1 -1
- package/dist/rule-evaluator/constants.d.ts.map +1 -1
- package/dist/rule-evaluator/constants.js +1 -0
- 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 +9 -0
- package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
- package/dist/rule-evaluator/custom-lists.js +43 -0
- package/dist/rule-evaluator/custom-lists.js.map +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts +1 -3
- package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
- package/dist/rule-evaluator/diagnostics.js +10 -53
- 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 +1 -1
- package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator/evaluator.js +16 -15
- package/dist/rule-evaluator/evaluator.js.map +1 -1
- package/dist/rule-evaluator/explain.d.ts.map +1 -1
- package/dist/rule-evaluator/explain.js +35 -20
- 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 +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +1 -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 +25 -2
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +276 -119
- 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 +50 -7
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +98 -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 +26 -2
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +311 -181
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +24 -5
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +353 -190
- 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.map +1 -1
- package/dist/rule-evaluator/minifier.js +16 -20
- package/dist/rule-evaluator/minifier.js.map +1 -1
- package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
- package/dist/rule-evaluator/normalizer.js +6 -6
- package/dist/rule-evaluator/normalizer.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +11 -4
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +62 -35
- package/dist/rule-evaluator/operators.js.map +1 -1
- package/dist/rule-evaluator/outcome.d.ts +2 -0
- package/dist/rule-evaluator/outcome.d.ts.map +1 -1
- package/dist/rule-evaluator/outcome.js +4 -0
- package/dist/rule-evaluator/outcome.js.map +1 -1
- 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 +318 -11
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +1314 -333
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +1 -1
- 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 +80 -21
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +228 -67
- 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 +24 -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.map +1 -1
- package/dist/rule-evaluator/validation.js +509 -251
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator.d.ts +2 -2
- package/dist/rule-evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator.js.map +1 -1
- package/package.json +5 -2
- package/dist/rule-evaluator/comparison.d.ts +0 -16
- package/dist/rule-evaluator/comparison.d.ts.map +0 -1
- package/dist/rule-evaluator/comparison.js +0 -43
- package/dist/rule-evaluator/comparison.js.map +0 -1
- 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/values.d.ts +0 -26
- package/dist/rule-evaluator/values.d.ts.map +0 -1
- package/dist/rule-evaluator/values.js +0 -55
- package/dist/rule-evaluator/values.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,25 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EQUALITY_ORDERING_OPERATORS, isDynamicValueNode,
|
|
1
|
+
import { ruleExpressionDiagnostic, } from './diagnostics.js';
|
|
2
|
+
import { EQUALITY_ORDERING_OPERATORS, isBooleanLiteralName, isConditionArgument, isDynamicValueNode, isEmptyArgument, isExpressionNode, isUncertainArgument, mayHaveFewerArguments, unhandledNode, validSetElements, } from './ast.js';
|
|
3
3
|
import { RULE_EXPRESSION_MATCH_ALL_EXPRESSION } from './constants.js';
|
|
4
|
+
import { isRuleExpressionFunctionAvailable, isRuleExpressionOperatorAvailable } from './function-availability.js';
|
|
4
5
|
import { getUnreliableRuleExpressionFunctionReason } from './functions.js';
|
|
5
|
-
import { validateFunctionSignature } from './function-signature-validation.js';
|
|
6
|
+
import { bindArguments, validateFunctionSignature, } from './function-signature-validation.js';
|
|
6
7
|
import { minifyRuleExpression } from './minifier.js';
|
|
7
8
|
import { parseExpression } from './parse-cache.js';
|
|
9
|
+
import { hasRuleExpressionSyntaxErrors } from './transformation.js';
|
|
8
10
|
import { isKnownRuleExpressionFieldName, isRuleExpressionFieldAvailable, resolveRuleExpressionFieldAvailability, } from './field-availability.js';
|
|
9
|
-
import {
|
|
11
|
+
import { deprecatedFieldReplacement, KNOWN_DEPRECATED_FIELDS, REQUEST_HEADER_ARRAYS, REQUEST_HEADER_MAPS } from './fields.js';
|
|
10
12
|
import { isRuleExpressionCountryCode } from './country-codes.js';
|
|
11
13
|
import { getRuleExpressionFieldValueDomain } from './field-value-domains.js';
|
|
12
14
|
import { customList, isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
|
|
13
|
-
import { formatRuleExpressionDataType, isArrayExpandedNode,
|
|
15
|
+
import { fieldDataType, formatRuleExpressionDataType, isArrayExpandedNode, isBooleanType, isConcreteComparisonFamily, isFieldNode, isFuncNode, inferFunctionInvocation, inferRuleExpressionArgument, isIpAddressValueType, isLiteralNode, isStringLiteral, isUnknownRuleExpressionDataType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, singleValueType, } from './rule-language-types.js';
|
|
14
16
|
import { extractQuantifierComparison, extractQuantifierPredicate } from './quantifier.js';
|
|
17
|
+
import { stringLiteralSource } from './tokenizer.js';
|
|
15
18
|
export const MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH = 4096;
|
|
16
19
|
export const RULE_EXPRESSION_VALIDATION_PRESETS = [
|
|
17
20
|
'url-rewrite-filter',
|
|
18
21
|
'url-rewrite-target',
|
|
19
22
|
'logging-source-key',
|
|
20
23
|
];
|
|
21
|
-
|
|
22
|
-
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']);
|
|
23
30
|
const SUPPORTED_LIST_KIND = 'ip';
|
|
24
31
|
export function validateRuleExpressionSyntax(source, config = {}) {
|
|
25
32
|
const minify = config?.minify ?? true;
|
|
@@ -28,7 +35,6 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
28
35
|
const preset = config.preset ?? 'url-rewrite-filter';
|
|
29
36
|
const expression = source.trim();
|
|
30
37
|
const errors = [];
|
|
31
|
-
const warnings = [];
|
|
32
38
|
const diagnostics = [];
|
|
33
39
|
const semanticContext = {
|
|
34
40
|
preset,
|
|
@@ -40,7 +46,7 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
40
46
|
if (!expression) {
|
|
41
47
|
const message = 'rule expression is required';
|
|
42
48
|
errors.push(message);
|
|
43
|
-
|
|
49
|
+
diagnostics.push({
|
|
44
50
|
id: 'rule_expression.required',
|
|
45
51
|
severity: 'error',
|
|
46
52
|
pos: null,
|
|
@@ -48,29 +54,25 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
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, 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
|
-
|
|
66
|
-
pushDiagnostic(diagnostics, { ...diagnostic, source: 'expression' });
|
|
67
|
-
collectValidationDiagnostics(errors, warnings, diagnostics, minifiedParseDiagnostics, 'minifiedExpression');
|
|
68
|
+
collectSemanticValidationDiagnostics(errors, diagnostics, parsedExpression, expression, semanticContext);
|
|
69
|
+
collectValidationDiagnostics(errors, diagnostics, minifiedParseDiagnostics, 'minifiedExpression');
|
|
68
70
|
if ((config.enforceMaxLength ?? true) && lengthCheckedExpression.length > maxLength) {
|
|
69
71
|
const expressionLabel = minify ? 'minified rule expression' : 'rule expression';
|
|
70
72
|
const limitLabel = maxLength === MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH ? 'Cloudflare limit' : 'configured limit';
|
|
71
73
|
const message = `${expressionLabel} is ${lengthCheckedExpression.length} characters; ${limitLabel} is ${maxLength}`;
|
|
72
74
|
errors.push(message);
|
|
73
|
-
|
|
75
|
+
diagnostics.push({
|
|
74
76
|
id: 'rule_expression.too_long',
|
|
75
77
|
params: {
|
|
76
78
|
actualLength: lengthCheckedExpression.length,
|
|
@@ -95,8 +97,8 @@ export function validateRuleExpressionSyntax(source, config = {}) {
|
|
|
95
97
|
return validation;
|
|
96
98
|
return {
|
|
97
99
|
...validation,
|
|
98
|
-
minifiedExpression
|
|
99
|
-
minifiedLength:
|
|
100
|
+
minifiedExpression,
|
|
101
|
+
minifiedLength: minifiedExpression.length,
|
|
100
102
|
};
|
|
101
103
|
}
|
|
102
104
|
/**
|
|
@@ -120,17 +122,16 @@ const evaluationErrors = new WeakMap();
|
|
|
120
122
|
* result is cached with the parsed expression.
|
|
121
123
|
*/
|
|
122
124
|
export function ruleExpressionEvaluationErrors(expression) {
|
|
123
|
-
const parseWarnings = [];
|
|
124
125
|
const parseDiagnostics = [];
|
|
125
|
-
const node = parseExpression(expression,
|
|
126
|
+
const node = parseExpression(expression, parseDiagnostics);
|
|
126
127
|
const cached = evaluationErrors.get(node);
|
|
127
128
|
if (cached)
|
|
128
129
|
return cached;
|
|
129
130
|
const errors = [];
|
|
130
131
|
const diagnostics = [];
|
|
131
|
-
collectValidationDiagnostics(errors,
|
|
132
|
-
collectSemanticValidationDiagnostics(errors,
|
|
133
|
-
preset:
|
|
132
|
+
collectValidationDiagnostics(errors, diagnostics, parseDiagnostics, 'expression');
|
|
133
|
+
collectSemanticValidationDiagnostics(errors, diagnostics, node, expression, {
|
|
134
|
+
preset: null,
|
|
134
135
|
fieldAvailability: null,
|
|
135
136
|
rejectListReferences: false,
|
|
136
137
|
deprecationNotices: false,
|
|
@@ -228,7 +229,7 @@ function isKnownFieldName(field) {
|
|
|
228
229
|
}
|
|
229
230
|
function pushValidationConfigError(message, errors, diagnostics, metadata) {
|
|
230
231
|
errors.push(message);
|
|
231
|
-
|
|
232
|
+
diagnostics.push({
|
|
232
233
|
...metadata,
|
|
233
234
|
severity: 'error',
|
|
234
235
|
pos: null,
|
|
@@ -236,100 +237,120 @@ function pushValidationConfigError(message, errors, diagnostics, metadata) {
|
|
|
236
237
|
source: 'expression',
|
|
237
238
|
});
|
|
238
239
|
}
|
|
239
|
-
function collectValidationDiagnostics(errors,
|
|
240
|
+
function collectValidationDiagnostics(errors, diagnostics, messageDiagnostics, source) {
|
|
240
241
|
for (const diagnostic of messageDiagnostics) {
|
|
241
|
-
|
|
242
|
-
|
|
242
|
+
if (diagnostic.severity === 'error')
|
|
243
|
+
errors.push(diagnostic.message);
|
|
244
|
+
diagnostics.push(ruleExpressionDiagnostic(diagnostic, source));
|
|
243
245
|
}
|
|
244
246
|
}
|
|
245
|
-
function
|
|
246
|
-
if (severity === 'error')
|
|
247
|
-
errors.push(message);
|
|
248
|
-
else
|
|
249
|
-
warnings.push(message);
|
|
250
|
-
}
|
|
251
|
-
function collectSemanticValidationDiagnostics(errors, warnings, diagnostics, node, expression, context) {
|
|
247
|
+
function collectSemanticValidationDiagnostics(errors, diagnostics, node, expression, context) {
|
|
252
248
|
const rootPosition = context.preset === 'url-rewrite-target' ? 'value' : 'predicate';
|
|
253
249
|
validateBooleanLiterals(node, errors, diagnostics);
|
|
254
|
-
validateSemanticNode(node, new Set(), errors,
|
|
255
|
-
|
|
256
|
-
validateRewriteTargetFunctionUsage(node, errors, diagnostics);
|
|
250
|
+
validateSemanticNode(node, new Set(), errors, diagnostics, expression, context, rootPosition);
|
|
251
|
+
validateFunctionUsage(node, context, errors, diagnostics);
|
|
257
252
|
}
|
|
258
|
-
function validateSemanticNode(node, guardedHeaders, errors,
|
|
253
|
+
function validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, position) {
|
|
259
254
|
switch (node.kind) {
|
|
260
255
|
case 'And': {
|
|
261
256
|
const andGuards = unionHeaderGuards(guardedHeaders, collectHeaderPresenceGuards(node));
|
|
262
257
|
for (const operand of node.operands) {
|
|
263
|
-
validateSemanticNode(operand, andGuards, errors,
|
|
258
|
+
validateSemanticNode(operand, andGuards, errors, diagnostics, expression, context, 'predicate');
|
|
264
259
|
}
|
|
265
260
|
return;
|
|
266
261
|
}
|
|
267
262
|
case 'Or':
|
|
268
263
|
case 'Xor':
|
|
269
264
|
for (const operand of node.operands) {
|
|
270
|
-
validateSemanticNode(operand, guardedHeaders, errors,
|
|
265
|
+
validateSemanticNode(operand, guardedHeaders, errors, diagnostics, expression, context, 'predicate');
|
|
271
266
|
}
|
|
272
267
|
return;
|
|
273
268
|
case 'Not':
|
|
274
|
-
validateSemanticNode(node.operand, guardedHeaders, errors,
|
|
269
|
+
validateSemanticNode(node.operand, guardedHeaders, errors, diagnostics, expression, context, 'predicate');
|
|
275
270
|
return;
|
|
276
271
|
case 'Comparison':
|
|
277
|
-
validateFieldOrFunc(node.left, guardedHeaders, errors,
|
|
272
|
+
validateFieldOrFunc(node.left, guardedHeaders, errors, diagnostics, expression, context);
|
|
278
273
|
if (isDynamicValueNode(node.right)) {
|
|
279
|
-
validateFieldOrFunc(node.right, guardedHeaders, errors,
|
|
274
|
+
validateFieldOrFunc(node.right, guardedHeaders, errors, diagnostics, expression, context);
|
|
280
275
|
}
|
|
281
276
|
validateComparisonRequirements(node, errors, diagnostics, context);
|
|
282
277
|
return;
|
|
283
|
-
case 'Func':
|
|
278
|
+
case 'Func': {
|
|
284
279
|
if (position === 'predicate')
|
|
285
280
|
validateFunctionPredicateResult(node, errors, diagnostics);
|
|
281
|
+
validateFunctionAvailability(node, errors, diagnostics, context);
|
|
286
282
|
validateAccessPath(node, errors, diagnostics);
|
|
287
|
-
validateFunctionSupport(node,
|
|
288
|
-
|
|
283
|
+
validateFunctionSupport(node, diagnostics);
|
|
284
|
+
const binding = bindArguments(node);
|
|
285
|
+
validateFunctionArgRequirements(node, binding, errors, diagnostics);
|
|
289
286
|
if (node.name === 'coalesce')
|
|
290
|
-
validateCoalesceFallbacks(node,
|
|
287
|
+
validateCoalesceFallbacks(node, binding, diagnostics);
|
|
291
288
|
if (node.name === 'any' || node.name === 'all') {
|
|
292
|
-
validateQuantifierPredicateArgs(node, guardedHeaders, errors,
|
|
289
|
+
validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
293
290
|
}
|
|
294
291
|
else {
|
|
295
|
-
validateFuncArgs(node, guardedHeaders, errors,
|
|
292
|
+
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
296
293
|
}
|
|
297
294
|
return;
|
|
295
|
+
}
|
|
298
296
|
case 'BoolField':
|
|
299
|
-
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);
|
|
300
303
|
return;
|
|
304
|
+
default:
|
|
305
|
+
unhandledNode(node);
|
|
301
306
|
}
|
|
302
307
|
}
|
|
303
308
|
function validateFunctionPredicateResult(node, errors, diagnostics) {
|
|
304
|
-
const
|
|
305
|
-
if (
|
|
306
|
-
return;
|
|
307
|
-
if (result.type.kind === 'scalar' && result.type.name === 'Boolean')
|
|
309
|
+
const type = conditionResultType(node);
|
|
310
|
+
if (!type || isUnknownRuleExpressionDataType(type) || isBooleanType(type))
|
|
308
311
|
return;
|
|
309
|
-
const actualType = formatRuleExpressionDataType(
|
|
312
|
+
const actualType = formatRuleExpressionDataType(type);
|
|
310
313
|
pushFuncError(node, `function '${node.name}' result must be Boolean in predicate position; received ${actualType}`, errors, diagnostics, {
|
|
311
314
|
id: 'function.result_type_mismatch',
|
|
312
315
|
params: { functionName: node.name, expectedType: 'Boolean', actualType },
|
|
313
316
|
});
|
|
314
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
|
+
}
|
|
315
331
|
function validateComparisonRequirements(node, errors, diagnostics, context) {
|
|
316
332
|
let invalidArrayComparison = false;
|
|
317
333
|
const leftInfo = inferRuleExpressionArgument(node.left);
|
|
318
|
-
|
|
334
|
+
const leftExpanded = !leftInfo.invalid && leftInfo.expanded;
|
|
335
|
+
if (isFieldNode(node.left) && leftExpanded) {
|
|
319
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' });
|
|
320
337
|
invalidArrayComparison = true;
|
|
321
338
|
}
|
|
322
|
-
if (node.left.kind === 'Func' &&
|
|
339
|
+
if (node.left.kind === 'Func' && leftExpanded) {
|
|
323
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' });
|
|
324
341
|
invalidArrayComparison = true;
|
|
325
342
|
}
|
|
326
|
-
|
|
327
|
-
if (isDynamicValueNode(node.right) && rightInfo?.expanded && rightInfo.accessIssues.length === 0) {
|
|
343
|
+
if (isArrayExpandedNode(node.right)) {
|
|
328
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' });
|
|
329
345
|
invalidArrayComparison = true;
|
|
330
346
|
}
|
|
331
|
-
|
|
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);
|
|
332
352
|
return;
|
|
353
|
+
}
|
|
333
354
|
// Only equality and ordering operators compare dynamic values; `in` reports its own list requirement.
|
|
334
355
|
if (isDynamicValueNode(node.right) && node.op !== 'in' && !EQUALITY_ORDERING_OPERATORS.has(node.op)) {
|
|
335
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 } });
|
|
@@ -351,16 +372,15 @@ function validateComparisonRequirements(node, errors, diagnostics, context) {
|
|
|
351
372
|
validateWildcardPattern(node.right, errors, diagnostics);
|
|
352
373
|
}
|
|
353
374
|
validateComparisonValueDomain(node, errors, diagnostics);
|
|
354
|
-
if (!invalidArrayComparison
|
|
355
|
-
validateComparisonTypeCompatibility(node, errors, diagnostics);
|
|
356
|
-
}
|
|
375
|
+
if (!invalidArrayComparison)
|
|
376
|
+
validateComparisonTypeCompatibility(node, node.right, errors, diagnostics);
|
|
357
377
|
}
|
|
358
378
|
function validateComparisonValueDomain(node, errors, diagnostics) {
|
|
359
379
|
if (!isFieldNode(node.left) || (node.op !== 'eq' && node.op !== 'ne' && node.op !== 'in'))
|
|
360
380
|
return;
|
|
361
381
|
if (getRuleExpressionFieldValueDomain(node.left.name) !== 'country-code')
|
|
362
382
|
return;
|
|
363
|
-
const right = node.right;
|
|
383
|
+
const right = node.right?.kind === 'StringLiteral' || node.right?.kind === 'SetLiteral' ? node.right : undefined;
|
|
364
384
|
for (const value of stringLiteralValues(right)) {
|
|
365
385
|
if (isRuleExpressionCountryCode(value))
|
|
366
386
|
continue;
|
|
@@ -375,25 +395,28 @@ function validateComparisonValueDomain(node, errors, diagnostics) {
|
|
|
375
395
|
function stringLiteralValues(node) {
|
|
376
396
|
if (node?.kind === 'StringLiteral')
|
|
377
397
|
return [node.value];
|
|
378
|
-
|
|
379
|
-
return [];
|
|
380
|
-
return node.elements.flatMap(element => element.type === 'string' ? [element.value] : []);
|
|
398
|
+
return node?.elements.flatMap(element => element.type === 'string' ? [element.value] : []) ?? [];
|
|
381
399
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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')
|
|
385
409
|
return;
|
|
386
|
-
if (
|
|
387
|
-
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' } });
|
|
388
412
|
return;
|
|
389
413
|
}
|
|
390
414
|
}
|
|
391
|
-
|
|
415
|
+
const leftInfo = inferRuleExpressionArgument(node.left);
|
|
416
|
+
if (leftInfo.invalid)
|
|
392
417
|
return;
|
|
393
|
-
const leftType =
|
|
394
|
-
const rightType = inferRuleExpressionArgument(node.right).type;
|
|
418
|
+
const leftType = leftInfo.type;
|
|
395
419
|
const leftFamily = ruleExpressionComparisonTypeFamily(leftType);
|
|
396
|
-
const rightFamily = ruleExpressionComparisonTypeFamily(rightType);
|
|
397
420
|
if (leftFamily === 'unsupported') {
|
|
398
421
|
const leftTypeLabel = formatRuleExpressionDataType(leftType);
|
|
399
422
|
pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftTypeLabel} values`, errors, diagnostics, {
|
|
@@ -402,39 +425,37 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
402
425
|
});
|
|
403
426
|
return;
|
|
404
427
|
}
|
|
405
|
-
|
|
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') {
|
|
406
432
|
const rightTypeLabel = formatRuleExpressionDataType(rightType);
|
|
407
|
-
pushComparisonTypeError(
|
|
433
|
+
pushComparisonTypeError(right, `operator '${node.op}' does not support ${rightTypeLabel} values`, errors, diagnostics, {
|
|
408
434
|
id: 'operator.unsupported_type',
|
|
409
435
|
params: { operator: node.op, side: 'right', valueType: rightTypeLabel },
|
|
410
436
|
});
|
|
411
437
|
return;
|
|
412
438
|
}
|
|
413
|
-
if (!isConcreteComparisonFamily(leftFamily) || !
|
|
439
|
+
if (!isConcreteComparisonFamily(leftFamily) || !validateLeftOperandType(node, leftFamily, errors, diagnostics))
|
|
440
|
+
return;
|
|
441
|
+
if (!right || !isConcreteComparisonFamily(rightFamily))
|
|
414
442
|
return;
|
|
415
443
|
const leftLabel = comparisonFamilyLabel(leftFamily);
|
|
416
444
|
const rightLabel = comparisonFamilyLabel(rightFamily);
|
|
417
|
-
const valueIsInlineSet =
|
|
445
|
+
const valueIsInlineSet = right.kind === 'SetLiteral';
|
|
418
446
|
const rightValueLabel = valueIsInlineSet ? `inline list of ${rightLabel}` : rightLabel;
|
|
419
447
|
switch (node.op) {
|
|
420
448
|
case 'eq':
|
|
421
449
|
case 'ne':
|
|
422
|
-
if (!isEqualityFamily(leftFamily)) {
|
|
423
|
-
pushComparisonTypeError(node.left, `operator '${node.op}' does not support ${leftLabel} values`, errors, diagnostics, {
|
|
424
|
-
id: 'operator.unsupported_type',
|
|
425
|
-
params: { operator: node.op, side: 'left', valueType: leftLabel },
|
|
426
|
-
});
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
429
450
|
if (!isEqualityFamily(rightFamily)) {
|
|
430
|
-
pushComparisonTypeError(
|
|
451
|
+
pushComparisonTypeError(right, `operator '${node.op}' does not support ${rightValueLabel}`, errors, diagnostics, {
|
|
431
452
|
id: 'operator.unsupported_type',
|
|
432
453
|
params: { operator: node.op, side: 'right', valueType: rightLabel, valueIsInlineSet },
|
|
433
454
|
});
|
|
434
455
|
return;
|
|
435
456
|
}
|
|
436
457
|
if (leftFamily !== rightFamily) {
|
|
437
|
-
pushComparisonTypeError(
|
|
458
|
+
pushComparisonTypeError(right, `operator '${node.op}' cannot compare ${leftLabel} with ${rightValueLabel}`, errors, diagnostics, {
|
|
438
459
|
id: 'operator.type_mismatch',
|
|
439
460
|
params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
|
|
440
461
|
});
|
|
@@ -444,15 +465,10 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
444
465
|
case 'le':
|
|
445
466
|
case 'gt':
|
|
446
467
|
case 'ge':
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
});
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
if (!isOrderingFamily(rightFamily) || leftFamily !== rightFamily) {
|
|
455
|
-
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, {
|
|
456
472
|
id: 'operator.type_mismatch',
|
|
457
473
|
params: { operator: node.op, leftType: leftLabel, rightType: rightLabel, valueIsInlineSet },
|
|
458
474
|
});
|
|
@@ -462,35 +478,51 @@ function validateComparisonTypeCompatibility(node, errors, diagnostics) {
|
|
|
462
478
|
case 'matches':
|
|
463
479
|
case 'wildcard':
|
|
464
480
|
case 'strict_wildcard':
|
|
465
|
-
if (leftFamily !== 'string') {
|
|
466
|
-
pushComparisonTypeError(node.left, `operator '${node.op}' requires String values; left side is ${leftLabel}`, errors, diagnostics, {
|
|
467
|
-
id: 'operator.requires_type',
|
|
468
|
-
params: { operator: node.op, side: 'left', requiredTypes: ['String'], actualType: leftLabel },
|
|
469
|
-
});
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
481
|
if (rightFamily !== 'string') {
|
|
473
|
-
pushComparisonTypeError(
|
|
482
|
+
pushComparisonTypeError(right, `operator '${node.op}' requires String values; right side is ${rightValueLabel}`, errors, diagnostics, {
|
|
474
483
|
id: 'operator.requires_type',
|
|
475
484
|
params: { operator: node.op, side: 'right', requiredTypes: ['String'], actualType: rightLabel, valueIsInlineSet },
|
|
476
485
|
});
|
|
477
486
|
}
|
|
478
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':
|
|
479
496
|
case 'in':
|
|
480
|
-
if (
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
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;
|
|
494
526
|
}
|
|
495
527
|
}
|
|
496
528
|
function isEqualityFamily(family) {
|
|
@@ -510,55 +542,106 @@ function comparisonFamilyLabel(family) {
|
|
|
510
542
|
function pushComparisonTypeError(node, message, errors, diagnostics, metadata) {
|
|
511
543
|
pushValueError(node, message, errors, diagnostics, metadata);
|
|
512
544
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
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
|
+
});
|
|
524
578
|
}
|
|
525
579
|
}
|
|
526
|
-
|
|
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
|
+
});
|
|
587
|
+
}
|
|
588
|
+
}
|
|
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) {
|
|
527
603
|
switch (node.kind) {
|
|
528
604
|
case 'Func': {
|
|
529
605
|
const functionName = node.name;
|
|
530
|
-
|
|
531
|
-
|
|
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);
|
|
532
622
|
}
|
|
533
|
-
const parentReplacementFunction = replacementFunctionStack.at(-1);
|
|
534
|
-
if (parentReplacementFunction && URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)) {
|
|
535
|
-
pushFuncError(node, `function '${functionName}' cannot be nested inside '${parentReplacementFunction}' in url-rewrite-target expressions`, errors, diagnostics, {
|
|
536
|
-
id: 'function.rewrite_target.nested_replacement',
|
|
537
|
-
params: { functionName, parentFunctionName: parentReplacementFunction, preset: 'url-rewrite-target' },
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
const nextReplacementFunctionStack = URL_REWRITE_TARGET_NON_NESTABLE_REPLACEMENT_FUNCTIONS.has(functionName)
|
|
541
|
-
? [...replacementFunctionStack, functionName]
|
|
542
|
-
: replacementFunctionStack;
|
|
543
|
-
for (const arg of node.args)
|
|
544
|
-
collectRewriteTargetFunctions(arg, nextReplacementFunctionStack, functions, errors, diagnostics);
|
|
545
623
|
return;
|
|
546
624
|
}
|
|
547
625
|
case 'And':
|
|
548
626
|
case 'Or':
|
|
549
627
|
case 'Xor':
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
628
|
+
usage.operators?.push({ operator: node.kind.toLowerCase(), node });
|
|
629
|
+
for (const operand of node.operands)
|
|
630
|
+
collectFunctionUsage(operand, replacementFunctionStack, usage);
|
|
553
631
|
return;
|
|
554
632
|
case 'Not':
|
|
555
|
-
|
|
633
|
+
usage.operators?.push({ operator: 'not', node });
|
|
634
|
+
collectFunctionUsage(node.operand, replacementFunctionStack, usage);
|
|
556
635
|
return;
|
|
557
636
|
case 'Comparison':
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
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);
|
|
562
645
|
return;
|
|
563
646
|
case 'Field':
|
|
564
647
|
case 'BoolField':
|
|
@@ -569,6 +652,8 @@ function collectRewriteTargetFunctions(node, replacementFunctionStack, functions
|
|
|
569
652
|
case 'SetLiteral':
|
|
570
653
|
case 'ListRef':
|
|
571
654
|
return;
|
|
655
|
+
default:
|
|
656
|
+
unhandledNode(node);
|
|
572
657
|
}
|
|
573
658
|
}
|
|
574
659
|
function validateListRef(node, left, op, errors, diagnostics, context) {
|
|
@@ -657,7 +742,8 @@ function isRecord(value) {
|
|
|
657
742
|
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
658
743
|
}
|
|
659
744
|
function validateInlineSetLiteral(node, errors, diagnostics) {
|
|
660
|
-
|
|
745
|
+
// An invalid member is not another element type.
|
|
746
|
+
const valueTypes = new Set(validSetElements(node).map(element => element.type));
|
|
661
747
|
if (valueTypes.size <= 1)
|
|
662
748
|
return;
|
|
663
749
|
pushValueError(node, 'inline list elements must all have the same data type', errors, diagnostics, {
|
|
@@ -696,7 +782,7 @@ function hasConsecutiveUnescapedWildcards(pattern) {
|
|
|
696
782
|
function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
|
|
697
783
|
const message = `${requirement}`;
|
|
698
784
|
errors.push(message);
|
|
699
|
-
|
|
785
|
+
diagnostics.push({
|
|
700
786
|
...metadata,
|
|
701
787
|
severity: 'error',
|
|
702
788
|
pos: node.pos,
|
|
@@ -707,7 +793,7 @@ function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
|
|
|
707
793
|
function pushFuncError(node, requirement, errors, diagnostics, metadata) {
|
|
708
794
|
const message = `${requirement}`;
|
|
709
795
|
errors.push(message);
|
|
710
|
-
|
|
796
|
+
diagnostics.push({
|
|
711
797
|
...metadata,
|
|
712
798
|
severity: 'error',
|
|
713
799
|
pos: node.pos ?? null,
|
|
@@ -718,7 +804,7 @@ function pushFuncError(node, requirement, errors, diagnostics, metadata) {
|
|
|
718
804
|
function pushValueError(node, message, errors, diagnostics, metadata) {
|
|
719
805
|
errors.push(message);
|
|
720
806
|
const span = argSpan(node);
|
|
721
|
-
|
|
807
|
+
diagnostics.push({
|
|
722
808
|
...metadata,
|
|
723
809
|
severity: 'error',
|
|
724
810
|
pos: span.pos,
|
|
@@ -726,14 +812,28 @@ function pushValueError(node, message, errors, diagnostics, metadata) {
|
|
|
726
812
|
source: 'expression',
|
|
727
813
|
});
|
|
728
814
|
}
|
|
729
|
-
|
|
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) {
|
|
730
832
|
const functionName = node.name;
|
|
731
833
|
const reason = getUnreliableRuleExpressionFunctionReason(functionName);
|
|
732
834
|
if (!reason)
|
|
733
835
|
return;
|
|
734
|
-
|
|
735
|
-
pushWarning(warnings, message);
|
|
736
|
-
pushDiagnostic(diagnostics, {
|
|
836
|
+
diagnostics.push({
|
|
737
837
|
id: 'function.unreliable',
|
|
738
838
|
params: { functionName, reason },
|
|
739
839
|
severity: 'warning',
|
|
@@ -748,17 +848,26 @@ function validateFunctionSupport(node, warnings, diagnostics) {
|
|
|
748
848
|
* access are certain: Cloudflare produces nil from map keys, array indexes,
|
|
749
849
|
* and functions, never from a field itself.
|
|
750
850
|
*/
|
|
751
|
-
function validateCoalesceFallbacks(node,
|
|
752
|
-
|
|
753
|
-
|
|
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)));
|
|
754
862
|
if (index < 0)
|
|
755
863
|
return;
|
|
756
|
-
const
|
|
757
|
-
|
|
758
|
-
|
|
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}`;
|
|
759
869
|
const span = argSpan(node.args[index]);
|
|
760
|
-
|
|
761
|
-
pushDiagnostic(diagnostics, {
|
|
870
|
+
diagnostics.push({
|
|
762
871
|
id: 'function.coalesce.never_nil_argument',
|
|
763
872
|
params: { functionName: 'coalesce', parameterName, unusedArgumentCount },
|
|
764
873
|
severity: 'warning',
|
|
@@ -767,54 +876,55 @@ function validateCoalesceFallbacks(node, warnings, diagnostics) {
|
|
|
767
876
|
source: 'expression',
|
|
768
877
|
});
|
|
769
878
|
}
|
|
770
|
-
function validateFieldOrFunc(node, guardedHeaders, errors,
|
|
879
|
+
function validateFieldOrFunc(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
771
880
|
if (isFieldNode(node)) {
|
|
772
|
-
validateFieldNode(node, guardedHeaders, errors,
|
|
881
|
+
validateFieldNode(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
773
882
|
return;
|
|
774
883
|
}
|
|
775
|
-
validateSemanticNode(node, guardedHeaders, errors,
|
|
884
|
+
validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, 'value');
|
|
776
885
|
}
|
|
777
|
-
function validateFuncArgs(node, guardedHeaders, errors,
|
|
886
|
+
function validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
778
887
|
for (const arg of node.args) {
|
|
779
888
|
if (isFieldNode(arg)) {
|
|
780
|
-
validateFieldNode(arg, guardedHeaders, errors,
|
|
889
|
+
validateFieldNode(arg, guardedHeaders, errors, diagnostics, expression, context);
|
|
781
890
|
}
|
|
782
891
|
else if (isExpressionNode(arg)) {
|
|
783
|
-
|
|
892
|
+
const position = isConditionArgument(arg) ? 'predicate' : 'value';
|
|
893
|
+
validateSemanticNode(arg, guardedHeaders, errors, diagnostics, expression, context, position);
|
|
784
894
|
}
|
|
785
895
|
else if (arg.kind === 'ListRef') {
|
|
786
896
|
validateFunctionListRefArg(node, arg, errors, diagnostics, context);
|
|
787
897
|
}
|
|
788
898
|
}
|
|
789
899
|
}
|
|
790
|
-
function validateQuantifierPredicateArgs(node, guardedHeaders, errors,
|
|
900
|
+
function validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
791
901
|
const quantifier = extractQuantifierPredicate(node);
|
|
792
902
|
if (!quantifier) {
|
|
793
903
|
const comparison = extractQuantifierComparison(node);
|
|
794
904
|
if (comparison && !comparison.comparison.right) {
|
|
795
|
-
validateFieldOrFunc(comparison.source, guardedHeaders, errors,
|
|
905
|
+
validateFieldOrFunc(comparison.source, guardedHeaders, errors, diagnostics, expression, context);
|
|
796
906
|
return;
|
|
797
907
|
}
|
|
798
|
-
validateFuncArgs(node, guardedHeaders, errors,
|
|
908
|
+
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
799
909
|
return;
|
|
800
910
|
}
|
|
801
|
-
validateFieldOrFunc(quantifier.source, guardedHeaders, errors,
|
|
911
|
+
validateFieldOrFunc(quantifier.source, guardedHeaders, errors, diagnostics, expression, context);
|
|
802
912
|
if (isDynamicValueNode(quantifier.value)) {
|
|
803
|
-
validateFieldOrFunc(quantifier.value, guardedHeaders, errors,
|
|
913
|
+
validateFieldOrFunc(quantifier.value, guardedHeaders, errors, diagnostics, expression, context);
|
|
804
914
|
}
|
|
805
915
|
if (quantifier.value.kind === 'ListRef') {
|
|
806
916
|
validateFunctionListRefArg(node, quantifier.value, errors, diagnostics, context);
|
|
807
917
|
}
|
|
808
918
|
}
|
|
809
|
-
function validateFunctionArgRequirements(node, errors, diagnostics) {
|
|
810
|
-
for (const signatureError of validateFunctionSignature(node)) {
|
|
919
|
+
function validateFunctionArgRequirements(node, binding, errors, diagnostics) {
|
|
920
|
+
for (const signatureError of validateFunctionSignature(node, binding)) {
|
|
811
921
|
pushFunctionSignatureError(signatureError, errors, diagnostics);
|
|
812
922
|
}
|
|
813
923
|
}
|
|
814
924
|
function pushFunctionSignatureError(signatureError, errors, diagnostics) {
|
|
815
925
|
errors.push(signatureError.message);
|
|
816
926
|
const span = argSpan(signatureError.node);
|
|
817
|
-
|
|
927
|
+
diagnostics.push({
|
|
818
928
|
id: signatureError.id,
|
|
819
929
|
...(signatureError.params ? { params: signatureError.params } : {}),
|
|
820
930
|
severity: 'error',
|
|
@@ -834,24 +944,29 @@ function argSpan(arg) {
|
|
|
834
944
|
}
|
|
835
945
|
return { pos: null, length: 0 };
|
|
836
946
|
}
|
|
837
|
-
function validateFieldNode(node, guardedHeaders, errors,
|
|
947
|
+
function validateFieldNode(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
838
948
|
validateAccessPath(node, errors, diagnostics);
|
|
949
|
+
const lowercaseKey = validateMapKeyCase(node, errors, diagnostics);
|
|
839
950
|
validateFieldAvailability(node, errors, diagnostics, expression, context);
|
|
840
|
-
validateDeprecatedField(node,
|
|
841
|
-
|
|
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'))
|
|
842
957
|
return;
|
|
843
958
|
const mapKey = node.accessPath.find(access => access.kind === 'MapKeyAccess');
|
|
844
959
|
const arrayIndex = node.accessPath.find(access => access.kind === 'ArrayIndexAccess');
|
|
845
960
|
if (!mapKey || !arrayIndex)
|
|
846
961
|
return;
|
|
847
|
-
const headerName = mapKey.key
|
|
962
|
+
const headerName = guardHeaderName(mapKey.key);
|
|
848
963
|
if (guardedHeaders.has(headerName))
|
|
849
964
|
return;
|
|
850
965
|
const fieldExpression = expression.slice(node.pos, node.pos + node.length);
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
966
|
+
// has_key() looks the key up exactly, so it quotes the key as the access reads it. The map's keys are
|
|
967
|
+
// lowercase, so it finds the header however the client spelled its name.
|
|
968
|
+
const guardExpression = `has_key(${node.name}, ${stringLiteralSource(mapKey.key)})`;
|
|
969
|
+
diagnostics.push({
|
|
855
970
|
id: 'field.header_index_requires_guard',
|
|
856
971
|
params: { fieldExpression, guardExpression, headerName },
|
|
857
972
|
severity: 'warning',
|
|
@@ -861,7 +976,8 @@ function validateFieldNode(node, guardedHeaders, errors, warnings, diagnostics,
|
|
|
861
976
|
});
|
|
862
977
|
}
|
|
863
978
|
function validateAccessPath(node, errors, diagnostics) {
|
|
864
|
-
|
|
979
|
+
const info = inferRuleExpressionArgument(node);
|
|
980
|
+
for (const issue of info.invalid ? info.accessIssues : []) {
|
|
865
981
|
const accessLabel = issue.accessKind === 'map-key'
|
|
866
982
|
? 'map key'
|
|
867
983
|
: issue.accessKind === 'array-index' ? 'array index' : 'array wildcard';
|
|
@@ -869,7 +985,7 @@ function validateAccessPath(node, errors, diagnostics) {
|
|
|
869
985
|
const expectedArticle = issue.expectedType === 'Array' ? 'an' : 'a';
|
|
870
986
|
const message = `${accessLabel} access requires ${expectedArticle} ${issue.expectedType} value; received ${actualType}`;
|
|
871
987
|
errors.push(message);
|
|
872
|
-
|
|
988
|
+
diagnostics.push({
|
|
873
989
|
id: 'access.type_mismatch',
|
|
874
990
|
params: {
|
|
875
991
|
accessKind: issue.accessKind,
|
|
@@ -883,21 +999,49 @@ function validateAccessPath(node, errors, diagnostics) {
|
|
|
883
999
|
});
|
|
884
1000
|
}
|
|
885
1001
|
}
|
|
886
|
-
|
|
1002
|
+
/**
|
|
1003
|
+
* Map fields with lowercase keys, where Cloudflare rejects a key with an
|
|
1004
|
+
* uppercase letter, ASCII or not (dry runs, 2026-10-09). It accepts one for
|
|
1005
|
+
* raw.http.response.headers, and on a function's result, such as
|
|
1006
|
+
* coalesce(http.request.headers, ...)["X-A"], which reads nil, as the map's
|
|
1007
|
+
* keys are lowercase (2026-10-10).
|
|
1008
|
+
*/
|
|
1009
|
+
const LOWERCASE_KEY_MAP_FIELDS = new Set([
|
|
1010
|
+
'http.request.headers',
|
|
1011
|
+
'http.response.headers',
|
|
1012
|
+
'raw.http.request.headers',
|
|
1013
|
+
]);
|
|
1014
|
+
/** Returns false when it rejects the field's map key. */
|
|
1015
|
+
function validateMapKeyCase(node, errors, diagnostics) {
|
|
1016
|
+
const access = node.accessPath[0];
|
|
1017
|
+
if (!LOWERCASE_KEY_MAP_FIELDS.has(node.name) || access?.kind !== 'MapKeyAccess' || !/\p{Uppercase}/u.test(access.key))
|
|
1018
|
+
return true;
|
|
1019
|
+
errors.push(`map key '${access.key}' of field '${node.name}' must be lowercase`);
|
|
1020
|
+
diagnostics.push({
|
|
1021
|
+
id: 'access.uppercase_map_key',
|
|
1022
|
+
params: { fieldName: node.name, key: access.key },
|
|
1023
|
+
severity: 'error',
|
|
1024
|
+
pos: access.pos ?? null,
|
|
1025
|
+
length: access.length ?? 0,
|
|
1026
|
+
source: 'expression',
|
|
1027
|
+
});
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
function validateBoolFieldNode(node, errors, diagnostics, expression, context) {
|
|
887
1031
|
// validateBooleanLiterals() reports `true` and `false`.
|
|
888
|
-
if (node.name
|
|
1032
|
+
if (isBooleanLiteralName(node.name))
|
|
889
1033
|
return;
|
|
890
1034
|
validateFieldAvailability(node, errors, diagnostics, expression, context);
|
|
891
|
-
validateDeprecatedField(node,
|
|
1035
|
+
validateDeprecatedField(node, diagnostics, expression, context);
|
|
892
1036
|
if (!isKnownFieldName(node.name))
|
|
893
1037
|
return;
|
|
894
|
-
const type =
|
|
895
|
-
if (isUnknownRuleExpressionDataType(type) || (type
|
|
1038
|
+
const type = fieldDataType(node.name);
|
|
1039
|
+
if (isUnknownRuleExpressionDataType(type) || isBooleanType(type))
|
|
896
1040
|
return;
|
|
897
1041
|
const actualType = formatRuleExpressionDataType(type);
|
|
898
1042
|
const message = `field '${node.name}' must be Boolean in predicate position; received ${actualType}`;
|
|
899
1043
|
errors.push(message);
|
|
900
|
-
|
|
1044
|
+
diagnostics.push({
|
|
901
1045
|
id: 'field.predicate_type_mismatch',
|
|
902
1046
|
params: { fieldName: node.name, expectedType: 'Boolean', actualType },
|
|
903
1047
|
severity: 'error',
|
|
@@ -914,9 +1058,9 @@ function validateBooleanLiterals(root, errors, diagnostics) {
|
|
|
914
1058
|
if (root.kind === 'BoolField' && root.name === RULE_EXPRESSION_MATCH_ALL_EXPRESSION)
|
|
915
1059
|
return;
|
|
916
1060
|
const visit = (node) => {
|
|
917
|
-
switch (node
|
|
1061
|
+
switch (node.kind) {
|
|
918
1062
|
case 'BoolField':
|
|
919
|
-
if (node.name
|
|
1063
|
+
if (isBooleanLiteralName(node.name))
|
|
920
1064
|
pushBooleanLiteralError(node, node.name, errors, diagnostics);
|
|
921
1065
|
return;
|
|
922
1066
|
case 'BoolLiteral':
|
|
@@ -924,7 +1068,8 @@ function validateBooleanLiterals(root, errors, diagnostics) {
|
|
|
924
1068
|
return;
|
|
925
1069
|
case 'Comparison':
|
|
926
1070
|
visit(node.left);
|
|
927
|
-
|
|
1071
|
+
if (node.right)
|
|
1072
|
+
visit(node.right);
|
|
928
1073
|
return;
|
|
929
1074
|
case 'Func':
|
|
930
1075
|
node.args.forEach(visit);
|
|
@@ -937,6 +1082,19 @@ function validateBooleanLiterals(root, errors, diagnostics) {
|
|
|
937
1082
|
case 'Xor':
|
|
938
1083
|
node.operands.forEach(visit);
|
|
939
1084
|
return;
|
|
1085
|
+
case 'Invalid':
|
|
1086
|
+
if (node.operand)
|
|
1087
|
+
visit(node.operand);
|
|
1088
|
+
return;
|
|
1089
|
+
case 'Field':
|
|
1090
|
+
case 'StringLiteral':
|
|
1091
|
+
case 'NumberLiteral':
|
|
1092
|
+
case 'IpLiteral':
|
|
1093
|
+
case 'SetLiteral':
|
|
1094
|
+
case 'ListRef':
|
|
1095
|
+
return;
|
|
1096
|
+
default:
|
|
1097
|
+
unhandledNode(node);
|
|
940
1098
|
}
|
|
941
1099
|
};
|
|
942
1100
|
visit(root);
|
|
@@ -944,7 +1102,7 @@ function validateBooleanLiterals(root, errors, diagnostics) {
|
|
|
944
1102
|
function pushBooleanLiteralError(node, literal, errors, diagnostics) {
|
|
945
1103
|
const message = `'${literal}' is not a value; Cloudflare accepts 'true' only as the entire expression`;
|
|
946
1104
|
errors.push(message);
|
|
947
|
-
|
|
1105
|
+
diagnostics.push({
|
|
948
1106
|
id: 'rule_expression.boolean_literal',
|
|
949
1107
|
params: { literal },
|
|
950
1108
|
severity: 'error',
|
|
@@ -953,16 +1111,12 @@ function pushBooleanLiteralError(node, literal, errors, diagnostics) {
|
|
|
953
1111
|
source: 'expression',
|
|
954
1112
|
});
|
|
955
1113
|
}
|
|
956
|
-
function validateDeprecatedField(node,
|
|
1114
|
+
function validateDeprecatedField(node, diagnostics, expression, context) {
|
|
957
1115
|
if (!context.deprecationNotices || !KNOWN_DEPRECATED_FIELDS.has(node.name))
|
|
958
1116
|
return;
|
|
959
|
-
const replacement =
|
|
1117
|
+
const replacement = availableReplacement(node.name, context.fieldAvailability);
|
|
960
1118
|
const fieldExpression = expressionForNode(node, expression);
|
|
961
|
-
|
|
962
|
-
? `field '${fieldExpression}' is deprecated; use '${replacement}' instead`
|
|
963
|
-
: `field '${fieldExpression}' is deprecated`;
|
|
964
|
-
pushWarning(warnings, message);
|
|
965
|
-
pushDiagnostic(diagnostics, {
|
|
1119
|
+
diagnostics.push({
|
|
966
1120
|
id: 'field.deprecated',
|
|
967
1121
|
params: replacement
|
|
968
1122
|
? { fieldExpression, replacement }
|
|
@@ -973,6 +1127,17 @@ function validateDeprecatedField(node, warnings, diagnostics, expression, contex
|
|
|
973
1127
|
source: 'expression',
|
|
974
1128
|
});
|
|
975
1129
|
}
|
|
1130
|
+
/**
|
|
1131
|
+
* The replacement of a deprecated field, where it is available. Cloudflare
|
|
1132
|
+
* judges an alias by its own verdict, so a replacement can be unavailable
|
|
1133
|
+
* where the alias is not.
|
|
1134
|
+
*/
|
|
1135
|
+
function availableReplacement(field, rules) {
|
|
1136
|
+
const replacement = deprecatedFieldReplacement(field);
|
|
1137
|
+
if (!replacement || !rules || isRuleExpressionFieldAvailable({ name: replacement }, rules))
|
|
1138
|
+
return replacement;
|
|
1139
|
+
return undefined;
|
|
1140
|
+
}
|
|
976
1141
|
function validateFieldAvailability(node, errors, diagnostics, expression, context) {
|
|
977
1142
|
// Unknown fields are invalid in every product, whatever its field availability.
|
|
978
1143
|
if (!isKnownFieldName(node.name)) {
|
|
@@ -982,13 +1147,17 @@ function validateFieldAvailability(node, errors, diagnostics, expression, contex
|
|
|
982
1147
|
const rules = context.fieldAvailability;
|
|
983
1148
|
if (!rules)
|
|
984
1149
|
return;
|
|
985
|
-
const
|
|
1150
|
+
const accessPath = isFieldNode(node) ? node.accessPath : [];
|
|
1151
|
+
const map = accessPath.some(access => access.kind === 'MapKeyAccess');
|
|
986
1152
|
if (isRuleExpressionFieldAvailable({ name: node.name, map }, rules))
|
|
987
1153
|
return;
|
|
1154
|
+
// An invalid segment may have been a map key, so the field is unavailable only if it is either way.
|
|
1155
|
+
if (accessPath.some(access => access.kind === 'Invalid') && isRuleExpressionFieldAvailable({ name: node.name, map: true }, rules))
|
|
1156
|
+
return;
|
|
988
1157
|
const fieldExpression = expressionForNode(node, expression);
|
|
989
1158
|
const message = `field '${fieldExpression}' is not available for ${rules.preset} expressions`;
|
|
990
1159
|
errors.push(message);
|
|
991
|
-
|
|
1160
|
+
diagnostics.push({
|
|
992
1161
|
id: 'field.unavailable',
|
|
993
1162
|
params: { fieldExpression, fieldName: node.name, preset: rules.preset },
|
|
994
1163
|
severity: 'error',
|
|
@@ -1001,7 +1170,7 @@ function pushUnknownFieldError(node, errors, diagnostics, expression) {
|
|
|
1001
1170
|
const fieldExpression = expressionForNode(node, expression);
|
|
1002
1171
|
const message = `unknown field '${fieldExpression}'`;
|
|
1003
1172
|
errors.push(message);
|
|
1004
|
-
|
|
1173
|
+
diagnostics.push({
|
|
1005
1174
|
id: 'field.unknown',
|
|
1006
1175
|
params: { fieldExpression, fieldName: node.name },
|
|
1007
1176
|
severity: 'error',
|
|
@@ -1015,52 +1184,141 @@ function expressionForNode(node, expression) {
|
|
|
1015
1184
|
? expression.slice(node.pos, node.pos + node.length)
|
|
1016
1185
|
: node.name;
|
|
1017
1186
|
}
|
|
1018
|
-
function deprecatedFieldReplacement(field) {
|
|
1019
|
-
if (!KNOWN_DEPRECATED_FIELDS.has(field))
|
|
1020
|
-
return undefined;
|
|
1021
|
-
return DEPRECATED_FIELD_REPLACEMENTS[field];
|
|
1022
|
-
}
|
|
1023
1187
|
function collectHeaderPresenceGuards(node) {
|
|
1024
1188
|
switch (node.kind) {
|
|
1025
1189
|
case 'And':
|
|
1026
1190
|
return node.operands.reduce((guards, operand) => unionHeaderGuards(guards, collectHeaderPresenceGuards(operand)), new Set());
|
|
1027
1191
|
case 'Func':
|
|
1028
1192
|
return getHeaderPresenceGuardNames(node);
|
|
1029
|
-
|
|
1193
|
+
case 'Comparison':
|
|
1194
|
+
case 'BoolField':
|
|
1195
|
+
case 'Or':
|
|
1196
|
+
case 'Xor':
|
|
1197
|
+
case 'Not':
|
|
1198
|
+
case 'Invalid':
|
|
1030
1199
|
return new Set();
|
|
1200
|
+
default:
|
|
1201
|
+
return unhandledNode(node);
|
|
1031
1202
|
}
|
|
1032
1203
|
}
|
|
1033
1204
|
function getHeaderPresenceGuardNames(node) {
|
|
1034
|
-
|
|
1205
|
+
// A call that validation rejects guards nothing; it reports its own error.
|
|
1206
|
+
if (validateFunctionSignature(node, bindArguments(node)).length > 0)
|
|
1035
1207
|
return new Set();
|
|
1036
|
-
|
|
1037
|
-
|
|
1208
|
+
switch (node.name) {
|
|
1209
|
+
case 'any':
|
|
1210
|
+
return getQuantifierHeaderGuardNames(node);
|
|
1211
|
+
case 'has_key':
|
|
1212
|
+
return getHasKeyHeaderGuardNames(node);
|
|
1213
|
+
case 'has_value':
|
|
1214
|
+
return getHasValueHeaderGuardNames(node);
|
|
1215
|
+
default:
|
|
1216
|
+
return new Set();
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
function getQuantifierHeaderGuardNames(node) {
|
|
1220
|
+
// Recovery from a syntax error may have cut the predicate short; see FuncNode.uncertainFrom.
|
|
1221
|
+
if (isUncertainArgument(node, 0))
|
|
1038
1222
|
return new Set();
|
|
1039
|
-
|
|
1223
|
+
const comparison = extractQuantifierComparison(node);
|
|
1224
|
+
if (!comparison)
|
|
1040
1225
|
return new Set();
|
|
1041
|
-
|
|
1226
|
+
const collection = headerCollection(comparison.source);
|
|
1227
|
+
// A value that meets the comparison, negated or not, whatever it compares with, proves the header has one.
|
|
1228
|
+
if (collection?.kind === 'values')
|
|
1229
|
+
return new Set([collection.header]);
|
|
1230
|
+
if (collection?.kind !== 'names')
|
|
1042
1231
|
return new Set();
|
|
1043
|
-
const
|
|
1044
|
-
if (
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
return names;
|
|
1232
|
+
const value = comparison.comparison.right;
|
|
1233
|
+
if (!value || comparison.negated)
|
|
1234
|
+
return new Set();
|
|
1235
|
+
if (comparison.op === 'eq' && value.kind === 'StringLiteral')
|
|
1236
|
+
return new Set([guardHeaderName(value.value)]);
|
|
1237
|
+
if (comparison.op === 'in' && value.kind === 'SetLiteral')
|
|
1238
|
+
return new Set(stringLiteralValues(value).map(guardHeaderName));
|
|
1239
|
+
return new Set();
|
|
1054
1240
|
}
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1241
|
+
/**
|
|
1242
|
+
* The header `has_key(http.request.headers, "x")` checks for, normalized by
|
|
1243
|
+
* guardHeaderName() as every guard's name is, so that it matches the indexed
|
|
1244
|
+
* access's name. The map's keys are lowercase, so a key with uppercase
|
|
1245
|
+
* letters never matches a header, and the call is always false. Counting it
|
|
1246
|
+
* as a guard is still right: the `and` around it is then false too, so the
|
|
1247
|
+
* access it guards never decides the result.
|
|
1248
|
+
*/
|
|
1249
|
+
function getHasKeyHeaderGuardNames(node) {
|
|
1250
|
+
if (isUncertainArgument(node, 1))
|
|
1251
|
+
return new Set();
|
|
1252
|
+
const [map, key] = node.args;
|
|
1253
|
+
const wholeMap = isFieldNode(map) && REQUEST_HEADER_MAPS.has(map.name) && map.accessPath.length === 0;
|
|
1254
|
+
return wholeMap && isStringLiteral(key) ? new Set([guardHeaderName(key.value)]) : new Set();
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* The header `has_value()` checks: the one whose values it searches, whatever
|
|
1258
|
+
* the value, or the one it looks for among the names, as in
|
|
1259
|
+
* `has_value(http.request.headers.names, "x")`.
|
|
1260
|
+
*/
|
|
1261
|
+
function getHasValueHeaderGuardNames(node) {
|
|
1262
|
+
if (isUncertainArgument(node, 0))
|
|
1263
|
+
return new Set();
|
|
1264
|
+
const [collectionArgument, value] = node.args;
|
|
1265
|
+
const collection = headerCollection(collectionArgument);
|
|
1266
|
+
// Whatever value is found among a header's values proves the header present, so only the names read it.
|
|
1267
|
+
if (collection?.kind === 'values')
|
|
1268
|
+
return new Set([collection.header]);
|
|
1269
|
+
if (collection?.kind !== 'names' || isUncertainArgument(node, 1) || !isStringLiteral(value))
|
|
1270
|
+
return new Set();
|
|
1271
|
+
return new Set([guardHeaderName(value.value)]);
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* The request header collection a guard reads: one header's values, as
|
|
1275
|
+
* `http.request.headers["x"]` or `["x"][*]`, directly or through calls that
|
|
1276
|
+
* map over them, as `lower()` does, or the header names, plain or through
|
|
1277
|
+
* lower(). A guard on either names field counts for both header maps:
|
|
1278
|
+
* Cloudflare documents only the raw fields as unaffected by transforms, but
|
|
1279
|
+
* live requests showed the plain ones list the headers from before them too
|
|
1280
|
+
* (2026-10-09).
|
|
1281
|
+
*
|
|
1282
|
+
* A names guard reads the names themselves, so only lower(), whose change
|
|
1283
|
+
* guardHeaderName() makes anyway, may come between. Only the existence of a
|
|
1284
|
+
* header's values counts, so any call that maps over them may.
|
|
1285
|
+
*/
|
|
1286
|
+
function headerCollection(node) {
|
|
1287
|
+
const names = unwrapMappedCalls(node, name => name === 'lower');
|
|
1288
|
+
if (isFieldNode(names) && REQUEST_HEADER_ARRAYS.get(names.name) === 'names') {
|
|
1289
|
+
return readsWholeArray(names.accessPath) ? { kind: 'names' } : null;
|
|
1058
1290
|
}
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
return
|
|
1291
|
+
const values = unwrapMappedCalls(node, () => true);
|
|
1292
|
+
if (!isFieldNode(values) || !REQUEST_HEADER_MAPS.has(values.name))
|
|
1293
|
+
return null;
|
|
1294
|
+
// An invalid segment is neither a key nor `[*]`, so it never counts as the header's key.
|
|
1295
|
+
const [key, ...rest] = values.accessPath;
|
|
1296
|
+
return key?.kind === 'MapKeyAccess' && readsWholeArray(rest) ? { kind: 'values', header: guardHeaderName(key.key) } : null;
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* The value under the calls that map over it and that `unwraps` accepts by
|
|
1300
|
+
* name; null when one of them reads its result other than whole, as
|
|
1301
|
+
* `lower(...)[0]` or an access that failed to parse does.
|
|
1302
|
+
*/
|
|
1303
|
+
function unwrapMappedCalls(node, unwraps) {
|
|
1304
|
+
let value = node;
|
|
1305
|
+
while (isFuncNode(value) && unwraps(value.name) && inferFunctionInvocation(value).mapping === 'mapped') {
|
|
1306
|
+
if (!readsWholeArray(value.accessPath))
|
|
1307
|
+
return null;
|
|
1308
|
+
value = value.args[0];
|
|
1062
1309
|
}
|
|
1063
|
-
return
|
|
1310
|
+
return value;
|
|
1311
|
+
}
|
|
1312
|
+
/** Whether an access path reads a whole array: no access, or `[*]`. */
|
|
1313
|
+
function readsWholeArray(accessPath) {
|
|
1314
|
+
return accessPath.length === 0 || (accessPath.length === 1 && accessPath[0].kind === 'ArrayWildcardAccess');
|
|
1315
|
+
}
|
|
1316
|
+
/**
|
|
1317
|
+
* A header name as guards and the indexed accesses they guard compare it:
|
|
1318
|
+
* lowercased, as names keep the client's spelling and map keys are lowercase.
|
|
1319
|
+
*/
|
|
1320
|
+
function guardHeaderName(name) {
|
|
1321
|
+
return name.toLowerCase();
|
|
1064
1322
|
}
|
|
1065
1323
|
function unionHeaderGuards(left, right) {
|
|
1066
1324
|
return new Set([...left, ...right]);
|