@libreflare/worker-runtime 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +235 -13
  2. package/dist/rule-evaluator/accept-encoding.d.ts +2 -2
  3. package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
  4. package/dist/rule-evaluator/accept-encoding.js +62 -15
  5. package/dist/rule-evaluator/accept-encoding.js.map +1 -1
  6. package/dist/rule-evaluator/access-values.d.ts +77 -14
  7. package/dist/rule-evaluator/access-values.d.ts.map +1 -1
  8. package/dist/rule-evaluator/access-values.js +149 -17
  9. package/dist/rule-evaluator/access-values.js.map +1 -1
  10. package/dist/rule-evaluator/analysis.d.ts +43 -8
  11. package/dist/rule-evaluator/analysis.d.ts.map +1 -1
  12. package/dist/rule-evaluator/analysis.js +72 -29
  13. package/dist/rule-evaluator/analysis.js.map +1 -1
  14. package/dist/rule-evaluator/ast.d.ts +145 -11
  15. package/dist/rule-evaluator/ast.d.ts.map +1 -1
  16. package/dist/rule-evaluator/ast.js +102 -4
  17. package/dist/rule-evaluator/ast.js.map +1 -1
  18. package/dist/rule-evaluator/byte-strings.d.ts +23 -0
  19. package/dist/rule-evaluator/byte-strings.d.ts.map +1 -0
  20. package/dist/rule-evaluator/byte-strings.js +150 -0
  21. package/dist/rule-evaluator/byte-strings.js.map +1 -0
  22. package/dist/rule-evaluator/constants.d.ts +1 -1
  23. package/dist/rule-evaluator/constants.d.ts.map +1 -1
  24. package/dist/rule-evaluator/constants.js +1 -0
  25. package/dist/rule-evaluator/constants.js.map +1 -1
  26. package/dist/rule-evaluator/country-codes.generated.js +1 -1
  27. package/dist/rule-evaluator/custom-lists.d.ts +9 -0
  28. package/dist/rule-evaluator/custom-lists.d.ts.map +1 -1
  29. package/dist/rule-evaluator/custom-lists.js +43 -0
  30. package/dist/rule-evaluator/custom-lists.js.map +1 -1
  31. package/dist/rule-evaluator/diagnostics.d.ts +1 -3
  32. package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
  33. package/dist/rule-evaluator/diagnostics.js +10 -53
  34. package/dist/rule-evaluator/diagnostics.js.map +1 -1
  35. package/dist/rule-evaluator/engine-observations.d.ts +37 -0
  36. package/dist/rule-evaluator/engine-observations.d.ts.map +1 -0
  37. package/dist/rule-evaluator/engine-observations.generated.d.ts +991 -0
  38. package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -0
  39. package/dist/rule-evaluator/engine-observations.generated.js +292 -0
  40. package/dist/rule-evaluator/engine-observations.generated.js.map +1 -0
  41. package/dist/rule-evaluator/engine-observations.js +13 -0
  42. package/dist/rule-evaluator/engine-observations.js.map +1 -0
  43. package/dist/rule-evaluator/evaluator.d.ts +1 -1
  44. package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
  45. package/dist/rule-evaluator/evaluator.js +16 -15
  46. package/dist/rule-evaluator/evaluator.js.map +1 -1
  47. package/dist/rule-evaluator/explain.d.ts.map +1 -1
  48. package/dist/rule-evaluator/explain.js +35 -20
  49. package/dist/rule-evaluator/explain.js.map +1 -1
  50. package/dist/rule-evaluator/field-availability.d.ts +11 -10
  51. package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
  52. package/dist/rule-evaluator/field-availability.js +136 -97
  53. package/dist/rule-evaluator/field-availability.js.map +1 -1
  54. package/dist/rule-evaluator/field-descriptions.generated.d.ts +1 -1
  55. package/dist/rule-evaluator/field-descriptions.generated.js +1 -1
  56. package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
  57. package/dist/rule-evaluator/field-value-domains.d.ts.map +1 -1
  58. package/dist/rule-evaluator/field-value-domains.js +2 -4
  59. package/dist/rule-evaluator/field-value-domains.js.map +1 -1
  60. package/dist/rule-evaluator/fields.d.ts +25 -2
  61. package/dist/rule-evaluator/fields.d.ts.map +1 -1
  62. package/dist/rule-evaluator/fields.js +276 -119
  63. package/dist/rule-evaluator/fields.js.map +1 -1
  64. package/dist/rule-evaluator/function-availability.d.ts +16 -0
  65. package/dist/rule-evaluator/function-availability.d.ts.map +1 -0
  66. package/dist/rule-evaluator/function-availability.js +28 -0
  67. package/dist/rule-evaluator/function-availability.js.map +1 -0
  68. package/dist/rule-evaluator/function-descriptions.d.ts +15 -1
  69. package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
  70. package/dist/rule-evaluator/function-descriptions.generated.d.ts +50 -7
  71. package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
  72. package/dist/rule-evaluator/function-descriptions.generated.js +98 -12
  73. package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
  74. package/dist/rule-evaluator/function-descriptions.js +11 -2
  75. package/dist/rule-evaluator/function-descriptions.js.map +1 -1
  76. package/dist/rule-evaluator/function-signature-validation.d.ts +26 -2
  77. package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
  78. package/dist/rule-evaluator/function-signature-validation.js +311 -181
  79. package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
  80. package/dist/rule-evaluator/functions.d.ts +24 -5
  81. package/dist/rule-evaluator/functions.d.ts.map +1 -1
  82. package/dist/rule-evaluator/functions.js +353 -190
  83. package/dist/rule-evaluator/functions.js.map +1 -1
  84. package/dist/rule-evaluator/hash-in-range.d.ts +33 -0
  85. package/dist/rule-evaluator/hash-in-range.d.ts.map +1 -0
  86. package/dist/rule-evaluator/hash-in-range.js +72 -0
  87. package/dist/rule-evaluator/hash-in-range.js.map +1 -0
  88. package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
  89. package/dist/rule-evaluator/log-sample.js +14 -6
  90. package/dist/rule-evaluator/log-sample.js.map +1 -1
  91. package/dist/rule-evaluator/match-chain.d.ts +3 -3
  92. package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
  93. package/dist/rule-evaluator/match-chain.js +19 -13
  94. package/dist/rule-evaluator/match-chain.js.map +1 -1
  95. package/dist/rule-evaluator/minifier.d.ts.map +1 -1
  96. package/dist/rule-evaluator/minifier.js +16 -20
  97. package/dist/rule-evaluator/minifier.js.map +1 -1
  98. package/dist/rule-evaluator/normalizer.d.ts.map +1 -1
  99. package/dist/rule-evaluator/normalizer.js +6 -6
  100. package/dist/rule-evaluator/normalizer.js.map +1 -1
  101. package/dist/rule-evaluator/operators.d.ts +11 -4
  102. package/dist/rule-evaluator/operators.d.ts.map +1 -1
  103. package/dist/rule-evaluator/operators.js +62 -35
  104. package/dist/rule-evaluator/operators.js.map +1 -1
  105. package/dist/rule-evaluator/outcome.d.ts +2 -0
  106. package/dist/rule-evaluator/outcome.d.ts.map +1 -1
  107. package/dist/rule-evaluator/outcome.js +4 -0
  108. package/dist/rule-evaluator/outcome.js.map +1 -1
  109. package/dist/rule-evaluator/parse-cache.d.ts +3 -3
  110. package/dist/rule-evaluator/parse-cache.d.ts.map +1 -1
  111. package/dist/rule-evaluator/parse-cache.js +5 -13
  112. package/dist/rule-evaluator/parse-cache.js.map +1 -1
  113. package/dist/rule-evaluator/parser.d.ts +318 -11
  114. package/dist/rule-evaluator/parser.d.ts.map +1 -1
  115. package/dist/rule-evaluator/parser.js +1314 -333
  116. package/dist/rule-evaluator/parser.js.map +1 -1
  117. package/dist/rule-evaluator/quantifier.d.ts +1 -1
  118. package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
  119. package/dist/rule-evaluator/quantifier.js +1 -16
  120. package/dist/rule-evaluator/quantifier.js.map +1 -1
  121. package/dist/rule-evaluator/request-headers.d.ts +101 -0
  122. package/dist/rule-evaluator/request-headers.d.ts.map +1 -0
  123. package/dist/rule-evaluator/request-headers.js +344 -0
  124. package/dist/rule-evaluator/request-headers.js.map +1 -0
  125. package/dist/rule-evaluator/rule-language-types.d.ts +80 -21
  126. package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
  127. package/dist/rule-evaluator/rule-language-types.js +228 -67
  128. package/dist/rule-evaluator/rule-language-types.js.map +1 -1
  129. package/dist/rule-evaluator/tokenizer.d.ts +14 -4
  130. package/dist/rule-evaluator/tokenizer.d.ts.map +1 -1
  131. package/dist/rule-evaluator/tokenizer.js +24 -11
  132. package/dist/rule-evaluator/tokenizer.js.map +1 -1
  133. package/dist/rule-evaluator/transformation.d.ts.map +1 -1
  134. package/dist/rule-evaluator/transformation.js +10 -6
  135. package/dist/rule-evaluator/transformation.js.map +1 -1
  136. package/dist/rule-evaluator/url-decode.d.ts +8 -0
  137. package/dist/rule-evaluator/url-decode.d.ts.map +1 -0
  138. package/dist/rule-evaluator/url-decode.js +126 -0
  139. package/dist/rule-evaluator/url-decode.js.map +1 -0
  140. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  141. package/dist/rule-evaluator/validation.js +405 -235
  142. package/dist/rule-evaluator/validation.js.map +1 -1
  143. package/dist/rule-evaluator.d.ts +2 -2
  144. package/dist/rule-evaluator.d.ts.map +1 -1
  145. package/dist/rule-evaluator.js.map +1 -1
  146. package/package.json +5 -2
  147. package/dist/rule-evaluator/comparison.d.ts +0 -16
  148. package/dist/rule-evaluator/comparison.d.ts.map +0 -1
  149. package/dist/rule-evaluator/comparison.js +0 -43
  150. package/dist/rule-evaluator/comparison.js.map +0 -1
  151. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts +0 -36
  152. package/dist/rule-evaluator/custom-security-rule-analysis.d.ts.map +0 -1
  153. package/dist/rule-evaluator/custom-security-rule-analysis.js +0 -382
  154. package/dist/rule-evaluator/custom-security-rule-analysis.js.map +0 -1
  155. package/dist/rule-evaluator/values.d.ts +0 -26
  156. package/dist/rule-evaluator/values.d.ts.map +0 -1
  157. package/dist/rule-evaluator/values.js +0 -55
  158. package/dist/rule-evaluator/values.js.map +0 -1
  159. package/dist/rule-evaluator/warnings.d.ts +0 -2
  160. package/dist/rule-evaluator/warnings.d.ts.map +0 -1
  161. package/dist/rule-evaluator/warnings.js +0 -5
  162. package/dist/rule-evaluator/warnings.js.map +0 -1
