@libreflare/worker-runtime 0.8.3 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +230 -23
- package/dist/log-handler.d.ts.map +1 -1
- package/dist/log-handler.js +12 -6
- package/dist/log-handler.js.map +1 -1
- package/dist/rule-evaluator/access-values.d.ts +19 -6
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +33 -8
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/analysis.d.ts +49 -17
- package/dist/rule-evaluator/analysis.d.ts.map +1 -1
- package/dist/rule-evaluator/analysis.js +39 -41
- package/dist/rule-evaluator/analysis.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +31 -18
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js +3 -4
- package/dist/rule-evaluator/ast.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/custom-lists.d.ts +0 -6
- package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
- package/dist/rule-evaluator/custom-lists.js +1 -12
- package/dist/rule-evaluator/custom-lists.js.map +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 +17 -0
- package/dist/rule-evaluator/diagnostics.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.d.ts +20 -0
- package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.js +5 -0
- package/dist/rule-evaluator/engine-observations.generated.js.map +1 -1
- package/dist/rule-evaluator/evaluator.d.ts +4 -2
- package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator/evaluator.js +5 -4
- package/dist/rule-evaluator/evaluator.js.map +1 -1
- package/dist/rule-evaluator/explain.d.ts +2 -1
- package/dist/rule-evaluator/explain.d.ts.map +1 -1
- package/dist/rule-evaluator/explain.js +49 -7
- package/dist/rule-evaluator/explain.js.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts +13 -0
- package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +33 -0
- package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/fields.d.ts +13 -1
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +71 -17
- package/dist/rule-evaluator/fields.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.d.ts +2 -0
- package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.d.ts +66 -10
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +129 -11
- package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.js.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.d.ts +2 -0
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +169 -19
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +24 -1
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +423 -95
- package/dist/rule-evaluator/functions.js.map +1 -1
- package/dist/rule-evaluator/ip-literals.d.ts +56 -0
- package/dist/rule-evaluator/ip-literals.d.ts.map +1 -0
- package/dist/rule-evaluator/ip-literals.js +162 -0
- package/dist/rule-evaluator/ip-literals.js.map +1 -0
- package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
- package/dist/rule-evaluator/log-sample.js +14 -2
- package/dist/rule-evaluator/log-sample.js.map +1 -1
- package/dist/rule-evaluator/match-chain.d.ts +2 -1
- package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
- package/dist/rule-evaluator/match-chain.js +12 -17
- package/dist/rule-evaluator/match-chain.js.map +1 -1
- package/dist/rule-evaluator/minifier.d.ts +1 -1
- package/dist/rule-evaluator/minifier.d.ts.map +1 -1
- package/dist/rule-evaluator/minifier.js +15 -14
- package/dist/rule-evaluator/minifier.js.map +1 -1
- package/dist/rule-evaluator/normalizer.d.ts +1 -1
- package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
- package/dist/rule-evaluator/normalizer.js +4 -5
- package/dist/rule-evaluator/normalizer.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +4 -0
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +26 -98
- 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 +0 -6
- package/dist/rule-evaluator/outcome.js.map +1 -1
- package/dist/rule-evaluator/parse-cache.d.ts +8 -3
- package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
- package/dist/rule-evaluator/parse-cache.js +9 -8
- package/dist/rule-evaluator/parse-cache.js.map +1 -1
- package/dist/rule-evaluator/parser.d.ts +155 -18
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +478 -81
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +96 -2
- package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
- package/dist/rule-evaluator/quantifier.js +263 -22
- package/dist/rule-evaluator/quantifier.js.map +1 -1
- package/dist/rule-evaluator/query-args.d.ts +23 -0
- package/dist/rule-evaluator/query-args.d.ts.map +1 -0
- package/dist/rule-evaluator/query-args.js +46 -0
- package/dist/rule-evaluator/query-args.js.map +1 -0
- package/dist/rule-evaluator/rule-language-types.d.ts +28 -17
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +117 -42
- package/dist/rule-evaluator/rule-language-types.js.map +1 -1
- package/dist/rule-evaluator/string-literals.d.ts +20 -0
- package/dist/rule-evaluator/string-literals.d.ts.map +1 -0
- package/dist/rule-evaluator/string-literals.js +44 -0
- package/dist/rule-evaluator/string-literals.js.map +1 -0
- package/dist/rule-evaluator/tokenizer.d.ts +34 -5
- package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
- package/dist/rule-evaluator/tokenizer.js +320 -74
- package/dist/rule-evaluator/tokenizer.js.map +1 -1
- package/dist/rule-evaluator/transformation.d.ts +2 -2
- package/dist/rule-evaluator/transformation.d.ts.map +1 -1
- package/dist/rule-evaluator/transformation.js +4 -5
- package/dist/rule-evaluator/transformation.js.map +1 -1
- package/dist/rule-evaluator/validation.d.ts.map +1 -1
- package/dist/rule-evaluator/validation.js +261 -122
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator.d.ts +3 -1
- package/dist/rule-evaluator.d.ts.map +1 -1
- package/dist/rule-evaluator.js +2 -0
- package/dist/rule-evaluator.js.map +1 -1
- package/dist/runtime-config.d.ts.map +1 -1
- package/dist/runtime-config.js +4 -3
- package/dist/runtime-config.js.map +1 -1
- package/dist/tools/explain-rule-miss.js +4 -6
- package/dist/tools/explain-rule-miss.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import { RULE_EXPRESSION_FUNCTIONS } from './constants.js';
|
|
2
|
-
import { isRuleExpressionIpAddress, isRuleExpressionIpLiteral } from './custom-lists.js';
|
|
3
2
|
import { KNOWN_AVAILABLE_FIELDS, KNOWN_UNAVAILABLE_FIELDS } from './fields.js';
|
|
4
|
-
import {
|
|
3
|
+
import { formatIpAddressValue, readIpAddress, readIpListMember } from './ip-literals.js';
|
|
4
|
+
import { acceptsComparisonArgument, canReturnBoolean, inferRuleExpressionArgument, integerLiteralValue, isArrayExpandedNode, isComparableValue, parameterDataType, ruleExpressionComparisonTypeFamily, } from './rule-language-types.js';
|
|
5
5
|
import { isDynamicValueNode, unhandledNode, } from './ast.js';
|
|
6
|
-
import { tokenLength } from './tokenizer.js';
|
|
6
|
+
import { stringLineBreak, tokenize, tokenLength, trimRuleExpression, } from './tokenizer.js';
|
|
7
|
+
/**
|
|
8
|
+
* The type to read a literal compared with a value of a type as; see
|
|
9
|
+
* LiteralType. An array's is its elements', which any() and all() compare
|
|
10
|
+
* one by one, as in `any(cf.waf.content_scan.obj_sizes[*] in {10-20})`.
|
|
11
|
+
*/
|
|
12
|
+
export function literalTypeOf(type) {
|
|
13
|
+
const family = ruleExpressionComparisonTypeFamily(type.kind === 'array' ? type.element : type);
|
|
14
|
+
return family === 'number' || family === 'ip' ? family : null;
|
|
15
|
+
}
|
|
7
16
|
const VALUE_CONTEXT_LOCATIONS = {
|
|
8
17
|
operator: 'on right side of operator',
|
|
9
18
|
function: 'as function argument',
|
|
@@ -194,8 +203,9 @@ function callName(tokens, partners, index) {
|
|
|
194
203
|
* Whether the call whose `(` is at `opener` can take the comparison at an
|
|
195
204
|
* index in the value argument it reads, as far as the tokens tell: not one
|
|
196
205
|
* right after a literal argument, which Cloudflare never compares, and one
|
|
197
|
-
* that callTakesComparison() allows
|
|
198
|
-
* see
|
|
206
|
+
* that callTakesComparison() allows, over each element where the argument's
|
|
207
|
+
* access path has `[*]`. The parser decides the rest from types; see
|
|
208
|
+
* takesComparison().
|
|
199
209
|
*/
|
|
200
210
|
function readsComparison(tokens, partners, opener, index, name, argumentIndex) {
|
|
201
211
|
const end = indexBefore(tokens, partners, index);
|
|
@@ -205,7 +215,18 @@ function readsComparison(tokens, partners, opener, index, name, argumentIndex) {
|
|
|
205
215
|
const before = indexBefore(tokens, partners, start);
|
|
206
216
|
if (literal && (before === opener || tokens[before].kind === 'Comma'))
|
|
207
217
|
return false;
|
|
208
|
-
return callTakesComparison(name, argumentIndex, []);
|
|
218
|
+
return callTakesComparison(name, argumentIndex, [], expandsAt(tokens, partners, end), tokens[index]);
|
|
219
|
+
}
|
|
220
|
+
/** Whether the access path that ends at an index has a `[*]`, as the tokens show it. */
|
|
221
|
+
function expandsAt(tokens, partners, end) {
|
|
222
|
+
let index = end;
|
|
223
|
+
while (tokens[index]?.kind === 'RBracket' && partners[index] >= 0) {
|
|
224
|
+
const opener = partners[index];
|
|
225
|
+
if (opener === index - 2 && tokens[index - 1].kind === 'Star')
|
|
226
|
+
return true;
|
|
227
|
+
index = opener - 1;
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
209
230
|
}
|
|
210
231
|
/** Whether the token at an index is a `[` that starts an access: one after a name, or after what ends one. */
|
|
211
232
|
function startsAccess(tokens, index) {
|
|
@@ -290,21 +311,30 @@ function endsOperand(token) {
|
|
|
290
311
|
* can take one; see callTakesComparison(). Otherwise the comparison compares
|
|
291
312
|
* the call's result.
|
|
292
313
|
*/
|
|
293
|
-
function takesComparison(call) {
|
|
314
|
+
function takesComparison(call, operator) {
|
|
294
315
|
const value = call.argumentValue;
|
|
295
316
|
if (value === 'literal' || (value !== null && !isComparableValue(value)))
|
|
296
317
|
return false;
|
|
297
|
-
return callTakesComparison(call.functionName, call.argumentIndex, call.args ?? []);
|
|
318
|
+
return callTakesComparison(call.functionName, call.argumentIndex, call.args ?? [], value !== null && isArrayExpandedNode(value), operator);
|
|
298
319
|
}
|
|
299
320
|
/**
|
|
300
321
|
* Whether a call without its `)` can take a comparison as its argument at an
|
|
301
322
|
* index: the parameter accepts one, with T fixed by the arguments before it
|
|
302
323
|
* (see acceptsComparisonArgument()), and the call's result can be a Boolean.
|
|
303
324
|
* A call that returns no Boolean, as to_string() does, could stand only where
|
|
304
|
-
* a value does, and is compared far more often than given a comparison.
|
|
325
|
+
* a value does, and is compared far more often than given a comparison. A
|
|
326
|
+
* comparison over each element, `elementwise`, is an array of Booleans,
|
|
327
|
+
* which such a call takes too, as `len()` does, unless the `operator` orders:
|
|
328
|
+
* a count is ordered far more often than elements are, as in
|
|
329
|
+
* `any(len(http.request.headers[*])[*] gt 1)`.
|
|
305
330
|
*/
|
|
306
|
-
function callTakesComparison(name, index, before) {
|
|
307
|
-
|
|
331
|
+
function callTakesComparison(name, index, before, elementwise, operator) {
|
|
332
|
+
if (canReturnBoolean(name))
|
|
333
|
+
return acceptsComparisonArgument(name, index, before, elementwise);
|
|
334
|
+
return elementwise && !isOrderingOperator(operator) && acceptsComparisonArgument(name, index, before, true);
|
|
335
|
+
}
|
|
336
|
+
function isOrderingOperator(token) {
|
|
337
|
+
return token.kind === 'Operator' && (token.value === 'lt' || token.value === 'le' || token.value === 'gt' || token.value === 'ge');
|
|
308
338
|
}
|
|
309
339
|
const NO_OPERATOR_CHARACTERS = new Set();
|
|
310
340
|
/**
|
|
@@ -362,7 +392,7 @@ function isUnknownCharacter(token) {
|
|
|
362
392
|
}
|
|
363
393
|
/** Whether a token is malformed: the tokenizer reported it already. */
|
|
364
394
|
function isMalformed(token) {
|
|
365
|
-
return token.kind === 'Invalid' || token.closed === false;
|
|
395
|
+
return token.kind === 'Invalid' || token.closed === false || token.invalidEscape === true;
|
|
366
396
|
}
|
|
367
397
|
const CLOSER_TEXT = { group: ')', call: ')', quantifier: ')', access: ']', list: '}' };
|
|
368
398
|
function readHiddenClosers(text) {
|
|
@@ -381,6 +411,42 @@ const MAX_ARRAY_INDEX = 0xffff_ffff;
|
|
|
381
411
|
/** Cloudflare stores integers as signed 64-bit values. */
|
|
382
412
|
const MIN_INTEGER = -(2n ** 63n);
|
|
383
413
|
const MAX_INTEGER = 2n ** 63n - 1n;
|
|
414
|
+
/** Number literal text that suggests an IP address when no type is expected: a `:`, a `/`, or three dotted parts. */
|
|
415
|
+
const IP_ADDRESS_LIKE = /[:/]|\d\.\d+\.\d/;
|
|
416
|
+
/** The same for a list member, where an IPv4 address may have fewer parts. */
|
|
417
|
+
const IP_MEMBER_LIKE = /[:/]|\d\.\d/;
|
|
418
|
+
/** An integer literal's value, when it is one in the 64-bit range. */
|
|
419
|
+
function int64LiteralValue(text) {
|
|
420
|
+
const value = integerLiteralValue(text);
|
|
421
|
+
return value !== null && value >= MIN_INTEGER && value <= MAX_INTEGER ? value : null;
|
|
422
|
+
}
|
|
423
|
+
/** The bounds of a list member that is a valid integer or integer range. */
|
|
424
|
+
function integerRangeBounds(text) {
|
|
425
|
+
const rangeAt = text.indexOf('..');
|
|
426
|
+
const first = int64LiteralValue(rangeAt < 0 ? text : text.slice(0, rangeAt));
|
|
427
|
+
const last = rangeAt < 0 ? first : int64LiteralValue(text.slice(rangeAt + 2));
|
|
428
|
+
return first !== null && last !== null && first <= last ? [first, last] : null;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The list members a number token holds on an Integer side. Two-digit
|
|
432
|
+
* numbers that `-` joins, which the tokenizer reads as hex bytes, are each a
|
|
433
|
+
* member there, as Cloudflare reads `10-20` as 10 and -20.
|
|
434
|
+
*/
|
|
435
|
+
function integerSideMembers(token) {
|
|
436
|
+
if (!/^\d{2}(?:-\d{2})+$/.test(token.value))
|
|
437
|
+
return [token];
|
|
438
|
+
let pos = token.pos;
|
|
439
|
+
return token.value.match(/-?\d{2}/g).map(value => {
|
|
440
|
+
const member = { kind: 'Number', value, pos, length: value.length };
|
|
441
|
+
pos += value.length;
|
|
442
|
+
return member;
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
/** The type a literal compared with a left side is read as; see LiteralType. */
|
|
446
|
+
function leftLiteralType(left) {
|
|
447
|
+
const info = inferRuleExpressionArgument(left);
|
|
448
|
+
return info.invalid ? null : literalTypeOf(info.type);
|
|
449
|
+
}
|
|
384
450
|
export class Parser {
|
|
385
451
|
source;
|
|
386
452
|
diagnostics;
|
|
@@ -413,6 +479,23 @@ export class Parser {
|
|
|
413
479
|
* call node is not in the set.
|
|
414
480
|
*/
|
|
415
481
|
cutShortCalls = new WeakSet();
|
|
482
|
+
/** Whether the input holds a line break, which only then a string can span; see reportMultilineStrings(). */
|
|
483
|
+
multiline;
|
|
484
|
+
/** Whether the input has a syntax error, from the parser, or from the tokenizer in input over lines. */
|
|
485
|
+
syntaxError;
|
|
486
|
+
/**
|
|
487
|
+
* The quoted strings it read as a value the other way than the tokenizer
|
|
488
|
+
* did, by position, with whether each is the value of `matches`; see
|
|
489
|
+
* parseSource().
|
|
490
|
+
*/
|
|
491
|
+
misreadStrings;
|
|
492
|
+
/**
|
|
493
|
+
* The left side of the comparison whose value parseComparisonValue()
|
|
494
|
+
* reads, kept here rather than passed, so that the frame each nested
|
|
495
|
+
* comparison adds stays small. Nothing that runs before it reads a literal
|
|
496
|
+
* value parses another comparison, so it is still that comparison's.
|
|
497
|
+
*/
|
|
498
|
+
comparisonLeft = null;
|
|
416
499
|
constructor(tokens, source, diagnostics) {
|
|
417
500
|
this.source = source;
|
|
418
501
|
this.diagnostics = diagnostics;
|
|
@@ -420,7 +503,10 @@ export class Parser {
|
|
|
420
503
|
this.tokens = read.tokens;
|
|
421
504
|
this.operatorCharacters = read.operatorCharacters;
|
|
422
505
|
this.partners = pairDelimiters(this.tokens);
|
|
423
|
-
this.hiddenClosers = readHiddenClosers(this.hiddenText(
|
|
506
|
+
this.hiddenClosers = readHiddenClosers(this.hiddenText());
|
|
507
|
+
this.multiline = /[\r\n]/.test(source);
|
|
508
|
+
// Most input is one line, which needs no scan.
|
|
509
|
+
this.syntaxError = this.multiline && tokens.some(isMalformed);
|
|
424
510
|
}
|
|
425
511
|
parse() {
|
|
426
512
|
const node = this.parseOr();
|
|
@@ -428,6 +514,7 @@ export class Parser {
|
|
|
428
514
|
// Recovery at the top level runs to the end, so this is only a safety net.
|
|
429
515
|
this.reportAtCurrent('unexpected tokens after expression end', { id: 'syntax.unexpected_tokens_after_expression_end' });
|
|
430
516
|
}
|
|
517
|
+
this.reportMultilineStrings();
|
|
431
518
|
return node;
|
|
432
519
|
}
|
|
433
520
|
/**
|
|
@@ -435,8 +522,40 @@ export class Parser {
|
|
|
435
522
|
* invalid, with an error. What follows the value is read as after an
|
|
436
523
|
* operand at the top level, and a field or call that recovery cuts short
|
|
437
524
|
* is kept as fieldCondition() keeps it, since the text may be a comparison.
|
|
525
|
+
* `literalType` is the type of the side the value is for; see LiteralType.
|
|
526
|
+
*/
|
|
527
|
+
parseValueExpression(afterMatches = false, literalType = null) {
|
|
528
|
+
const node = this.readValueExpression(afterMatches, literalType);
|
|
529
|
+
this.reportMultilineStrings();
|
|
530
|
+
return node;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* Warns at each closed quoted string whose text holds a line break, when
|
|
534
|
+
* the input has a syntax error. A string may hold one, but a missing
|
|
535
|
+
* closing quote takes in the lines after it, up to the next `"`, so the
|
|
536
|
+
* errors land after the mistake, and the warning points back at it. A line
|
|
537
|
+
* break in an escape, or in a regex class, can't come from a missing quote;
|
|
538
|
+
* see stringLineBreak().
|
|
438
539
|
*/
|
|
439
|
-
|
|
540
|
+
reportMultilineStrings() {
|
|
541
|
+
if (!this.multiline || !this.syntaxError)
|
|
542
|
+
return;
|
|
543
|
+
for (const token of this.tokens) {
|
|
544
|
+
if (token.kind !== 'String' || token.closed === false)
|
|
545
|
+
continue;
|
|
546
|
+
const lineBreak = stringLineBreak(this.source, token);
|
|
547
|
+
if (lineBreak < 0)
|
|
548
|
+
continue;
|
|
549
|
+
this.diagnostics?.push({
|
|
550
|
+
severity: 'warning',
|
|
551
|
+
message: 'string literal spans lines; check for a missing closing quote',
|
|
552
|
+
pos: token.pos,
|
|
553
|
+
length: lineBreak - token.pos,
|
|
554
|
+
id: 'syntax.multiline_string_literal',
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
readValueExpression(afterMatches, literalType) {
|
|
440
559
|
if (startsArgument(this.peekPastStrayClosers(0)))
|
|
441
560
|
this.skipStrayClosers();
|
|
442
561
|
const start = this.peek();
|
|
@@ -446,9 +565,11 @@ export class Parser {
|
|
|
446
565
|
// A malformed token has its own error.
|
|
447
566
|
if (start.kind !== 'Invalid')
|
|
448
567
|
this.reportUnexpectedToken(start);
|
|
568
|
+
if (afterMatches)
|
|
569
|
+
this.noteMatchesValueAfterNoise();
|
|
449
570
|
return this.readRest(start.pos);
|
|
450
571
|
}
|
|
451
|
-
const value = this.parseLoneValue();
|
|
572
|
+
const value = this.parseLoneValue(afterMatches, literalType);
|
|
452
573
|
this.skipStrayClosers();
|
|
453
574
|
if (isComparisonStart(this.tokens, this.pos))
|
|
454
575
|
return this.conditionInsteadOfValue(start.pos);
|
|
@@ -457,18 +578,30 @@ export class Parser {
|
|
|
457
578
|
return this.conditionInsteadOfValue(start.pos);
|
|
458
579
|
if (!isDynamicValueNode(value) || !this.isCutShort(value, recoveryCutsShort))
|
|
459
580
|
return value;
|
|
460
|
-
return this.invalidNode(value.pos, value.length
|
|
581
|
+
return this.invalidNode(value.pos, value.length, value);
|
|
461
582
|
}
|
|
462
583
|
/** The value at the current token. A name is a field, since a value read on its own has no side. */
|
|
463
|
-
parseLoneValue() {
|
|
584
|
+
parseLoneValue(afterMatches, literalType) {
|
|
464
585
|
const token = this.peek();
|
|
465
586
|
// No name but a list's reaches parseValue() here, so its context goes unused.
|
|
466
587
|
if (token.kind !== 'Identifier' || token.value.startsWith('$'))
|
|
467
|
-
return this.parseValue('operator');
|
|
588
|
+
return this.parseValue('operator', afterMatches, literalType);
|
|
468
589
|
if (isFunctionName(token.value) && this.peekPastStrayClosers(1).kind !== 'LParen')
|
|
469
590
|
return this.missingOpeningParenthesis();
|
|
470
591
|
return this.tryParseDynamicValue() ?? this.parseFieldRef();
|
|
471
592
|
}
|
|
593
|
+
/**
|
|
594
|
+
* Notes the string after the noise and unknown characters at the current
|
|
595
|
+
* token, which starts no value, as the value of `matches`: it is once they
|
|
596
|
+
* are gone, even where recovery reads them as the value instead. Unlike
|
|
597
|
+
* skipNoise(), it reads past an unknown character.
|
|
598
|
+
*/
|
|
599
|
+
noteMatchesValueAfterNoise() {
|
|
600
|
+
let index = this.pos;
|
|
601
|
+
while (isUnknownCharacter(this.tokens[index]) || this.isNoiseBeforeComparisonValue(index))
|
|
602
|
+
index++;
|
|
603
|
+
this.noteReading(this.tokenAt(index), true);
|
|
604
|
+
}
|
|
472
605
|
/** Reports a condition where a value belongs, and reads it as invalid. */
|
|
473
606
|
conditionInsteadOfValue(startPos) {
|
|
474
607
|
const invalid = this.readRest(startPos);
|
|
@@ -512,22 +645,20 @@ export class Parser {
|
|
|
512
645
|
* delimiter, or '' for any other. What was meant to follow such a token
|
|
513
646
|
* may be in it, but only where its text holds it: in
|
|
514
647
|
* `coalesce(http.host eq "x, ssl)` the string holds the `)`, and in
|
|
515
|
-
* `lower(http.host eq "ab` it holds nothing.
|
|
516
|
-
*
|
|
517
|
-
* reads the tokenizer's tokens, since an unknown character that the
|
|
518
|
-
* parser reads past still follows the string, on a later line.
|
|
648
|
+
* `lower(http.host eq "ab` it holds nothing. Every token without its
|
|
649
|
+
* closing delimiter runs to the end of the input.
|
|
519
650
|
*/
|
|
520
|
-
hiddenText(
|
|
521
|
-
const token = tokens.at(-2);
|
|
522
|
-
if (token?.closed !== false || tokens.at(-1)?.kind !== 'EOF')
|
|
651
|
+
hiddenText() {
|
|
652
|
+
const token = this.tokens.at(-2);
|
|
653
|
+
if (token?.closed !== false || this.tokens.at(-1)?.kind !== 'EOF')
|
|
523
654
|
return '';
|
|
524
655
|
return this.source.slice(token.pos, token.pos + tokenLength(token));
|
|
525
656
|
}
|
|
526
657
|
rawSpan(startPos) {
|
|
527
658
|
const lastToken = this.tokens[this.lastRead];
|
|
528
659
|
if (!lastToken)
|
|
529
|
-
return this.source.slice(startPos)
|
|
530
|
-
return this.source.slice(startPos, lastToken.pos + tokenLength(lastToken))
|
|
660
|
+
return trimRuleExpression(this.source.slice(startPos));
|
|
661
|
+
return trimRuleExpression(this.source.slice(startPos, lastToken.pos + tokenLength(lastToken)));
|
|
531
662
|
}
|
|
532
663
|
// or_expr = and_expr ("or" and_expr)*
|
|
533
664
|
parseOr(first) {
|
|
@@ -671,7 +802,7 @@ export class Parser {
|
|
|
671
802
|
return invalid;
|
|
672
803
|
}
|
|
673
804
|
else {
|
|
674
|
-
if (!isComparisonStart(this.tokens, this.pos) || !this.endsAtComparison())
|
|
805
|
+
if (!isComparisonStart(this.tokens, this.pos) || !this.endsAtComparison(this.pos))
|
|
675
806
|
this.reportUnexpectedToken(token);
|
|
676
807
|
if (this.handlerOf(this.pos) !== null && !isLogicalOperator(token)) {
|
|
677
808
|
invalid = this.emptyInvalid(token);
|
|
@@ -773,6 +904,7 @@ export class Parser {
|
|
|
773
904
|
const accessPath = [];
|
|
774
905
|
for (this.skipStrayClosers(); this.peek().kind === 'LBracket'; this.skipStrayClosers()) {
|
|
775
906
|
const start = this.peek();
|
|
907
|
+
this.reportSpaceBeforeAccess(start);
|
|
776
908
|
const access = this.open('access');
|
|
777
909
|
const value = this.parseAccessValue(start);
|
|
778
910
|
this.finishOperand();
|
|
@@ -786,6 +918,22 @@ export class Parser {
|
|
|
786
918
|
}
|
|
787
919
|
return accessPath;
|
|
788
920
|
}
|
|
921
|
+
/**
|
|
922
|
+
* Reports a space, or a comment, before the `[` of an access. Cloudflare
|
|
923
|
+
* reads an access only right after its field, call, or previous access, and
|
|
924
|
+
* ends the value at the space. The access is read as written all the same.
|
|
925
|
+
* After a stray closer, or an access without its `]`, the space is part of
|
|
926
|
+
* that error.
|
|
927
|
+
*/
|
|
928
|
+
reportSpaceBeforeAccess(start) {
|
|
929
|
+
const before = this.tokens[this.pos - 1];
|
|
930
|
+
if (before !== this.tokens[this.lastRead] || !(before.kind === 'Identifier' || isCloser(before)))
|
|
931
|
+
return;
|
|
932
|
+
const end = before.pos + tokenLength(before);
|
|
933
|
+
if (end === start.pos)
|
|
934
|
+
return;
|
|
935
|
+
this.reportAtSpan('space before \'[\' in an access; Cloudflare reads an access only right after its field, call, or previous access', end, start.pos - end, { id: 'syntax.space_before_access' });
|
|
936
|
+
}
|
|
789
937
|
/**
|
|
790
938
|
* The value of an access, or null when it is not a valid one, after
|
|
791
939
|
* reporting it. A number or string touching an unknown character is the
|
|
@@ -817,6 +965,19 @@ export class Parser {
|
|
|
817
965
|
}
|
|
818
966
|
if (value.kind === 'String') {
|
|
819
967
|
this.consume();
|
|
968
|
+
this.noteReading(value, false);
|
|
969
|
+
// A bad escape, or a missing closing quote, has its own error.
|
|
970
|
+
if (value.invalidEscape || value.closed === false)
|
|
971
|
+
return null;
|
|
972
|
+
if (value.format === 'raw') {
|
|
973
|
+
this.reportAtToken('map key must be a quoted string, not a raw string', value, { id: 'syntax.raw_string_map_key' });
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
// A key that is not UTF-8 holds a lone surrogate; see byte-strings.ts.
|
|
977
|
+
if (!value.value.isWellFormed()) {
|
|
978
|
+
this.reportAtToken('map key must be valid UTF-8', value, { id: 'syntax.non_utf8_map_key' });
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
820
981
|
return { kind: 'MapKeyAccess', key: value.value, pos: start.pos };
|
|
821
982
|
}
|
|
822
983
|
if (value.kind === 'Invalid') {
|
|
@@ -838,9 +999,15 @@ export class Parser {
|
|
|
838
999
|
parseComparisonValue(op, insertionPos) {
|
|
839
1000
|
this.skipNoise(this.isNoiseBeforeComparisonValue, startsValue);
|
|
840
1001
|
const token = this.peek();
|
|
1002
|
+
if (op === 'matches' && !startsValue(token))
|
|
1003
|
+
this.noteMatchesValueAfterNoise();
|
|
841
1004
|
// An unknown character starts recovery, since what follows it may belong to it.
|
|
842
1005
|
if (token.kind === 'Invalid') {
|
|
1006
|
+
const start = this.pos;
|
|
843
1007
|
const invalid = this.readInvalid();
|
|
1008
|
+
// A string an unknown character touches is part of the value, as in `@"\d"`.
|
|
1009
|
+
for (let index = start; index <= this.lastRead; index++)
|
|
1010
|
+
this.noteReading(this.tokens[index], op === 'matches');
|
|
844
1011
|
this.recover();
|
|
845
1012
|
return invalid;
|
|
846
1013
|
}
|
|
@@ -855,7 +1022,18 @@ export class Parser {
|
|
|
855
1022
|
this.reportAtSpan(`missing right-hand value for operator '${op}'`, insertionPos, 0, { id: 'syntax.missing_operator_value', params: { operator: op } });
|
|
856
1023
|
return undefined;
|
|
857
1024
|
}
|
|
858
|
-
return this.tryParseDynamicValue() ?? this.
|
|
1025
|
+
return this.tryParseDynamicValue() ?? this.parseLiteralComparisonValue(op);
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* A comparison's literal value, with a number literal read as the type of
|
|
1029
|
+
* `comparisonLeft` first; see LiteralType. Its own method, so that the
|
|
1030
|
+
* frame each nested comparison adds stays small.
|
|
1031
|
+
*/
|
|
1032
|
+
parseLiteralComparisonValue(op) {
|
|
1033
|
+
// Only a number or a list reads its literals as a type, so the type is looked up for no other value.
|
|
1034
|
+
const kind = this.peek().kind;
|
|
1035
|
+
const literalType = kind === 'Number' || kind === 'LBrace' ? leftLiteralType(this.comparisonLeft) : null;
|
|
1036
|
+
return this.parseValue('operator', op === 'matches', literalType);
|
|
859
1037
|
}
|
|
860
1038
|
// dynamic_value = func_call | field_ref
|
|
861
1039
|
// Unknown identifiers stay literal so unquoted strings keep their diagnostic.
|
|
@@ -912,27 +1090,42 @@ export class Parser {
|
|
|
912
1090
|
this.consume(); // 'wildcard'
|
|
913
1091
|
return 'strict_wildcard';
|
|
914
1092
|
}
|
|
915
|
-
/**
|
|
916
|
-
|
|
1093
|
+
/**
|
|
1094
|
+
* Notes a quoted string read as a value the other way than the tokenizer
|
|
1095
|
+
* did: as the value of `matches`, or as any other value.
|
|
1096
|
+
*/
|
|
1097
|
+
noteReading(token, matchesValue) {
|
|
1098
|
+
if (token.kind !== 'String' || token.format === 'raw' || (token.format === 'regex') === matchesValue)
|
|
1099
|
+
return;
|
|
1100
|
+
(this.misreadStrings ??= new Map()).set(token.pos, matchesValue);
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* A literal value, or an InvalidNode for one that is not valid, such as an
|
|
1104
|
+
* unquoted name, after reporting it. A number literal is read as
|
|
1105
|
+
* `literalType` first; see LiteralType.
|
|
1106
|
+
*/
|
|
1107
|
+
parseValue(context, matchesValue = false, literalType = null) {
|
|
917
1108
|
const t = this.peek();
|
|
918
1109
|
if (t.kind === 'String') {
|
|
919
1110
|
this.consume();
|
|
1111
|
+
this.noteReading(t, matchesValue);
|
|
1112
|
+
// A bad escape has its own error.
|
|
1113
|
+
if (t.invalidEscape)
|
|
1114
|
+
return this.invalidNode(t.pos, tokenLength(t));
|
|
920
1115
|
return { kind: 'StringLiteral', value: t.value, pos: t.pos, length: tokenLength(t) };
|
|
921
1116
|
}
|
|
922
1117
|
if (t.kind === 'Number') {
|
|
923
1118
|
this.consume();
|
|
924
1119
|
if (this.reportHexBytesLiteral(t))
|
|
925
1120
|
return this.invalidNode(t.pos, tokenLength(t));
|
|
926
|
-
|
|
927
|
-
return { kind: 'IpLiteral', value: t.value, pos: t.pos, length: tokenLength(t) };
|
|
928
|
-
return this.numberLiteral(t);
|
|
1121
|
+
return this.numberValue(t, context === 'function', literalType);
|
|
929
1122
|
}
|
|
930
1123
|
if (t.kind === 'Boolean') {
|
|
931
1124
|
this.consume();
|
|
932
1125
|
return { kind: 'BoolLiteral', value: t.value === 'true', pos: t.pos, length: tokenLength(t) };
|
|
933
1126
|
}
|
|
934
1127
|
if (t.kind === 'LBrace')
|
|
935
|
-
return this.parseSetLiteral();
|
|
1128
|
+
return this.parseSetLiteral(literalType);
|
|
936
1129
|
if (t.kind === 'Identifier' && t.value.startsWith('$')) {
|
|
937
1130
|
this.consume();
|
|
938
1131
|
return { kind: 'ListRef', name: t.value.slice(1), pos: t.pos, length: tokenLength(t) };
|
|
@@ -964,42 +1157,73 @@ export class Parser {
|
|
|
964
1157
|
this.pos = partner;
|
|
965
1158
|
this.consume();
|
|
966
1159
|
}
|
|
967
|
-
/**
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1160
|
+
/**
|
|
1161
|
+
* A number token as one value: an integer, with its exact `integer` value,
|
|
1162
|
+
* or one IP address. Text that neither reads gets the error of the type
|
|
1163
|
+
* its place expects, or with none, of the type its text suggests. A literal
|
|
1164
|
+
* that only the other type reads stays one, for the type check to report,
|
|
1165
|
+
* as in `ip.src eq 1`. Cloudflare reads a function argument as an IP
|
|
1166
|
+
* address, then an integer, whatever its parameter, so the parameter's
|
|
1167
|
+
* type picks only the error. An argument can't be hex, which Cloudflare
|
|
1168
|
+
* reads as a name there.
|
|
1169
|
+
*/
|
|
1170
|
+
numberValue(token, argument, literalType) {
|
|
1171
|
+
const text = token.value;
|
|
1172
|
+
if (argument && /^0[xX]/.test(text)) {
|
|
1173
|
+
this.reportAtToken(`hex integer literal '${text}' is read as a name in a function argument; write it in decimal`, token, { id: 'syntax.hex_integer_argument', params: { literal: text } });
|
|
971
1174
|
return this.invalidNode(token.pos, tokenLength(token));
|
|
972
|
-
|
|
1175
|
+
}
|
|
1176
|
+
const integer = int64LiteralValue(text);
|
|
1177
|
+
if (integer !== null)
|
|
1178
|
+
return { kind: 'NumberLiteral', value: Number(integer), integer, raw: text, pos: token.pos, length: tokenLength(token) };
|
|
1179
|
+
if (readIpAddress(text) !== null)
|
|
1180
|
+
return { kind: 'IpLiteral', value: text, pos: token.pos, length: tokenLength(token) };
|
|
1181
|
+
const expected = argument ? this.argumentLiteralType() : literalType;
|
|
1182
|
+
if (expected === 'ip' || (expected === null && IP_ADDRESS_LIKE.test(text))) {
|
|
1183
|
+
const member = readIpListMember(text);
|
|
1184
|
+
// An argument can't be the list a range needs.
|
|
1185
|
+
if (!argument && 'range' in member && (text.includes('/') || text.includes('..'))) {
|
|
1186
|
+
this.reportAtToken(`IP range '${text}' is valid only in an inline list, as in 'in {${text}}'`, token, { id: 'syntax.ip_range_requires_list', params: { literal: text } });
|
|
1187
|
+
}
|
|
1188
|
+
else {
|
|
1189
|
+
this.reportInvalidIpAddress(token);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
else {
|
|
1193
|
+
this.readInteger(token, text);
|
|
1194
|
+
}
|
|
1195
|
+
return this.invalidNode(token.pos, tokenLength(token));
|
|
1196
|
+
}
|
|
1197
|
+
/** The type the innermost call's parameter for the argument read declares, as a LiteralType. */
|
|
1198
|
+
argumentLiteralType() {
|
|
1199
|
+
const call = this.constructs.at(-1);
|
|
1200
|
+
return literalTypeOf(parameterDataType(call.functionName, call.argumentIndex));
|
|
973
1201
|
}
|
|
974
|
-
parseSetLiteral() {
|
|
1202
|
+
parseSetLiteral(literalType) {
|
|
975
1203
|
const start = this.peek();
|
|
976
1204
|
const list = this.open('list');
|
|
977
1205
|
const elements = [];
|
|
978
1206
|
// A list with its `}` owns every token up to it; one without ends where an enclosing construct resumes.
|
|
979
1207
|
while (this.handlerOf(this.pos) === null) {
|
|
980
1208
|
const t = this.peek();
|
|
981
|
-
const unquotedType = t.kind === 'Number' ? unquotedSetElementType(t.value) : null;
|
|
982
1209
|
if (t.kind === 'String') {
|
|
983
|
-
elements.push({ type: 'string', value: t.value });
|
|
984
1210
|
this.consume();
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
this.consume();
|
|
989
|
-
// Ranges stay as written.
|
|
990
|
-
elements.push(first === null || last === null
|
|
1211
|
+
this.noteReading(t, false);
|
|
1212
|
+
// A bad escape has its own error.
|
|
1213
|
+
elements.push(t.invalidEscape
|
|
991
1214
|
? this.invalidMember(t)
|
|
992
|
-
: { type: '
|
|
1215
|
+
: { type: 'string', value: t.value, pos: t.pos, length: tokenLength(t) });
|
|
993
1216
|
}
|
|
994
|
-
else if (
|
|
995
|
-
elements.push({ type: 'ip', value: t.value });
|
|
1217
|
+
else if (t.kind === 'Number') {
|
|
996
1218
|
this.consume();
|
|
1219
|
+
for (const member of literalType === 'number' ? integerSideMembers(t) : [t])
|
|
1220
|
+
elements.push(this.setMember(member, literalType));
|
|
997
1221
|
}
|
|
998
1222
|
else {
|
|
999
1223
|
if (t.kind === 'Identifier')
|
|
1000
1224
|
this.reportUnquotedName(t, 'set');
|
|
1001
1225
|
// A malformed token has its own error.
|
|
1002
|
-
else if (
|
|
1226
|
+
else if (t.kind !== 'Invalid') {
|
|
1003
1227
|
this.reportAtToken(`unexpected token in set literal: ${t.kind} "${t.value}"`, t, { id: 'syntax.unexpected_token_in_set_literal', params: { tokenKind: t.kind, tokenValue: t.value } });
|
|
1004
1228
|
}
|
|
1005
1229
|
this.skipInvalidValue();
|
|
@@ -1009,9 +1233,97 @@ export class Parser {
|
|
|
1009
1233
|
this.close(list, () => this.reportAtCurrent('missing closing brace', { id: 'syntax.missing_closing_brace' }));
|
|
1010
1234
|
return { kind: 'SetLiteral', elements, pos: start.pos, length: this.fieldLength(start.pos) };
|
|
1011
1235
|
}
|
|
1012
|
-
/**
|
|
1013
|
-
|
|
1014
|
-
|
|
1236
|
+
/**
|
|
1237
|
+
* An unquoted list member, read as `literalType` first, as Cloudflare
|
|
1238
|
+
* reads each member as the type of the list's left side: an integer or
|
|
1239
|
+
* integer range, or an IP address, CIDR, or IP range. A member that only
|
|
1240
|
+
* the other type reads stays one, for the type check to report. One that
|
|
1241
|
+
* neither reads gets the error of the type its place expects, or with
|
|
1242
|
+
* none, of the type its text suggests.
|
|
1243
|
+
*/
|
|
1244
|
+
setMember(token, literalType) {
|
|
1245
|
+
// A member spans its token, which may be one that integerSideMembers() split off, as `08` is from `08-10`.
|
|
1246
|
+
const length = tokenLength(token);
|
|
1247
|
+
if (this.reportHexBytesLiteral(token))
|
|
1248
|
+
return this.invalidMember(token, length);
|
|
1249
|
+
const text = token.value;
|
|
1250
|
+
const bounds = integerRangeBounds(text);
|
|
1251
|
+
const integer = bounds && { type: 'integer', value: text.includes('..') ? text : Number(bounds[0]), raw: text, bounds };
|
|
1252
|
+
// Digits alone are an IP address, such as `10` for 10.0.0.0, only where one is expected.
|
|
1253
|
+
const ipMember = literalType === 'ip' || IP_MEMBER_LIKE.test(text) ? readIpListMember(text) : null;
|
|
1254
|
+
const ip = ipMember && 'range' in ipMember ? { type: 'ip', value: text, range: ipMember.range } : null;
|
|
1255
|
+
const member = literalType === 'ip' ? ip ?? integer : integer ?? ip;
|
|
1256
|
+
if (member) {
|
|
1257
|
+
if (member === ip && literalType !== 'number' && ipMember && 'range' in ipMember && ipMember.shortFormPrefix !== null) {
|
|
1258
|
+
this.warnShortIpv4Address(token, ipMember.range.first, ipMember.shortFormPrefix);
|
|
1259
|
+
}
|
|
1260
|
+
return member;
|
|
1261
|
+
}
|
|
1262
|
+
if (literalType !== 'number' && ipMember && 'error' in ipMember)
|
|
1263
|
+
this.reportInvalidIpMember(token, ipMember);
|
|
1264
|
+
else
|
|
1265
|
+
this.reportInvalidIntegerMember(token);
|
|
1266
|
+
return this.invalidMember(token, length);
|
|
1267
|
+
}
|
|
1268
|
+
/** Reports an integer list member that is not valid: a bound that is not an integer, a missing bound, or reversed bounds. */
|
|
1269
|
+
reportInvalidIntegerMember(token) {
|
|
1270
|
+
const text = token.value;
|
|
1271
|
+
const bounds = text.split('..');
|
|
1272
|
+
if (bounds.length > 2 || bounds.includes('')) {
|
|
1273
|
+
this.reportAtToken(`number literal '${text}' must be an integer or an integer range`, token, { id: 'syntax.invalid_integer_literal', params: { literal: text } });
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
const [first, last = first] = bounds.map(bound => this.readInteger(token, bound));
|
|
1277
|
+
if (first === null || last === null)
|
|
1278
|
+
return;
|
|
1279
|
+
this.reportReversedRange(token, bounds[0], bounds[1]);
|
|
1280
|
+
}
|
|
1281
|
+
/** Reports why an IP list member is not valid. */
|
|
1282
|
+
reportInvalidIpMember(token, member) {
|
|
1283
|
+
const text = token.value;
|
|
1284
|
+
switch (member.error) {
|
|
1285
|
+
case 'address':
|
|
1286
|
+
this.reportInvalidIpAddress(token);
|
|
1287
|
+
return;
|
|
1288
|
+
case 'prefix':
|
|
1289
|
+
this.reportAtToken(`CIDR '${text}' needs a decimal prefix length from 0 to ${member.max}`, token, { id: 'syntax.invalid_cidr_prefix', params: { literal: text, prefix: member.prefix, max: member.max } });
|
|
1290
|
+
return;
|
|
1291
|
+
case 'host_bits':
|
|
1292
|
+
this.reportAtToken(`CIDR '${text}' has bits set past its prefix; write ${member.network}`, token, { id: 'syntax.cidr_host_bits', params: { literal: text, network: member.network } });
|
|
1293
|
+
return;
|
|
1294
|
+
case 'reversed':
|
|
1295
|
+
this.reportReversedRange(token, member.first, member.last);
|
|
1296
|
+
return;
|
|
1297
|
+
case 'mixed_versions':
|
|
1298
|
+
this.reportAtToken(`IP range '${text}' has an IPv4 and an IPv6 bound`, token, { id: 'syntax.mixed_ip_range', params: { literal: text } });
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
reportReversedRange(token, first, last) {
|
|
1303
|
+
this.reportAtToken(`range '${token.value}' starts after it ends; write the lower bound first`, token, { id: 'syntax.reversed_range', params: { literal: token.value, first, last } });
|
|
1304
|
+
}
|
|
1305
|
+
reportInvalidIpAddress(token) {
|
|
1306
|
+
this.reportAtToken(`'${token.value}' is not a valid IP address`, token, { id: 'syntax.invalid_ip_address', params: { literal: token.value } });
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Warns at an IPv4 list member with fewer than four parts and no prefix,
|
|
1310
|
+
* which is one address, such as 10.1.0.0 for `10.1`, while the network,
|
|
1311
|
+
* 10.1.0.0/16, is a likely meaning. It is no syntax error.
|
|
1312
|
+
*/
|
|
1313
|
+
warnShortIpv4Address(token, address, prefix) {
|
|
1314
|
+
const written = formatIpAddressValue({ version: 4, value: address });
|
|
1315
|
+
this.diagnostics?.push({
|
|
1316
|
+
severity: 'warning',
|
|
1317
|
+
message: `'${token.value}' is the one address ${written}; write ${written}/${prefix} for the network`,
|
|
1318
|
+
pos: token.pos,
|
|
1319
|
+
length: tokenLength(token),
|
|
1320
|
+
id: 'inline_list.short_ipv4_address',
|
|
1321
|
+
params: { literal: token.value, address: written, network: `${written}/${prefix}` },
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
/** A list member that is not a valid element, from its token to the last token read, or of the length given. */
|
|
1325
|
+
invalidMember(token, length = this.fieldLength(token.pos)) {
|
|
1326
|
+
return { ...this.invalidNode(token.pos, length), type: 'invalid' };
|
|
1015
1327
|
}
|
|
1016
1328
|
// func_call = funcName "(" args ")"
|
|
1017
1329
|
parseFuncCall(startPos) {
|
|
@@ -1115,7 +1427,7 @@ export class Parser {
|
|
|
1115
1427
|
break;
|
|
1116
1428
|
// A call that ends at a comparison operator ends before it, and the comparison then compares its
|
|
1117
1429
|
// result; see parseArgumentComparison().
|
|
1118
|
-
if (isComparisonStart(this.tokens, this.pos) && this.endsAtComparison())
|
|
1430
|
+
if (isComparisonStart(this.tokens, this.pos) && this.endsAtComparison(this.pos))
|
|
1119
1431
|
break;
|
|
1120
1432
|
// What follows a complete argument starts the next one. It may be a stray token instead, which may
|
|
1121
1433
|
// belong to the argument before it, so that argument and the ones after it are uncertain, and
|
|
@@ -1151,12 +1463,18 @@ export class Parser {
|
|
|
1151
1463
|
const cutShort = this.finishOperand();
|
|
1152
1464
|
if (!isLogicalOperator(this.peek()))
|
|
1153
1465
|
return argument;
|
|
1154
|
-
// Cloudflare needs parentheses to combine conditions here; read the rest as if they were there.
|
|
1155
1466
|
const condition = this.asCondition(argument, cutShort);
|
|
1467
|
+
this.constructs.at(-1).readingCondition = true;
|
|
1468
|
+
return this.parseUnparenthesizedLogicalRest(condition, startPos);
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Reads the logical operators after an argument's first condition as if
|
|
1472
|
+
* parentheses were around them, which Cloudflare needs, and reports them.
|
|
1473
|
+
*/
|
|
1474
|
+
parseUnparenthesizedLogicalRest(first, startPos) {
|
|
1156
1475
|
const operator = this.peek().value;
|
|
1157
1476
|
this.reportAtCurrent(`conditions combined with '${operator}' in a function argument need parentheses`, { id: 'syntax.unparenthesized_logical_argument', params: { operator } });
|
|
1158
|
-
this.
|
|
1159
|
-
return this.parseOr({ node: condition, pos: startPos });
|
|
1477
|
+
return this.parseOr({ node: first, pos: startPos });
|
|
1160
1478
|
}
|
|
1161
1479
|
/**
|
|
1162
1480
|
* Reports and skips each token that stands for nothing before what
|
|
@@ -1178,13 +1496,16 @@ export class Parser {
|
|
|
1178
1496
|
}
|
|
1179
1497
|
/**
|
|
1180
1498
|
* Skips the tokens that stand for nothing before an argument; see
|
|
1181
|
-
* skipNoise().
|
|
1182
|
-
*
|
|
1183
|
-
* the
|
|
1499
|
+
* skipNoise(). Before a quantifier's predicate, `starts` is
|
|
1500
|
+
* startsCondition(), since a value there is no predicate: the tokens
|
|
1501
|
+
* before one stay, and the first of them gets the error. A comparison
|
|
1502
|
+
* operator among them may have lost its left side, which would make the
|
|
1503
|
+
* argument a comparison, so that argument and the ones after it are
|
|
1504
|
+
* uncertain.
|
|
1184
1505
|
*/
|
|
1185
|
-
skipNoiseBeforeArgument() {
|
|
1506
|
+
skipNoiseBeforeArgument(starts = startsArgument) {
|
|
1186
1507
|
const start = this.pos;
|
|
1187
|
-
this.skipNoise(this.isNoiseBeforeArgument,
|
|
1508
|
+
this.skipNoise(this.isNoiseBeforeArgument, starts);
|
|
1188
1509
|
for (let index = start; index < this.pos; index++) {
|
|
1189
1510
|
if (!isComparisonStart(this.tokens, index))
|
|
1190
1511
|
continue;
|
|
@@ -1275,7 +1596,7 @@ export class Parser {
|
|
|
1275
1596
|
case 'BoolLiteral':
|
|
1276
1597
|
case 'SetLiteral':
|
|
1277
1598
|
case 'ListRef':
|
|
1278
|
-
return this.invalidNode(argument.pos, argument.length
|
|
1599
|
+
return this.invalidNode(argument.pos, argument.length);
|
|
1279
1600
|
default:
|
|
1280
1601
|
return unhandledNode(argument);
|
|
1281
1602
|
}
|
|
@@ -1301,7 +1622,7 @@ export class Parser {
|
|
|
1301
1622
|
nearest: {
|
|
1302
1623
|
closed: closer !== null ? depth : outside.closed,
|
|
1303
1624
|
call: kind === 'call' ? depth : outside.call,
|
|
1304
|
-
logical: kind === 'group' || kind === 'call' ? depth : outside.logical,
|
|
1625
|
+
logical: kind === 'group' || kind === 'call' || (kind === 'quantifier' && closer !== null) ? depth : outside.logical,
|
|
1305
1626
|
access: closer === null && kind === 'access' ? depth : outside.access,
|
|
1306
1627
|
},
|
|
1307
1628
|
};
|
|
@@ -1366,7 +1687,7 @@ export class Parser {
|
|
|
1366
1687
|
handler = Math.max(handler, nearest.logical);
|
|
1367
1688
|
if (argumentStarts && startsArgument(token))
|
|
1368
1689
|
handler = Math.max(handler, nearest.call);
|
|
1369
|
-
if (isComparisonStart(this.tokens, index) && this.endsAtComparison())
|
|
1690
|
+
if (isComparisonStart(this.tokens, index) && this.endsAtComparison(index))
|
|
1370
1691
|
handler = Math.max(handler, this.constructs.length - 1);
|
|
1371
1692
|
if (token.kind === 'LBracket')
|
|
1372
1693
|
handler = Math.max(handler, nearest.access);
|
|
@@ -1377,19 +1698,19 @@ export class Parser {
|
|
|
1377
1698
|
return isLogicalOperator(token) ? -1 : null;
|
|
1378
1699
|
}
|
|
1379
1700
|
/**
|
|
1380
|
-
* Whether the innermost construct ends at
|
|
1381
|
-
* without its `]`, or a call without its `)` that reads a
|
|
1382
|
-
* the comparison cannot be; see takesComparison(). A
|
|
1383
|
-
* does not: the comparison belongs to the innermost
|
|
1384
|
-
* one holds, such as a condition.
|
|
1701
|
+
* Whether the innermost construct ends at the comparison operator at an
|
|
1702
|
+
* index: an access without its `]`, or a call without its `)` that reads a
|
|
1703
|
+
* value argument the comparison cannot be; see takesComparison(). A
|
|
1704
|
+
* construct outside it does not: the comparison belongs to the innermost
|
|
1705
|
+
* one, or to what that one holds, such as a condition.
|
|
1385
1706
|
*/
|
|
1386
|
-
endsAtComparison() {
|
|
1707
|
+
endsAtComparison(operator) {
|
|
1387
1708
|
const construct = this.constructs.at(-1);
|
|
1388
1709
|
if (construct === undefined || construct.closer !== null)
|
|
1389
1710
|
return false;
|
|
1390
1711
|
if (construct.kind === 'access')
|
|
1391
1712
|
return true;
|
|
1392
|
-
return construct.kind === 'call' && !construct.readingCondition && !takesComparison(construct);
|
|
1713
|
+
return construct.kind === 'call' && !construct.readingCondition && !takesComparison(construct, this.tokens[operator]);
|
|
1393
1714
|
}
|
|
1394
1715
|
/** Whether a construct outside the one at `depth` resumes at the current token. */
|
|
1395
1716
|
resumesOutside(depth) {
|
|
@@ -1522,7 +1843,7 @@ export class Parser {
|
|
|
1522
1843
|
* parseFunctionArg() for a logical operator after it.
|
|
1523
1844
|
*/
|
|
1524
1845
|
parseArgumentComparison(left, startPos) {
|
|
1525
|
-
if (this.endsAtComparison())
|
|
1846
|
+
if (this.endsAtComparison(this.pos))
|
|
1526
1847
|
return null;
|
|
1527
1848
|
return this.parseComparisonRest(left, startPos);
|
|
1528
1849
|
}
|
|
@@ -1532,6 +1853,7 @@ export class Parser {
|
|
|
1532
1853
|
const op = this.tryParseCompOp();
|
|
1533
1854
|
if (op === null)
|
|
1534
1855
|
return null;
|
|
1856
|
+
this.comparisonLeft = left;
|
|
1535
1857
|
const right = this.parseComparisonValue(op, this.previousTokenEnd());
|
|
1536
1858
|
const raw = this.rawSpan(startPos);
|
|
1537
1859
|
return { kind: 'Comparison', left, op, right, raw, pos: startPos, length: raw.length };
|
|
@@ -1553,9 +1875,9 @@ export class Parser {
|
|
|
1553
1875
|
functionCondition(funcNode, recoveryCutsShort) {
|
|
1554
1876
|
if (!this.isCutShort(funcNode, recoveryCutsShort))
|
|
1555
1877
|
return funcNode;
|
|
1556
|
-
return this.invalidNode(funcNode.pos, funcNode.length
|
|
1878
|
+
return this.invalidNode(funcNode.pos, funcNode.length, funcNode);
|
|
1557
1879
|
}
|
|
1558
|
-
// any(
|
|
1880
|
+
// any(predicate) or all(predicate); see parseQuantifierPredicate()
|
|
1559
1881
|
parseAnyAll(name, startPos) {
|
|
1560
1882
|
const quantifier = this.open('quantifier');
|
|
1561
1883
|
this.skipStrayClosers();
|
|
@@ -1573,12 +1895,49 @@ export class Parser {
|
|
|
1573
1895
|
this.reportAtSpan(`missing argument for function '${name}'`, startPos, Math.max(raw.length, name.length + 2), { id: 'syntax.missing_function_argument', params: { functionName: name } });
|
|
1574
1896
|
return this.funcNode(name, quantifierArguments([], undefined, false), [], startPos);
|
|
1575
1897
|
}
|
|
1576
|
-
const predicate = this.
|
|
1577
|
-
this.finishOperand();
|
|
1898
|
+
const predicate = this.parseQuantifierPredicate();
|
|
1578
1899
|
const closing = this.close(quantifier, () => this.reportMissingCallCloser(name));
|
|
1579
1900
|
const parsed = quantifierArguments([predicate], quantifier.uncertainFrom ?? (closing === 'read' ? undefined : 1), quantifier.recoveredIn !== null || closing !== 'read');
|
|
1580
1901
|
return this.funcNode(name, parsed, this.parseAccessPath(), startPos);
|
|
1581
1902
|
}
|
|
1903
|
+
/**
|
|
1904
|
+
* Parses the argument of any() or all() as Cloudflare does, as a function
|
|
1905
|
+
* argument is read: one that starts with `(`, `not`, any() or all() is a
|
|
1906
|
+
* whole logical expression, and any other is one condition, which needs
|
|
1907
|
+
* parentheses to combine with others. Without its `)`, the quantifier
|
|
1908
|
+
* leaves a logical operator after such a condition to the condition around
|
|
1909
|
+
* it; see NearestConstructs.
|
|
1910
|
+
*/
|
|
1911
|
+
parseQuantifierPredicate() {
|
|
1912
|
+
const closed = this.constructs.at(-1).closer !== null;
|
|
1913
|
+
this.skipNoiseBeforeArgument(startsCondition);
|
|
1914
|
+
const combines = this.startsLogicalArgument();
|
|
1915
|
+
if (combines && closed) {
|
|
1916
|
+
const condition = this.parseOr();
|
|
1917
|
+
if (condition.kind === 'Func')
|
|
1918
|
+
condition.condition = true;
|
|
1919
|
+
return condition;
|
|
1920
|
+
}
|
|
1921
|
+
const startPos = this.conditionStart();
|
|
1922
|
+
const condition = isLogicalOperator(this.peek()) ? this.invalidLogicalPredicate() : this.parseNot();
|
|
1923
|
+
this.finishOperand();
|
|
1924
|
+
if (!isLogicalOperator(this.peek()) || (!closed && !combines)) {
|
|
1925
|
+
// As in a function argument, a lone call that starts a logical argument is a condition.
|
|
1926
|
+
if (combines && condition.kind === 'Func')
|
|
1927
|
+
condition.condition = true;
|
|
1928
|
+
return condition;
|
|
1929
|
+
}
|
|
1930
|
+
// The `)` may be missing before the operator instead, which would end the predicate there.
|
|
1931
|
+
this.leaveArgumentUncertain();
|
|
1932
|
+
return combines ? this.parseOr({ node: condition, pos: startPos }) : this.parseUnparenthesizedLogicalRest(condition, startPos);
|
|
1933
|
+
}
|
|
1934
|
+
/** A logical operator that starts a predicate combines nothing, so it is the predicate, invalid; see invalidArgument(). */
|
|
1935
|
+
invalidLogicalPredicate() {
|
|
1936
|
+
this.reportUnexpectedToken(this.peek());
|
|
1937
|
+
const invalid = this.readInvalid();
|
|
1938
|
+
this.consume();
|
|
1939
|
+
return invalid;
|
|
1940
|
+
}
|
|
1582
1941
|
/**
|
|
1583
1942
|
* Reads an integer literal, reporting one Cloudflare rejects: one with a
|
|
1584
1943
|
* fraction, an octal one with the digit 8 or 9, or one outside the 64-bit
|
|
@@ -1607,7 +1966,7 @@ export class Parser {
|
|
|
1607
1966
|
* package does not decode. Text that is also an IPv6 address stays an address.
|
|
1608
1967
|
*/
|
|
1609
1968
|
reportHexBytesLiteral(token) {
|
|
1610
|
-
if (!/^[\da-fA-F]{2}(?:[:-][\da-fA-F]{2})+$/.test(token.value) ||
|
|
1969
|
+
if (!/^[\da-fA-F]{2}(?:[:-][\da-fA-F]{2})+$/.test(token.value) || 'range' in readIpListMember(token.value))
|
|
1611
1970
|
return false;
|
|
1612
1971
|
this.reportAtToken(`hex byte literal '${token.value}' is not supported; write the value as a quoted string`, token, { id: 'syntax.unsupported_hex_bytes_literal', params: { literal: token.value } });
|
|
1613
1972
|
return true;
|
|
@@ -1640,7 +1999,45 @@ export class Parser {
|
|
|
1640
1999
|
});
|
|
1641
2000
|
}
|
|
1642
2001
|
report(detail) {
|
|
2002
|
+
this.syntaxError = true;
|
|
1643
2003
|
this.diagnostics?.push(detail);
|
|
1644
2004
|
}
|
|
1645
2005
|
}
|
|
2006
|
+
/** How many times parseSource() reads the input at most. */
|
|
2007
|
+
const MAX_PASSES = 3;
|
|
2008
|
+
/**
|
|
2009
|
+
* Tokenizes and parses the input. The tokenizer reads a quoted string right
|
|
2010
|
+
* after `matches` as a regex literal, which is always the value of `matches`
|
|
2011
|
+
* in valid input. Recovery may read a string in input with errors the other
|
|
2012
|
+
* way, as in `http.host matches , "\d"`, so each string the parser read as a
|
|
2013
|
+
* value the other way is read again its way, and the input parsed again; see
|
|
2014
|
+
* docs/string-literal-alignment.md. Each pass writes into the caller's
|
|
2015
|
+
* diagnostics and comments, and a pass that is redone takes its own back.
|
|
2016
|
+
* The tokenizer's diagnostics come first; `tokenizerDiagnosticCount` says
|
|
2017
|
+
* how many of the last pass's there are. `afterMatches` says that the input
|
|
2018
|
+
* follows `matches`; see InternalTokenizeOptions.
|
|
2019
|
+
*/
|
|
2020
|
+
export function parseSource(source, diagnostics, options, metadata, parse, afterMatches = false) {
|
|
2021
|
+
const diagnosticCount = diagnostics?.length ?? 0;
|
|
2022
|
+
const commentCount = metadata.comments?.length ?? 0;
|
|
2023
|
+
// Only the public option is copied, so that a caller's options object can't set the internal ones.
|
|
2024
|
+
const tokenizeOptions = { comments: options.comments, afterMatches };
|
|
2025
|
+
let regexStrings;
|
|
2026
|
+
for (let pass = 1;; pass++) {
|
|
2027
|
+
const tokens = tokenize(source, diagnostics, regexStrings ? { ...tokenizeOptions, regexStrings } : tokenizeOptions, metadata);
|
|
2028
|
+
const tokenizerDiagnosticCount = (diagnostics?.length ?? 0) - diagnosticCount;
|
|
2029
|
+
const parser = new Parser(tokens, source, diagnostics);
|
|
2030
|
+
const result = parse(parser);
|
|
2031
|
+
const misread = parser.misreadStrings;
|
|
2032
|
+
// A string read back the way an earlier pass read it would only repeat that pass.
|
|
2033
|
+
if (misread === undefined || pass === MAX_PASSES || [...misread.keys()].some(pos => regexStrings?.has(pos))) {
|
|
2034
|
+
return { tokens, result, tokenizerDiagnosticCount };
|
|
2035
|
+
}
|
|
2036
|
+
if (diagnostics)
|
|
2037
|
+
diagnostics.length = diagnosticCount;
|
|
2038
|
+
if (metadata.comments)
|
|
2039
|
+
metadata.comments.length = commentCount;
|
|
2040
|
+
regexStrings = new Map([...regexStrings ?? [], ...misread]);
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
1646
2043
|
//# sourceMappingURL=parser.js.map
|