@libreflare/worker-runtime 0.10.3 → 0.10.5
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 +87 -37
- package/dist/rule-evaluator/access-values.d.ts +42 -27
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +123 -52
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +6 -0
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js.map +1 -1
- package/dist/rule-evaluator/byte-strings.d.ts.map +1 -1
- package/dist/rule-evaluator/byte-strings.js +27 -11
- package/dist/rule-evaluator/byte-strings.js.map +1 -1
- 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 +3 -0
- package/dist/rule-evaluator/constants.js.map +1 -1
- package/dist/rule-evaluator/country-codes.generated.js +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
- package/dist/rule-evaluator/diagnostics.js +8 -0
- package/dist/rule-evaluator/diagnostics.js.map +1 -1
- package/dist/rule-evaluator/edge-field-gaps.d.ts +6 -4
- package/dist/rule-evaluator/edge-field-gaps.d.ts.map +1 -1
- package/dist/rule-evaluator/edge-field-gaps.js +14 -39
- package/dist/rule-evaluator/edge-field-gaps.js.map +1 -1
- package/dist/rule-evaluator/edge-record.js +2 -0
- package/dist/rule-evaluator/edge-record.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.d.ts +4 -4
- package/dist/rule-evaluator/engine-observations.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.d.ts +496 -3
- package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.js +389 -248
- package/dist/rule-evaluator/engine-observations.generated.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.js +1 -0
- package/dist/rule-evaluator/engine-observations.js.map +1 -1
- package/dist/rule-evaluator/explain.js +10 -7
- package/dist/rule-evaluator/explain.js.map +1 -1
- package/dist/rule-evaluator/field-availability.d.ts +6 -1
- package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
- package/dist/rule-evaluator/field-availability.js +16 -4
- package/dist/rule-evaluator/field-availability.js.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts +147 -5
- package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +345 -5
- package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/fields.d.ts +0 -1
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +67 -8
- package/dist/rule-evaluator/fields.js.map +1 -1
- package/dist/rule-evaluator/function-availability.d.ts +7 -0
- package/dist/rule-evaluator/function-availability.d.ts.map +1 -1
- package/dist/rule-evaluator/function-availability.js +38 -0
- package/dist/rule-evaluator/function-availability.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.d.ts +10 -2
- package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.d.ts +55 -4
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +351 -1
- package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.js +3 -1
- package/dist/rule-evaluator/function-descriptions.js.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.d.ts +18 -0
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +146 -13
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +10 -6
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +469 -172
- package/dist/rule-evaluator/functions.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +0 -6
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +4 -52
- package/dist/rule-evaluator/operators.js.map +1 -1
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +4 -1
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +17 -2
- package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
- package/dist/rule-evaluator/quantifier.js +90 -7
- package/dist/rule-evaluator/quantifier.js.map +1 -1
- package/dist/rule-evaluator/replacement.d.ts +37 -0
- package/dist/rule-evaluator/replacement.d.ts.map +1 -0
- package/dist/rule-evaluator/replacement.js +54 -0
- package/dist/rule-evaluator/replacement.js.map +1 -0
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +5 -0
- package/dist/rule-evaluator/rule-language-types.js.map +1 -1
- package/dist/rule-evaluator/validation.d.ts +1 -1
- package/dist/rule-evaluator/validation.d.ts.map +1 -1
- package/dist/rule-evaluator/validation.js +160 -106
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator/wildcard.d.ts +65 -0
- package/dist/rule-evaluator/wildcard.d.ts.map +1 -0
- package/dist/rule-evaluator/wildcard.js +168 -0
- package/dist/rule-evaluator/wildcard.js.map +1 -0
- package/package.json +2 -2
|
@@ -3,7 +3,7 @@ import { EQUALITY_ORDERING_OPERATORS, isBooleanLiteralName, isConditionArgument,
|
|
|
3
3
|
import { RULE_EXPRESSION_MATCH_ALL_EXPRESSION } from './constants.js';
|
|
4
4
|
import { isRuleExpressionFunctionAvailable, isRuleExpressionOperatorAvailable } from './function-availability.js';
|
|
5
5
|
import { getUnreliableRuleExpressionFunctionReason } from './functions.js';
|
|
6
|
-
import { bindArguments, PREDICATE_SHAPE_TYPES, validateFunctionSignature, } from './function-signature-validation.js';
|
|
6
|
+
import { bindArguments, PREDICATE_SHAPE_TYPES, validateFunctionSignature, validatePresetArgumentFields, wildcardPatternDiagnostic, } from './function-signature-validation.js';
|
|
7
7
|
import { minifyRuleExpression } from './minifier.js';
|
|
8
8
|
import { parseExpression } from './parse-cache.js';
|
|
9
9
|
import { trimRuleExpression } from './tokenizer.js';
|
|
@@ -15,24 +15,32 @@ import { isRuleExpressionCountryCode } from './country-codes.js';
|
|
|
15
15
|
import { getRuleExpressionFieldValueDomain } from './field-value-domains.js';
|
|
16
16
|
import { customList, isRuleExpressionIpListItem, isRuleExpressionListName, } from './custom-lists.js';
|
|
17
17
|
import { collectionElementDataType, conditionShape, fieldDataType, formatRuleExpressionDataType, isArrayConditionArgument, isArrayExpandedNode, isBooleanArrayType, isBooleanType, isConcreteComparisonFamily, isFieldNode, isFuncNode, inferFunctionInvocation, inferRuleExpressionArgument, isIpAddressValueType, isStringLiteral, isUnknownRuleExpressionDataType, ruleExpressionComparisonTypeFamily, ruleExpressionValueTypes, singleValueType, } from './rule-language-types.js';
|
|
18
|
-
import { conditionArray, isArrayCall, isArrayComparison, isArrayCondition, predicateArrays, quantifierPredicateConditions, readQuantifierPredicate, readsWholeArray, unalignedArray, unwrapMappedCalls, } from './quantifier.js';
|
|
18
|
+
import { conditionArray, isArrayCall, isArrayComparison, isArrayCondition, keepsPlacesWhenMapped, pairedArgumentArrays, predicateArrays, quantifierPredicateConditions, readQuantifierPredicate, readsWholeArray, unalignedArray, unwrapMappedCalls, } from './quantifier.js';
|
|
19
19
|
import { renderRuleExpressionStringLiteral } from './string-literals.js';
|
|
20
20
|
export const MAX_CLOUDFLARE_RULE_EXPRESSION_LENGTH = 4096;
|
|
21
21
|
export const RULE_EXPRESSION_VALIDATION_PRESETS = [
|
|
22
22
|
'url-rewrite-filter',
|
|
23
23
|
'url-rewrite-target',
|
|
24
|
+
'custom-rule',
|
|
24
25
|
'logging-source-key',
|
|
25
26
|
];
|
|
26
27
|
function isArgumentPosition(position) {
|
|
27
28
|
return position === 'argument' || position === 'boolean-argument' || position === 'array-argument';
|
|
28
29
|
}
|
|
29
|
-
/**
|
|
30
|
-
const
|
|
30
|
+
/** How many calls of a function Cloudflare accepts in an expression, a limit that applies in every preset. */
|
|
31
|
+
const FUNCTION_CALL_LIMITS = new Map([
|
|
32
|
+
['encode_hex', 4],
|
|
33
|
+
['hash_in_range', 1],
|
|
34
|
+
['hmac_sha256', 4],
|
|
35
|
+
['regex_replace', 1],
|
|
36
|
+
['uuidv4', 1],
|
|
37
|
+
['wildcard_replace', 1],
|
|
38
|
+
]);
|
|
31
39
|
/**
|
|
32
|
-
*
|
|
33
|
-
* rewrite target. Header transforms accept two concat() calls.
|
|
40
|
+
* How many calls of a function Cloudflare accepts in a URL rewrite filter, a
|
|
41
|
+
* rewrite target, or a Custom Rule. Header transforms accept two concat() calls.
|
|
34
42
|
*/
|
|
35
|
-
const
|
|
43
|
+
const PHASE_FUNCTION_CALL_LIMITS = new Map([['concat', 1]]);
|
|
36
44
|
/** Replacement functions Cloudflare does not accept inside each other, in every preset. */
|
|
37
45
|
const NON_NESTABLE_REPLACEMENT_FUNCTIONS = new Set(['regex_replace', 'wildcard_replace']);
|
|
38
46
|
const SUPPORTED_LIST_KIND = 'ip';
|
|
@@ -260,7 +268,8 @@ function collectSemanticValidationDiagnostics(errors, diagnostics, node, express
|
|
|
260
268
|
validateSemanticNode(node, new Set(), errors, diagnostics, expression, context, rootPosition);
|
|
261
269
|
validateFunctionUsage(node, context, errors, diagnostics);
|
|
262
270
|
}
|
|
263
|
-
|
|
271
|
+
/** Checks a node and the ones in it. `counted` says that len() only counts the array the node gives. */
|
|
272
|
+
function validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, position, counted = false) {
|
|
264
273
|
// The conditions an argument combines are arguments' conditions too, with the first one's type.
|
|
265
274
|
let operandPosition = 'predicate';
|
|
266
275
|
if (position === 'boolean-argument' || position === 'array-argument')
|
|
@@ -271,54 +280,54 @@ function validateSemanticNode(node, guardedHeaders, errors, diagnostics, express
|
|
|
271
280
|
case 'And': {
|
|
272
281
|
const andGuards = unionHeaderGuards(guardedHeaders, collectHeaderPresenceGuards(node));
|
|
273
282
|
for (const operand of node.operands) {
|
|
274
|
-
validateSemanticNode(operand, andGuards, errors, diagnostics, expression, context, operandPosition);
|
|
283
|
+
validateSemanticNode(operand, andGuards, errors, diagnostics, expression, context, operandPosition, counted);
|
|
275
284
|
}
|
|
276
285
|
return;
|
|
277
286
|
}
|
|
278
287
|
case 'Or':
|
|
279
288
|
case 'Xor':
|
|
280
289
|
for (const operand of node.operands) {
|
|
281
|
-
validateSemanticNode(operand, guardedHeaders, errors, diagnostics, expression, context, operandPosition);
|
|
290
|
+
validateSemanticNode(operand, guardedHeaders, errors, diagnostics, expression, context, operandPosition, counted);
|
|
282
291
|
}
|
|
283
292
|
return;
|
|
284
293
|
case 'Not':
|
|
285
|
-
validateSemanticNode(node.operand, guardedHeaders, errors, diagnostics, expression, context, isArgumentPosition(position) ? position : 'predicate');
|
|
294
|
+
validateSemanticNode(node.operand, guardedHeaders, errors, diagnostics, expression, context, isArgumentPosition(position) ? position : 'predicate', counted);
|
|
286
295
|
return;
|
|
287
296
|
case 'Comparison':
|
|
288
|
-
validateFieldOrFunc(node.left, guardedHeaders, errors, diagnostics, expression, context);
|
|
297
|
+
validateFieldOrFunc(node.left, guardedHeaders, errors, diagnostics, expression, context, counted);
|
|
289
298
|
if (isDynamicValueNode(node.right)) {
|
|
290
299
|
validateFieldOrFunc(node.right, guardedHeaders, errors, diagnostics, expression, context);
|
|
291
300
|
}
|
|
292
301
|
validateComparisonRequirements(node, errors, diagnostics, context, isArgumentPosition(position));
|
|
293
302
|
return;
|
|
294
303
|
case 'Func': {
|
|
295
|
-
if (position !== 'value'
|
|
304
|
+
if (position !== 'value')
|
|
296
305
|
validateFunctionPredicateResult(node, position, errors, diagnostics);
|
|
297
306
|
validateFunctionAvailability(node, errors, diagnostics, context);
|
|
298
307
|
validateAccessPath(node, errors, diagnostics);
|
|
299
308
|
const binding = bindArguments(node);
|
|
300
|
-
const argumentsValid = validateFunctionArgRequirements(node, binding, errors, diagnostics);
|
|
309
|
+
const argumentsValid = validateFunctionArgRequirements(node, binding, errors, diagnostics, context);
|
|
310
|
+
const countsArguments = countsArrayArguments(node, counted);
|
|
301
311
|
// An order warning or a dropped-header notice on a call the signature rejects would only add to its
|
|
302
312
|
// error. Other errors, such as an access after the call, a missing comparison, or a rejected call
|
|
303
313
|
// around it, leave the read itself valid, so it still gets them.
|
|
304
314
|
if (argumentsValid) {
|
|
305
|
-
validateHeaderOrderReads(node, diagnostics, expression);
|
|
315
|
+
validateHeaderOrderReads(node, diagnostics, expression, context);
|
|
306
316
|
validateDroppedHeaderLookups(node, diagnostics);
|
|
317
|
+
validateDroppedHeaderConditions(node, countsArguments ? selectingArguments(node) : node.args, diagnostics);
|
|
307
318
|
}
|
|
308
319
|
if (node.name === 'coalesce')
|
|
309
320
|
validateCoalesceFallbacks(node, binding, diagnostics);
|
|
310
321
|
const errorCount = errors.length;
|
|
311
|
-
// Whether len() only counts the arrays the call's arguments give.
|
|
312
|
-
const counted = node.name === 'len' || position === 'counted';
|
|
313
322
|
if (node.name === 'any' || node.name === 'all') {
|
|
314
323
|
validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
315
324
|
}
|
|
316
325
|
else {
|
|
317
|
-
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context,
|
|
326
|
+
validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context, countsArguments);
|
|
318
327
|
}
|
|
319
328
|
// A pairing warning on conditions any check rejects would only add to their errors.
|
|
320
329
|
if (argumentsValid && errors.length === errorCount)
|
|
321
|
-
validateConditionPairing(node, diagnostics, expression,
|
|
330
|
+
validateConditionPairing(node, diagnostics, expression, context, countsArguments);
|
|
322
331
|
return;
|
|
323
332
|
}
|
|
324
333
|
case 'BoolField':
|
|
@@ -423,9 +432,7 @@ function validateComparisonRequirements(node, errors, diagnostics, context, elem
|
|
|
423
432
|
if (node.right?.kind === 'SetLiteral') {
|
|
424
433
|
validateInlineSetLiteral(node.right, errors, diagnostics);
|
|
425
434
|
}
|
|
426
|
-
|
|
427
|
-
validateWildcardPattern(node.right, errors, diagnostics);
|
|
428
|
-
}
|
|
435
|
+
validateComparisonWildcardPattern(node, errors, diagnostics);
|
|
429
436
|
validateComparisonValueDomain(node, errors, diagnostics);
|
|
430
437
|
if (!invalidArrayComparison)
|
|
431
438
|
validateComparisonTypeCompatibility(node, node.right, errors, diagnostics);
|
|
@@ -614,8 +621,8 @@ function validateFunctionUsage(node, context, errors, diagnostics) {
|
|
|
614
621
|
collectFunctionUsage(node, [], usage);
|
|
615
622
|
for (const functionName of usage.calls.keys())
|
|
616
623
|
validateFunctionSupport(functionName, diagnostics);
|
|
617
|
-
for (const functionName of
|
|
618
|
-
|
|
624
|
+
for (const [functionName, maxCalls] of FUNCTION_CALL_LIMITS)
|
|
625
|
+
reportExcessCalls(functionName, maxCalls, usage, preset, errors, diagnostics);
|
|
619
626
|
// Cloudflare reports a nested replacement even where it does not accept the outer function.
|
|
620
627
|
for (const { node: nested, parentFunctionName } of usage.nestedReplacements) {
|
|
621
628
|
pushFuncError(nested, `function '${nested.name}' cannot be nested inside '${parentFunctionName}'`, errors, diagnostics, {
|
|
@@ -623,10 +630,10 @@ function validateFunctionUsage(node, context, errors, diagnostics) {
|
|
|
623
630
|
params: { functionName: nested.name, parentFunctionName },
|
|
624
631
|
});
|
|
625
632
|
}
|
|
626
|
-
if (preset
|
|
633
|
+
if (preset === null || preset === 'logging-source-key')
|
|
627
634
|
return;
|
|
628
|
-
for (const functionName of
|
|
629
|
-
|
|
635
|
+
for (const [functionName, maxCalls] of PHASE_FUNCTION_CALL_LIMITS)
|
|
636
|
+
reportExcessCalls(functionName, maxCalls, usage, preset, errors, diagnostics);
|
|
630
637
|
for (const { operator, node: operatorNode } of usage.operators ?? []) {
|
|
631
638
|
if (isRuleExpressionOperatorAvailable(operator, preset))
|
|
632
639
|
continue;
|
|
@@ -642,17 +649,25 @@ function validateFunctionUsage(node, context, errors, diagnostics) {
|
|
|
642
649
|
}
|
|
643
650
|
}
|
|
644
651
|
/**
|
|
645
|
-
* Reports every call of a function
|
|
652
|
+
* Reports every call of a function past its limit. A function the preset
|
|
646
653
|
* does not accept has only its availability errors, as in Cloudflare.
|
|
647
654
|
*/
|
|
648
|
-
function
|
|
655
|
+
function reportExcessCalls(functionName, maxCalls, usage, preset, errors, diagnostics) {
|
|
649
656
|
if (preset && !isRuleExpressionFunctionAvailable(functionName, preset))
|
|
650
657
|
return;
|
|
651
|
-
for (const
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
658
|
+
for (const call of (usage.calls.get(functionName) ?? []).slice(maxCalls)) {
|
|
659
|
+
if (maxCalls === 1) {
|
|
660
|
+
pushFuncError(call, `function '${functionName}' can appear only once in an expression`, errors, diagnostics, {
|
|
661
|
+
id: 'function.single_use',
|
|
662
|
+
params: { functionName },
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
pushFuncError(call, `function '${functionName}' can appear at most ${maxCalls} times in an expression`, errors, diagnostics, {
|
|
667
|
+
id: 'function.call_limit',
|
|
668
|
+
params: { functionName, maxCalls },
|
|
669
|
+
});
|
|
670
|
+
}
|
|
656
671
|
}
|
|
657
672
|
}
|
|
658
673
|
/**
|
|
@@ -765,6 +780,11 @@ function validateListReference(arg, errors, diagnostics, context) {
|
|
|
765
780
|
});
|
|
766
781
|
}
|
|
767
782
|
}
|
|
783
|
+
/**
|
|
784
|
+
* Checks that a list takes the left side's type. Libreflare supports only IP
|
|
785
|
+
* lists, so a list the config does not give is checked as one, although
|
|
786
|
+
* Cloudflare has lists of other kinds.
|
|
787
|
+
*/
|
|
768
788
|
function validateKnownListRefValueType(node, left, errors, diagnostics, context) {
|
|
769
789
|
const listKind = customList(context.customLists, node.name)?.kind ?? SUPPORTED_LIST_KIND;
|
|
770
790
|
const valueTypes = ruleExpressionValueTypes(left);
|
|
@@ -811,33 +831,13 @@ function validateInlineSetLiteral(node, errors, diagnostics) {
|
|
|
811
831
|
params: { elementTypes: [...valueTypes] },
|
|
812
832
|
});
|
|
813
833
|
}
|
|
814
|
-
|
|
815
|
-
|
|
834
|
+
/** Checks the pattern of a wildcard comparison; see wildcardPatternDiagnostic(). */
|
|
835
|
+
function validateComparisonWildcardPattern(node, errors, diagnostics) {
|
|
836
|
+
if ((node.op !== 'wildcard' && node.op !== 'strict_wildcard') || node.right?.kind !== 'StringLiteral' || node.right.unclosed)
|
|
816
837
|
return;
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
}
|
|
821
|
-
function hasConsecutiveUnescapedWildcards(pattern) {
|
|
822
|
-
let previousWildcard = false;
|
|
823
|
-
for (let index = 0; index < pattern.length; index++) {
|
|
824
|
-
const char = pattern[index];
|
|
825
|
-
if (char === '\\') {
|
|
826
|
-
const next = pattern[index + 1];
|
|
827
|
-
if (next === '*' || next === '\\')
|
|
828
|
-
index++;
|
|
829
|
-
previousWildcard = false;
|
|
830
|
-
continue;
|
|
831
|
-
}
|
|
832
|
-
if (char === '*') {
|
|
833
|
-
if (previousWildcard)
|
|
834
|
-
return true;
|
|
835
|
-
previousWildcard = true;
|
|
836
|
-
continue;
|
|
837
|
-
}
|
|
838
|
-
previousWildcard = false;
|
|
839
|
-
}
|
|
840
|
-
return false;
|
|
838
|
+
const problem = wildcardPatternDiagnostic(node.right.value);
|
|
839
|
+
if (problem)
|
|
840
|
+
pushValueError(node.right, problem.message, errors, diagnostics, problem.metadata);
|
|
841
841
|
}
|
|
842
842
|
function pushComparisonError(node, requirement, errors, diagnostics, metadata) {
|
|
843
843
|
const message = `${requirement}`;
|
|
@@ -911,12 +911,12 @@ function validateFunctionSupport(functionName, diagnostics) {
|
|
|
911
911
|
* however its result is used, as in `len(join(http.request.headers.names, ""))`,
|
|
912
912
|
* which would take tracking the string it gives.
|
|
913
913
|
*/
|
|
914
|
-
function validateHeaderOrderReads(node, diagnostics, expression) {
|
|
914
|
+
function validateHeaderOrderReads(node, diagnostics, expression, context) {
|
|
915
915
|
const fields = node.name === 'join'
|
|
916
916
|
? orderedHeaderFields(node.args[0], true)
|
|
917
917
|
: node.accessPath[0]?.kind === 'ArrayIndexAccess' ? resultHeaderFields(node) : [];
|
|
918
918
|
for (const field of fields)
|
|
919
|
-
pushHeaderOrderWarning(field, diagnostics, expression);
|
|
919
|
+
pushHeaderOrderWarning(field, diagnostics, expression, context);
|
|
920
920
|
}
|
|
921
921
|
/**
|
|
922
922
|
* The header name or value fields whose order a value keeps whole. `first`
|
|
@@ -941,13 +941,15 @@ function orderedHeaderFields(node, first) {
|
|
|
941
941
|
* The header name or value fields whose order a call's result keeps, before
|
|
942
942
|
* its own access: a call that maps over its first argument keeps that
|
|
943
943
|
* argument's order, coalesce() gives one of its arguments as is, and
|
|
944
|
-
* concat() joins its arguments in turn.
|
|
944
|
+
* concat() joins its arguments in turn. filter() keeps some of its array's
|
|
945
|
+
* elements in that array's order.
|
|
945
946
|
*/
|
|
946
947
|
function resultHeaderFields(node) {
|
|
947
948
|
if (node.name === 'coalesce' || node.name === 'concat') {
|
|
948
949
|
return node.args.flatMap((arg, index) => orderedHeaderFields(arg, index === 0));
|
|
949
950
|
}
|
|
950
|
-
|
|
951
|
+
const keepsOrder = node.name === 'filter' || inferFunctionInvocation(node).mapping === 'mapped';
|
|
952
|
+
return keepsOrder ? orderedHeaderFields(node.args[0], true) : [];
|
|
951
953
|
}
|
|
952
954
|
/**
|
|
953
955
|
* Whether a field lists the request headers in an order Worker context may
|
|
@@ -962,8 +964,11 @@ function isRebuiltHeaderArray(name) {
|
|
|
962
964
|
* Warns about a read of the request header names or values in order, which
|
|
963
965
|
* a challenge solve may get wrong. has_value(), len(), and any() and all()
|
|
964
966
|
* over arrays that line up do not depend on it; see validateConditionPairing().
|
|
967
|
+
* A Custom Rule is never evaluated in Worker context, so it gets no warning.
|
|
965
968
|
*/
|
|
966
|
-
function pushHeaderOrderWarning(node, diagnostics, expression) {
|
|
969
|
+
function pushHeaderOrderWarning(node, diagnostics, expression, context) {
|
|
970
|
+
if (context.preset === 'custom-rule')
|
|
971
|
+
return;
|
|
967
972
|
// Reads can reach a field more than once, as an index on a call and the pairing of its arguments do.
|
|
968
973
|
if (diagnostics.some(diagnostic => diagnostic.id === 'field.header_order_unreliable' && diagnostic.pos === node.pos))
|
|
969
974
|
return;
|
|
@@ -1006,36 +1011,43 @@ function validateCoalesceFallbacks(node, binding, diagnostics) {
|
|
|
1006
1011
|
source: 'expression',
|
|
1007
1012
|
});
|
|
1008
1013
|
}
|
|
1009
|
-
function validateFieldOrFunc(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
1014
|
+
function validateFieldOrFunc(node, guardedHeaders, errors, diagnostics, expression, context, counted = false) {
|
|
1010
1015
|
if (isFieldNode(node)) {
|
|
1011
1016
|
validateFieldNode(node, guardedHeaders, errors, diagnostics, expression, context);
|
|
1012
1017
|
return;
|
|
1013
1018
|
}
|
|
1014
|
-
validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, 'value');
|
|
1019
|
+
validateSemanticNode(node, guardedHeaders, errors, diagnostics, expression, context, 'value', counted);
|
|
1015
1020
|
}
|
|
1016
|
-
function validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context,
|
|
1021
|
+
function validateFuncArgs(node, guardedHeaders, errors, diagnostics, expression, context, countsArguments = false) {
|
|
1017
1022
|
for (let index = 0; index < node.args.length; index++) {
|
|
1018
1023
|
const arg = node.args[index];
|
|
1019
1024
|
if (isFieldNode(arg)) {
|
|
1020
1025
|
validateFieldNode(arg, guardedHeaders, errors, diagnostics, expression, context);
|
|
1021
1026
|
}
|
|
1022
1027
|
else if (isExpressionNode(arg)) {
|
|
1023
|
-
|
|
1028
|
+
// How many elements filter() keeps depends on its conditions, and other calls have no other arrays to count.
|
|
1029
|
+
const counted = countsArguments && (index === 0 || node.name === 'coalesce' || node.name === 'concat');
|
|
1030
|
+
validateSemanticNode(arg, guardedHeaders, errors, diagnostics, expression, context, isConditionArgument(arg) ? 'argument' : 'value', counted);
|
|
1024
1031
|
}
|
|
1025
1032
|
else if (arg.kind === 'ListRef') {
|
|
1026
1033
|
validateListReference(arg, errors, diagnostics, context);
|
|
1027
1034
|
}
|
|
1028
1035
|
}
|
|
1029
1036
|
}
|
|
1030
|
-
/**
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1037
|
+
/** Functions whose result len() counts by counting the arrays among their arguments, without mapping over one. */
|
|
1038
|
+
const COUNTED_FUNCTIONS = new Set(['coalesce', 'concat', 'filter']);
|
|
1039
|
+
/** The arguments whose results settle how many elements a call gives: filter()'s conditions, unless it maps. */
|
|
1040
|
+
function selectingArguments(node) {
|
|
1041
|
+
return node.name === 'filter' && inferFunctionInvocation(node).mapping !== 'mapped' ? node.args.slice(1) : [];
|
|
1042
|
+
}
|
|
1043
|
+
/** Whether len() only counts the arrays among a call's arguments, where `counted` says that it only counts the call's. */
|
|
1044
|
+
function countsArrayArguments(node, counted) {
|
|
1045
|
+
const mapped = inferFunctionInvocation(node).mapping === 'mapped';
|
|
1046
|
+
if (node.name === 'len' && !mapped)
|
|
1047
|
+
return true;
|
|
1048
|
+
if (!counted || !readsWholeArray(node.accessPath))
|
|
1049
|
+
return false;
|
|
1050
|
+
return mapped ? keepsPlacesWhenMapped(node.name) : COUNTED_FUNCTIONS.has(node.name);
|
|
1039
1051
|
}
|
|
1040
1052
|
function validateQuantifierPredicateArgs(node, guardedHeaders, errors, diagnostics, expression, context) {
|
|
1041
1053
|
const reading = readQuantifierPredicate(node);
|
|
@@ -1071,8 +1083,27 @@ function validateQuantifierComparisonOperands(comparison, guardedHeaders, errors
|
|
|
1071
1083
|
validateFieldOrFunc(value, guardedHeaders, errors, diagnostics, expression, context);
|
|
1072
1084
|
if (value?.kind === 'ListRef')
|
|
1073
1085
|
validateListRef(value, comparison.left, comparison.op, errors, diagnostics, context);
|
|
1086
|
+
validateComparisonWildcardPattern(comparison, errors, diagnostics);
|
|
1087
|
+
validateDroppedHeaderNames(comparison, diagnostics);
|
|
1088
|
+
}
|
|
1089
|
+
/** Gives a notice where a comparison an argument combines looks a request header up by name, as validateDroppedHeaderNames() does. */
|
|
1090
|
+
function validateDroppedHeaderConditions(node, args, diagnostics) {
|
|
1091
|
+
// any() and all() check their own comparisons.
|
|
1092
|
+
if (node.name === 'any' || node.name === 'all')
|
|
1093
|
+
return;
|
|
1094
|
+
for (const arg of args) {
|
|
1095
|
+
if (!isExpressionNode(arg) || !isConditionArgument(arg))
|
|
1096
|
+
continue;
|
|
1097
|
+
for (const { node: condition } of quantifierPredicateConditions(arg)) {
|
|
1098
|
+
if (condition.kind === 'Comparison')
|
|
1099
|
+
validateDroppedHeaderNames(condition, diagnostics);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
/** Gives a notice where a comparison over the request header names looks one up by name; see pushDroppedHeaderNotice(). */
|
|
1104
|
+
function validateDroppedHeaderNames(comparison, diagnostics) {
|
|
1074
1105
|
// Only a String compared with each whole name looks a header up, so other comparisons skip the walk.
|
|
1075
|
-
const literals = NAME_LOOKUP_OPERATORS.has(comparison.op) ? stringLiterals(
|
|
1106
|
+
const literals = NAME_LOOKUP_OPERATORS.has(comparison.op) ? stringLiterals(comparison.right) : [];
|
|
1076
1107
|
const names = literals.length > 0 ? headerCollection(comparison.left) : null;
|
|
1077
1108
|
if (names?.kind === 'names') {
|
|
1078
1109
|
for (const literal of literals) {
|
|
@@ -1101,14 +1132,16 @@ function validateDroppedHeaderLookups(node, diagnostics) {
|
|
|
1101
1132
|
/**
|
|
1102
1133
|
* Gives a notice where a rule reads a request header that Cloudflare drops
|
|
1103
1134
|
* when it rebuilds a challenged request after a solve, such as Pragma. The
|
|
1104
|
-
* rule never sees the header on the rebuilt request, so a URL rewrite
|
|
1105
|
-
* depends on it no longer applies, but whether a rule's
|
|
1106
|
-
* is not known here. A read that can never match the header is left out, as a
|
|
1135
|
+
* rule never sees the header on the rebuilt request, so a URL rewrite or a
|
|
1136
|
+
* Custom Rule that depends on it no longer applies, but whether a rule's
|
|
1137
|
+
* traffic is challenged is not known here. A read that can never match the header is left out, as a
|
|
1107
1138
|
* solve does not change its result. Only a comparison with the whole name
|
|
1108
1139
|
* counts: patterns and string functions over the names, such as `contains`,
|
|
1109
1140
|
* `matches`, starts_with() and join(), get no notice, as one that matches the
|
|
1110
1141
|
* header's name usually matches others too, and neither do ordering
|
|
1111
|
-
* comparisons, or reads by position, which the order warning covers.
|
|
1142
|
+
* comparisons, or reads by position, which the order warning covers. Nor do
|
|
1143
|
+
* lookups among names a call such as filter() or concat() gives, as
|
|
1144
|
+
* headerCollection() reads only the fields, in one case or as they are.
|
|
1112
1145
|
*/
|
|
1113
1146
|
function pushDroppedHeaderNotice(name, node, diagnostics) {
|
|
1114
1147
|
const headerName = name.toLowerCase();
|
|
@@ -1124,18 +1157,20 @@ function pushDroppedHeaderNotice(name, node, diagnostics) {
|
|
|
1124
1157
|
});
|
|
1125
1158
|
}
|
|
1126
1159
|
/**
|
|
1127
|
-
* Warns when a call
|
|
1128
|
-
*
|
|
1129
|
-
* for each element
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
1133
|
-
* `counted` says that len() only
|
|
1160
|
+
* Warns when a call pairs arrays whose elements may not line up: those of a
|
|
1161
|
+
* quantifier's predicate, or of an argument read as a condition with a result
|
|
1162
|
+
* for each element, or filter()'s array and those of its conditions.
|
|
1163
|
+
* Cloudflare pairs them by index and stops at the shorter one, so conditions
|
|
1164
|
+
* on arrays from different lists rarely mean what they say. Such pairs also
|
|
1165
|
+
* depend on the order of each array, so the request header names and values
|
|
1166
|
+
* among them get the order warning, unless `counted` says that len() only
|
|
1167
|
+
* counts the results.
|
|
1134
1168
|
*/
|
|
1135
|
-
function validateConditionPairing(node, diagnostics, expression, counted) {
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1169
|
+
function validateConditionPairing(node, diagnostics, expression, context, counted) {
|
|
1170
|
+
// A filter() that maps pairs the conditions with each array in a list, which no list lines up with.
|
|
1171
|
+
const mapped = node.name === 'filter' && inferFunctionInvocation(node).mapping === 'mapped';
|
|
1172
|
+
for (const arrays of pairedArrays(node)) {
|
|
1173
|
+
const unaligned = mapped ? arrays[1] : unalignedArray(arrays);
|
|
1139
1174
|
if (!unaligned)
|
|
1140
1175
|
continue;
|
|
1141
1176
|
diagnostics.push({
|
|
@@ -1146,25 +1181,44 @@ function validateConditionPairing(node, diagnostics, expression, counted) {
|
|
|
1146
1181
|
length: typeof unaligned.length === 'number' ? unaligned.length : 0,
|
|
1147
1182
|
source: 'expression',
|
|
1148
1183
|
});
|
|
1184
|
+
// How many results there are does not depend on the arrays' order, but how many filter() keeps depends on
|
|
1185
|
+
// its conditions'. Where it maps, or its result is only counted, its own array gives only their order.
|
|
1186
|
+
let ordered = arrays;
|
|
1149
1187
|
if (counted)
|
|
1150
|
-
|
|
1151
|
-
|
|
1188
|
+
ordered = selectingArguments(node).flatMap(pairedArgumentArrays);
|
|
1189
|
+
else if (mapped)
|
|
1190
|
+
ordered = node.args.slice(1).flatMap(pairedArgumentArrays);
|
|
1191
|
+
for (const array of ordered) {
|
|
1152
1192
|
for (const field of orderedHeaderFields(array, true))
|
|
1153
|
-
pushHeaderOrderWarning(field, diagnostics, expression);
|
|
1193
|
+
pushHeaderOrderWarning(field, diagnostics, expression, context);
|
|
1154
1194
|
}
|
|
1155
1195
|
}
|
|
1156
1196
|
}
|
|
1157
|
-
/**
|
|
1158
|
-
|
|
1197
|
+
/**
|
|
1198
|
+
* The arrays a call pairs by index, in lists of those paired with each other:
|
|
1199
|
+
* the arrays of a quantifier's predicate read as conditions, those of each
|
|
1200
|
+
* argument that is a condition with a result for each element, or all of
|
|
1201
|
+
* filter()'s.
|
|
1202
|
+
*/
|
|
1203
|
+
function pairedArrays(node) {
|
|
1159
1204
|
if (node.name === 'any' || node.name === 'all') {
|
|
1160
1205
|
const reading = readQuantifierPredicate(node);
|
|
1161
|
-
return reading?.kind === 'conditions' && !isUncertainArgument(node, 0) ? [reading.predicate] : [];
|
|
1206
|
+
return reading?.kind === 'conditions' && !isUncertainArgument(node, 0) ? [predicateArrays(reading.predicate)] : [];
|
|
1162
1207
|
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1208
|
+
if (node.name === 'filter') {
|
|
1209
|
+
// An invalid argument, such as one whose access failed to parse, has an error of its own.
|
|
1210
|
+
const uncertain = node.args.some((arg, index) => isUncertainArgument(node, index) || inferRuleExpressionArgument(arg).invalid);
|
|
1211
|
+
return uncertain ? [] : [node.args.flatMap(pairedArgumentArrays)];
|
|
1212
|
+
}
|
|
1213
|
+
return node.args
|
|
1214
|
+
.filter((arg, index) => isArrayConditionArgument(arg) && !isUncertainArgument(node, index))
|
|
1215
|
+
.map(predicateArrays);
|
|
1216
|
+
}
|
|
1217
|
+
/** Reports arguments the function's signature or the preset's phase rejects, and returns whether there were none. */
|
|
1218
|
+
function validateFunctionArgRequirements(node, binding, errors, diagnostics, context) {
|
|
1219
|
+
let signatureErrors = validateFunctionSignature(node, binding);
|
|
1220
|
+
if (signatureErrors.length === 0)
|
|
1221
|
+
signatureErrors = validatePresetArgumentFields(node, binding, context.preset);
|
|
1168
1222
|
for (const signatureError of signatureErrors) {
|
|
1169
1223
|
pushFunctionSignatureError(signatureError, errors, diagnostics);
|
|
1170
1224
|
}
|
|
@@ -1199,7 +1253,7 @@ function validateFieldNode(node, guardedHeaders, errors, diagnostics, expression
|
|
|
1199
1253
|
validateFieldAvailability(node, errors, diagnostics, expression, context);
|
|
1200
1254
|
validateDeprecatedField(node, diagnostics, expression, context);
|
|
1201
1255
|
if (isRebuiltHeaderArray(node.name) && node.accessPath[0]?.kind === 'ArrayIndexAccess') {
|
|
1202
|
-
pushHeaderOrderWarning(node, diagnostics, expression);
|
|
1256
|
+
pushHeaderOrderWarning(node, diagnostics, expression, context);
|
|
1203
1257
|
}
|
|
1204
1258
|
// A guard for a rejected key would only repeat its error.
|
|
1205
1259
|
if (!REQUEST_HEADER_MAPS.has(node.name) || !lowercaseKey)
|