@amritk/nish-x86_64-linux 0.44.0 → 0.46.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 (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
package/std/net/url.ts CHANGED
@@ -96,6 +96,8 @@ export const URL_DECODE_ERR_ESCAPE: i32 = -1
96
96
  export const URL_DECODE_ERR_NUL: i32 = -2
97
97
  /** `out` has no room for the next byte; nothing is written at or past `out.length`. */
98
98
  export const URL_DECODE_ERR_SPACE: i32 = -3
99
+ /** `tryPercentDecode` only: the window lies outside `src`, or `at` outside `out`; nothing is written. */
100
+ export const URL_DECODE_ERR_WINDOW: i32 = -4
99
101
 
100
102
  /** `next()` found a pair; the iterator's windows bound it. */
101
103
  export const URL_QUERY_PAIR: i32 = 1
@@ -115,12 +117,14 @@ const URL_SLASH: i32 = 47
115
117
  const URL_EQUALS: i32 = 61
116
118
  const URL_QUESTION: i32 = 63
117
119
 
118
- /** Panics unless `bytes[off, off + len)` lies inside `bytes`. */
119
- const urlCheckWindow = (bytes: u8[], off: i32, len: i32, who: string): void => {
120
+ /** Panics unless `bytes[off, off + len)` lies inside `bytes`, and answers the window's end, `off + len`. */
121
+ const urlCheckWindow = (bytes: u8[], off: i32, len: i32, who: string): i32 => {
120
122
  const size: i32 = toI32(bytes.length)
121
123
  if (off < 0 || len < 0 || off > size || len > size - off) {
122
124
  panic(who + ": the window is outside the array")
123
125
  }
126
+ // The test above bounds the sum by `bytes.length`, so it cannot wrap.
127
+ return toI32(toU32(off) + toU32(len))
124
128
  }
125
129
 
126
130
  /** The value of one hex digit, either case, or -1. */
@@ -139,7 +143,10 @@ const urlHexValue = (b: i32): i32 => {
139
143
 
140
144
  /** The byte the escape at `bytes[i]` (a `%`) stands for, or -1 when two hex digits do not follow it before `end`. */
141
145
  const urlEscapeAt = (bytes: u8[], i: i32, end: i32): i32 => {
142
- if (end - i < 3) {
146
+ // Callers pass `0 <= i < end <= bytes.length`, so `i + 3 > end` is the
147
+ // only test that can fail; the others are spelled out for the bounds proof.
148
+ const n: i32 = toI32(bytes.length)
149
+ if (i < 0 || i >= n - 2 || i + 2 >= end) {
143
150
  return -1
144
151
  }
145
152
  const hi: i32 = urlHexValue(toI32(bytes[i + 1]))
@@ -147,7 +154,20 @@ const urlEscapeAt = (bytes: u8[], i: i32, end: i32): i32 => {
147
154
  if (hi < 0 || lo < 0) {
148
155
  return -1
149
156
  }
150
- return hi * 16 + lo
157
+ return (hi << 4) | lo
158
+ }
159
+
160
+ /** `urlDecodeRun`'s answer for an `at` outside `out`, kept apart from a bad window so each panic names its argument. */
161
+ const URL_DECODE_AT_OUTSIDE: i32 = -5
162
+
163
+ /** Panics on a bad window or `at`, as the decoders that are not `tryPercentDecode` do, and answers `result` otherwise. */
164
+ const urlDecodeChecked = (result: i32, who: string): i32 => {
165
+ if (result === URL_DECODE_ERR_WINDOW || result === URL_DECODE_AT_OUTSIDE) {
166
+ panic(
167
+ who + (result === URL_DECODE_AT_OUTSIDE ? ": at is outside out" : ": the window is outside the array")
168
+ )
169
+ }
170
+ return result
151
171
  }
152
172
 
153
173
  /**
@@ -164,18 +184,21 @@ const urlDecodeRun = (
164
184
  flags: i32,
165
185
  wantPos: boolean
166
186
  ): i32 => {
167
- urlCheckWindow(src, off, len, "percentDecode")
187
+ const n: i32 = toI32(src.length)
168
188
  const size: i32 = toI32(out.length)
189
+ if (off < 0 || len < 0 || off > n || len > n - off) {
190
+ return URL_DECODE_ERR_WINDOW
191
+ }
169
192
  if (at < 0 || at > size) {
170
- panic("percentDecode: at is outside out")
193
+ return URL_DECODE_AT_OUTSIDE
171
194
  }
195
+ // The window test bounds the sum by `src.length`, so it cannot wrap.
196
+ const end: i32 = toI32(toU32(off) + toU32(len))
172
197
  const form: boolean = (flags & URL_DECODE_FORM) !== 0
173
198
  const lenient: boolean = (flags & URL_DECODE_LENIENT) !== 0
174
199
  const allowNul: boolean = (flags & URL_DECODE_ALLOW_NUL) !== 0
175
- const end: i32 = off + len
176
200
  let i: i32 = off
177
201
  let w: i32 = at
178
- const n: i32 = toI32(src.length)
179
202
  while (i >= 0 && i < end && i < n) {
180
203
  let b: i32 = toI32(src[i])
181
204
  let step: i32 = 1
@@ -198,24 +221,16 @@ const urlDecodeRun = (
198
221
  }
199
222
  out[w] = toU8(b)
200
223
  w += 1
201
- i += step
224
+ // An escape is taken only when its digits lie inside `src`, so the step
225
+ // ends at most at `src.length` and cannot wrap.
226
+ i = toI32(toU32(i) + toU32(step))
202
227
  }
203
- return wantPos ? -1 : w - at
228
+ // `w` starts at `at` and only grows, so the difference cannot wrap.
229
+ return wantPos ? -1 : toI32(toU32(w) - toU32(at))
204
230
  }
205
231
 
206
- /**
207
- * The index of the first `?` in `bytes[off, off + len)`, or -1 when there is
208
- * none. The path is `[off, q)` and the query `(q, off + len)`; nothing is
209
- * copied. A `#` is not looked for: a request target holds no fragment.
210
- *
211
- * @param bytes The array holding the request target.
212
- * @param off Where the target starts in `bytes`.
213
- * @param len The target's length in bytes; a window outside `bytes` panics.
214
- * @returns The index in `bytes` of the `?`, or -1 when the target has no query.
215
- */
216
- export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 => {
217
- urlCheckWindow(bytes, off, len, "urlQueryStart")
218
- const end: i32 = off + len
232
+ /** `urlQueryStart` of `bytes[off, end)`, for a caller that built the window from `bytes.length` and so has nothing to check. */
233
+ const urlQueryScan = (bytes: u8[], off: i32, end: i32): i32 => {
219
234
  const n: i32 = toI32(bytes.length)
220
235
  let i: i32 = off
221
236
  while (i >= 0 && i < end && i < n) {
@@ -227,6 +242,19 @@ export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 => {
227
242
  return -1
228
243
  }
229
244
 
245
+ /**
246
+ * The index of the first `?` in `bytes[off, off + len)`, or -1 when there is
247
+ * none. The path is `[off, q)` and the query `(q, off + len)`; nothing is
248
+ * copied. A `#` is not looked for: a request target holds no fragment.
249
+ *
250
+ * @param bytes The array holding the request target.
251
+ * @param off Where the target starts in `bytes`.
252
+ * @param len The target's length in bytes; a window outside `bytes` panics.
253
+ * @returns The index in `bytes` of the `?`, or -1 when the target has no query.
254
+ */
255
+ export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 =>
256
+ urlQueryScan(bytes, off, urlCheckWindow(bytes, off, len, "urlQueryStart"))
257
+
230
258
  /**
231
259
  * Percent-decodes `src[off, off + len)` into `out` from `out[at]` on, and
232
260
  * answers the number of bytes written, or a negative `URL_DECODE_ERR_*` code;
@@ -245,7 +273,25 @@ export const urlQueryStart = (bytes: u8[], off: i32, len: i32): i32 => {
245
273
  * @returns How many bytes were written from `out[at]` on, or `URL_DECODE_ERR_ESCAPE`, `URL_DECODE_ERR_NUL` or `URL_DECODE_ERR_SPACE`.
246
274
  */
247
275
  export const percentDecode = (src: u8[], off: i32, len: i32, out: u8[], at: i32, flags: i32): i32 =>
248
- urlDecodeRun(src, off, len, out, at, flags, false)
276
+ urlDecodeChecked(urlDecodeRun(src, off, len, out, at, flags, false), "percentDecode")
277
+
278
+ /**
279
+ * `percentDecode` that answers `URL_DECODE_ERR_WINDOW`, writing nothing,
280
+ * where `percentDecode` panics: a window outside `src` or an `at` outside
281
+ * `out`. A caller a strict module reaches uses this one.
282
+ *
283
+ * @param src The array holding the encoded text.
284
+ * @param off Where the encoded text starts in `src`.
285
+ * @param len The encoded text's length in bytes.
286
+ * @param out The array the decoded bytes are written into.
287
+ * @param at The index in `out` the first decoded byte goes to.
288
+ * @param flags `URL_DECODE_FORM`, `URL_DECODE_LENIENT` and `URL_DECODE_ALLOW_NUL`, summed, or 0 for strict.
289
+ * @returns How many bytes were written from `out[at]` on, or a negative `URL_DECODE_ERR_*` code, `URL_DECODE_ERR_WINDOW` included.
290
+ */
291
+ export const tryPercentDecode = (src: u8[], off: i32, len: i32, out: u8[], at: i32, flags: i32): i32 => {
292
+ const result: i32 = urlDecodeRun(src, off, len, out, at, flags, false)
293
+ return result === URL_DECODE_AT_OUTSIDE ? URL_DECODE_ERR_WINDOW : result
294
+ }
249
295
 
250
296
  /**
251
297
  * The offset in `src` of the byte `percentDecode` with the same arguments
@@ -262,7 +308,7 @@ export const percentDecode = (src: u8[], off: i32, len: i32, out: u8[], at: i32,
262
308
  * @returns The index in `src` decoding stops at, or -1 when all of it decodes.
263
309
  */
264
310
  export const percentDecodeErrorAt = (src: u8[], off: i32, len: i32, out: u8[], at: i32, flags: i32): i32 =>
265
- urlDecodeRun(src, off, len, out, at, flags, true)
311
+ urlDecodeChecked(urlDecodeRun(src, off, len, out, at, flags, true), "percentDecodeErrorAt")
266
312
 
267
313
  /**
268
314
  * `percentDecode` for one path segment: `+` is literal whatever `flags` say,
@@ -284,7 +330,11 @@ export const percentDecodePathSegment = (
284
330
  out: u8[],
285
331
  at: i32,
286
332
  flags: i32
287
- ): i32 => urlDecodeRun(src, off, len, out, at, (flags | URL_DECODE_FORM) ^ URL_DECODE_FORM, false)
333
+ ): i32 =>
334
+ urlDecodeChecked(
335
+ urlDecodeRun(src, off, len, out, at, (flags | URL_DECODE_FORM) ^ URL_DECODE_FORM, false),
336
+ "percentDecodePathSegment"
337
+ )
288
338
 
289
339
  /**
290
340
  * The segments of a path, split on the raw `/`. A leading `/` starts the
@@ -316,14 +366,16 @@ export class UrlPathSegments {
316
366
  * @param len How many bytes the path takes.
317
367
  */
318
368
  reset(bytes: u8[], off: i32, len: i32): void {
319
- urlCheckWindow(bytes, off, len, "UrlPathSegments")
369
+ const end: i32 = urlCheckWindow(bytes, off, len, "UrlPathSegments")
320
370
  this.bytes = bytes
321
371
  this.pos = off
322
- this.end = off + len
372
+ this.end = end
323
373
  this.start = off
324
374
  this.length = 0
325
375
  this.done = len === 0
326
- if (len > 0 && toI32(bytes[off]) === URL_SLASH) {
376
+ // With `len > 0` the window check puts `off` inside `bytes`; the bounds
377
+ // are spelled out for the proof.
378
+ if (len > 0 && off >= 0 && off < end && off < toI32(bytes.length) && toI32(bytes[off]) === URL_SLASH) {
327
379
  this.pos = off + 1
328
380
  }
329
381
  }
@@ -337,14 +389,17 @@ export class UrlPathSegments {
337
389
  return false
338
390
  }
339
391
  const bytes: u8[] = this.bytes
340
- let j: i32 = this.pos
392
+ const pos: i32 = this.pos
393
+ const end: i32 = this.end
394
+ let j: i32 = pos
341
395
  const n: i32 = toI32(bytes.length)
342
- while (j >= 0 && j < this.end && j < n && toI32(bytes[j]) !== URL_SLASH) {
396
+ while (j >= 0 && j < end && j < n && toI32(bytes[j]) !== URL_SLASH) {
343
397
  j += 1
344
398
  }
345
- this.start = this.pos
346
- this.length = j - this.pos
347
- if (j < this.end) {
399
+ this.start = pos
400
+ // `j` starts at `pos` and only grows, so the difference cannot wrap.
401
+ this.length = toI32(toU32(j) - toU32(pos))
402
+ if (j < end) {
348
403
  this.pos = j + 1
349
404
  } else {
350
405
  this.done = true
@@ -406,10 +461,19 @@ export class UrlQuery {
406
461
  * @param len How many bytes the query takes.
407
462
  */
408
463
  reset(bytes: u8[], off: i32, len: i32): void {
409
- urlCheckWindow(bytes, off, len, "UrlQuery")
464
+ this.over(bytes, off, urlCheckWindow(bytes, off, len, "UrlQuery"))
465
+ }
466
+
467
+ /**
468
+ * @internal `reset` on `bytes[off, end)`, for `urlQueryGet`, which builds the window from `bytes.length` and so has nothing to check.
469
+ * @param bytes The array that holds the query.
470
+ * @param off Where the query starts in `bytes`, past its `?`.
471
+ * @param end Where the query ends in `bytes`.
472
+ */
473
+ over(bytes: u8[], off: i32, end: i32): void {
410
474
  this.bytes = bytes
411
475
  this.pos = off
412
- this.end = off + len
476
+ this.end = end
413
477
  this.count = 0
414
478
  this.failed = 0
415
479
  this.keyStart = off
@@ -438,7 +502,10 @@ export class UrlQuery {
438
502
  if (i >= end) {
439
503
  return URL_QUERY_END
440
504
  }
441
- if (this.count >= this.maxPairs) {
505
+ const start: i32 = i
506
+ const count: i32 = this.count
507
+ const maxPairs: i32 = this.maxPairs
508
+ if (count >= maxPairs) {
442
509
  this.failed = URL_QUERY_ERR_PAIRS
443
510
  return this.failed
444
511
  }
@@ -453,20 +520,22 @@ export class UrlQuery {
453
520
  }
454
521
  i += 1
455
522
  }
456
- this.keyStart = this.pos
523
+ // `start <= eq < i` when `eq` was found, as the loop only moves `i` on,
524
+ // so none of the differences below can wrap.
525
+ this.keyStart = start
457
526
  if (eq < 0) {
458
- this.keyLength = i - this.pos
527
+ this.keyLength = toI32(toU32(i) - toU32(start))
459
528
  this.valueStart = i
460
529
  this.valueLength = 0
461
530
  this.hasValue = false
462
531
  } else {
463
- this.keyLength = eq - this.pos
464
- this.valueStart = eq + 1
465
- this.valueLength = i - eq - 1
532
+ this.keyLength = toI32(toU32(eq) - toU32(start))
533
+ this.valueStart = toI32(toU32(eq) + 1)
534
+ this.valueLength = toI32(toU32(i) - toU32(eq) - 1)
466
535
  this.hasValue = true
467
536
  }
468
537
  this.pos = i
469
- this.count += 1
538
+ this.count = count + 1
470
539
  return URL_QUERY_PAIR
471
540
  }
472
541
  }
@@ -474,14 +543,20 @@ export class UrlQuery {
474
543
  /**
475
544
  * Whether `bytes[off, off + len)`, decoded strictly in form mode, is `name`'s
476
545
  * bytes. Nothing is written: a malformed escape or `%00` in the key matches
477
- * no name.
546
+ * no name, and so does a window outside `bytes`. Its one caller,
547
+ * `urlQueryFind`, passes the window `UrlQuery.next` has just set inside
548
+ * `bytes`, so that refusal is only for a caller that edits the public fields.
478
549
  */
479
550
  const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean => {
480
551
  const n: i32 = toI32(name.length)
481
- const end: i32 = off + len
552
+ const size: i32 = toI32(bytes.length)
553
+ if (off < 0 || len < 0 || off > size || len > size - off) {
554
+ return false
555
+ }
556
+ // `off + len` is at most `size`, checked above, so it cannot wrap.
557
+ const end: i32 = toI32(toU32(off) + toU32(len))
482
558
  let i: i32 = off
483
559
  let o: i32 = 0
484
- const size: i32 = toI32(bytes.length)
485
560
  while (i >= 0 && i < end && i < size) {
486
561
  let b: i32 = toI32(bytes[i])
487
562
  let step: i32 = 1
@@ -498,7 +573,9 @@ const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean =>
498
573
  return false
499
574
  }
500
575
  o += 1
501
- i += step
576
+ // An escape is taken only when its digits lie inside `bytes`, so the step
577
+ // ends at most at `bytes.length` and cannot wrap.
578
+ i = toI32(toU32(i) + toU32(step))
502
579
  }
503
580
  return o === n
504
581
  }
@@ -569,15 +646,28 @@ const urlIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
569
646
  if (second < lo || second > hi) {
570
647
  return false
571
648
  }
572
- let k: i32 = 2
573
- while (k <= need) {
574
- const c: i32 = toI32(buf[i + k])
649
+ // The test on `end` above already keeps the sequence inside `buf`; each
650
+ // byte's own guard is spelled out for the bounds proof.
651
+ if (need >= 2) {
652
+ if (i >= n - 2) {
653
+ return false
654
+ }
655
+ const c: i32 = toI32(buf[i + 2])
575
656
  if (c < 0x80 || c > 0xbf) {
576
657
  return false
577
658
  }
578
- k += 1
579
659
  }
580
- i += need + 1
660
+ if (need >= 3) {
661
+ if (i >= n - 3) {
662
+ return false
663
+ }
664
+ const c: i32 = toI32(buf[i + 3])
665
+ if (c < 0x80 || c > 0xbf) {
666
+ return false
667
+ }
668
+ }
669
+ // Every byte of the sequence was read inside `buf`, so the step cannot wrap.
670
+ i = toI32(toU32(i) + toU32(need) + 1)
581
671
  }
582
672
  return true
583
673
  }
@@ -600,7 +690,11 @@ const urlBytesOf = (s: string): u8[] | null => {
600
690
 
601
691
  /** `percentDecode` of `src[off, off + len)` as a new string, or null on an error or when the result is not UTF-8. */
602
692
  const urlDecodeString = (src: u8[], off: i32, len: i32, flags: i32): string | null => {
693
+ // `len` bytes of room; decoding never makes the text longer.
603
694
  const out: u8[] = new Array<u8>(len)
695
+ // A window outside `src` is a caller contract, kept as `percentDecode`'s
696
+ // panic rather than answered as null: both callers, `urlDecode` and
697
+ // `urlQueryGet`, cut the window from `src.length`.
604
698
  const n: i32 = percentDecode(src, off, len, out, 0, flags)
605
699
  if (n < 0 || !urlIsUtf8(out, 0, n)) {
606
700
  return null
@@ -643,12 +737,15 @@ export const urlQueryGet = (target: string, name: string): string | null => {
643
737
  return null
644
738
  }
645
739
  const len: i32 = toI32(bytes.length)
646
- const q: i32 = urlQueryStart(bytes, 0, len)
647
- if (q < 0) {
740
+ const q: i32 = urlQueryScan(bytes, 0, len)
741
+ // `urlQueryScan` answers -1 or an index below `len`; the upper bound is
742
+ // spelled out for the proof of the window below.
743
+ if (q < 0 || q >= len) {
648
744
  return null
649
745
  }
650
746
  const query: UrlQuery = new UrlQuery(new UrlQueryLimits())
651
- query.reset(bytes, q + 1, len - q - 1)
747
+ // `q < len = bytes.length`, so `[q + 1, len)` lies inside `bytes`.
748
+ query.over(bytes, q + 1, len)
652
749
  if (urlQueryFind(query, name) !== URL_QUERY_PAIR) {
653
750
  return null
654
751
  }
@@ -697,14 +794,21 @@ export class Url {
697
794
 
698
795
  /** `bytes[a, b)` as a string with A to Z lower-cased. */
699
796
  const urlLowerString = (bytes: u8[], a: i32, b: i32): string => {
700
- const out: u8[] = new Array<u8>(b - a)
701
- let i: i32 = a
702
- while (i >= 0 && i < b && i < toI32(bytes.length) && i - a < toI32(out.length)) {
797
+ // `a <= b` is the caller's contract: a window the other way round is a
798
+ // negative length, and `new Array` refuses it.
799
+ const out: u8[] = new Array<u8>(toI32(toU32(b) - toU32(a)))
800
+ let j: i32 = 0
801
+ while (j < toI32(out.length)) {
802
+ const i: i32 = toI32(toU32(a) + toU32(j))
803
+ if (i < 0 || i >= toI32(bytes.length)) {
804
+ break
805
+ }
703
806
  const c: i32 = toI32(bytes[i])
704
- out[i - a] = c >= 65 && c <= 90 ? toU8(c + 32) : toU8(c)
705
- i += 1
807
+ // `c | 32` is `c + 32` for A to Z, whose bit 5 is clear.
808
+ out[j] = c >= 65 && c <= 90 ? toU8(c | 32) : toU8(c)
809
+ j += 1
706
810
  }
707
- return String.fromBytes(out, 0, b - a)
811
+ return String.fromBytes(out, 0, toI32(out.length))
708
812
  }
709
813
 
710
814
  /** The first `c` in `bytes[a, b)`, or `b`. */
@@ -733,9 +837,24 @@ const urlIsIpv4 = (bytes: u8[], a: i32, b: i32): boolean => {
733
837
  let parts: i32 = 0
734
838
  let i: i32 = a
735
839
  while (parts < 4) {
840
+ // A part that starts before the array has no digits; saying so first
841
+ // keeps `i - start` below in range.
842
+ if (i < 0) {
843
+ return false
844
+ }
736
845
  const start: i32 = i
737
846
  let value: i32 = 0
738
- while (i >= 0 && i < b && i < toI32(bytes.length) && i - start < 4 && urlIsDigit(toI32(bytes[i]))) {
847
+ // Three digits make at most 999, so `value <= 999` never stops the loop
848
+ // before `i - start < 4` does; it bounds the sum for the proof.
849
+ while (
850
+ i >= 0 &&
851
+ i < b &&
852
+ i < toI32(bytes.length) &&
853
+ i - start < 4 &&
854
+ value >= 0 &&
855
+ value <= 999 &&
856
+ urlIsDigit(toI32(bytes[i]))
857
+ ) {
739
858
  value = value * 10 + toI32(bytes[i]) - 48
740
859
  i += 1
741
860
  }
@@ -769,11 +888,25 @@ const urlIsIpv6 = (bytes: u8[], a: i32, b: i32): boolean => {
769
888
  let groups: i32 = 0
770
889
  let elided: boolean = false
771
890
  let i: i32 = a
772
- if (b - a >= 2 && toI32(bytes[a]) === URL_COLON && toI32(bytes[a + 1]) === URL_COLON) {
891
+ const n: i32 = toI32(bytes.length)
892
+ // The window lies inside `bytes`, so `a < n - 1` holds whenever
893
+ // `a + 1 < b` does; it is spelled out for the bounds proof.
894
+ if (
895
+ a >= 0 &&
896
+ a < n - 1 &&
897
+ a + 1 < b &&
898
+ toI32(bytes[a]) === URL_COLON &&
899
+ toI32(bytes[a + 1]) === URL_COLON
900
+ ) {
773
901
  elided = true
774
902
  i = a + 2
775
903
  }
776
904
  while (i < b) {
905
+ // A group before the array has no digits, and nine groups are already
906
+ // too many; answering both first keeps the counts below in range.
907
+ if (i < 0 || groups > 8) {
908
+ return false
909
+ }
777
910
  const start: i32 = i
778
911
  while (i >= 0 && i < b && i < toI32(bytes.length) && urlHexValue(toI32(bytes[i])) >= 0) {
779
912
  i += 1
@@ -818,19 +951,28 @@ const urlIsIpv6 = (bytes: u8[], a: i32, b: i32): boolean => {
818
951
  * that reads them as 127.0.0.1.
819
952
  */
820
953
  const urlLooksNumeric = (bytes: u8[], a: i32, b: i32): boolean => {
954
+ // The window lies inside `bytes`, so the bounds beside `a` below always
955
+ // hold; they are spelled out for the proof.
956
+ const n: i32 = toI32(bytes.length)
821
957
  let end: i32 = b
822
- if (end > a && toI32(bytes[end - 1]) === URL_DOT) {
958
+ if (end > a && end > 0 && end <= toI32(bytes.length) && toI32(bytes[end - 1]) === URL_DOT) {
823
959
  end -= 1
824
960
  }
825
961
  let start: i32 = end
826
- while (start > a && toI32(bytes[start - 1]) !== URL_DOT) {
962
+ while (start > a && start > 0 && start <= toI32(bytes.length) && toI32(bytes[start - 1]) !== URL_DOT) {
827
963
  start -= 1
828
964
  }
829
965
  if (start === end) {
830
966
  return false
831
967
  }
832
968
  let i: i32 = start
833
- if (end - start >= 2 && toI32(bytes[start]) === 48 && (toI32(bytes[start + 1]) | 32) === 120) {
969
+ if (
970
+ start >= 0 &&
971
+ start < n - 1 &&
972
+ start + 1 < end &&
973
+ toI32(bytes[start]) === 48 &&
974
+ (toI32(bytes[start + 1]) | 32) === 120
975
+ ) {
834
976
  i = start + 2
835
977
  while (i >= 0 && i < end && i < toI32(bytes.length) && urlHexValue(toI32(bytes[i])) >= 0) {
836
978
  i += 1
@@ -865,12 +1007,14 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
865
1007
  if (portAt < 0) {
866
1008
  return false
867
1009
  }
868
- if (h < b && toI32(bytes[h]) === URL_OPEN_BRACKET) {
1010
+ // `h` lies inside the window, which lies inside `bytes`; the bounds are
1011
+ // spelled out for the proof.
1012
+ if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
869
1013
  u.host = urlLowerString(bytes, h + 1, portAt - 1)
870
1014
  } else {
871
1015
  u.host = urlLowerString(bytes, h, portAt)
872
1016
  }
873
- if (portAt + 1 < b) {
1017
+ if (portAt < b && portAt + 1 < b) {
874
1018
  const port: i32 = urlPortOf(bytes, portAt + 1, b)
875
1019
  if (port < 0) {
876
1020
  return false
@@ -890,10 +1034,12 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
890
1034
  const urlHostEnd = (bytes: u8[], h: i32, b: i32): i32 => {
891
1035
  if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
892
1036
  const close: i32 = urlFind(bytes, h, b, URL_CLOSE_BRACKET)
893
- if (close === b || !urlIsIpv6(bytes, h + 1, close)) {
1037
+ // `urlFind` answers an index in `[h, b]`, so `close >= b` is `close === b`
1038
+ // and the other bound always holds; both keep `close + 1` in range.
1039
+ if (close < 0 || close >= b || !urlIsIpv6(bytes, h + 1, close)) {
894
1040
  return -1
895
1041
  }
896
- if (close + 1 < b && toI32(bytes[close + 1]) !== URL_COLON) {
1042
+ if (close + 1 < b && close + 1 < toI32(bytes.length) && toI32(bytes[close + 1]) !== URL_COLON) {
897
1043
  return -1
898
1044
  }
899
1045
  return close + 1
@@ -918,10 +1064,13 @@ const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
918
1064
  let i: i32 = a
919
1065
  while (i >= 0 && i < b && i < toI32(bytes.length)) {
920
1066
  const c: i32 = toI32(bytes[i])
921
- if (!urlIsDigit(c)) {
1067
+ // `urlIsDigit`, written out so the proof sees the digit's range.
1068
+ if (c < 48 || c > 57) {
922
1069
  return -1
923
1070
  }
924
- port = port * 10 + c - 48
1071
+ // `port` is at most 65535 here and `c` a digit, so the sum is at most
1072
+ // 655359 and cannot wrap.
1073
+ port = toI32(toU32(port) * 10 + toU32(c) - 48)
925
1074
  if (port > URL_MAX_PORT) {
926
1075
  return -1
927
1076
  }
@@ -952,7 +1101,8 @@ export const urlIsHostPort = (bytes: u8[], a: i32, b: i32): boolean => {
952
1101
  if (portAt < 0) {
953
1102
  return false
954
1103
  }
955
- return portAt + 1 >= b || urlPortOf(bytes, portAt + 1, b) >= 0
1104
+ // `portAt >= b` is spelled out so the proof sees `portAt + 1` in range.
1105
+ return portAt >= b || portAt + 1 >= b || urlPortOf(bytes, portAt + 1, b) >= 0
956
1106
  }
957
1107
 
958
1108
  /**
@@ -962,13 +1112,14 @@ export const urlIsHostPort = (bytes: u8[], a: i32, b: i32): boolean => {
962
1112
  * only a resolved URL is held to.
963
1113
  */
964
1114
  const urlReadReference = (s: string): Url | null => {
965
- const bytes: u8[] | null = urlBytesOf(s)
966
- if (bytes === null) {
1115
+ const maybe: u8[] | null = urlBytesOf(s)
1116
+ if (maybe === null) {
967
1117
  return null
968
1118
  }
1119
+ const bytes: u8[] = maybe
969
1120
  const n: i32 = toI32(bytes.length)
970
1121
  let i: i32 = 0
971
- while (i < n) {
1122
+ while (i >= 0 && i < n) {
972
1123
  const c: i32 = toI32(bytes[i])
973
1124
  // A WHATWG parser reads `\` as `/` under http(s), so it would end the
974
1125
  // authority or start one where this reading does not: refused everywhere.
@@ -982,6 +1133,8 @@ const urlReadReference = (s: string): Url | null => {
982
1133
  }
983
1134
  i += 1
984
1135
  }
1136
+ // `urlFind` answers an index at most `n`, so the tests against the length
1137
+ // below always hold; they are spelled out for the proof.
985
1138
  const u: Url = new Url()
986
1139
  const hash: i32 = urlFind(bytes, 0, n, URL_HASH)
987
1140
  if (hash < n) {
@@ -992,21 +1145,27 @@ const urlReadReference = (s: string): Url | null => {
992
1145
  u.query = String.fromBytes(bytes, question + 1, hash)
993
1146
  }
994
1147
  let pos: i32 = 0
995
- if (question > 0 && urlIsAlpha(toI32(bytes[0]))) {
1148
+ if (question > 0 && toI32(bytes.length) > 0 && urlIsAlpha(toI32(bytes[0]))) {
996
1149
  let j: i32 = 1
997
- while (j < question) {
1150
+ while (j < question && j < toI32(bytes.length)) {
998
1151
  const c: i32 = toI32(bytes[j])
999
1152
  if (!(urlIsAlpha(c) || urlIsDigit(c) || c === URL_PLUS || c === 45 || c === URL_DOT)) {
1000
1153
  break
1001
1154
  }
1002
1155
  j += 1
1003
1156
  }
1004
- if (j < question && toI32(bytes[j]) === URL_COLON) {
1157
+ if (j < question && j < toI32(bytes.length) && toI32(bytes[j]) === URL_COLON) {
1005
1158
  u.scheme = urlLowerString(bytes, 0, j)
1006
1159
  pos = j + 1
1007
1160
  }
1008
1161
  }
1009
- if (question - pos >= 2 && toI32(bytes[pos]) === URL_SLASH && toI32(bytes[pos + 1]) === URL_SLASH) {
1162
+ if (
1163
+ pos >= 0 &&
1164
+ pos < toI32(bytes.length) - 1 &&
1165
+ pos + 1 < question &&
1166
+ toI32(bytes[pos]) === URL_SLASH &&
1167
+ toI32(bytes[pos + 1]) === URL_SLASH
1168
+ ) {
1010
1169
  const end: i32 = urlFind(bytes, pos + 2, question, URL_SLASH)
1011
1170
  u.hasAuthority = true
1012
1171
  if (!urlReadAuthority(bytes, pos + 2, end, u)) {
@@ -1020,11 +1179,18 @@ const urlReadReference = (s: string): Url | null => {
1020
1179
 
1021
1180
  /** The width of a `.` (1) or `%2e` (3) at `s[i]`, or 0. */
1022
1181
  const urlDotAt = (s: string, i: i32, end: i32): i32 => {
1023
- if (i < end && toI32(s.charCodeAt(i)) === URL_DOT) {
1182
+ // Callers pass `0 <= i` and `end <= s.length`, so only `i >= end` can
1183
+ // answer here; the other bounds are spelled out for the proof.
1184
+ const n: i32 = toI32(s.length)
1185
+ if (i < 0 || i >= end || i >= n) {
1186
+ return 0
1187
+ }
1188
+ if (toI32(s.charCodeAt(i)) === URL_DOT) {
1024
1189
  return 1
1025
1190
  }
1026
1191
  if (
1027
- end - i >= 3 &&
1192
+ i < n - 2 &&
1193
+ i + 2 < end &&
1028
1194
  toI32(s.charCodeAt(i)) === URL_PERCENT &&
1029
1195
  toI32(s.charCodeAt(i + 1)) === 50 &&
1030
1196
  (toI32(s.charCodeAt(i + 2)) | 32) === 101
@@ -1040,25 +1206,19 @@ const urlDotSegment = (s: string, a: i32, b: i32): i32 => {
1040
1206
  if (first === 0) {
1041
1207
  return 0
1042
1208
  }
1043
- if (a + first === b) {
1209
+ // `urlDotAt` answers a width only for characters inside `s`, so each sum
1210
+ // is at most `s.length` and cannot wrap.
1211
+ const next: i32 = toI32(toU32(a) + toU32(first))
1212
+ if (next === b) {
1044
1213
  return 1
1045
1214
  }
1046
- const second: i32 = urlDotAt(s, a + first, b)
1047
- if (second > 0 && a + first + second === b) {
1215
+ const second: i32 = urlDotAt(s, next, b)
1216
+ if (second > 0 && toI32(toU32(next) + toU32(second)) === b) {
1048
1217
  return 2
1049
1218
  }
1050
1219
  return 0
1051
1220
  }
1052
1221
 
1053
- /** The first `/` in `s[i, n)`, or `n`. */
1054
- const urlSlashFrom = (s: string, i: i32, n: i32): i32 => {
1055
- let j: i32 = i
1056
- while (j < n && toI32(s.charCodeAt(j)) !== URL_SLASH) {
1057
- j += 1
1058
- }
1059
- return j
1060
- }
1061
-
1062
1222
  /**
1063
1223
  * RFC 3986 §5.2.4 remove_dot_segments, reading `%2e` as `.` the way the WHATWG
1064
1224
  * URL Standard does, so `/a/%2e%2e/b` is `/b` here and at a server that
@@ -1070,19 +1230,32 @@ const urlRemoveDotSegments = (s: string): string => {
1070
1230
  const out: string[] = []
1071
1231
  let i: i32 = 0
1072
1232
  while (i >= 0 && i < n) {
1233
+ // Each segment's end is found by a scan written out here, not in a
1234
+ // helper, so the proof sees it inside `s` where the segment is cut.
1073
1235
  if (toI32(s.charCodeAt(i)) !== URL_SLASH) {
1074
- const j: i32 = urlSlashFrom(s, i, n)
1236
+ let j: i32 = i
1237
+ while (j >= 0 && j < n && toI32(s.charCodeAt(j)) !== URL_SLASH) {
1238
+ j += 1
1239
+ }
1075
1240
  if (urlDotSegment(s, i, j) > 0) {
1076
1241
  i = j < n ? j + 1 : n
1077
1242
  } else {
1078
- out.push(s.slice(i, j))
1243
+ // The scan starts at `i` and stops at `n` at most, so this test always
1244
+ // holds; it is spelled out so the cut keeps no clamp.
1245
+ if (i >= 0 && i <= j && j <= toI32(s.length)) {
1246
+ out.push(s.substring(i, j))
1247
+ }
1079
1248
  i = j
1080
1249
  }
1081
1250
  } else {
1082
- const j: i32 = urlSlashFrom(s, i + 1, n)
1251
+ let j: i32 = i + 1
1252
+ while (j >= 0 && j < n && toI32(s.charCodeAt(j)) !== URL_SLASH) {
1253
+ j += 1
1254
+ }
1083
1255
  const dots: i32 = urlDotSegment(s, i + 1, j)
1084
- if (dots === 0) {
1085
- out.push(s.slice(i, j))
1256
+ // Always true, as above; spelled out so the cut keeps no clamp.
1257
+ if (dots === 0 && i >= 0 && i <= j && j <= toI32(s.length)) {
1258
+ out.push(s.substring(i, j))
1086
1259
  } else if (dots === 2 && out.length > 0) {
1087
1260
  out.pop()
1088
1261
  }
@@ -1139,7 +1312,7 @@ const urlMerge = (base: Url, path: string): string => {
1139
1312
  while (k > 0 && toI32(base.path.charCodeAt(k - 1)) !== URL_SLASH) {
1140
1313
  k -= 1
1141
1314
  }
1142
- return base.path.slice(0, k) + path
1315
+ return base.path.substring(0, k) + path
1143
1316
  }
1144
1317
 
1145
1318
  /**
@@ -1173,8 +1346,10 @@ export const urlResolve = (base: Url, ref: string): Url | null => {
1173
1346
  }
1174
1347
  return urlFinish(r)
1175
1348
  }
1176
- if (toI32(r.path.charCodeAt(0)) !== URL_SLASH) {
1177
- r.path = urlMerge(base, r.path)
1349
+ // The path is not empty here; its length is tested for the proof.
1350
+ const path: string = r.path
1351
+ if (toI32(path.length) > 0 && toI32(path.charCodeAt(0)) !== URL_SLASH) {
1352
+ r.path = urlMerge(base, path)
1178
1353
  }
1179
1354
  }
1180
1355
  }