package/README.md CHANGED
@@ -52,7 +52,7 @@ Formatting:
52
52
  Reference metadata for dashboard editors:
53
53
 
54
54
  - `getRuleExpressionFieldDescriptions`: field descriptions filtered by the same preset and field availability override shape used by rule expression validation.
55
- - `getRuleExpressionFunctionDescriptions`: function reference metadata, including signatures, return types, parameter roles, and evaluator support notes.
55
+ - `getRuleExpressionFunctionDescriptions`: function reference metadata, including signatures, return types, parameter roles, and evaluator support notes, filtered by preset like field descriptions.
56
56
  - `RULE_EXPRESSION_COUNTRY_CODES`: generated ISO 3166-1 alpha-2 country codes plus Cloudflare's `T1` and `XX` special values, used by field metadata and semantic validation.
57
57
 
58
58
  ### Diagnostics
@@ -64,6 +64,97 @@ and analysis diagnostics identify their source expression, while normalization
64
64
  and minification diagnostics apply directly to their single input expression.
65
65
  Integrations derive user-visible messages from diagnostic IDs and params.
66
66
 
67
+ ### Analysis of Invalid Expressions
68
+
69
+ `analyzeRuleExpression` also returns an AST for an expression with syntax
70
+ errors, and keeps its valid parts. Syntax with an error is an `Invalid` node,
71
+ with its source text in `raw` and its `span`, wherever a condition, a function
72
+ argument, or a value belongs; its `raw` is empty where nothing is written, as
73
+ in the argument slot a stray comma leaves. A field or function call that the
74
+ error cut short, which may have been a comparison's left side, as `http.host`
75
+ in `http.host * "x"`, is the `operand` of its `Invalid` node, and the symbols
76
+ list it. An access segment with an error is `{ kind: 'invalid', raw, span }`
77
+ in its `accessPath`, and an inline list member with an error is an `Invalid`
78
+ node among the list's `values`, whose `elementType` comes from the valid
79
+ members.
80
+ What invalid input keeps from being known is unknown, never guessed: a value
81
+ whose type depends on invalid input has `valueType: 'Unknown'`, such as a field
82
+ with an invalid access segment, or a call whose result type comes from an
83
+ invalid argument: one to a function that can map over its first argument, as
84
+ `lower()` and `concat()` can, or to `coalesce()`, which returns its arguments'
85
+ type. Any other function keeps its catalog type, as `cidr()` returns an IP
86
+ address whatever its arguments are. A call to a function that can map over its
87
+ first argument has `evaluationMode: 'unknown'` in its node and its symbol when
88
+ that argument is invalid, or a syntax error left it uncertain.
89
+ `declaredReturnType` stays the function's catalog type.
90
+
91
+ ### Documented and Observed Behavior
92
+
93
+ Where Cloudflare's documentation and its ruleset engine disagree, validation
94
+ follows the engine, as observed with dry-run rule validation. For example, URL
95
+ rewrite expressions accept every field URL rewrite filters accept,
96
+ `hash_in_range()` takes one seed, `lookup_json_*()` keys must be literals,
97
+ header map keys such as `http.request.headers["x-a"]` must be lowercase, and
98
+ integer literals must fit in 64 bits, with a leading `0` making one octal. Field availability keeps each preset's
99
+ documented fields and the observed differences apart, and `fieldAvailability`
100
+ can still allow a field the engine was observed to reject. Source-key rules run
101
+ in the Worker rather than in a Cloudflare phase, so the URL rewrite phase's
102
+ observed differences do not apply to them.
103
+
104
+ Cloudflare does not document how `hash_in_range()` hashes, and may change it
105
+ without notice. The evaluator reproduces the results Cloudflare returned to
106
+ live requests, recorded in
107
+ [`test/fixtures/hash-in-range-probes.json`](https://github.com/Func86/libreflare-worker-runtime/blob/master/test/fixtures/hash-in-range-probes.json).
108
+ That includes results outside the range once a bound is beyond 2^53, where
109
+ Cloudflare's floating-point arithmetic rounds. Strings are hashed as the bytes
110
+ they hold, but a request value whose bytes are not valid UTF-8 can reach the
111
+ Worker changed, and then hashes differently. A missing (nil) seed gives nil, as
112
+ in Cloudflare, so `coalesce()` can supply a fallback.
113
+
114
+ Cloudflare's string functions work on bytes, as live requests showed:
115
+ `lower()`, `upper()` and case-insensitive wildcards fold ASCII letters only,
116
+ `len()` and `substring()` count bytes, and `remove_bytes()` removes each listed
117
+ byte. `url_decode()` keeps invalid escapes, and its `r` option repeats until
118
+ nothing changes. `decode_base64()` gives nil for input that is not standard
119
+ Base64. `to_string()` prints IPv6 addresses compressed, as in `2001:db8::1`,
120
+ and so do `cidr()` and `cidr6()`. A result that is not valid UTF-8 keeps its
121
+ bytes, such as a `substring()` that splits a character or a `url_decode()` of
122
+ `%FF`, and comparisons, `len()` and `hash_in_range()` read them as Cloudflare
123
+ does. `lookup_json_string()` and `lookup_json_integer()` give nil when such
124
+ bytes are in a key of an object on the path or in the string looked up.
125
+
126
+ The evaluator holds integers as JavaScript numbers, so integers beyond 2^53 are
127
+ approximate: literals, `lookup_json_integer()` values and `hash_in_range()`
128
+ results that large round, and so do the comparisons and `to_string()` output
129
+ that use them. An integer `hash_in_range()` seed that large gives an unknown
130
+ result instead, since a rounded seed hashes into an unrelated bucket.
131
+
132
+ URL rewrite filters and rewrite targets also reject the functions the URL
133
+ rewrite phase does not accept, such as `split()` in either and `to_string()`
134
+ in filters, with `function.unavailable`. Rewrite targets reject operators,
135
+ quantifiers, and conditions used as function arguments, such as `eq`, `any()`,
136
+ and `to_string((ssl))`, with `operator.unavailable`. In both,
137
+ `concat()`, `regex_replace()`, and `wildcard_replace()` can appear only once,
138
+ and the replacement functions cannot be nested. These phase rules depend on the
139
+ preset alone: `fieldAvailability` does not change them, and they do not apply
140
+ to source-key rules or when `evaluateExpression` decides whether an expression
141
+ is valid.
142
+ `getRuleExpressionFunctionDescriptions()` lists only the functions a preset
143
+ accepts, `url-rewrite-filter` by default as in validation; the
144
+ `logging-source-key` preset lists every function.
145
+
146
+ The observed behavior is recorded in
147
+ `src/rule-evaluator/engine-observations.generated.ts` by
148
+ `npm run probe:rules-engine`, which dry-runs probe expressions against a URL
149
+ rewrite ruleset. It also records every probe with the engine's verdict in
150
+ `test/fixtures/rules-engine-probes.json`, and `npm test` fails on any probe
151
+ where this package's validator disagrees with the recorded verdict. It needs
152
+ `CLOUDFLARE_API_TOKEN` (Zone Read and Transform Rules Edit),
153
+ `CLOUDFLARE_ZONE_ID`, and `CLOUDFLARE_URL_REWRITE_RULESET_ID`, and never changes
154
+ the ruleset. To probe through another client, write the probe list with
155
+ `--write-probes <file>` and pass the API responses back with
156
+ `--responses <file>`, as described in the script.
157
+
67
158
  ## Rule Expression Evaluation
68
159
 
69
160
  ### Valid Expressions Only
@@ -86,13 +177,30 @@ missing-value (nil) comparison results to absent map keys, out-of-bounds
86
177
  indexes, and nil function results. For example, both sides being missing makes
87
178
  `eq` true, and one missing side makes `ne` true.
88
179
 
89
- Equality and ordering comparisons use each operand's declared type. Values are
90
- converted to that type before comparing, so a numeric `request.cf` value that
91
- arrives as text still compares as a number. Strings are ordered by code point,
92
- which matches Cloudflare's byte order, and IP addresses are compared as
93
- addresses rather than as text. A value that cannot be converted to its
94
- declared type, such as a non-numeric `request.cf` value for a Number field,
95
- makes the comparison unknown.
180
+ A function argument can also be a condition, which is a Boolean value that is
181
+ never nil. For example, `has_value(http.request.uri.args["a"], "x")` is nil
182
+ when the `a` query argument is missing, so
183
+ `coalesce(has_value(http.request.uri.args["a"], "x"), http.host eq "x")` then
184
+ falls back to the comparison. As in Cloudflare, an argument that starts with
185
+ `(`, `not`, `any()` or `all()` is always a condition. It can combine
186
+ conditions with `and`, `or` and `xor`, while any other argument needs
187
+ parentheses around them, as in `(http.host eq "x" or ssl)`. A function call in
188
+ such parentheses is a condition too: it must return a Boolean, and a nil
189
+ result counts as false, so with the `a` query argument missing,
190
+ `coalesce((has_value(http.request.uri.args["a"], "x")), ssl)` is false. Since
191
+ conditions are never nil, validation warns that `coalesce()` never uses the
192
+ arguments after one. In the analysis AST, condition arguments keep their
193
+ predicate shape, a function call argument evaluated as a condition has
194
+ `condition: true`, and the AST version is 5.
195
+
196
+ Equality and ordering comparisons use each operand's declared type. Field
197
+ values are converted to that type, so a numeric `request.cf` value that
198
+ arrives as decimal text still compares as a number. Strings are ordered by code
199
+ point, which matches Cloudflare's byte order, with bytes that are not UTF-8
200
+ ordered as bytes, and IP addresses are compared as addresses rather than as
201
+ text. A field value that does not represent its declared type, such as other
202
+ text for a Number field or a number for a String field, is unknown, with a
203
+ warning.
96
204
 
97
205
  ### Literals and Inline Lists
98
206
 
@@ -114,16 +222,130 @@ such as `coalesce()` and `has_key()` accept them, and `["key"]` and `[*]` work
114
222
  on a function's map result as on the field itself, so
115
223
  `coalesce(http.request.uri.args, http.request.cookies)["x"][0]` reads the query
116
224
  argument. A map is never nil, so `coalesce()` returns its first map argument.
225
+ As in Cloudflare, `[*]` iterates a map's keys in byte order, so
226
+ `http.request.uri.args[*]` reads `?b=1&a=2` as `a`, then `b`.
227
+
228
+ ### Request Headers
229
+
230
+ Workers lowercases and sorts request header names, joins repeated headers into
231
+ one value, and adds headers of its own, such as `cf-ray`. The evaluator rebuilds
232
+ Cloudflare's header list from the undocumented `request.cf.requestHeaderNames`,
233
+ which names the headers the client sent, in order and spelling, and reads their
234
+ values from the Worker's headers. Live requests showed how Cloudflare lists
235
+ them, recorded in
236
+ [`test/fixtures/request-header-probes.json`](https://github.com/Func86/libreflare-worker-runtime/blob/master/test/fixtures/request-header-probes.json)
237
+ by `npm run probe:request-headers`, whose script describes the zone it needs:
238
+
239
+ - `http.request.headers.names` keeps each name's first spelling and groups
240
+ repeated names at their first position, in the order Cloudflare uses for the
241
+ request's HTTP version. Cloudflare leaves out some headers, such as
242
+ `Keep-Alive` and `CF-Ray`, and adds its own `X-Forwarded-Proto`.
243
+ - Header map keys are lowercase and compared exactly, so
244
+ `has_key(http.request.headers, "X-A")` is false.
245
+ - A repeated header's joined value is split into one value per header, unless
246
+ a value contains `, `, or `; ` for an HTTP/1 `Cookie`, which Cloudflare
247
+ joins with `; `.
248
+ - Values the Worker does not have are unknown: `Connection`,
249
+ `CF-Connecting-IP` and `CF-Visitor`, which Cloudflare replaces, headers
250
+ Workers does not receive, such as `CDN-Loop`, and `Accept-Encoding` without
251
+ `request.cf.clientAcceptEncoding`. Their names stay known, and so do the
252
+ other values: `any()` and `all()` over `http.request.headers.values` or `[*]`
253
+ on the header map, and `has_value()`, can still match or fail on a known
254
+ value, and `len()` still counts them, but whatever reads every value, such
255
+ as `join()`, is unknown.
256
+ - For a body sent without `Content-Length`, Cloudflare lists `Content-Length`
257
+ if the whole body arrived before its rules ran, and `Transfer-Encoding:
258
+ chunked` if not. The Worker sees a `Content-Length` either way, so that
259
+ header is unknown, name and value, though `len()` still counts it. Over
260
+ HTTP/3, Cloudflare listed `transfer-encoding` for every body size probed,
261
+ so it is known there.
262
+ - Other `Accept-Encoding` values come from `request.cf.clientAcceptEncoding`,
263
+ which keeps only the encodings Cloudflare supports, without weights.
264
+ - `request.cf.requestHeaderNames` lists at most 50 names. For a request with
265
+ that many, the headers it lists stay known, so `any()` can find one, but the
266
+ headers it does not list are unknown, and so are positions in the names and
267
+ values. A listed header can repeat after them too, so its value is unknown
268
+ when it contains `, `, or `; ` for an HTTP/1 `Cookie`, and so is
269
+ `Accept-Encoding`.
270
+ - `raw.http.request.headers` and its `names` and `values` list the same
271
+ headers as the other header fields, with two spellings of their own. Over
272
+ HTTP/3, raw names spell `X-Forwarded-Proto` in title case. A client header
273
+ that a Managed Transform replaced, such as `CF-IPCity` under Add visitor
274
+ location headers, has a lowercase raw name, which shows over HTTP/1, where
275
+ names keep the client's case. Worker context cannot tell what a Managed
276
+ Transform replaced, so raw names keep the client's spelling there.
277
+ - Cloudflare's header fields, raw or not, read the client's headers and values
278
+ from before request header transform rules and Managed Transforms, even in
279
+ the response phase, and `request.cf.requestHeaderNames` lists them too.
280
+ Workers sees the headers after them, so a value a transform replaced reads
281
+ the new value, with no warning, and one it removed is unknown. The
282
+ single-header fields, such as `http.user_agent`, read the Worker's values
283
+ too. Headers a transform adds are not listed, as in Cloudflare. Probes
284
+ covered a request header transform rule and the Managed Transforms Add
285
+ visitor location headers, Add TLS client auth headers and Remove visitor IP
286
+ headers.
287
+
288
+ Without `request.cf.requestHeaderNames`, as in local development, the Worker's
289
+ headers stand in, a repeated header holds one joined value, and the raw header
290
+ fields are unknown.
291
+
292
+ On a request from a Worker, which Cloudflare marks with `CF-Worker`, the header
293
+ fields are unknown when `request.cf.requestHeaderNames` is present. On the
294
+ subrequests probed, it named the headers of the request that Worker received,
295
+ while Cloudflare's header fields listed the subrequest's own.
296
+
297
+ ### Request Metadata
298
+
299
+ Most other fields read `request.cf`, and `ip.src` and `cf.ray_id` read headers
300
+ Cloudflare adds. Live requests compared them with Cloudflare's values over
301
+ HTTP/1.1, HTTP/2 and HTTP/3, and the evaluator follows what they showed:
302
+
303
+ - `cf.ray_id` adds the data center to the Worker's `CF-Ray`, as in
304
+ `8f1c2a3b4d5e6f70-LAX`.
305
+ - `cf.edge.client_tcp` is false over HTTP/3 and true over the other HTTP
306
+ versions, which run over TCP. Cloudflare's `cf.timings.client_tcp_rtt_msec`
307
+ was 1 ms more than `request.cf.clientTcpRtt` in every TCP request probed, and
308
+ the evaluator adds it. None had a TCP RTT of 0, which may mean under 1 ms or
309
+ not measured, so that is unknown. Over HTTP/3, both read 0.
310
+ - `ip.src.is_in_european_union` reads `request.cf.isEUCountry`, which Workers
311
+ gives as a Boolean rather than the `"1"` it documents, and is unknown without
312
+ it.
313
+ - `ip.src` reads `CF-Connecting-IPv6` when `CF-Connecting-IP` holds a Class E
314
+ address, from `240.0.0.0/4`. Pseudo IPv4, set to overwrite headers, puts one
315
+ there and keeps the client's IPv6 address in `CF-Connecting-IPv6`, which
316
+ Cloudflare's `ip.src` reads. A Class E address without that header is
317
+ unknown. Cloudflare removes a copy of the header sent by a client or relayed
318
+ by a Worker's subrequest, and with overwrite on, replaces a relayed one with
319
+ the real address.
320
+ - `cf.worker.upstream_zone` reads `CF-Worker`, which Cloudflare sets on a
321
+ Worker's subrequests, removing a client's copy, and is empty without it. Live
322
+ requests compared subrequests from another zone, and same-zone ones under the
323
+ `global_fetch_strictly_public` compatibility flag and to a Custom Domain;
324
+ without the flag, a same-zone subrequest reaches a Worker only on a Custom
325
+ Domain, and otherwise goes to the origin. On all of them, Cloudflare reads
326
+ `cf.tls_version` as empty, `cf.edge.l4.delivery_rate` and
327
+ `cf.timings.client_tcp_rtt_msec` as 0, and `cf.edge.client_tcp` as false, and
328
+ so does the evaluator. The other fields read `request.cf`, since they matched
329
+ it. A subrequest comes from the fixed address `2a06:98c0:3600::103`, unless
330
+ it stays on the same zone without the flag and the calling Worker sends
331
+ `X-Real-IP`, which Cloudflare then uses. `ip.src` reads the address from
332
+ `CF-Connecting-IP`, or `CF-Connecting-IPv6` under Pseudo IPv4, as Cloudflare
333
+ does. The location followed the fixed address, except on a same-zone
334
+ subrequest without the flag, where Cloudflare and `request.cf` both kept the
335
+ location of the request the calling Worker received, whatever `ip.src` held.
336
+ - `cf.edge.server_port` is unknown: the Worker's URL takes its port from the
337
+ `Host` header, and Cloudflare's field the port the client connected to.
117
338
 
118
339
  ### Unknown Values and List Inference
119
340
 
120
341
  Values the evaluator cannot resolve are unknown rather than nil. These include
121
- `raw.*` request fields in Worker context, request metadata missing from the
342
+ `raw.*` URI fields in Worker context, request metadata missing from the
122
343
  request, such as `request.cf` properties or the `cf-connecting-ip` header behind
123
- `ip.src`, and results of functions it cannot reproduce, such as `sha256()` and
124
- `is_jwt_valid()`. An unknown value makes the functions and comparisons that use
125
- it unknown. It also makes `not`, `and`, `or` and `xor` unknown, unless another
126
- operand settles the result. An unknown result does not match, and the
344
+ `ip.src`, which the Remove visitor IP headers Managed Transform removes, and
345
+ results of functions it cannot reproduce, such as `sha256()` and
346
+ `is_jwt_valid()`. An unknown value makes the functions and comparisons
347
+ that use it unknown. It also makes `not`, `and`, `or` and `xor` unknown, unless
348
+ another operand settles the result. An unknown result does not match, and the
127
349
  evaluation warnings name the values involved.
128
350
 
129
351
  Unresolved Cloudflare list references are the exception: they count as
@@ -1,5 +1,5 @@
1
1
  import { type ComparisonNode, type FuncNode } from './ast.js';
2
- import type { ResolverCtx } from './fields.js';
3
- import type { Outcome } from './outcome.js';
2
+ import { type ResolverCtx } from './fields.js';
3
+ import { type Outcome } from './outcome.js';
4
4
  export declare function warnIfAcceptEncodingZstdSensitive(node: ComparisonNode | FuncNode, ctx: ResolverCtx, currentResult: boolean, evaluateWithCtx: (testCtx: ResolverCtx) => Outcome): void;
5
5
  //# sourceMappingURL=accept-encoding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"accept-encoding.d.ts","sourceRoot":"","sources":["../../src/rule-evaluator/accept-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,cAAc,EAAkB,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA4D5C,wBAAgB,iCAAiC,CAChD,IAAI,EAAE,cAAc,GAAG,QAAQ,EAC/B,GAAG,EAAE,WAAW,EAChB,aAAa,EAAE,OAAO,EACtB,eAAe,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,GAChD,IAAI,CAiBN"}
1
+ {"version":3,"file":"accept-encoding.d.ts","sourceRoot":"","sources":["../../src/rule-evaluator/accept-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,cAAc,EAAkB,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAC7F,OAAO,EAAiE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAC9G,OAAO,EAAa,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAgGvD,wBAAgB,iCAAiC,CAChD,IAAI,EAAE,cAAc,GAAG,QAAQ,EAC/B,GAAG,EAAE,WAAW,EAChB,aAAa,EAAE,OAAO,EACtB,eAAe,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,OAAO,GAChD,IAAI,CAoBN"}
@@ -1,10 +1,33 @@
1
- import { pushWarning } from './ast.js';
2
- /** Reads the Accept-Encoding header directly, or reads the whole headers map, such as with [*] or has_key(). */
1
+ import { unhandledNode } from './ast.js';
2
+ import { REQUEST_HEADER_ARRAYS, REQUEST_HEADER_MAPS, requestHeaderList } from './fields.js';
3
+ import { isUnknown } from './outcome.js';
4
+ import { isUnknownHeaderValues, requestHeaderKeyValues } from './request-headers.js';
5
+ /**
6
+ * Reads the Accept-Encoding header directly, reads the whole headers map, such
7
+ * as with [*] or coalesce(), or reads every header value. An invalid access
8
+ * may stand for any of them. Each Accept-Encoding value reads
9
+ * request.cf.clientAcceptEncoding. A field that reads it without using it,
10
+ * such as http.request.headers.values[0], costs only the second evaluation;
11
+ * recording reads would count it too, as the header arrays are built whole.
12
+ */
3
13
  function isAcceptEncodingFieldNode(node) {
4
- if (node.name !== 'http.request.headers')
14
+ if (REQUEST_HEADER_ARRAYS.get(node.name) === 'values')
15
+ return true;
16
+ if (!REQUEST_HEADER_MAPS.has(node.name))
5
17
  return false;
6
- const mapKey = node.accessPath[0];
7
- return mapKey?.kind !== 'MapKeyAccess' || mapKey.key.toLowerCase() === 'accept-encoding';
18
+ const access = node.accessPath[0];
19
+ if (!access)
20
+ return true;
21
+ switch (access.kind) {
22
+ case 'MapKeyAccess':
23
+ return access.key === 'accept-encoding';
24
+ case 'ArrayIndexAccess':
25
+ case 'ArrayWildcardAccess':
26
+ case 'Invalid':
27
+ return true;
28
+ default:
29
+ return unhandledNode(access);
30
+ }
8
31
  }
9
32
  /**
10
33
  * Whether an expression reads the Accept-Encoding header anywhere, including
@@ -12,21 +35,31 @@ function isAcceptEncodingFieldNode(node) {
12
35
  * any()/all() predicates.
13
36
  */
14
37
  function usesAcceptEncoding(node) {
15
- switch (node?.kind) {
38
+ switch (node.kind) {
16
39
  case 'Field':
17
40
  return isAcceptEncodingFieldNode(node);
18
41
  case 'Func':
19
- return node.args.some(usesAcceptEncoding);
42
+ // Which keys a map has does not depend on their values.
43
+ return node.args.slice(node.name === 'has_key' ? 1 : 0).some(usesAcceptEncoding);
20
44
  case 'Comparison':
21
- return usesAcceptEncoding(node.left) || usesAcceptEncoding(node.right);
45
+ return usesAcceptEncoding(node.left) || (node.right !== undefined && usesAcceptEncoding(node.right));
22
46
  case 'Not':
23
47
  return usesAcceptEncoding(node.operand);
24
48
  case 'And':
25
49
  case 'Or':
26
50
  case 'Xor':
27
51
  return node.operands.some(usesAcceptEncoding);
28
- default:
52
+ case 'BoolField':
53
+ case 'StringLiteral':
54
+ case 'NumberLiteral':
55
+ case 'IpLiteral':
56
+ case 'BoolLiteral':
57
+ case 'SetLiteral':
58
+ case 'ListRef':
59
+ case 'Invalid':
29
60
  return false;
61
+ default:
62
+ return unhandledNode(node);
30
63
  }
31
64
  }
32
65
  function parseAcceptEncodingTokens(value) {
@@ -47,13 +80,19 @@ function toggleZstdInAcceptEncoding(value) {
47
80
  }
48
81
  return [...tokens, 'zstd'].join(', ');
49
82
  }
83
+ /** Header values read the override when read, so the copy keeps the header list. */
50
84
  function withAcceptEncodingOverride(ctx, acceptEncoding) {
51
85
  return {
52
86
  ...ctx,
53
87
  cf: { ...ctx.cf, clientAcceptEncoding: acceptEncoding },
54
- warnings: [],
88
+ warnings: new Set(),
55
89
  };
56
90
  }
91
+ /** The Accept-Encoding value rules read, which overriding changes, or null when it is unknown or absent. */
92
+ function acceptEncodingValue(ctx) {
93
+ const values = requestHeaderKeyValues(requestHeaderList(ctx), 'accept-encoding', ctx.cf);
94
+ return values === null || isUnknownHeaderValues(values) ? null : values[0] ?? null;
95
+ }
57
96
  export function warnIfAcceptEncodingZstdSensitive(node, ctx, currentResult, evaluateWithCtx) {
58
97
  const responseContentEncoding = ctx.response?.headers.get('content-encoding') ?? '';
59
98
  if (parseAcceptEncodingTokens(responseContentEncoding).some(token => token === 'zstd')) {
@@ -61,11 +100,19 @@ export function warnIfAcceptEncodingZstdSensitive(node, ctx, currentResult, eval
61
100
  }
62
101
  if (!usesAcceptEncoding(node))
63
102
  return;
64
- const toggledAcceptEncoding = toggleZstdInAcceptEncoding(ctx.cf.clientAcceptEncoding ?? ctx.request.headers.get('accept-encoding') ?? '');
65
- const toggledCtx = withAcceptEncodingOverride(ctx, toggledAcceptEncoding);
103
+ const acceptEncoding = acceptEncodingValue(ctx);
104
+ if (acceptEncoding === null)
105
+ return;
106
+ const toggledCtx = withAcceptEncodingOverride(ctx, toggleZstdInAcceptEncoding(acceptEncoding));
66
107
  const toggledResult = evaluateWithCtx(toggledCtx);
67
- if (toggledResult !== currentResult) {
68
- pushWarning(ctx.warnings, `Accept-Encoding checks may be unreliable in Workers: request.cf.clientAcceptEncoding currently drops 'zstd', and '${node.raw}' would evaluate differently if 'zstd' were toggled`);
69
- }
108
+ if (toggledResult === currentResult)
109
+ return;
110
+ const unknown = isUnknown(toggledResult);
111
+ ctx.warnings.add(`Accept-Encoding checks may be unreliable in Workers: request.cf.clientAcceptEncoding currently drops 'zstd', and '${node.raw}' ${unknown ? 'might' : 'would'} evaluate differently if 'zstd' were toggled`);
112
+ // The toggled run's warnings tell what left its result unknown.
113
+ if (!unknown)
114
+ return;
115
+ for (const warning of toggledCtx.warnings)
116
+ ctx.warnings.add(warning);
70
117
  }
71
118
  //# sourceMappingURL=accept-encoding.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"accept-encoding.js","sourceRoot":"","sources":["../../src/rule-evaluator/accept-encoding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAsD,MAAM,UAAU,CAAC;AAI3F,gHAAgH;AAChH,SAAS,yBAAyB,CAAC,IAAe;IACjD,IAAI,IAAI,CAAC,IAAI,KAAK,sBAAsB;QAAE,OAAO,KAAK,CAAC;IACvD,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IAClC,OAAO,MAAM,EAAE,IAAI,KAAK,cAAc,IAAI,MAAM,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,iBAAiB,CAAC;AAC1F,CAAC;AAED;;;;GAIG;AACH,SAAS,kBAAkB,CAAC,IAA0C;IACrE,QAAQ,IAAI,EAAE,IAAI,EAAE,CAAC;QACpB,KAAK,OAAO;YACX,OAAO,yBAAyB,CAAC,IAAI,CAAC,CAAC;QACxC,KAAK,MAAM;YACV,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QAC3C,KAAK,YAAY;YAChB,OAAO,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACxE,KAAK,KAAK;YACT,OAAO,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzC,KAAK,KAAK,CAAC;QACX,KAAK,IAAI,CAA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GAAG,CAAC,QAAQ,CAAC,GAAG,CACf,qHAAqH,IAAI,CAAC,GAAG,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,8CAA8C,CAC3M,CAAC;IACF,gEAAgE;IAChE,IAAI,CAAC,OAAO;QAAE,OAAO;IACrB,KAAK,MAAM,OAAO,IAAI,UAAU,CAAC,QAAQ;QAAE,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtE,CAAC"}
@@ -1,28 +1,91 @@
1
1
  import type { AccessNode, CfValue } from './ast.js';
2
+ import { type Evaluated, type Known, type Unknown } from './outcome.js';
3
+ /**
4
+ * Why a value is unknown, told only when a result depends on it, as telling
5
+ * it warns. A result the known elements settle, such as any() matching one,
6
+ * then warns about nothing it did not use.
7
+ */
8
+ export type UnknownElement = () => Unknown;
9
+ /** An element of an array while evaluating, or a map key's values. */
10
+ export type ElementValue = Known<CfValue> | UnknownElement;
11
+ export declare function isUnknownElement(element: ElementValue): element is UnknownElement;
12
+ /**
13
+ * An array some of whose elements are unknown while evaluating, such as the
14
+ * request header values when Worker context lacks one. Its known elements can
15
+ * still settle any() and all(), and an index whose position is certain;
16
+ * anything that reads the whole array is unknown. Unknown elements keep their
17
+ * positions, unless `more` says the array may hold further elements, at
18
+ * positions it cannot tell.
19
+ */
20
+ export declare class PartialArrayValue {
21
+ readonly elements: readonly ElementValue[];
22
+ readonly more: UnknownElement | undefined;
23
+ /** The first unknown element, or `more`. */
24
+ private readonly firstUnknown;
25
+ private constructor();
26
+ /** An array of elements, partly known when one is unknown or more may follow. */
27
+ static from(elements: readonly ElementValue[], more?: UnknownElement): CfValue[] | PartialArrayValue;
28
+ /** Why the whole array is unknown: its first unknown element, or that it may hold more. */
29
+ unknown(): Unknown;
30
+ /** The known elements' values, and why the others, or more, are unknown. */
31
+ knownValues(): {
32
+ values: CfValue[];
33
+ unknown: UnknownElement;
34
+ };
35
+ /** The element at an index, nil past the end. */
36
+ at(index: number): Evaluated<CfValue>;
37
+ }
38
+ /**
39
+ * A map's keys in the order `[*]` iterates them. Cloudflare's maps are
40
+ * wirefilter's BTreeMap, which iterates keys in byte order, as live requests
41
+ * showed for headers and query arguments.
42
+ */
43
+ export declare function mapKeys(keys: Iterable<string>): ReadonlySet<string>;
2
44
  /**
3
45
  * A map field's value while evaluating, such as http.request.headers. Maps
4
46
  * only reach functions that accept them, such as has_key() and coalesce(),
5
- * and postfix access; validation keeps them out of comparisons.
47
+ * and postfix access; validation keeps them out of comparisons. In Worker
48
+ * context, a key's values can be unknown, and so can keys it does not list.
6
49
  */
7
50
  export declare class RuleMapValue {
51
+ /** A key's values, nil for a key the map does not have, or unknown. */
8
52
  private readonly lookup;
53
+ /** The keys the map has, from mapKeys(). */
9
54
  private readonly keys;
10
- constructor(lookup: (key: string) => CfValue, keys: () => readonly string[]);
11
- /** The values for a key, or null when the map has no such key. */
12
- get(key: string): CfValue;
55
+ /** Why the map may have keys `keys()` does not list, when it may. */
56
+ private readonly more?;
57
+ constructor(
58
+ /** A key's values, nil for a key the map does not have, or unknown. */
59
+ lookup: (key: string) => ElementValue,
60
+ /** The keys the map has, from mapKeys(). */
61
+ keys: () => ReadonlySet<string>,
62
+ /** Why the map may have keys `keys()` does not list, when it may. */
63
+ more?: UnknownElement | undefined);
64
+ /** The values for a key, or nil when the map has no such key. */
65
+ get(key: string): Evaluated<CfValue>;
66
+ /**
67
+ * Whether the map has a key, which is known for a listed key even when its
68
+ * values are not. Values are nil only for a key the map does not have, so
69
+ * the keys are read only when the values are unknown.
70
+ */
71
+ has(key: string): Evaluated<boolean>;
13
72
  /** Every key's values, which `[*]` iterates. */
14
- values(): CfValue[];
73
+ values(): CfValue[] | PartialArrayValue;
15
74
  }
16
- /** A value while evaluating: a rule-language value, or a map. */
17
- export type RuntimeValue = CfValue | RuleMapValue;
75
+ /** A value while evaluating: a rule-language value, a partly known array, or a map. */
76
+ export type RuntimeValue = CfValue | PartialArrayValue | RuleMapValue;
77
+ /**
78
+ * Applies postfix access to a value that may be a map or a partly known
79
+ * array. On a map, `["key"]` reads one key's values and `[*]` iterates every
80
+ * key's values; either can be unknown. Later segments apply as on arrays.
81
+ */
82
+ export declare function applyRuntimeAccessPath(value: RuntimeValue, accessPath: readonly AccessNode[]): Evaluated<RuntimeValue>;
18
83
  /**
19
- * Applies postfix map and array access while preserving Cloudflare's
20
- * vectorized `[*]` state. Once expanded, later access segments are mapped over
21
- * every active element instead of indexing the outer result array, and, as in
22
- * Wirefilter, an element without the accessed index is skipped. Without `[*]`,
23
- * a missing index is nil. On a map, `["key"]` reads one key's values and `[*]`
24
- * iterates every key's values.
84
+ * Applies postfix array access while preserving Cloudflare's vectorized `[*]`
85
+ * state. Once expanded, later access segments are mapped over every active
86
+ * element instead of indexing the outer result array, and, as in Wirefilter,
87
+ * an element without the accessed index is skipped. Without `[*]`, a missing
88
+ * index is nil.
25
89
  */
26
90
  export declare function applyValueAccessPath(value: CfValue, accessPath: readonly AccessNode[]): CfValue;
27
- export declare function applyValueAccessPath(value: RuntimeValue, accessPath: readonly AccessNode[]): RuntimeValue;
28
91
  //# sourceMappingURL=access-values.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"access-values.d.ts","sourceRoot":"","sources":["../../src/rule-evaluator/access-values.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAEpD;;;;GAIG;AACH,qBAAa,YAAY;IAEvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;gBADJ,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,EAChC,IAAI,EAAE,MAAM,SAAS,MAAM,EAAE;IAG/C,kEAAkE;IAClE,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB,gDAAgD;IAChD,MAAM,IAAI,OAAO,EAAE;CAGnB;AAED,iEAAiE;AACjE,MAAM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