@libreflare/worker-runtime 0.10.4 → 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 +80 -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/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 +5 -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 +13 -4
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +109 -25
- 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 +465 -167
- package/dist/rule-evaluator/functions.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 +152 -77
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator/wildcard.d.ts +6 -3
- package/dist/rule-evaluator/wildcard.d.ts.map +1 -1
- package/dist/rule-evaluator/wildcard.js +10 -4
- package/dist/rule-evaluator/wildcard.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,18 +1,38 @@
|
|
|
1
|
-
import { EQUALITY_ORDERING_OPERATORS, isConditionArgument, isDynamicValueNode, unhandledNode, } from './ast.js';
|
|
2
|
-
import { applyRuntimeAccessPath, isUnknownElement, PartialArrayValue, RuleMapValue, } from './access-values.js';
|
|
1
|
+
import { EQUALITY_ORDERING_OPERATORS, isConditionArgument, isDynamicValueNode, isExpressionNode, unhandledNode, } from './ast.js';
|
|
2
|
+
import { applyRuntimeAccessPath, inPlaceOf, isUnknownElement, listedElement, OptionalElement, PartialArrayValue, RuleMapValue, } from './access-values.js';
|
|
3
3
|
import { byteLength, bytesToText, canonicalText, textToBytes } from './byte-strings.js';
|
|
4
4
|
import { formatIpAddress, ipAddressOctets } from './custom-lists.js';
|
|
5
5
|
import { resolveBoolField, resolveFieldNode } from './fields.js';
|
|
6
6
|
import { hashInRange } from './hash-in-range.js';
|
|
7
7
|
import { applyOp, compareOperands, findMatchingSetMember, literalValue, reachableSetMembers } from './operators.js';
|
|
8
8
|
import { andOutcome, incomparable, isUnknown, known, negateOutcome, orOutcome, outcomeValue, unavailable, valueOutcome, xorOutcome, } from './outcome.js';
|
|
9
|
-
import { conditionArray, extractQuantifierPredicate, givesNilForNilArgument, keepsPlacesWhenMapped, quantifierPredicateConditions, readQuantifierPredicate, unalignedArray, } from './quantifier.js';
|
|
9
|
+
import { conditionArray, extractQuantifierPredicate, givesNilForNilArgument, keepsPlacesWhenMapped, pairedArgumentArrays, quantifierPredicateConditions, readQuantifierPredicate, unalignedArray, } from './quantifier.js';
|
|
10
10
|
import { removeQueryArgs } from './query-args.js';
|
|
11
|
+
import { expandReplacement, MAX_REPLACEMENT_REFERENCES, measureReplacement } from './replacement.js';
|
|
11
12
|
import { urlDecode } from './url-decode.js';
|
|
12
13
|
import { compileWildcard, wildcardCaptures } from './wildcard.js';
|
|
13
14
|
import { collectionElementDataType, compareRuleExpressionDataTypes, formatRuleExpressionDataType, evaluatedValueType, inferFunctionInvocation, isArrayConditionArgument, } from './rule-language-types.js';
|
|
15
|
+
/**
|
|
16
|
+
* The most a call's result holds, over all its elements when it maps; see
|
|
17
|
+
* elementSize(). A request's URL and headers fit, while join(), replacements
|
|
18
|
+
* and a mapped concat() can make many megabytes of them, which Cloudflare
|
|
19
|
+
* gives whole. An array keeps the elements that fit, and the others are
|
|
20
|
+
* unknown. It bounds memory rather than accounting for it: what a call holds
|
|
21
|
+
* while it runs can be a few times as much.
|
|
22
|
+
*/
|
|
23
|
+
const MAX_RESULT_SIZE = 512 * 1024;
|
|
24
|
+
/** The fewest characters an array element counts as: even an empty one takes memory, and an unknown one more. */
|
|
25
|
+
const MIN_ELEMENT_SIZE = 8;
|
|
26
|
+
/** The most elements concat() of arrays joins, whatever they hold, as its result shares their text. */
|
|
27
|
+
const MAX_CONCAT_ELEMENTS = MAX_RESULT_SIZE / MIN_ELEMENT_SIZE;
|
|
28
|
+
const RESULT_SIZE_REASON = `its result holds more than ${MAX_RESULT_SIZE} characters, counting each array element as at least ${MIN_ELEMENT_SIZE}`;
|
|
29
|
+
/** A result that holds more, which is not built; see tooLargeResult(). */
|
|
30
|
+
const TOO_LARGE = Symbol('too large');
|
|
31
|
+
/** How much the pieces of a result may hold where joining them can unite their bytes; see surelyTooLarge(). */
|
|
32
|
+
const MAX_JOINED_SIZE = MAX_RESULT_SIZE * 3;
|
|
14
33
|
const UNRELIABLE_FUNCTION_REASONS = {
|
|
15
34
|
bit_slice: 'packet bit slicing is not available in Worker request context',
|
|
35
|
+
hmac_sha256: 'synchronous HMAC-SHA-256 is not available in this evaluator',
|
|
16
36
|
is_jwt_present: 'Cloudflare API JWT token configurations are not available in Worker request context',
|
|
17
37
|
is_jwt_valid: 'Cloudflare API JWT token configurations are not available in Worker request context',
|
|
18
38
|
is_timed_hmac_valid_v0: 'synchronous HMAC validation is not available in this evaluator',
|
|
@@ -39,8 +59,8 @@ function evaluateRuntimeOperand(node, ctx) {
|
|
|
39
59
|
* Narrows a value to a rule-language value. Only coalesce(), has_key(), and
|
|
40
60
|
* postfix access read maps, and validation rejects maps anywhere else, so a
|
|
41
61
|
* map here is unknown. So is a partly known array, which only any(), all(),
|
|
42
|
-
* has_value(), len(), concat(), mapped functions and postfix access
|
|
43
|
-
* part.
|
|
62
|
+
* has_value(), len(), concat(), filter(), mapped functions and postfix access
|
|
63
|
+
* read in part.
|
|
44
64
|
*/
|
|
45
65
|
function ruleValue(evaluated) {
|
|
46
66
|
if (isUnknown(evaluated))
|
|
@@ -95,15 +115,21 @@ export function evaluateComparison(node, ctx) {
|
|
|
95
115
|
*/
|
|
96
116
|
export function evaluateCollection(fieldArg, ctx) {
|
|
97
117
|
const evaluated = evaluateRuntimeOperand(fieldArg, ctx);
|
|
98
|
-
const array = isUnknown(evaluated) ? evaluated :
|
|
118
|
+
const array = isUnknown(evaluated) ? evaluated : arrayValue(evaluated.value);
|
|
99
119
|
if (isUnknown(array))
|
|
100
120
|
return array;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return known(
|
|
121
|
+
if (!(array.value instanceof PartialArrayValue))
|
|
122
|
+
return known({ values: array.value.filter(isCollectionValue) });
|
|
123
|
+
const { elements, more, shortest } = array.value;
|
|
124
|
+
const values = array.value.knownValues();
|
|
125
|
+
const optional = elements.flatMap(listed => listed instanceof OptionalElement && !isUnknownElement(listed.element) ? [listed.element.value] : []);
|
|
126
|
+
return known({
|
|
127
|
+
values: values.filter(isCollectionValue),
|
|
128
|
+
unknown: array.value.firstUnknown(),
|
|
129
|
+
optional: optional.filter(isCollectionValue),
|
|
130
|
+
mayHoldUnknown: more !== undefined || values.length + optional.length < elements.length,
|
|
131
|
+
mayBeEmpty: shortest === 0,
|
|
132
|
+
});
|
|
107
133
|
}
|
|
108
134
|
function isCollectionValue(value) {
|
|
109
135
|
return typeof value === 'string' || typeof value === 'number';
|
|
@@ -195,30 +221,31 @@ function evaluateQuantifier(node, ctx) {
|
|
|
195
221
|
const reading = readQuantifierPredicate(node);
|
|
196
222
|
return reading ? evaluatePairedQuantifier(node, reading, ctx).outcome : false;
|
|
197
223
|
}
|
|
198
|
-
/** A value
|
|
199
|
-
function
|
|
224
|
+
/** A value read as an array: nil has no elements, a scalar is one, and a map's are unknown. */
|
|
225
|
+
function arrayValue(value) {
|
|
200
226
|
if (value instanceof RuleMapValue)
|
|
201
227
|
return unavailable('a map value');
|
|
202
|
-
if (value instanceof PartialArrayValue)
|
|
203
|
-
return known({ elements: value.elements, more: value.more });
|
|
204
228
|
if (value === null)
|
|
205
|
-
return known(
|
|
206
|
-
return known(
|
|
229
|
+
return known([]);
|
|
230
|
+
return known(value instanceof PartialArrayValue || Array.isArray(value) ? value : [value]);
|
|
207
231
|
}
|
|
208
|
-
const NO_PAIRS = { arrays: new Map(),
|
|
232
|
+
const NO_PAIRS = { arrays: new Map(), end: 0, tailFrom: 0 };
|
|
209
233
|
/**
|
|
210
234
|
* Evaluates the collections of a predicate's conditions; see PairedArrays.
|
|
211
235
|
* An empty one leaves no pairs, whatever the others hold, so the ones after
|
|
212
236
|
* it are not read. `valueCall` is a quantifier's predicate read as a value,
|
|
213
|
-
* which is no array when it gives nil.
|
|
237
|
+
* which is no array when it gives nil. `source` is an array that pairs with
|
|
238
|
+
* them.
|
|
214
239
|
*/
|
|
215
|
-
function pairArrays(owner, predicate, ctx, valueCall) {
|
|
240
|
+
function pairArrays(owner, predicate, ctx, valueCall, source) {
|
|
241
|
+
const paired = source?.array;
|
|
242
|
+
if (paired && paired.length === 0 && !paired.more)
|
|
243
|
+
return NO_PAIRS;
|
|
216
244
|
const arrays = new Map();
|
|
217
|
-
let
|
|
218
|
-
let
|
|
219
|
-
let
|
|
220
|
-
let
|
|
221
|
-
let shortestMore;
|
|
245
|
+
let end = paired && !paired.more ? paired.length : Infinity;
|
|
246
|
+
let longest = paired ? paired.length : 0;
|
|
247
|
+
let shortest = paired;
|
|
248
|
+
let partial = paired?.unknownLength !== undefined;
|
|
222
249
|
for (const { node: condition } of quantifierPredicateConditions(predicate)) {
|
|
223
250
|
// Conditions that validation accepts have a result for each element at each comparison and call (see
|
|
224
251
|
// isArrayCondition()), so their kind tells them.
|
|
@@ -230,35 +257,48 @@ function pairArrays(owner, predicate, ctx, valueCall) {
|
|
|
230
257
|
giveWarnings(ctx, warnings);
|
|
231
258
|
return { ...NO_PAIRS, nilCall: valueCall };
|
|
232
259
|
}
|
|
233
|
-
const
|
|
260
|
+
const compare = condition.kind === 'Comparison' ? comparatorFor(owner, condition, ctx) : isTrueValue;
|
|
261
|
+
const array = pairedArray(evaluated, warnings, compare, ctx);
|
|
234
262
|
if (array.length === 0 && !array.more)
|
|
235
263
|
return NO_PAIRS;
|
|
236
264
|
arrays.set(condition, array);
|
|
237
|
-
if (array.
|
|
238
|
-
|
|
239
|
-
shortestMore = array.more;
|
|
240
|
-
}
|
|
265
|
+
if (!shortest || array.shortest < shortest.shortest)
|
|
266
|
+
shortest = array;
|
|
241
267
|
longest = Math.max(longest, array.length);
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
else
|
|
268
|
+
partial ||= array.unknownLength !== undefined;
|
|
269
|
+
if (!array.more)
|
|
245
270
|
end = Math.min(end, array.length);
|
|
246
271
|
}
|
|
247
272
|
// Validation rejects a predicate that compares no collection.
|
|
248
|
-
if (arrays.size === 0)
|
|
273
|
+
if (!shortest || arrays.size === 0)
|
|
249
274
|
return NO_PAIRS;
|
|
250
|
-
|
|
275
|
+
if (!partial)
|
|
276
|
+
return { arrays, end, tailFrom: end };
|
|
277
|
+
const all = paired ? [paired, ...arrays.values()] : [...arrays.values()];
|
|
278
|
+
const tail = end === Infinity ? all[0].more : undefined;
|
|
251
279
|
if (end === Infinity)
|
|
252
280
|
end = longest;
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
array.elements = Array(array.length).fill(array.more);
|
|
259
|
-
}
|
|
281
|
+
if (describeSameItems(arrays, source)) {
|
|
282
|
+
// Each index pairs the elements of one item, which the collections have or lack together.
|
|
283
|
+
const [{ elements, length, shortest: certain }] = all;
|
|
284
|
+
const missing = certain < length ? (index) => missingReason(elements[index]) : undefined;
|
|
285
|
+
return { arrays, end, missing, tail, tailFrom: Math.min(...all.map(array => array.moreFrom ?? array.length)) };
|
|
260
286
|
}
|
|
261
|
-
|
|
287
|
+
// Past its leading elements, a collection that may lack or hold others has its elements at positions it cannot
|
|
288
|
+
// tell, but for the last, which is the only one that can be at the last position when it lists them all.
|
|
289
|
+
for (const array of all) {
|
|
290
|
+
const { leading = array.length, more, unknownLength } = array;
|
|
291
|
+
if (!unknownLength)
|
|
292
|
+
continue;
|
|
293
|
+
const last = more ? -1 : array.length - 1;
|
|
294
|
+
array.elements = array.elements.map((listed, index) => index < leading || index === last ? listed : unknownLength);
|
|
295
|
+
}
|
|
296
|
+
const { shortest: certain, unknownLength } = shortest;
|
|
297
|
+
return { arrays, end, missing: index => index < certain ? undefined : unknownLength, tail, tailFrom: end };
|
|
298
|
+
}
|
|
299
|
+
/** Why an array may lack an element it lists, when it may. */
|
|
300
|
+
function missingReason(listed) {
|
|
301
|
+
return listed instanceof OptionalElement ? listed.reason : undefined;
|
|
262
302
|
}
|
|
263
303
|
/**
|
|
264
304
|
* Evaluates any() or all() as Cloudflare does, over the pairs of its
|
|
@@ -268,12 +308,12 @@ function pairArrays(owner, predicate, ctx, valueCall) {
|
|
|
268
308
|
export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
269
309
|
const settling = node.name === 'any';
|
|
270
310
|
const { predicate } = reading;
|
|
271
|
-
const { arrays,
|
|
311
|
+
const { arrays, end, missing, tail, nilCall } = pairArrays(node, predicate, ctx, reading.kind === 'value' ? reading.predicate : undefined);
|
|
272
312
|
// A call read as a value that gives nil is no array, which neither any() nor all() holds for; see
|
|
273
313
|
// QuantifierPredicateReading.
|
|
274
314
|
if (nilCall)
|
|
275
315
|
return { outcome: false, paired: 0, mayLackSome: false, mayHaveMore: false, nilCall };
|
|
276
|
-
const result = (outcome) => ({ outcome, paired: end, mayLackSome: end
|
|
316
|
+
const result = (outcome) => ({ outcome, paired: end, mayLackSome: firstMissing(end, missing) !== undefined, mayHaveMore: tail !== undefined });
|
|
277
317
|
// The unknown outcomes of known elements, from every position, and the first outcome that reads an unknown
|
|
278
318
|
// element, which is combined after them, so that an unknown comparison value, which applies at every
|
|
279
319
|
// element, names the cause before one element does.
|
|
@@ -281,14 +321,12 @@ export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
|
281
321
|
let pending;
|
|
282
322
|
for (let index = 0; index < end; index++) {
|
|
283
323
|
let outcome = pairedOutcome(predicate, index, arrays);
|
|
284
|
-
if (
|
|
285
|
-
//
|
|
286
|
-
|
|
287
|
-
if (
|
|
288
|
-
outcome
|
|
289
|
-
|
|
290
|
-
else if (outcome === settling) {
|
|
291
|
-
return result(outcome);
|
|
324
|
+
if (outcome === settling) {
|
|
325
|
+
// One that may be missing settles the result only if it is there.
|
|
326
|
+
const reason = missing?.(index);
|
|
327
|
+
if (!reason)
|
|
328
|
+
return result(outcome);
|
|
329
|
+
outcome = reason;
|
|
292
330
|
}
|
|
293
331
|
if (typeof outcome === 'function')
|
|
294
332
|
pending ??= outcome;
|
|
@@ -306,71 +344,108 @@ export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
|
306
344
|
/**
|
|
307
345
|
* Evaluates a function argument read as a condition with a result for each
|
|
308
346
|
* element: the array of its results, at the pairs of its collections (see
|
|
309
|
-
* PairedArrays), with none over nil. An unknown result keeps its place
|
|
310
|
-
* when the shortest collection may hold more, so may the array, which lists
|
|
311
|
-
* the results that may follow when it knows them all.
|
|
347
|
+
* PairedArrays), with none over nil. An unknown result keeps its place.
|
|
312
348
|
*/
|
|
313
349
|
function evaluateConditionArray(node, ctx) {
|
|
314
|
-
const
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
results.push(resultElement(node, index, arrays, ctx));
|
|
322
|
-
return known(PartialArrayValue.from(results.slice(0, certain), moreResults(pairing), tail ? undefined : results.slice(certain)));
|
|
350
|
+
const { arrays, end, missing, tail, tailFrom } = pairArrays(node, node, ctx);
|
|
351
|
+
const results = resultElements(node, end, arrays, ctx);
|
|
352
|
+
return known(PartialArrayValue.from(missing ? results.map((result, index) => optionalElement(result, missing(index))) : results, tail, tailFrom));
|
|
353
|
+
}
|
|
354
|
+
/** An element that the array may lack for a reason, when there is one. */
|
|
355
|
+
function optionalElement(element, reason) {
|
|
356
|
+
return reason ? new OptionalElement(element, reason) : element;
|
|
323
357
|
}
|
|
324
358
|
/** The number of results evaluateConditionArray() gives, which reads none of them. */
|
|
325
359
|
function countConditionResults(node, ctx) {
|
|
326
|
-
const
|
|
327
|
-
return
|
|
360
|
+
const { end, missing, tail } = pairArrays(node, node, ctx);
|
|
361
|
+
return (firstMissing(end, missing) ?? tail)?.() ?? known(end);
|
|
328
362
|
}
|
|
329
|
-
/** Why
|
|
330
|
-
function
|
|
331
|
-
|
|
363
|
+
/** Why the first pair that may be missing may be, when one may. */
|
|
364
|
+
function firstMissing(end, missing) {
|
|
365
|
+
if (!missing)
|
|
366
|
+
return undefined;
|
|
367
|
+
for (let index = 0; index < end; index++) {
|
|
368
|
+
const reason = missing(index);
|
|
369
|
+
if (reason)
|
|
370
|
+
return reason;
|
|
371
|
+
}
|
|
372
|
+
return undefined;
|
|
332
373
|
}
|
|
333
374
|
/**
|
|
334
|
-
* The
|
|
335
|
-
* not known settles nothing, and tells why only when
|
|
336
|
-
* element does.
|
|
375
|
+
* The results at the first `count` indexes as elements of the array of
|
|
376
|
+
* results. One that is not known settles nothing, and tells why only when
|
|
377
|
+
* read, as an unknown element does. Results unknown for one cause, as where
|
|
378
|
+
* they compare with an unknown value, share an element, as there can be
|
|
379
|
+
* thousands.
|
|
337
380
|
*/
|
|
338
|
-
function
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
|
|
381
|
+
function resultElements(node, count, arrays, ctx) {
|
|
382
|
+
const results = [];
|
|
383
|
+
let last;
|
|
384
|
+
for (let index = 0; index < count; index++) {
|
|
385
|
+
const { result: outcome, warnings } = collectWarnings(ctx, () => pairedOutcome(node, index, arrays));
|
|
386
|
+
if (typeof outcome === 'boolean') {
|
|
387
|
+
giveWarnings(ctx, warnings);
|
|
388
|
+
results.push(known(outcome));
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const cause = typeof outcome === 'function' ? outcome : outcome.cause;
|
|
392
|
+
if (last?.cause !== cause || !sameWarnings(last.warnings, warnings)) {
|
|
393
|
+
const read = () => outcomeValue(typeof outcome === 'function' ? outcome() : outcome);
|
|
394
|
+
last = { cause, warnings, element: deferWarnings(ctx, warnings, read) };
|
|
395
|
+
}
|
|
396
|
+
results.push(last.element);
|
|
342
397
|
}
|
|
343
|
-
|
|
344
|
-
|
|
398
|
+
return results;
|
|
399
|
+
}
|
|
400
|
+
function sameWarnings(left, right) {
|
|
401
|
+
return left.length === right.length && left.every((warning, index) => warning === right[index]);
|
|
345
402
|
}
|
|
346
403
|
/**
|
|
347
404
|
* Whether a predicate's collections describe the same item at each index:
|
|
348
|
-
* they come from one list (see pairedArrayList()), and
|
|
349
|
-
*
|
|
350
|
-
* a Set-Cookie value repeated past a cut header list,
|
|
405
|
+
* they come from one list (see pairedArrayList()), and list as many elements,
|
|
406
|
+
* and may lack the same ones. An element dropped, as a mapped call's nil
|
|
407
|
+
* result, or added, as a Set-Cookie value repeated past a cut header list,
|
|
408
|
+
* changes how many there are.
|
|
351
409
|
*/
|
|
352
|
-
function describeSameItems(arrays,
|
|
353
|
-
|
|
354
|
-
|
|
410
|
+
function describeSameItems(arrays, source) {
|
|
411
|
+
if (arrays.size === 1 && !source)
|
|
412
|
+
return true;
|
|
413
|
+
const nodes = [...arrays.keys()].map(conditionArray);
|
|
414
|
+
const all = [...arrays.values()];
|
|
415
|
+
if (source) {
|
|
416
|
+
nodes.push(...pairedArgumentArrays(source.node));
|
|
417
|
+
all.push(source.array);
|
|
418
|
+
}
|
|
419
|
+
const [first] = all;
|
|
420
|
+
for (const { elements, length, shortest } of all) {
|
|
421
|
+
if (length !== first.length || shortest !== first.shortest)
|
|
422
|
+
return false;
|
|
423
|
+
if (shortest === length)
|
|
424
|
+
continue;
|
|
425
|
+
if (elements.some((listed, index) => (listed instanceof OptionalElement) !== (first.elements[index] instanceof OptionalElement)))
|
|
355
426
|
return false;
|
|
356
427
|
}
|
|
357
|
-
return !unalignedArray(
|
|
358
|
-
}
|
|
359
|
-
/**
|
|
360
|
-
function
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
428
|
+
return !unalignedArray(nodes);
|
|
429
|
+
}
|
|
430
|
+
/** Whether a call's result holds at an element: where it is true. */
|
|
431
|
+
function isTrueValue(value) {
|
|
432
|
+
return value === true;
|
|
433
|
+
}
|
|
434
|
+
/** A condition's array, or a PairedSource's, from its evaluated collection and the warnings evaluating it gave. */
|
|
435
|
+
function pairedArray(evaluated, warnings, compare, ctx) {
|
|
436
|
+
const array = isUnknown(evaluated) ? evaluated : arrayValue(evaluated.value);
|
|
437
|
+
if (isUnknown(array)) {
|
|
438
|
+
// A collection unknown whole tells why only when read, since the other collections may settle the result.
|
|
439
|
+
const more = deferWarnings(ctx, warnings, () => array);
|
|
440
|
+
return { values: undefined, elements: [], length: 0, shortest: 0, more, unknownLength: more, compare };
|
|
366
441
|
}
|
|
367
|
-
const array = isUnknown(evaluated) ? evaluated : arrayElements(evaluated.value);
|
|
368
|
-
// A collection unknown whole tells why only when read, since the other collections may settle the result.
|
|
369
|
-
if (isUnknown(array))
|
|
370
|
-
return { values: undefined, elements: [], length: 0, more: deferWarnings(ctx, warnings, () => array), compare };
|
|
371
442
|
giveWarnings(ctx, warnings);
|
|
372
|
-
|
|
373
|
-
|
|
443
|
+
if (!(array.value instanceof PartialArrayValue)) {
|
|
444
|
+
const { length } = array.value;
|
|
445
|
+
return { values: array.value, elements: [], length, shortest: length, compare };
|
|
446
|
+
}
|
|
447
|
+
const { elements, shortest, more, moreFrom, leading, unknownLength } = array.value;
|
|
448
|
+
return { values: undefined, elements, length: elements.length, shortest, more, moreFrom, leading, unknownLength, compare };
|
|
374
449
|
}
|
|
375
450
|
/**
|
|
376
451
|
* Runs `compute` with warnings of its own, to give when its result is read:
|
|
@@ -428,9 +503,13 @@ function elementOutcome(array, index) {
|
|
|
428
503
|
// A collection known whole holds no more, so it reaches every position read.
|
|
429
504
|
if (array.values)
|
|
430
505
|
return array.compare(array.values[index]);
|
|
431
|
-
const element = array
|
|
506
|
+
const element = elementAt(array, index);
|
|
432
507
|
return isUnknownElement(element) ? element : array.compare(element.value);
|
|
433
508
|
}
|
|
509
|
+
/** The element at an index of a collection known in part, unknown where it lists none. */
|
|
510
|
+
function elementAt(array, index) {
|
|
511
|
+
return index < array.length ? listedElement(array.elements[index]) : array.more;
|
|
512
|
+
}
|
|
434
513
|
/** Combines operands at an index, stopping at the first known one that decides the result, if any does. */
|
|
435
514
|
function combinePairedOutcomes(operands, deciding, combine, index, arrays) {
|
|
436
515
|
const outcomes = [];
|
|
@@ -496,10 +575,41 @@ export function evaluateFuncValue(node, ctx) {
|
|
|
496
575
|
const evaluated = invocation.mapping === 'mapped'
|
|
497
576
|
? evaluateFuncArray(node, ctx, invocation)
|
|
498
577
|
: evaluateDirectFuncValue(node, ctx, invocation);
|
|
578
|
+
if (evaluated === TOO_LARGE)
|
|
579
|
+
return tooLargeResult(ctx, node.name);
|
|
499
580
|
if (isUnknown(evaluated))
|
|
500
581
|
return evaluated;
|
|
582
|
+
// Only concat() and mapped calls can make an array too large, and they count it as they get its elements.
|
|
583
|
+
if (typeof evaluated.value === 'string' && evaluated.value.length > MAX_RESULT_SIZE)
|
|
584
|
+
return tooLargeResult(ctx, node.name);
|
|
501
585
|
return applyRuntimeAccessPath(evaluated.value, node.accessPath);
|
|
502
586
|
}
|
|
587
|
+
/** How many characters an array element counts as. An unknown one is not read, as reading it warns. */
|
|
588
|
+
function elementSize(element) {
|
|
589
|
+
return isUnknownElement(element) ? MIN_ELEMENT_SIZE : valueSize(element.value);
|
|
590
|
+
}
|
|
591
|
+
function valueSize(value) {
|
|
592
|
+
let size = 0;
|
|
593
|
+
if (typeof value === 'string')
|
|
594
|
+
size = value.length;
|
|
595
|
+
else if (Array.isArray(value))
|
|
596
|
+
for (const element of value)
|
|
597
|
+
size += valueSize(element);
|
|
598
|
+
return Math.max(size, MIN_ELEMENT_SIZE);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Whether a result joined from pieces that hold `size` characters in all is
|
|
602
|
+
* surely too large. Joining text can unite bytes into characters, which leaves
|
|
603
|
+
* a third of their number at least, so where a piece is text with such bytes,
|
|
604
|
+
* only a result past MAX_JOINED_SIZE surely is.
|
|
605
|
+
*/
|
|
606
|
+
function surelyTooLarge(size, ...pieces) {
|
|
607
|
+
if (size <= MAX_RESULT_SIZE)
|
|
608
|
+
return false;
|
|
609
|
+
if (size > MAX_JOINED_SIZE)
|
|
610
|
+
return true;
|
|
611
|
+
return !pieces.some(list => list.some(piece => typeof piece === 'string' && !piece.isWellFormed()));
|
|
612
|
+
}
|
|
503
613
|
/**
|
|
504
614
|
* Evaluates a call on its arguments, or one that maps on one element:
|
|
505
615
|
* `element` is then its first argument, and `later` keeps the arguments
|
|
@@ -520,16 +630,12 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
520
630
|
case 'any':
|
|
521
631
|
return outcomeValue(evaluateQuantifier(node, ctx));
|
|
522
632
|
case 'concat': {
|
|
523
|
-
const parts =
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (isUnknown(part))
|
|
527
|
-
return part;
|
|
528
|
-
parts.push(part.value);
|
|
529
|
-
}
|
|
633
|
+
const parts = readConcatArguments(args.length, runtimeArgument, read, () => tooLargeResult(ctx, name));
|
|
634
|
+
if (parts === TOO_LARGE || isUnknown(parts))
|
|
635
|
+
return parts;
|
|
530
636
|
// An array known in part keeps its known elements, as any() and all() read them.
|
|
531
|
-
if (parts.some(part => part instanceof PartialArrayValue))
|
|
532
|
-
return concatElements(parts, invocation.argumentTypes);
|
|
637
|
+
if (parts.value.some(part => part instanceof PartialArrayValue))
|
|
638
|
+
return concatElements(parts.value, invocation.argumentTypes);
|
|
533
639
|
break;
|
|
534
640
|
}
|
|
535
641
|
case 'coalesce':
|
|
@@ -540,6 +646,8 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
540
646
|
return value;
|
|
541
647
|
}
|
|
542
648
|
return known(null);
|
|
649
|
+
case 'filter':
|
|
650
|
+
return later ? filterValues(element, argument(1)) : evaluateFilter(node, ctx);
|
|
543
651
|
case 'has_key': {
|
|
544
652
|
// A nil key gives nil without reading the map, as Cloudflare documents.
|
|
545
653
|
const key = argument(1);
|
|
@@ -558,7 +666,7 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
558
666
|
return collection;
|
|
559
667
|
// A partly known array's known elements can settle it, as they do any().
|
|
560
668
|
const partial = collection.value instanceof PartialArrayValue ? collection.value : undefined;
|
|
561
|
-
const values = partial ? known(partial.knownValues()
|
|
669
|
+
const values = partial ? known(partial.knownValues()) : ruleValue(collection);
|
|
562
670
|
if (isUnknown(values))
|
|
563
671
|
return values;
|
|
564
672
|
const value = argument(1);
|
|
@@ -578,9 +686,9 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
578
686
|
const source = runtimeArgument(0);
|
|
579
687
|
if (isUnknown(source))
|
|
580
688
|
return source;
|
|
581
|
-
// Unknown elements keep their places, so only
|
|
689
|
+
// Unknown elements keep their places, so only elements it may lack or not list leave the length unknown.
|
|
582
690
|
if (source.value instanceof PartialArrayValue)
|
|
583
|
-
return source.value.
|
|
691
|
+
return source.value.unknownLength?.() ?? known(source.value.elements.length);
|
|
584
692
|
const value = ruleValue(source);
|
|
585
693
|
return isUnknown(value) ? value : known(lengthOfValue(value.value));
|
|
586
694
|
}
|
|
@@ -589,6 +697,7 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
589
697
|
case 'lookup_json_string':
|
|
590
698
|
return lookupJsonValue('string', args.length, argument);
|
|
591
699
|
case 'bit_slice':
|
|
700
|
+
case 'hmac_sha256':
|
|
592
701
|
case 'is_jwt_present':
|
|
593
702
|
case 'is_jwt_valid':
|
|
594
703
|
case 'is_timed_hmac_valid_v0':
|
|
@@ -608,7 +717,9 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
608
717
|
values.push(value.value);
|
|
609
718
|
}
|
|
610
719
|
const value = functionResult(name, values, invocation);
|
|
611
|
-
|
|
720
|
+
if (value === undefined)
|
|
721
|
+
return unavailable(functionSource(name));
|
|
722
|
+
return value === TOO_LARGE ? value : known(canonicalResult(value));
|
|
612
723
|
}
|
|
613
724
|
/**
|
|
614
725
|
* Puts each string of a result in the one form byte-strings.ts gives its
|
|
@@ -647,6 +758,8 @@ function functionResult(name, values, invocation) {
|
|
|
647
758
|
return decodeBase64Value(at(0));
|
|
648
759
|
case 'encode_base64':
|
|
649
760
|
return encodeBase64Value(at(0), at(1));
|
|
761
|
+
case 'encode_hex':
|
|
762
|
+
return encodeHexValue(at(0));
|
|
650
763
|
case 'ends_with':
|
|
651
764
|
case 'starts_with':
|
|
652
765
|
return matchesAffix(name, at(0), at(1));
|
|
@@ -686,10 +799,13 @@ function evaluateFuncArray(node, ctx, invocation) {
|
|
|
686
799
|
// Over nil, such as a missing header's values, the call gives nil, not an empty array.
|
|
687
800
|
if (!isUnknown(source) && source.value === null)
|
|
688
801
|
return source;
|
|
689
|
-
const array = isUnknown(source) ? source :
|
|
802
|
+
const array = isUnknown(source) ? source : arrayValue(source.value);
|
|
690
803
|
if (isUnknown(array))
|
|
691
804
|
return array;
|
|
692
|
-
const {
|
|
805
|
+
const { value } = array;
|
|
806
|
+
const { elements, more, moreFrom } = value instanceof PartialArrayValue
|
|
807
|
+
? value
|
|
808
|
+
: { elements: value.map(element => known(element)), more: undefined, moreFrom: value.length };
|
|
693
809
|
const later = [];
|
|
694
810
|
// A nil later argument gives every element nil, unknown ones too, so the call gives an empty array.
|
|
695
811
|
if ((elements.length > 0 || more) && givesNilForNilArgument(node.name)) {
|
|
@@ -701,27 +817,80 @@ function evaluateFuncArray(node, ctx, invocation) {
|
|
|
701
817
|
return known([]);
|
|
702
818
|
}
|
|
703
819
|
}
|
|
820
|
+
// What a nil second argument gives is not known, so neither is the array's length; see NEVER_NIL_FUNCTIONS.
|
|
821
|
+
if ((elements.length > 0 || more) && node.name === 'hmac_sha256') {
|
|
822
|
+
const value = (later[1] = evaluateArgValue(node.args[1], ctx));
|
|
823
|
+
if (isUnknown(value))
|
|
824
|
+
return value;
|
|
825
|
+
if (value.value === null)
|
|
826
|
+
return unreliableFunction(ctx, node.name);
|
|
827
|
+
}
|
|
704
828
|
let keepsPlaces = keepsPlacesWhenMapped(node.name);
|
|
829
|
+
const tooLarge = () => tooLargeResult(ctx, node.name);
|
|
830
|
+
const evaluate = (element) => {
|
|
831
|
+
const direct = evaluateDirectFuncValue(node, ctx, invocation, later, element);
|
|
832
|
+
return direct === TOO_LARGE ? direct : ruleValue(direct);
|
|
833
|
+
};
|
|
834
|
+
// Elements it may lack leave the others their whole budget, and those with an unknown result warn only when
|
|
835
|
+
// read. The first may have read a later argument for all, so they share their warnings.
|
|
836
|
+
let size = 0;
|
|
837
|
+
let optionalSize = 0;
|
|
838
|
+
let optionalWarnings;
|
|
839
|
+
let resultsFrom = 0;
|
|
705
840
|
const results = [];
|
|
706
|
-
for (
|
|
841
|
+
for (let index = 0; index < elements.length; index++) {
|
|
842
|
+
const listed = elements[index];
|
|
843
|
+
const optional = listed instanceof OptionalElement;
|
|
844
|
+
const element = listedElement(listed);
|
|
845
|
+
// The results that fit can still settle what reads them, so the ones past them are unknown elements.
|
|
846
|
+
let result = tooLarge;
|
|
707
847
|
if (isUnknownElement(element)) {
|
|
708
|
-
|
|
709
|
-
continue;
|
|
848
|
+
result = element;
|
|
710
849
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
850
|
+
else if ((optional ? optionalSize : size) <= MAX_RESULT_SIZE) {
|
|
851
|
+
let evaluated;
|
|
852
|
+
if (optional) {
|
|
853
|
+
const collected = collectWarnings(ctx, () => evaluate(element.value));
|
|
854
|
+
evaluated = collected.result;
|
|
855
|
+
optionalWarnings ??= new Set();
|
|
856
|
+
for (const warning of collected.warnings)
|
|
857
|
+
optionalWarnings.add(warning);
|
|
858
|
+
}
|
|
859
|
+
else {
|
|
860
|
+
evaluated = evaluate(element.value);
|
|
861
|
+
}
|
|
862
|
+
if (evaluated !== TOO_LARGE) {
|
|
863
|
+
if (isUnknown(evaluated)) {
|
|
864
|
+
const unknown = evaluated;
|
|
865
|
+
result = deferWarnings(ctx, optionalWarnings ?? [], () => unknown);
|
|
866
|
+
if (!optional)
|
|
867
|
+
return result();
|
|
868
|
+
}
|
|
869
|
+
else if (evaluated.value === null) {
|
|
870
|
+
// A listed function that gives nil after all may for an unknown element too.
|
|
871
|
+
keepsPlaces = false;
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
result = evaluated;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
const total = optional ? (optionalSize += elementSize(result)) : (size += elementSize(result));
|
|
880
|
+
if (total > MAX_RESULT_SIZE)
|
|
881
|
+
result = tooLarge;
|
|
882
|
+
results.push(inPlaceOf(listed, result));
|
|
883
|
+
if (index < moreFrom)
|
|
884
|
+
resultsFrom = results.length;
|
|
719
885
|
}
|
|
720
886
|
if (keepsPlaces)
|
|
721
|
-
return known(PartialArrayValue.from(results, more));
|
|
722
|
-
// An unknown element may give nil, which drops it
|
|
723
|
-
const
|
|
724
|
-
|
|
887
|
+
return known(PartialArrayValue.from(results, more, resultsFrom));
|
|
888
|
+
// An unknown element may give nil, which drops it.
|
|
889
|
+
const droppable = results.map(listed => {
|
|
890
|
+
const element = listedElement(listed);
|
|
891
|
+
return isUnknownElement(element) ? new OptionalElement(element, element) : listed;
|
|
892
|
+
});
|
|
893
|
+
return known(PartialArrayValue.from(droppable, more, resultsFrom));
|
|
725
894
|
}
|
|
726
895
|
function evaluateArgValue(arg, ctx) {
|
|
727
896
|
// An omitted optional argument is null, which functions treat as absent.
|
|
@@ -797,6 +966,47 @@ function nonValueArgumentSource(arg) {
|
|
|
797
966
|
return `list '$${arg.name}'`;
|
|
798
967
|
return 'an inline list';
|
|
799
968
|
}
|
|
969
|
+
/**
|
|
970
|
+
* Evaluates filter(): its array's elements at the positions where its
|
|
971
|
+
* conditions hold, as Cloudflare pairs them by index and stops at the shorter
|
|
972
|
+
* array. An unknown condition may keep its element or not, so the result may
|
|
973
|
+
* lack it, as it may one whose position an array may lack. Validation rejects
|
|
974
|
+
* arguments that may be nil.
|
|
975
|
+
*/
|
|
976
|
+
function evaluateFilter(node, ctx) {
|
|
977
|
+
const [arrayNode, predicate] = node.args;
|
|
978
|
+
// No field is an array of Booleans, so only a condition or a call gives one.
|
|
979
|
+
if (!isExpressionNode(predicate))
|
|
980
|
+
return unavailable(functionSource(node.name));
|
|
981
|
+
const { result: evaluated, warnings } = collectWarnings(ctx, () => evaluateArgValue(arrayNode, ctx));
|
|
982
|
+
const array = pairedArray(evaluated, warnings, isTrueValue, ctx);
|
|
983
|
+
const { arrays, end, missing, tail, tailFrom } = pairArrays(predicate, predicate, ctx, undefined, { node: arrayNode, array });
|
|
984
|
+
const kept = [];
|
|
985
|
+
let keptFrom = 0;
|
|
986
|
+
resultElements(predicate, end, arrays, ctx).forEach((result, index) => {
|
|
987
|
+
const unknown = isUnknownElement(result);
|
|
988
|
+
if (!unknown && result.value !== true)
|
|
989
|
+
return;
|
|
990
|
+
const element = array.values ? known(array.values[index]) : elementAt(array, index);
|
|
991
|
+
kept.push(optionalElement(element, unknown ? result : missing?.(index)));
|
|
992
|
+
if (index < tailFrom)
|
|
993
|
+
keptFrom = kept.length;
|
|
994
|
+
});
|
|
995
|
+
return known(PartialArrayValue.from(kept, tail, keptFrom));
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Evaluates filter() on one element of the arrays it maps over. An unknown
|
|
999
|
+
* condition leaves the whole call unknown, its length too, as an unknown later
|
|
1000
|
+
* argument leaves any call that maps, even where the element does not reach
|
|
1001
|
+
* it: no list lines up with each of the arrays, so such a call gets a warning
|
|
1002
|
+
* anyway.
|
|
1003
|
+
*/
|
|
1004
|
+
function filterValues(array, conditions) {
|
|
1005
|
+
if (isUnknown(conditions))
|
|
1006
|
+
return conditions;
|
|
1007
|
+
const { value } = conditions;
|
|
1008
|
+
return known(Array.isArray(array) && Array.isArray(value) ? array.filter((_, index) => value[index] === true) : []);
|
|
1009
|
+
}
|
|
800
1010
|
/** Returns nil (null) for a nil collection or value, as Cloudflare documents. */
|
|
801
1011
|
function evaluateHasValue(collection, value) {
|
|
802
1012
|
if (collection === null || value === null)
|
|
@@ -813,6 +1023,63 @@ function matchesAffix(name, value, affix) {
|
|
|
813
1023
|
? String(value).startsWith(String(affix))
|
|
814
1024
|
: String(value).endsWith(String(affix));
|
|
815
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Reads the arguments of concat() until one is unknown, or the result is
|
|
1028
|
+
* surely too large; arrays end after the elements that fit. Those that arrays
|
|
1029
|
+
* may lack have a budget of their own, as sharing the others' would make the
|
|
1030
|
+
* result depend on the order of the arguments, and arrays do not list those
|
|
1031
|
+
* past it: the result can do without. `read`, which keeps the arguments, loses
|
|
1032
|
+
* them too.
|
|
1033
|
+
*/
|
|
1034
|
+
function readConcatArguments(count, argument, read, tooLarge) {
|
|
1035
|
+
const parts = [];
|
|
1036
|
+
let size = 0;
|
|
1037
|
+
let limit = MAX_RESULT_SIZE;
|
|
1038
|
+
let elementCount = 0;
|
|
1039
|
+
let optionalCount = 0;
|
|
1040
|
+
for (let index = 0; index < count; index++) {
|
|
1041
|
+
const part = argument(index);
|
|
1042
|
+
if (isUnknown(part))
|
|
1043
|
+
return part;
|
|
1044
|
+
let { value } = part;
|
|
1045
|
+
if (value instanceof PartialArrayValue) {
|
|
1046
|
+
optionalCount += value.elements.length - value.shortest;
|
|
1047
|
+
if (optionalCount > MAX_CONCAT_ELEMENTS) {
|
|
1048
|
+
value = value.withoutOptional();
|
|
1049
|
+
read[index] = known(value);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (Array.isArray(value) || value instanceof PartialArrayValue) {
|
|
1053
|
+
const room = MAX_CONCAT_ELEMENTS - elementCount;
|
|
1054
|
+
elementCount += value instanceof PartialArrayValue ? value.shortest : value.length;
|
|
1055
|
+
if (elementCount > MAX_CONCAT_ELEMENTS) {
|
|
1056
|
+
// The elements that fit can still settle any() and has_value().
|
|
1057
|
+
parts.push(firstElements(value, room, tooLarge));
|
|
1058
|
+
return known(parts);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
parts.push(value);
|
|
1062
|
+
if (typeof value === 'string')
|
|
1063
|
+
size += value.length;
|
|
1064
|
+
if (size <= limit)
|
|
1065
|
+
continue;
|
|
1066
|
+
if (surelyTooLarge(size, parts))
|
|
1067
|
+
return TOO_LARGE;
|
|
1068
|
+
// A part has bytes that can unite, so the parts are not scanned again.
|
|
1069
|
+
limit = MAX_JOINED_SIZE;
|
|
1070
|
+
}
|
|
1071
|
+
return known(parts);
|
|
1072
|
+
}
|
|
1073
|
+
/** An array up to its first `count` elements, of those it surely has, as one that `more` says holds more. */
|
|
1074
|
+
function firstElements(array, count, more) {
|
|
1075
|
+
if (Array.isArray(array))
|
|
1076
|
+
return PartialArrayValue.from(array.slice(0, count).map(value => known(value)), more, count);
|
|
1077
|
+
let end = 0;
|
|
1078
|
+
for (let left = count; left > 0; end++)
|
|
1079
|
+
if (!(array.elements[end] instanceof OptionalElement))
|
|
1080
|
+
left--;
|
|
1081
|
+
return PartialArrayValue.from(array.elements.slice(0, end), more, Math.min(array.moreFrom, end));
|
|
1082
|
+
}
|
|
816
1083
|
function concatValues(values, argumentTypes) {
|
|
817
1084
|
if (values.length < 2 || concatTypesDiffer(argumentTypes))
|
|
818
1085
|
return null;
|
|
@@ -836,30 +1103,25 @@ function concatTypesDiffer(argumentTypes) {
|
|
|
836
1103
|
}
|
|
837
1104
|
/**
|
|
838
1105
|
* concat() over arrays some of which are known only in part: the result
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
* value that is no array gives nil, as in concatValues(); a map is unknown,
|
|
843
|
-
* as in ruleValue().
|
|
1106
|
+
* lists each one's elements in turn, and may lack or hold others where one
|
|
1107
|
+
* may. A value that is no array gives nil, as in concatValues(); a map is
|
|
1108
|
+
* unknown, as in ruleValue().
|
|
844
1109
|
*/
|
|
845
1110
|
function concatElements(parts, argumentTypes) {
|
|
846
|
-
if (
|
|
1111
|
+
if (argumentTypes.length < 2 || concatTypesDiffer(argumentTypes))
|
|
847
1112
|
return known(null);
|
|
848
1113
|
const elements = [];
|
|
849
1114
|
let more;
|
|
850
|
-
let
|
|
1115
|
+
let moreFrom = 0;
|
|
851
1116
|
for (const part of parts) {
|
|
852
1117
|
if (part instanceof RuleMapValue)
|
|
853
1118
|
return unavailable('a map value');
|
|
854
1119
|
if (part instanceof PartialArrayValue) {
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1120
|
+
if (!more)
|
|
1121
|
+
moreFrom = elements.length + part.moreFrom;
|
|
1122
|
+
for (const element of part.elements)
|
|
1123
|
+
elements.push(element);
|
|
858
1124
|
more ??= part.more;
|
|
859
|
-
if (part.optional)
|
|
860
|
-
optional?.push(...part.optional);
|
|
861
|
-
else
|
|
862
|
-
optional = undefined;
|
|
863
1125
|
}
|
|
864
1126
|
else if (Array.isArray(part)) {
|
|
865
1127
|
for (const value of part)
|
|
@@ -869,7 +1131,7 @@ function concatElements(parts, argumentTypes) {
|
|
|
869
1131
|
return known(null);
|
|
870
1132
|
}
|
|
871
1133
|
}
|
|
872
|
-
return known(PartialArrayValue.from(elements, more,
|
|
1134
|
+
return known(PartialArrayValue.from(elements, more, moreFrom));
|
|
873
1135
|
}
|
|
874
1136
|
/** Cloudflare counts the bytes of a string. */
|
|
875
1137
|
function lengthOfValue(value) {
|
|
@@ -897,13 +1159,39 @@ function encodeBase64Value(value, flagsValue) {
|
|
|
897
1159
|
if (value === null)
|
|
898
1160
|
return null;
|
|
899
1161
|
const flags = flagsValue === null ? '' : String(flagsValue);
|
|
900
|
-
|
|
1162
|
+
const bytes = bytesToEncode(String(value), 4 / 3);
|
|
1163
|
+
if (bytes === TOO_LARGE)
|
|
1164
|
+
return bytes;
|
|
1165
|
+
let encoded = bytesToBase64(bytes);
|
|
901
1166
|
if (flags.includes('u'))
|
|
902
1167
|
encoded = encoded.replaceAll('+', '-').replaceAll('/', '_');
|
|
903
1168
|
if (!flags.includes('p'))
|
|
904
1169
|
encoded = encoded.replace(/=+$/, '');
|
|
905
1170
|
return encoded;
|
|
906
1171
|
}
|
|
1172
|
+
/** The bytes of a text, unless an encoding that writes `ratio` characters for each would hold too much. */
|
|
1173
|
+
function bytesToEncode(text, ratio) {
|
|
1174
|
+
// Each character is at least one byte.
|
|
1175
|
+
if (text.length * ratio > MAX_RESULT_SIZE)
|
|
1176
|
+
return TOO_LARGE;
|
|
1177
|
+
const bytes = textToBytes(text);
|
|
1178
|
+
return Math.ceil(bytes.length * ratio) > MAX_RESULT_SIZE ? TOO_LARGE : bytes;
|
|
1179
|
+
}
|
|
1180
|
+
const HEX_DIGITS = new TextEncoder().encode('0123456789abcdef');
|
|
1181
|
+
/** Writes each byte as two lowercase hex digits. */
|
|
1182
|
+
function encodeHexValue(value) {
|
|
1183
|
+
if (value === null)
|
|
1184
|
+
return null;
|
|
1185
|
+
const bytes = bytesToEncode(String(value), 2);
|
|
1186
|
+
if (bytes === TOO_LARGE)
|
|
1187
|
+
return bytes;
|
|
1188
|
+
const digits = new Uint8Array(bytes.length * 2);
|
|
1189
|
+
for (let index = 0; index < bytes.length; index++) {
|
|
1190
|
+
digits[index * 2] = HEX_DIGITS[bytes[index] >> 4];
|
|
1191
|
+
digits[index * 2 + 1] = HEX_DIGITS[bytes[index] & 15];
|
|
1192
|
+
}
|
|
1193
|
+
return bytesToText(digits);
|
|
1194
|
+
}
|
|
907
1195
|
/**
|
|
908
1196
|
* Decodes standard Base64 as Cloudflare does, observed with live requests: it
|
|
909
1197
|
* skips whitespace and takes input with or without padding. Other input is
|
|
@@ -932,10 +1220,15 @@ function bytesToBase64(bytes) {
|
|
|
932
1220
|
binary += String.fromCharCode(byte);
|
|
933
1221
|
return globalThis.btoa(binary);
|
|
934
1222
|
}
|
|
935
|
-
function joinValues(items,
|
|
936
|
-
if (!Array.isArray(items) ||
|
|
1223
|
+
function joinValues(items, separatorValue) {
|
|
1224
|
+
if (!Array.isArray(items) || separatorValue === null)
|
|
937
1225
|
return null;
|
|
938
|
-
|
|
1226
|
+
const texts = items.map(item => String(item));
|
|
1227
|
+
const separator = String(separatorValue);
|
|
1228
|
+
let size = separator.length * Math.max(texts.length - 1, 0);
|
|
1229
|
+
for (const text of texts)
|
|
1230
|
+
size += text.length;
|
|
1231
|
+
return surelyTooLarge(size, texts, [separator]) ? TOO_LARGE : texts.join(separator);
|
|
939
1232
|
}
|
|
940
1233
|
/**
|
|
941
1234
|
* Cloudflare accepts one seed, and a nil seed gives nil. The bounds are integer
|
|
@@ -1039,16 +1332,27 @@ function jsonMember(container, key) {
|
|
|
1039
1332
|
function regexReplace(sourceValue, patternValue, replacementValue) {
|
|
1040
1333
|
if (sourceValue === null || patternValue === null || replacementValue === null)
|
|
1041
1334
|
return null;
|
|
1335
|
+
const source = String(sourceValue);
|
|
1336
|
+
let match;
|
|
1042
1337
|
try {
|
|
1043
|
-
|
|
1044
|
-
return String(sourceValue).replace(regex, (...matchParts) => {
|
|
1045
|
-
const captures = matchParts.slice(1, -2);
|
|
1046
|
-
return replaceRuleCaptureReferences(String(replacementValue), captures);
|
|
1047
|
-
});
|
|
1338
|
+
match = new RegExp(String(patternValue)).exec(source);
|
|
1048
1339
|
}
|
|
1049
1340
|
catch {
|
|
1050
|
-
return
|
|
1341
|
+
return source;
|
|
1342
|
+
}
|
|
1343
|
+
return match ? replaceMatch(source, match.index, match, String(replacementValue)) : source;
|
|
1344
|
+
}
|
|
1345
|
+
/** Writes a replacement in place of a source's match, which is the first of the captures. */
|
|
1346
|
+
function replaceMatch(source, start, captures, replacement) {
|
|
1347
|
+
const before = source.slice(0, start);
|
|
1348
|
+
const after = source.slice(start + (captures[0]?.length ?? 0));
|
|
1349
|
+
// The size is counted only where it could be too large.
|
|
1350
|
+
if (replacement.length + source.length * (MAX_REPLACEMENT_REFERENCES + 1) > MAX_RESULT_SIZE) {
|
|
1351
|
+
const { length, written } = measureReplacement(replacement, captures);
|
|
1352
|
+
if (surelyTooLarge(before.length + length + after.length, [before, replacement, after], written))
|
|
1353
|
+
return TOO_LARGE;
|
|
1051
1354
|
}
|
|
1355
|
+
return before + expandReplacement(replacement, captures) + after;
|
|
1052
1356
|
}
|
|
1053
1357
|
/** Removes each listed byte wherever it appears, as Cloudflare does, so a result can split a character. */
|
|
1054
1358
|
function removeBytes(sourceValue, removeValue) {
|
|
@@ -1125,16 +1429,7 @@ function wildcardReplaceValue(sourceValue, patternValue, replacementValue, flags
|
|
|
1125
1429
|
function wildcardReplace(source, pattern, replacement, flags) {
|
|
1126
1430
|
const compiled = compileWildcard(pattern, flags.includes('s'));
|
|
1127
1431
|
const captures = compiled && wildcardCaptures(compiled, source);
|
|
1128
|
-
|
|
1129
|
-
return source;
|
|
1130
|
-
return replaceRuleCaptureReferences(replacement, captures);
|
|
1131
|
-
}
|
|
1132
|
-
function replaceRuleCaptureReferences(replacement, captures) {
|
|
1133
|
-
return replacement.replace(/\$\$|\$\{([1-8])\}/g, (full, index) => {
|
|
1134
|
-
if (full === '$$')
|
|
1135
|
-
return '$';
|
|
1136
|
-
return captures[Number.parseInt(index ?? '0', 10) - 1]?.toString() ?? '';
|
|
1137
|
-
});
|
|
1432
|
+
return captures ? replaceMatch(source, 0, captures, replacement) : source;
|
|
1138
1433
|
}
|
|
1139
1434
|
/** Returns the network address, printed as Cloudflare prints IP addresses. */
|
|
1140
1435
|
function evaluateCidr(addressValue, ipv4BitsValue, ipv6BitsValue) {
|
|
@@ -1154,6 +1449,9 @@ function evaluateCidr(addressValue, ipv4BitsValue, ipv6BitsValue) {
|
|
|
1154
1449
|
}
|
|
1155
1450
|
return formatIpAddress(octets);
|
|
1156
1451
|
}
|
|
1452
|
+
function tooLargeResult(ctx, name) {
|
|
1453
|
+
return unreliableFunction(ctx, name, RESULT_SIZE_REASON);
|
|
1454
|
+
}
|
|
1157
1455
|
/** A function result this evaluator cannot reproduce is unknown. */
|
|
1158
1456
|
function unreliableFunction(ctx, name, reason = getUnreliableRuleExpressionFunctionReason(name)) {
|
|
1159
1457
|
if (reason)
|