@amritk/nish-x86_64-linux 0.44.0 → 0.45.1

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 (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/std/net/http3.ts CHANGED
@@ -416,6 +416,13 @@ const http3CheckCap = (what: string, value: i32, low: i32, high: i32): void => {
416
416
  }
417
417
  }
418
418
 
419
+ /**
420
+ * The millisecond `wait` after `since`. The sum is taken in `f64`, exact for
421
+ * any clock below 2^53 ms, and saturates at the last millisecond an `i64`
422
+ * holds: a deadline no clock reaches, where the `i64` sum would overflow.
423
+ */
424
+ const http3Deadline = (since: i64, wait: i32): i64 => toI64(toF64(since) + toF64(wait))
425
+
419
426
  /** Whether one of `names` is `date` in any ASCII case (RFC 9110 §5.1). */
420
427
  const http3NamesHoldDate = (names: u8[][]): boolean => {
421
428
  // An ASCII letter's case is its 0x20 bit, and every octet of `date` is a letter.
@@ -525,9 +532,8 @@ export class Http3Connection {
525
532
  /** The frame being read: its type, and what is left of its payload (-1 between frames). */
526
533
  private frameType: i64[]
527
534
  private frameLeft: i64[]
528
- /** DATA bytes received on the request, and its `content-length` or -1. */
529
- private dataTotal: i64[]
530
- private contentLength: i64[]
535
+ /** What the request's `content-length` still owes after the DATA received, or -1 without one. */
536
+ private contentLeft: i64[]
531
537
  /** Bytes still owed on the stream: a filler's payload, and the DATA frame's payload the program is writing. */
532
538
  private fillLeft: i64[]
533
539
  private dataLeft: i64[]
@@ -644,7 +650,8 @@ export class Http3Connection {
644
650
  * HEADERS frame of `maxFieldSectionSize` fit half a stream's buffer, and
645
651
  * one on every request stream fit half the connection's window, and let
646
652
  * the client open the three unidirectional streams it needs and this side
647
- * open its own three (§6.2). A cap out of range panics.
653
+ * open its own three (§6.2). Each QUIC stream limit is at most 1,024. A cap
654
+ * out of range panics.
648
655
  *
649
656
  * @param config The caps; kept, not copied, so it must not change after this call.
650
657
  * @param quic The server QUIC connection to run on, kept for this connection's life. Its stream caps
@@ -659,10 +666,13 @@ export class Http3Connection {
659
666
  http3CheckCap("maxFieldSectionSize", config.maxFieldSectionSize, 1, buffer / 2 - H3_HEAD)
660
667
  // The connection's credit too: every request stream may hold a HEADERS
661
668
  // frame not yet whole, and QUIC raises MAX_DATA once half the window is read.
662
- const held: i64 = quic.streams.maxBidi * toI64(config.maxFieldSectionSize + H3_HEAD) * toI64(2)
663
- if (quic.streams.windowData < held) {
669
+ // The window must hold `bidi` times `each`; dividing tests that with no product to overflow.
670
+ const bidi: i64 = quic.streams.maxBidi
671
+ http3CheckCap("the QUIC bidirectional stream limit", toI32(bidi), 0, 1024)
672
+ const each: i64 = toI64(2) * toI64(config.maxFieldSectionSize) + toI64(2) * toI64(H3_HEAD)
673
+ if (bidi > 0 && quic.streams.windowData / bidi < each) {
664
674
  panic(
665
- `Http3Connection: a connection window of ${quic.streams.windowData}, under the ${held} that ${quic.streams.maxBidi} request streams' field sections need`
675
+ `Http3Connection: a connection window of ${quic.streams.windowData}, under ${each} bytes for each of ${bidi} request streams' field sections`
666
676
  )
667
677
  }
668
678
  http3CheckCap("bodyChunk", config.bodyChunk, 1, H3_MAX_CHUNK)
@@ -731,23 +741,26 @@ export class Http3Connection {
731
741
  this.settingIds.push(H3_SETTINGS_WEBTRANSPORT_MAX_SESSIONS)
732
742
  this.settingValues.push(toI64(config.webtransportSessions))
733
743
  }
734
- const n: i32 = toI32(quic.streams.slots.length)
744
+ // `QuicStreams` builds one slot per stream its three limits allow, each
745
+ // checked above to be at most 1,024, so the table holds at most 3,072
746
+ // and the `u16` holds it and every table below exactly.
747
+ const n: u16 = toU16(quic.streams.slots.length)
748
+ const bytes: u16 = toU16(toI32(n) * H3_HEAD)
735
749
  this.slotId = new Array<i64>(n)
736
750
  this.frameType = new Array<i64>(n)
737
751
  this.frameLeft = new Array<i64>(n)
738
- this.dataTotal = new Array<i64>(n)
739
- this.contentLength = new Array<i64>(n)
752
+ this.contentLeft = new Array<i64>(n)
740
753
  this.fillLeft = new Array<i64>(n)
741
754
  this.dataLeft = new Array<i64>(n)
742
755
  this.phase = new Array<i32>(n)
743
756
  this.flags = new Array<i32>(n)
744
757
  this.headLen = new Array<i32>(n)
745
- this.head = new Array<u8>(n * H3_HEAD)
758
+ this.head = new Array<u8>(bytes)
746
759
  this.pendAt = new Array<i32>(n)
747
760
  this.pendLen = new Array<i32>(n)
748
- this.pend = new Array<u8>(n * H3_HEAD)
749
- this.again = new Array<i64>(n > 0 ? n : H3_ONE)
750
- this.waiting = new Array<i64>(n > 0 ? n : H3_ONE)
761
+ this.pend = new Array<u8>(bytes)
762
+ this.again = new Array<i64>(n > 0 ? n : toU16(H3_ONE))
763
+ this.waiting = new Array<i64>(n > 0 ? n : toU16(H3_ONE))
751
764
  this.restart()
752
765
  }
753
766
 
@@ -799,7 +812,8 @@ export class Http3Connection {
799
812
  this.heads = 0
800
813
  this.clockHeads = 0
801
814
  this.clockMode = H3_CLOCK_OFF
802
- this.generation = this.generation < 2147483647 ? this.generation + 1 : H3_ONE
815
+ const generation: i32 = this.generation
816
+ this.generation = generation < 2147483647 ? generation + 1 : H3_ONE
803
817
  this.dataStart = 0
804
818
  this.dataLength = 0
805
819
  this.controlFill = 0
@@ -882,9 +896,9 @@ export class Http3Connection {
882
896
  if (mode === H3_CLOCK_BUSY) {
883
897
  return -1
884
898
  }
885
- let due: i64 = this.clockSince + toI64(this.config.idleTimeout)
899
+ let due: i64 = http3Deadline(this.clockSince, this.config.idleTimeout)
886
900
  if (mode === H3_CLOCK_HEAD) {
887
- due = http3Min(due, this.headSince + toI64(this.config.headTimeout))
901
+ due = http3Min(due, http3Deadline(this.headSince, this.config.headTimeout))
888
902
  }
889
903
  if (now < due) {
890
904
  return due
@@ -896,10 +910,16 @@ export class Http3Connection {
896
910
  /** Whether a request stream the client opened, counted in `live`, has not yet given its head. */
897
911
  private awaitsHead(): boolean {
898
912
  const skip: i32 = H3_FLAG_DELIVERED | H3_FLAG_WT
899
- for (let k: i32 = 0; k < toI32(this.slotId.length); k++) {
900
- const flags: i32 = this.flags[k]
913
+ const flagsOf: i32[] = this.flags
914
+ const phases: i32[] = this.phase
915
+ for (
916
+ let k: i32 = 0;
917
+ k < toI32(this.slotId.length) && k < toI32(flagsOf.length) && k < toI32(phases.length);
918
+ k++
919
+ ) {
920
+ const flags: i32 = flagsOf[k]
901
921
  if (
902
- this.phase[k] === H3_PHASE_HEADERS &&
922
+ phases[k] === H3_PHASE_HEADERS &&
903
923
  (flags & H3_FLAG_COUNTED) !== 0 &&
904
924
  (flags & skip) === 0 &&
905
925
  http3IsRequest(this.slotId[k])
@@ -986,9 +1006,9 @@ export class Http3Connection {
986
1006
 
987
1007
  /**
988
1008
  * Lends this connection its server's pools in place of its own, giving
989
- * back to the old pools whatever it holds. Panics unless their buffers are
1009
+ * back to the old pools whatever it holds. Pools whose buffers are not
990
1010
  * `bodyChunk` and `maxFieldSectionSize` bytes long, as this connection's
991
- * reads need.
1011
+ * reads need, are not taken: the connection keeps its own.
992
1012
  *
993
1013
  * @param data The pool H3_DATA's buffer is borrowed from from now on; kept, not copied.
994
1014
  * @param section The pool a HEADERS payload's buffer is borrowed from from now on; kept, not copied.
@@ -996,9 +1016,7 @@ export class Http3Connection {
996
1016
  */
997
1017
  share(data: BufferPool, section: BufferPool): void {
998
1018
  if (data.size !== this.config.bodyChunk || section.size !== this.config.maxFieldSectionSize) {
999
- panic(
1000
- `Http3Connection: pools of ${data.size} and ${section.size} bytes shared with a connection of bodyChunk ${this.config.bodyChunk} and maxFieldSectionSize ${this.config.maxFieldSectionSize}`
1001
- )
1019
+ return
1002
1020
  }
1003
1021
  this.giveBack()
1004
1022
  this.dataPool = data
@@ -1109,12 +1127,19 @@ export class Http3Connection {
1109
1127
 
1110
1128
  /** The oldest stream on the `again` ring, taken off it. */
1111
1129
  private takeAgain(): i64 {
1112
- const size: i32 = toI32(this.again.length)
1113
- const id: i64 = this.again[this.againHead]
1114
- this.againHead = (this.againHead + 1) % size
1115
- this.againCount = this.againCount - 1
1130
+ const again: i64[] = this.again
1131
+ const head: i32 = this.againHead
1132
+ const count: i32 = this.againCount
1133
+ // `nextEvent` asks only while the ring holds one, and the head stays on
1134
+ // the ring, so this never returns; H3_NONE is no stream.
1135
+ if (head < 0 || head >= toI32(again.length) || count <= 0) {
1136
+ return H3_NONE
1137
+ }
1138
+ const id: i64 = again[head]
1139
+ this.againHead = head + 1 < toI32(again.length) ? head + 1 : H3_ZERO
1140
+ this.againCount = count - 1
1116
1141
  const k: i32 = this.quic.streams.slotOf(id)
1117
- if (k >= 0 && k < toI32(this.flags.length) && this.slotId[k] === id) {
1142
+ if (k >= 0 && k < toI32(this.flags.length) && k < toI32(this.slotId.length) && this.slotId[k] === id) {
1118
1143
  this.flags[k] = this.flags[k] & ~H3_FLAG_AGAIN
1119
1144
  }
1120
1145
  return id
@@ -1122,19 +1147,53 @@ export class Http3Connection {
1122
1147
 
1123
1148
  /** Puts stream `id`, slot `k`, on the `again` ring, unless it is there. */
1124
1149
  private stepAgain(k: i32, id: i64): void {
1125
- const size: i32 = toI32(this.again.length)
1126
- if ((this.flags[k] & H3_FLAG_AGAIN) !== 0 || this.againCount >= size) {
1150
+ const again: i64[] = this.again
1151
+ const size: i32 = toI32(again.length)
1152
+ const head: i32 = this.againHead
1153
+ const count: i32 = this.againCount
1154
+ // The ring has a place per slot, at most 3,072; a full ring
1155
+ // cannot happen, as a slot is on it at most once.
1156
+ if (
1157
+ k < 0 ||
1158
+ k >= toI32(this.flags.length) ||
1159
+ size > 3072 ||
1160
+ head < 0 ||
1161
+ head >= size ||
1162
+ count < 0 ||
1163
+ count >= size
1164
+ ) {
1165
+ this.fail(H3_INTERNAL_ERROR) // never, as above; a dropped re-step would strand the stream
1166
+ return
1167
+ }
1168
+ if ((this.flags[k] & H3_FLAG_AGAIN) !== 0) {
1169
+ return
1170
+ }
1171
+ // Both below `size`, so their sum wraps onto the ring at most once.
1172
+ if (head >= 3072 || count >= 3072) {
1173
+ this.fail(H3_INTERNAL_ERROR) // never: `size` is at most 3072
1127
1174
  return
1128
1175
  }
1129
- this.again[(this.againHead + this.againCount) % size] = id
1130
- this.againCount = this.againCount + 1
1176
+ const end: i32 = head + count
1177
+ const at: i32 = end < size ? end : end - size
1178
+ if (at < 0 || at >= size) {
1179
+ this.fail(H3_INTERNAL_ERROR) // never: see above
1180
+ return
1181
+ }
1182
+ again[at] = id
1183
+ this.againCount = count + 1
1131
1184
  this.flags[k] = this.flags[k] | H3_FLAG_AGAIN
1132
1185
  }
1133
1186
 
1134
1187
  /** The next event stream `id` has, or H3_NEED_MORE when it has nothing more to say now. */
1135
1188
  private step(id: i64): i32 {
1136
1189
  const k: i32 = this.quic.streams.slotOf(id)
1137
- if (k < 0 || k >= toI32(this.slotId.length)) {
1190
+ const phases: i32[] = this.phase
1191
+ if (
1192
+ k < 0 ||
1193
+ k >= toI32(this.slotId.length) ||
1194
+ k >= toI32(this.flags.length) ||
1195
+ k >= toI32(phases.length)
1196
+ ) {
1138
1197
  return H3_NEED_MORE
1139
1198
  }
1140
1199
  this.slot = k
@@ -1151,7 +1210,10 @@ export class Http3Connection {
1151
1210
  if (this.slotId[k] !== id) {
1152
1211
  this.open(k, id)
1153
1212
  }
1154
- const phase: i32 = this.phase[k]
1213
+ if (k >= toI32(phases.length)) {
1214
+ return H3_NEED_MORE // never: `open` leaves the table as it was
1215
+ }
1216
+ const phase: i32 = phases[k]
1155
1217
  if (phase >= H3_PHASE_UNI_TYPE) {
1156
1218
  return this.uniStep(k, id)
1157
1219
  }
@@ -1160,11 +1222,28 @@ export class Http3Connection {
1160
1222
 
1161
1223
  /** The state of a stream the client just opened in slot `k`. */
1162
1224
  private open(k: i32, id: i64): void {
1225
+ // `step` found `k` in the stream table every per-slot array is as long
1226
+ // as (the constructor), so this never returns.
1227
+ if (
1228
+ k < 0 ||
1229
+ k >= toI32(this.slotId.length) ||
1230
+ k >= toI32(this.frameType.length) ||
1231
+ k >= toI32(this.frameLeft.length) ||
1232
+ k >= toI32(this.contentLeft.length) ||
1233
+ k >= toI32(this.fillLeft.length) ||
1234
+ k >= toI32(this.dataLeft.length) ||
1235
+ k >= toI32(this.flags.length) ||
1236
+ k >= toI32(this.headLen.length) ||
1237
+ k >= toI32(this.pendAt.length) ||
1238
+ k >= toI32(this.pendLen.length) ||
1239
+ k >= toI32(this.phase.length)
1240
+ ) {
1241
+ return
1242
+ }
1163
1243
  this.slotId[k] = id
1164
1244
  this.frameType[k] = H3_SKIP_TYPE
1165
1245
  this.frameLeft[k] = -1
1166
- this.dataTotal[k] = 0
1167
- this.contentLength[k] = -1
1246
+ this.contentLeft[k] = -1
1168
1247
  this.fillLeft[k] = 0
1169
1248
  this.dataLeft[k] = 0
1170
1249
  this.flags[k] = 0
@@ -1186,13 +1265,17 @@ export class Http3Connection {
1186
1265
  return
1187
1266
  }
1188
1267
  this.flags[k] = H3_FLAG_COUNTED
1189
- this.live = this.live + 1
1268
+ // Below the table's size, so `h3Count` never saturates it.
1269
+ this.live = h3Count(this.live)
1190
1270
  }
1191
1271
 
1192
1272
  /** §5.2: a request past the GOAWAY this side sent is refused, both ways, unprocessed. */
1193
1273
  private reject(k: i32, id: i64): void {
1194
1274
  this.quic.streamReset(id, H3_REQUEST_REJECTED)
1195
1275
  this.quic.streamStopSending(id, H3_REQUEST_REJECTED)
1276
+ if (k < 0 || k >= toI32(this.flags.length) || k >= toI32(this.phase.length)) {
1277
+ return // never: `k` is the slot `open` checked
1278
+ }
1196
1279
  this.flags[k] = H3_FLAG_SEND_DONE
1197
1280
  this.phase[k] = H3_PHASE_DISCARD
1198
1281
  this.rejected = h3Count(this.rejected)
@@ -1200,7 +1283,11 @@ export class Http3Connection {
1200
1283
 
1201
1284
  /** One of this side's own streams has news: the client may not stop it (§6.2.1, RFC 9204 §4.2). */
1202
1285
  private ownStream(k: i32): i32 {
1203
- const stream: QuicStream = this.quic.streams.slots[k]
1286
+ const slots: QuicStream[] = this.quic.streams.slots
1287
+ if (k < 0 || k >= toI32(slots.length)) {
1288
+ return H3_NEED_MORE // never: `slotOf` gave `k` from this table
1289
+ }
1290
+ const stream: QuicStream = slots[k]
1204
1291
  if (stream.stopCode >= 0) {
1205
1292
  return this.fail(H3_CLOSED_CRITICAL_STREAM)
1206
1293
  }
@@ -1210,14 +1297,22 @@ export class Http3Connection {
1210
1297
  /** Lets a slot's request count as finished once both of its sides are done. */
1211
1298
  private settle(k: i32): void {
1212
1299
  const done: i32 = H3_FLAG_SEND_DONE | H3_FLAG_RECV_DONE | H3_FLAG_COUNTED
1213
- if ((this.flags[k] & done) === done) {
1300
+ if (k < 0 || k >= toI32(this.flags.length)) {
1301
+ return // never: every caller holds a slot of the table
1302
+ }
1303
+ const live: i32 = this.live
1304
+ // A counted slot is one of `live`, so it is above 0 here.
1305
+ if ((this.flags[k] & done) === done && live > 0) {
1214
1306
  this.flags[k] = this.flags[k] & ~H3_FLAG_COUNTED
1215
- this.live = this.live - 1
1307
+ this.live = live - 1
1216
1308
  }
1217
1309
  }
1218
1310
 
1219
1311
  /** Marks flags on slot `k` and settles it. */
1220
1312
  private mark(k: i32, bits: i32): void {
1313
+ if (k < 0 || k >= toI32(this.flags.length)) {
1314
+ return // never: every caller holds a slot of the table
1315
+ }
1221
1316
  this.flags[k] = this.flags[k] | bits
1222
1317
  this.settle(k)
1223
1318
  }
@@ -1230,25 +1325,46 @@ export class Http3Connection {
1230
1325
  * that ended the read.
1231
1326
  */
1232
1327
  private readHeader(k: i32, id: i64): i32 {
1328
+ // `k` is a slot of the table, at most 3,072 long, so this never returns.
1329
+ const headLens: i32[] = this.headLen
1330
+ if (k < 0 || k >= toI32(headLens.length) || k >= 3072) {
1331
+ return H3_ZERO
1332
+ }
1233
1333
  const base: i32 = k * H3_HEAD
1234
- while (this.headLen[k] < H3_HEAD) {
1235
- const have: i32 = this.headLen[k]
1334
+ let have: i32 = headLens[k]
1335
+ // 16 is `H3_HEAD`, spelled out so the bound reaches the sums below.
1336
+ while (have >= 0 && have < 16) {
1236
1337
  // The type's size from its first byte, then the length's from its.
1237
1338
  let need: i32 = 1
1238
1339
  if (have > 0) {
1239
1340
  const typeSize: i32 = h3VarintLength(this.head, base)
1341
+ // A varint is 1 to 8 bytes, so neither size ever returns here.
1342
+ if (typeSize < 1 || typeSize > 8) {
1343
+ return H3_ZERO
1344
+ }
1240
1345
  need = have < typeSize ? typeSize - have : 1
1241
1346
  if (have > typeSize) {
1242
- need = typeSize + h3VarintLength(this.head, base + typeSize) - have
1347
+ const lengthSize: i32 = h3VarintLength(this.head, base + typeSize)
1348
+ if (lengthSize < 1 || lengthSize > 8) {
1349
+ return H3_ZERO
1350
+ }
1351
+ need = typeSize + lengthSize - have
1243
1352
  }
1244
1353
  }
1245
1354
  const n: i32 = this.quic.streamRead(id, this.head, base + have, need)
1246
1355
  if (n <= 0) {
1247
1356
  return n
1248
1357
  }
1249
- this.headLen[k] = have + n
1250
- if (h3ReadFrameHeader(this.header, this.head, base, this.headLen[k]) > 0) {
1251
- this.headLen[k] = 0
1358
+ // Never: a read takes at most the bytes asked for, into the table's slot.
1359
+ if (n > need || n > 16 || k >= toI32(headLens.length)) {
1360
+ return H3_ZERO
1361
+ }
1362
+ have = have + n
1363
+ headLens[k] = have
1364
+ if (h3ReadFrameHeader(this.header, this.head, base, have) > 0) {
1365
+ if (k < toI32(headLens.length)) {
1366
+ headLens[k] = 0
1367
+ }
1252
1368
  return 1
1253
1369
  }
1254
1370
  }
@@ -1259,22 +1375,38 @@ export class Http3Connection {
1259
1375
 
1260
1376
  /** The next event of the client's unidirectional stream `id` in slot `k`. */
1261
1377
  private uniStep(k: i32, id: i64): i32 {
1378
+ const phases: i32[] = this.phase
1379
+ const headLens: i32[] = this.headLen
1262
1380
  while (this.state !== H3_STATE_FAILED) {
1263
- const phase: i32 = this.phase[k]
1381
+ // `step` found `k` in the table, at most 3,072 long, and
1382
+ // nothing resizes it, so this never returns.
1383
+ if (k < 0 || k >= toI32(phases.length) || k >= toI32(headLens.length) || k >= 3072) {
1384
+ return H3_NEED_MORE
1385
+ }
1386
+ const phase: i32 = phases[k]
1264
1387
  if (phase === H3_PHASE_UNI_TYPE) {
1265
1388
  const base: i32 = k * H3_HEAD
1266
- const n: i32 = this.quic.streamRead(id, this.head, base + this.headLen[k], H3_ONE)
1389
+ const have: i32 = headLens[k]
1390
+ // A type is a varint, read a byte at a time until it is whole: at
1391
+ // most 8 of the slot's 16 (`H3_HEAD`), so this never returns.
1392
+ if (have < 0 || have >= 16) {
1393
+ return H3_NEED_MORE
1394
+ }
1395
+ const n: i32 = this.quic.streamRead(id, this.head, base + have, H3_ONE)
1396
+ if (k >= toI32(phases.length) || k >= toI32(headLens.length)) {
1397
+ return H3_NEED_MORE // never, as above
1398
+ }
1267
1399
  if (n !== 1) {
1268
1400
  // §6.2: a stream may end or be reset before its type; nothing is owed.
1269
1401
  if (n < 0) {
1270
- this.phase[k] = H3_PHASE_DONE
1402
+ phases[k] = H3_PHASE_DONE
1271
1403
  }
1272
1404
  return H3_NEED_MORE
1273
1405
  }
1274
- this.headLen[k] = this.headLen[k] + 1
1275
- const type: i64 = h3ReadVarint(this.head, base, base + this.headLen[k])
1276
- if (type >= 0) {
1277
- this.headLen[k] = 0
1406
+ headLens[k] = have + 1
1407
+ const type: i64 = h3ReadVarint(this.head, base, base + have + 1)
1408
+ if (type >= 0 && k < toI32(headLens.length)) {
1409
+ headLens[k] = 0
1278
1410
  const event: i32 = this.typed(k, id, type)
1279
1411
  if (event !== H3_NEED_MORE) {
1280
1412
  return event
@@ -1308,6 +1440,9 @@ export class Http3Connection {
1308
1440
 
1309
1441
  /** The client's unidirectional stream `id` turned out to be of `type` (§6.2). */
1310
1442
  private typed(k: i32, id: i64, type: i64): i32 {
1443
+ if (k < 0 || k >= toI32(this.phase.length) || k >= toI32(this.flags.length)) {
1444
+ return H3_NEED_MORE // never: `uniStep` holds a slot of the table
1445
+ }
1311
1446
  if (type === H3_STREAM_CONTROL) {
1312
1447
  if (this.peerControl >= 0) {
1313
1448
  return this.fail(H3_STREAM_CREATION_ERROR)
@@ -1351,6 +1486,9 @@ export class Http3Connection {
1351
1486
  /** Frames on the client's control stream (§6.2.1, §7.2). */
1352
1487
  private controlStep(k: i32, id: i64): i32 {
1353
1488
  while (this.state !== H3_STATE_FAILED) {
1489
+ if (k < 0 || k >= toI32(this.frameLeft.length) || k >= toI32(this.frameType.length)) {
1490
+ return H3_NEED_MORE // never: `uniStep` holds a slot of the table
1491
+ }
1354
1492
  if (this.frameLeft[k] < 0) {
1355
1493
  const got: i32 = this.readHeader(k, id)
1356
1494
  if (got === 0) {
@@ -1381,28 +1519,40 @@ export class Http3Connection {
1381
1519
  if (whole && length > toI64(H3_CONTROL_FRAME_MAX)) {
1382
1520
  return this.fail(type === H3_FRAME_SETTINGS ? H3_EXCESSIVE_LOAD : H3_FRAME_ERROR)
1383
1521
  }
1522
+ if (k >= toI32(this.frameLeft.length) || k >= toI32(this.frameType.length)) {
1523
+ return H3_NEED_MORE // never: nothing resizes the table
1524
+ }
1384
1525
  this.frameType[k] = whole ? type : H3_SKIP_TYPE
1385
1526
  this.frameLeft[k] = length
1386
1527
  this.controlFill = 0
1387
1528
  }
1388
- if (this.frameLeft[k] > 0) {
1529
+ const left: i64 = this.frameLeft[k]
1530
+ const fill: i32 = this.controlFill
1531
+ // `controlFill` stays within `control`, H3_CONTROL_FRAME_MAX (1024) bytes long.
1532
+ if (left > 0 && fill >= 0 && fill <= 1024 && fill <= toI32(this.control.length)) {
1389
1533
  const skip: boolean = this.frameType[k] === H3_SKIP_TYPE
1390
- const room: i32 = skip ? this.config.bodyChunk : toI32(this.control.length) - this.controlFill
1391
- const want: i32 = toI32(http3Min(this.frameLeft[k], toI64(room)))
1392
- const n: i32 = skip
1393
- ? this.readData(id, want)
1394
- : this.quic.streamRead(id, this.control, this.controlFill, want)
1534
+ const room: i32 = skip ? this.config.bodyChunk : toI32(this.control.length) - fill
1535
+ const want: i32 = toI32(http3Min(left, toI64(room)))
1536
+ const n: i32 = skip ? this.readData(id, want) : this.quic.streamRead(id, this.control, fill, want)
1395
1537
  if (n === 0) {
1396
1538
  return H3_NEED_MORE
1397
1539
  }
1398
1540
  if (n < 0) {
1399
1541
  return this.fail(H3_CLOSED_CRITICAL_STREAM)
1400
1542
  }
1401
- this.frameLeft[k] = this.frameLeft[k] - toI64(n)
1402
- if (this.frameType[k] !== H3_SKIP_TYPE) {
1403
- this.controlFill = this.controlFill + n
1543
+ // Never: a read takes at most the bytes asked for, `room` at most
1544
+ // `control`'s 1024 when it fills that, and nothing resizes the table.
1545
+ if (n > want || n > room || k >= toI32(this.frameLeft.length)) {
1546
+ return this.fail(H3_INTERNAL_ERROR)
1547
+ }
1548
+ this.frameLeft[k] = left - toI64(n)
1549
+ if (!skip && n <= 1024) {
1550
+ this.controlFill = fill + n
1404
1551
  }
1405
1552
  }
1553
+ if (k >= toI32(this.frameLeft.length) || k >= toI32(this.frameType.length)) {
1554
+ return H3_NEED_MORE // never, as above
1555
+ }
1406
1556
  if (this.frameLeft[k] === 0) {
1407
1557
  this.frameLeft[k] = -1
1408
1558
  const event: i32 = this.controlFrame(this.frameType[k])
@@ -1430,7 +1580,12 @@ export class Http3Connection {
1430
1580
  for (let j: i32 = 0; j < this.waitCount && j < toI32(this.waiting.length); j++) {
1431
1581
  const id: i64 = this.waiting[j]
1432
1582
  const w: i32 = this.quic.streams.slotOf(id)
1433
- if (w >= 0 && w < toI32(this.flags.length) && this.slotId[w] === id) {
1583
+ if (
1584
+ w >= 0 &&
1585
+ w < toI32(this.flags.length) &&
1586
+ w < toI32(this.slotId.length) &&
1587
+ this.slotId[w] === id
1588
+ ) {
1434
1589
  this.flags[w] = this.flags[w] & ~H3_FLAG_WAITING
1435
1590
  this.stepAgain(w, id)
1436
1591
  }
@@ -1470,7 +1625,7 @@ export class Http3Connection {
1470
1625
  /** Reads and drops what is left of stream `id`, slot `k`, to its end or reset. */
1471
1626
  private discard(k: i32, id: i64): i32 {
1472
1627
  const n: i32 = this.drop(id)
1473
- if (n < 0) {
1628
+ if (n < 0 && k >= 0 && k < toI32(this.phase.length)) {
1474
1629
  this.phase[k] = H3_PHASE_DONE
1475
1630
  this.mark(k, H3_FLAG_RECV_DONE)
1476
1631
  }
@@ -1504,6 +1659,9 @@ export class Http3Connection {
1504
1659
  this.rejected = h3Count(this.rejected)
1505
1660
  return H3_NEED_MORE
1506
1661
  }
1662
+ if (k < 0 || k >= toI32(this.phase.length) || k >= toI32(this.frameLeft.length)) {
1663
+ return H3_NEED_MORE // never: `wtStep` holds a slot of the table
1664
+ }
1507
1665
  this.phase[k] = H3_PHASE_WT_HELD
1508
1666
  this.frameLeft[k] = -1
1509
1667
  this.stream = id
@@ -1518,7 +1676,18 @@ export class Http3Connection {
1518
1676
  * client's STOP_SENDING; and H3_WRITABLE.
1519
1677
  */
1520
1678
  private wtStep(k: i32, id: i64): i32 {
1521
- const stream: QuicStream = this.quic.streams.slots[k]
1679
+ const slots: QuicStream[] = this.quic.streams.slots
1680
+ // `step` found `k` in the table, at most 3,072 long, so this never returns.
1681
+ if (
1682
+ k < 0 ||
1683
+ k >= toI32(slots.length) ||
1684
+ k >= toI32(this.flags.length) ||
1685
+ k >= toI32(this.phase.length) ||
1686
+ k >= 3072
1687
+ ) {
1688
+ return H3_NEED_MORE
1689
+ }
1690
+ const stream: QuicStream = slots[k]
1522
1691
  if (stream.stopCode >= 0 && (this.flags[k] & H3_FLAG_SEND_DONE) === 0) {
1523
1692
  this.mark(k, H3_FLAG_SEND_DONE)
1524
1693
  this.stream = id
@@ -1528,20 +1697,30 @@ export class Http3Connection {
1528
1697
  const phase: i32 = this.phase[k]
1529
1698
  if (phase === H3_PHASE_WT_SESSION) {
1530
1699
  const base: i32 = k * H3_HEAD
1531
- while (this.headLen[k] < 8) {
1532
- const n: i32 = this.quic.streamRead(id, this.head, base + this.headLen[k], H3_ONE)
1700
+ const headLens: i32[] = this.headLen
1701
+ const phases: i32[] = this.phase
1702
+ // Never past the table, which nothing resizes; a session ID is a varint, at most 8 bytes.
1703
+ while (k < 3072 && k < toI32(headLens.length) && k < toI32(phases.length)) {
1704
+ const have: i32 = headLens[k]
1705
+ if (have < 0 || have >= 8) {
1706
+ return H3_NEED_MORE
1707
+ }
1708
+ const n: i32 = this.quic.streamRead(id, this.head, base + have, H3_ONE)
1533
1709
  if (n !== 1) {
1534
1710
  // Ended or reset before its session ID: nothing is owed (§6.2).
1535
- if (n < 0) {
1536
- this.phase[k] = H3_PHASE_DONE
1711
+ if (n < 0 && k < toI32(phases.length)) {
1712
+ phases[k] = H3_PHASE_DONE
1537
1713
  this.mark(k, H3_FLAG_RECV_DONE)
1538
1714
  }
1539
1715
  return H3_NEED_MORE
1540
1716
  }
1541
- this.headLen[k] = this.headLen[k] + 1
1542
- const session: i64 = h3ReadVarint(this.head, base, base + this.headLen[k])
1717
+ if (k >= toI32(headLens.length)) {
1718
+ return H3_NEED_MORE // never, as above
1719
+ }
1720
+ headLens[k] = have + 1
1721
+ const session: i64 = h3ReadVarint(this.head, base, base + have + 1)
1543
1722
  if (session >= 0) {
1544
- this.headLen[k] = 0
1723
+ headLens[k] = 0
1545
1724
  return this.held(k, id, session)
1546
1725
  }
1547
1726
  }
@@ -1558,7 +1737,9 @@ export class Http3Connection {
1558
1737
  return H3_DATA
1559
1738
  }
1560
1739
  if (n < 0) {
1561
- this.phase[k] = H3_PHASE_DONE
1740
+ if (k < toI32(this.phase.length)) {
1741
+ this.phase[k] = H3_PHASE_DONE
1742
+ }
1562
1743
  this.mark(k, H3_FLAG_RECV_DONE)
1563
1744
  if (n === QUIC_STREAM_END) {
1564
1745
  this.stream = id
@@ -1586,6 +1767,8 @@ export class Http3Connection {
1586
1767
  if (
1587
1768
  k < 0 ||
1588
1769
  k >= toI32(this.slotId.length) ||
1770
+ k >= toI32(this.flags.length) ||
1771
+ k >= toI32(this.phase.length) ||
1589
1772
  this.slotId[k] !== id ||
1590
1773
  (this.flags[k] & H3_FLAG_WT) === 0
1591
1774
  ) {
@@ -1605,10 +1788,12 @@ export class Http3Connection {
1605
1788
  */
1606
1789
  acceptStream(id: i64): i32 {
1607
1790
  const k: i32 = this.wtSlot(id)
1608
- if (k < 0 || this.phase[k] !== H3_PHASE_WT_HELD) {
1791
+ const phases: i32[] = this.phase
1792
+ // `wtSlot` answers -1 or a slot of the table, so the length test never refuses.
1793
+ if (k < 0 || k >= toI32(phases.length) || phases[k] !== H3_PHASE_WT_HELD) {
1609
1794
  return H3_CLOSED
1610
1795
  }
1611
- this.phase[k] = H3_PHASE_WT
1796
+ phases[k] = H3_PHASE_WT
1612
1797
  this.stepAgain(k, id)
1613
1798
  return 0
1614
1799
  }
@@ -1636,9 +1821,13 @@ export class Http3Connection {
1636
1821
 
1637
1822
  /** Asks the client to stop stream `id`, slot `k`, with `code`, and drops what it still sends, unless its side is over or being dropped. */
1638
1823
  private stopReading(k: i32, id: i64, code: i64): void {
1639
- if (this.phase[k] !== H3_PHASE_DONE && this.phase[k] !== H3_PHASE_DISCARD) {
1824
+ const phases: i32[] = this.phase
1825
+ // Every caller holds a slot of the table, so the length test never skips.
1826
+ if (k >= 0 && k < toI32(phases.length) && phases[k] !== H3_PHASE_DONE && phases[k] !== H3_PHASE_DISCARD) {
1640
1827
  this.quic.streamStopSending(id, code)
1641
- this.phase[k] = H3_PHASE_DISCARD
1828
+ if (k < toI32(phases.length)) {
1829
+ phases[k] = H3_PHASE_DISCARD
1830
+ }
1642
1831
  this.stepAgain(k, id)
1643
1832
  }
1644
1833
  }
@@ -1668,6 +1857,9 @@ export class Http3Connection {
1668
1857
  return toI64(H3_AGAIN)
1669
1858
  }
1670
1859
  this.open(k, id)
1860
+ if (k >= toI32(this.flags.length) || k >= toI32(this.phase.length)) {
1861
+ return toI64(H3_AGAIN) // never: the table matches `slotId`, and nothing resizes it
1862
+ }
1671
1863
  // A unidirectional stream of this side's has no side for the client to write.
1672
1864
  this.flags[k] = bidirectional ? H3_FLAG_WT : H3_FLAG_WT | H3_FLAG_RECV_DONE
1673
1865
  this.phase[k] = bidirectional ? H3_PHASE_WT : H3_PHASE_DONE
@@ -1704,11 +1896,15 @@ export class Http3Connection {
1704
1896
  writeStream(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1705
1897
  h3CheckWindow("Http3Connection.writeStream", buf, off, len)
1706
1898
  const k: i32 = this.wtSlot(id)
1899
+ const slots: QuicStream[] = this.quic.streams.slots
1707
1900
  if (
1708
1901
  k < 0 ||
1902
+ k >= toI32(this.flags.length) ||
1903
+ k >= toI32(this.phase.length) ||
1904
+ k >= toI32(slots.length) ||
1709
1905
  (this.flags[k] & H3_FLAG_SEND_DONE) !== 0 ||
1710
1906
  this.phase[k] === H3_PHASE_WT_HELD ||
1711
- this.quic.streams.slots[k].stopCode >= 0
1907
+ slots[k].stopCode >= 0
1712
1908
  ) {
1713
1909
  return H3_CLOSED
1714
1910
  }
@@ -1770,8 +1966,16 @@ export class Http3Connection {
1770
1966
 
1771
1967
  /** The next event of request stream `id` in slot `k`. */
1772
1968
  private requestStep(k: i32, id: i64): i32 {
1773
- const stream: QuicStream = this.quic.streams.slots[k]
1969
+ const slots: QuicStream[] = this.quic.streams.slots
1970
+ if (k < 0 || k >= toI32(slots.length)) {
1971
+ return this.fail(H3_INTERNAL_ERROR) // never: `step` found `k` in the table
1972
+ }
1973
+ const stream: QuicStream = slots[k]
1774
1974
  while (this.state !== H3_STATE_FAILED) {
1975
+ // Never: the table is as long as `slotId`, and nothing resizes it.
1976
+ if (k >= toI32(this.phase.length) || k >= toI32(this.flags.length)) {
1977
+ return this.fail(H3_INTERNAL_ERROR)
1978
+ }
1775
1979
  const phase: i32 = this.phase[k]
1776
1980
  if (stream.stopCode >= 0 && (this.flags[k] & H3_FLAG_SEND_DONE) === 0) {
1777
1981
  // The client stopped the response (QUIC answered with RESET_STREAM, §3.5 of RFC 9000).
@@ -1782,7 +1986,7 @@ export class Http3Connection {
1782
1986
  return charged
1783
1987
  }
1784
1988
  if (told) {
1785
- if (phase !== H3_PHASE_DONE) {
1989
+ if (phase !== H3_PHASE_DONE && k < toI32(this.phase.length)) {
1786
1990
  this.phase[k] = H3_PHASE_DISCARD
1787
1991
  }
1788
1992
  return this.resetEvent(id, stream.stopCode, true)
@@ -1794,16 +1998,26 @@ export class Http3Connection {
1794
1998
  if (phase === H3_PHASE_DISCARD) {
1795
1999
  return this.discard(k, id)
1796
2000
  }
2001
+ if (
2002
+ k >= toI32(this.flags.length) ||
2003
+ k >= toI32(this.headLen.length) ||
2004
+ k >= toI32(this.frameLeft.length) ||
2005
+ k >= toI32(this.frameType.length)
2006
+ ) {
2007
+ return this.fail(H3_INTERNAL_ERROR) // never, as above
2008
+ }
1797
2009
  if (this.webtransport && !this.settingsSeen && phase === H3_PHASE_HEADERS && this.headLen[k] === 0) {
1798
2010
  // draft-02 §3.1: with WebTransport on, a request waits for the client's SETTINGS, which say whether it may be a session.
1799
- if ((this.flags[k] & H3_FLAG_WAITING) === 0 && this.waitCount < toI32(this.waiting.length)) {
1800
- this.waiting[this.waitCount] = id
1801
- this.waitCount = this.waitCount + 1
2011
+ const count: i32 = this.waitCount
2012
+ if ((this.flags[k] & H3_FLAG_WAITING) === 0 && count >= 0 && count < toI32(this.waiting.length)) {
2013
+ this.waiting[count] = id
2014
+ this.waitCount = count + 1
1802
2015
  this.flags[k] = this.flags[k] | H3_FLAG_WAITING
1803
2016
  }
1804
2017
  return H3_NEED_MORE
1805
2018
  }
1806
- if (this.frameLeft[k] < 0) {
2019
+ const left: i64 = this.frameLeft[k]
2020
+ if (left < 0) {
1807
2021
  const event: i32 = this.requestFrame(k, id)
1808
2022
  if (event === H3_CONTINUE) {
1809
2023
  continue
@@ -1812,7 +2026,7 @@ export class Http3Connection {
1812
2026
  return event
1813
2027
  }
1814
2028
  // The header is not all in yet, unless the stream moved on to another phase.
1815
- if (this.phase[k] === phase) {
2029
+ if (k < toI32(this.phase.length) && this.phase[k] === phase) {
1816
2030
  return this.writable(k, id)
1817
2031
  }
1818
2032
  continue
@@ -1820,13 +2034,16 @@ export class Http3Connection {
1820
2034
  const type: i64 = this.frameType[k]
1821
2035
  if (type === H3_FRAME_HEADERS) {
1822
2036
  // A field section is decoded whole: wait until all of it is in.
1823
- if (stream.resetCode < 0 && stream.readable() < this.frameLeft[k]) {
1824
- if (stream.recvFinal >= 0 && stream.recvRead + this.frameLeft[k] > stream.recvFinal) {
2037
+ if (stream.resetCode < 0 && stream.readable() < left) {
2038
+ const read: i64 = stream.recvRead
2039
+ const final: i64 = stream.recvFinal
2040
+ // The section runs past the stream's final size. `recvRead` is never negative.
2041
+ if (final >= 0 && read >= 0 && left > final - read) {
1825
2042
  return this.fail(H3_FRAME_ERROR)
1826
2043
  }
1827
2044
  return this.writable(k, id)
1828
2045
  }
1829
- const length: i32 = toI32(this.frameLeft[k])
2046
+ const length: i32 = toI32(left)
1830
2047
  const n: i32 = this.quic.streamRead(id, this.borrowSection(), H3_ZERO, length)
1831
2048
  if (n < 0) {
1832
2049
  this.giveSection(H3_ZERO)
@@ -1836,7 +2053,9 @@ export class Http3Connection {
1836
2053
  this.giveSection(n)
1837
2054
  return this.writable(k, id)
1838
2055
  }
1839
- this.frameLeft[k] = -1
2056
+ if (k < toI32(this.frameLeft.length)) {
2057
+ this.frameLeft[k] = -1 // always: nothing resizes the table
2058
+ }
1840
2059
  // `fieldSection` copies what it keeps into `fields`: the section is not read after it.
1841
2060
  const event: i32 = this.fieldSection(k, id, length)
1842
2061
  this.giveSection(length)
@@ -1846,7 +2065,7 @@ export class Http3Connection {
1846
2065
  continue
1847
2066
  }
1848
2067
  // DATA, a payload being skipped, or an unknown frame's.
1849
- const want: i32 = toI32(http3Min(this.frameLeft[k], toI64(this.config.bodyChunk)))
2068
+ const want: i32 = toI32(http3Min(left, toI64(this.config.bodyChunk)))
1850
2069
  const resetCode: i64 = stream.resetCode
1851
2070
  const n: i32 = this.readData(id, want)
1852
2071
  if (n === 0) {
@@ -1855,21 +2074,34 @@ export class Http3Connection {
1855
2074
  if (n < 0) {
1856
2075
  return n === QUIC_STREAM_END ? this.fail(H3_FRAME_ERROR) : this.ended(k, id, n, resetCode)
1857
2076
  }
1858
- this.frameLeft[k] = this.frameLeft[k] - toI64(n)
2077
+ // Never: nothing resizes the table.
2078
+ if (
2079
+ k >= toI32(this.frameLeft.length) ||
2080
+ k >= toI32(this.contentLeft.length) ||
2081
+ k >= toI32(this.phase.length)
2082
+ ) {
2083
+ return this.fail(H3_INTERNAL_ERROR)
2084
+ }
2085
+ const rest: i64 = left - toI64(n)
2086
+ this.frameLeft[k] = rest
1859
2087
  if (type === H3_FRAME_DATA) {
1860
- if (this.frameLeft[k] === 0) {
2088
+ if (rest === 0) {
1861
2089
  this.frameLeft[k] = -1
1862
2090
  }
1863
- this.dataTotal[k] = this.dataTotal[k] + toI64(n)
1864
- if (this.contentLength[k] >= 0 && this.dataTotal[k] > this.contentLength[k]) {
1865
- return this.streamError(k, id, H3_MESSAGE_ERROR)
2091
+ const owed: i64 = this.contentLeft[k]
2092
+ if (owed >= 0) {
2093
+ if (toI64(n) > owed) {
2094
+ // §4.1.2: the body is longer than its content-length.
2095
+ return this.streamError(k, id, H3_MESSAGE_ERROR)
2096
+ }
2097
+ this.contentLeft[k] = owed - toI64(n)
1866
2098
  }
1867
2099
  this.stream = id
1868
2100
  this.dataStart = 0
1869
2101
  this.dataLength = n
1870
2102
  return H3_DATA
1871
2103
  }
1872
- if (this.frameLeft[k] === 0) {
2104
+ if (rest === 0) {
1873
2105
  this.frameLeft[k] = -1
1874
2106
  if (this.phase[k] === H3_PHASE_SKIP) {
1875
2107
  return this.answerTooLarge(k, id)
@@ -1886,12 +2118,25 @@ export class Http3Connection {
1886
2118
  * RFC 9114 says so. A stream that ends here ends the request.
1887
2119
  */
1888
2120
  private requestFrame(k: i32, id: i64): i32 {
1889
- const stream: QuicStream = this.quic.streams.slots[k]
1890
- const resetCode: i64 = stream.resetCode
2121
+ const slots: QuicStream[] = this.quic.streams.slots
2122
+ if (k < 0 || k >= toI32(slots.length)) {
2123
+ return this.fail(H3_INTERNAL_ERROR) // never: `requestStep` holds a slot of the table
2124
+ }
2125
+ const resetCode: i64 = slots[k].resetCode
1891
2126
  const got: i32 = this.readHeader(k, id)
1892
2127
  if (got === 0) {
1893
2128
  return H3_NEED_MORE
1894
2129
  }
2130
+ // Never: the table is as long as `slotId`, and nothing resizes it.
2131
+ if (
2132
+ k >= toI32(this.headLen.length) ||
2133
+ k >= toI32(this.flags.length) ||
2134
+ k >= toI32(this.phase.length) ||
2135
+ k >= toI32(this.frameType.length) ||
2136
+ k >= toI32(this.frameLeft.length)
2137
+ ) {
2138
+ return this.fail(H3_INTERNAL_ERROR)
2139
+ }
1895
2140
  if (got < 0) {
1896
2141
  if (got === QUIC_STREAM_END && this.headLen[k] > 0) {
1897
2142
  return this.fail(H3_FRAME_ERROR)
@@ -1913,9 +2158,9 @@ export class Http3Connection {
1913
2158
  // Its "length" is the session ID. A stream past this side's GOAWAY counts once it is held, for a session below it.
1914
2159
  this.flags[k] = this.flags[k] | H3_FLAG_WT
1915
2160
  const event: i32 = this.held(k, id, length)
1916
- if (event === H3_STREAM && (this.flags[k] & H3_FLAG_LATE) !== 0) {
2161
+ if (event === H3_STREAM && k < toI32(this.flags.length) && (this.flags[k] & H3_FLAG_LATE) !== 0) {
1917
2162
  this.flags[k] = (this.flags[k] & ~H3_FLAG_LATE) | H3_FLAG_COUNTED
1918
- this.live = this.live + 1
2163
+ this.live = h3Count(this.live)
1919
2164
  }
1920
2165
  return event
1921
2166
  }
@@ -1964,6 +2209,9 @@ export class Http3Connection {
1964
2209
  ) {
1965
2210
  return this.fail(H3_FRAME_UNEXPECTED)
1966
2211
  }
2212
+ if (k >= toI32(this.frameType.length) || k >= toI32(this.frameLeft.length)) {
2213
+ return this.fail(H3_INTERNAL_ERROR) // never, as above
2214
+ }
1967
2215
  // §9: an unknown frame type is skipped, wherever it comes.
1968
2216
  this.frameType[k] = H3_SKIP_TYPE
1969
2217
  this.frameLeft[k] = length > 0 ? length : -1
@@ -1977,6 +2225,9 @@ export class Http3Connection {
1977
2225
  */
1978
2226
  private fieldSection(k: i32, id: i64, length: i32): i32 {
1979
2227
  const result: i64 = this.decoder.decode(this.section, H3_ZERO, length)
2228
+ if (k < 0 || k >= toI32(this.phase.length)) {
2229
+ return this.fail(H3_INTERNAL_ERROR) // never: `requestStep` holds a slot of the table, which nothing resizes
2230
+ }
1980
2231
  const first: boolean = this.phase[k] === H3_PHASE_HEADERS
1981
2232
  if (result === QPACK_SECTION_TOO_LARGE) {
1982
2233
  return first ? this.answerTooLarge(k, id) : this.streamError(k, id, H3_EXCESSIVE_LOAD)
@@ -1984,11 +2235,16 @@ export class Http3Connection {
1984
2235
  if (result !== QPACK_OK) {
1985
2236
  return this.fail(result)
1986
2237
  }
1987
- this.copyFields()
2238
+ if (!this.copyFields()) {
2239
+ return this.fail(H3_INTERNAL_ERROR) // never: see `copyOf`
2240
+ }
1988
2241
  if (!first) {
1989
2242
  if (this.fields.readTrailers(this.nameList, this.valueList) !== HTTP_FIELDS_OK) {
1990
2243
  return this.streamError(k, id, H3_MESSAGE_ERROR)
1991
2244
  }
2245
+ if (k >= toI32(this.phase.length)) {
2246
+ return this.fail(H3_INTERNAL_ERROR) // never, as above
2247
+ }
1992
2248
  this.phase[k] = H3_PHASE_TRAILED
1993
2249
  this.stream = id
1994
2250
  return H3_TRAILERS
@@ -1998,55 +2254,104 @@ export class Http3Connection {
1998
2254
  }
1999
2255
  // An extended CONNECT reaches the program like any request, `fields.protocol` set:
2000
2256
  // `nish/net/webtransport` makes a session of one whose protocol is `webtransport`.
2001
- this.contentLength[k] = this.fields.contentLength
2257
+ if (
2258
+ k >= toI32(this.contentLeft.length) ||
2259
+ k >= toI32(this.phase.length) ||
2260
+ k >= toI32(this.flags.length)
2261
+ ) {
2262
+ return this.fail(H3_INTERNAL_ERROR) // never, as above
2263
+ }
2264
+ this.contentLeft[k] = this.fields.contentLength
2002
2265
  this.phase[k] = H3_PHASE_BODY
2003
2266
  this.flags[k] = this.flags[k] | H3_FLAG_DELIVERED
2004
- this.heads = this.heads < 2147483647 ? this.heads + 1 : H3_ZERO
2267
+ const heads: i32 = this.heads
2268
+ this.heads = heads >= 0 && heads < 2147483647 ? heads + 1 : H3_ZERO
2005
2269
  this.stream = id
2006
2270
  return H3_REQUEST
2007
2271
  }
2008
2272
 
2009
- /** Copies the decoder's fields into the connection's own arrays and the lists `fields` reads. */
2010
- private copyFields(): void {
2273
+ /**
2274
+ * Copies the decoder's fields into the connection's own arrays and the lists `fields` reads.
2275
+ * False, both lists emptied, if a copy could not be made (never: see `copyOf`).
2276
+ */
2277
+ private copyFields(): boolean {
2011
2278
  const d: QpackDecoder = this.decoder
2012
2279
  http3EmptyList(this.nameList)
2013
2280
  http3EmptyList(this.valueList)
2014
2281
  this.poolUsed.fill(H3_ZERO)
2015
- for (let i: i32 = 0; i < d.count; i++) {
2016
- this.nameList.push(this.copyOf(d, d.nameStart[i], d.nameLength[i]))
2017
- this.valueList.push(this.copyOf(d, d.valueStart[i], d.valueLength[i]))
2282
+ // The decoder's four arrays hold `count` fields each, so the length tests never end the loop early.
2283
+ for (
2284
+ let i: i32 = 0;
2285
+ i < d.count &&
2286
+ i < toI32(d.nameStart.length) &&
2287
+ i < toI32(d.nameLength.length) &&
2288
+ i < toI32(d.valueStart.length) &&
2289
+ i < toI32(d.valueLength.length);
2290
+ i++
2291
+ ) {
2292
+ const valueStart: i32 = d.valueStart[i]
2293
+ const valueLength: i32 = d.valueLength[i]
2294
+ if (
2295
+ !this.copyOf(this.nameList, d, d.nameStart[i], d.nameLength[i]) ||
2296
+ !this.copyOf(this.valueList, d, valueStart, valueLength)
2297
+ ) {
2298
+ http3EmptyList(this.nameList)
2299
+ http3EmptyList(this.valueList)
2300
+ return false
2301
+ }
2018
2302
  }
2303
+ return true
2019
2304
  }
2020
2305
 
2021
2306
  /**
2022
- * A copy of the decoder's bytes `[start, start + length)`, in an array of
2023
- * the size class `length` falls in: class `j` holds arrays made with room
2024
- * for 2^j bytes, for strings shorter than that and at least half as long.
2025
- * A section takes the arrays of each class in order and the next takes the
2026
- * same ones again, so a class grows only when one section needs more of its
2027
- * size than any before it, and the total stays bounded by the field-section
2028
- * cap (`docs/security/http3.md`, H3-2).
2307
+ * Pushes onto `list` a copy of the decoder's bytes `[start, start + length)`,
2308
+ * in an array of the size class `length` falls in: class `j` holds arrays
2309
+ * made with room for 2^j bytes, for strings shorter than that and at least
2310
+ * half as long. A section takes the arrays of each class in order and the
2311
+ * next takes the same ones again, so a class grows only when one section
2312
+ * needs more of its size than any before it, and the total stays bounded by
2313
+ * the field-section cap (`docs/security/http3.md`, H3-2). False, pushing nothing, where a
2314
+ * guard below found the pools not as built (never).
2029
2315
  */
2030
- private copyOf(d: QpackDecoder, start: i32, length: i32): u8[] {
2316
+ private copyOf(list: u8[][], d: QpackDecoder, start: i32, length: i32): boolean {
2031
2317
  let j: i32 = 0
2032
- while (j < H3_FIELD_CLASSES - 1 && 1 << j <= length) {
2318
+ // 23 is H3_FIELD_CLASSES - 1, spelled out so the prover bounds `1 << j`.
2319
+ while (j < 23 && 1 << j <= length) {
2033
2320
  j++
2034
2321
  }
2035
- const pool: u8[][] = this.pools[j]
2036
- const used: i32 = this.poolUsed[j]
2322
+ const pools: u8[][][] = this.pools
2323
+ const poolUsed: i32[] = this.poolUsed
2324
+ // Never: both hold H3_FIELD_CLASSES entries, and `used` counts the arrays of its class this
2325
+ // section took, at most one past the class's last.
2326
+ if (j < 0 || j > 23 || j >= toI32(pools.length) || j >= toI32(poolUsed.length)) {
2327
+ return false
2328
+ }
2329
+ const pool: u8[][] = pools[j]
2330
+ const used: i32 = poolUsed[j]
2331
+ if (used < 0 || used > toI32(pool.length) || used >= 2147483646) {
2332
+ return false // never, as above
2333
+ }
2334
+ poolUsed[j] = used + 1
2335
+ if (used === toI32(pool.length)) {
2336
+ // Made with room for the class, then emptied below like any reused one. A `u16` length
2337
+ // needs no check, and holds every class the default 16,384-byte cap reaches; a class past
2338
+ // 2^15, under a larger cap, starts at 2^15 and grows on the pushes below.
2339
+ const room: i32 = j <= 15 ? 1 << j : 32768
2340
+ pool.push(new Array<u8>(toU16(room)))
2341
+ }
2037
2342
  if (used >= toI32(pool.length)) {
2038
- // Made with room for the class, then emptied below like any reused one.
2039
- pool.push(new Array<u8>(1 << j))
2343
+ return false // never: the push above made it
2040
2344
  }
2041
- this.poolUsed[j] = used + 1
2042
2345
  const out: u8[] = pool[used]
2043
2346
  http3Empty(out)
2044
- const from: i32 = start > 0 ? start : H3_ZERO
2045
- const end: i32 = start + length < toI32(d.bytes.length) ? start + length : toI32(d.bytes.length)
2046
- for (let k: i32 = from; k < end; k++) {
2047
- out.push(d.bytes[k])
2347
+ const bytes: u8[] = d.bytes
2348
+ let at: i32 = start > 0 ? start : H3_ZERO
2349
+ for (let i: i32 = 0; i < length && at >= 0 && at < toI32(bytes.length); i++) {
2350
+ out.push(bytes[at])
2351
+ at = at + 1
2048
2352
  }
2049
- return out
2353
+ list.push(out)
2354
+ return true
2050
2355
  }
2051
2356
 
2052
2357
  /**
@@ -2054,10 +2359,20 @@ export class Http3Connection {
2054
2359
  * client's RESET_STREAM with `resetCode`, at a frame boundary.
2055
2360
  */
2056
2361
  private ended(k: i32, id: i64, n: i32, resetCode: i64): i32 {
2057
- const phase: i32 = this.phase[k]
2058
- this.phase[k] = H3_PHASE_DONE
2059
- this.mark(k, H3_FLAG_RECV_DONE)
2362
+ const phases: i32[] = this.phase
2363
+ if (
2364
+ k < 0 ||
2365
+ k >= toI32(phases.length) ||
2366
+ k >= toI32(this.flags.length) ||
2367
+ k >= toI32(this.contentLeft.length)
2368
+ ) {
2369
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2370
+ }
2371
+ const phase: i32 = phases[k]
2372
+ phases[k] = H3_PHASE_DONE
2060
2373
  const delivered: boolean = (this.flags[k] & H3_FLAG_DELIVERED) !== 0
2374
+ const owed: i64 = this.contentLeft[k]
2375
+ this.mark(k, H3_FLAG_RECV_DONE)
2061
2376
  if (n === QUIC_STREAM_ERR_RESET) {
2062
2377
  // §4.1.1: a response to a request that will not finish is abandoned too.
2063
2378
  this.abandon(k, id, resetCode === H3_REQUEST_CANCELLED ? H3_REQUEST_CANCELLED : H3_REQUEST_INCOMPLETE)
@@ -2077,7 +2392,7 @@ export class Http3Connection {
2077
2392
  const charged: i32 = this.chargeReset(k)
2078
2393
  return charged !== H3_NEED_MORE ? charged : this.dropped(id)
2079
2394
  }
2080
- if (this.contentLength[k] >= 0 && this.dataTotal[k] !== this.contentLength[k]) {
2395
+ if (owed > 0) {
2081
2396
  // §4.1.2: the body is shorter than its content-length.
2082
2397
  this.abandon(k, id, H3_MESSAGE_ERROR)
2083
2398
  this.streamErrors = h3Count(this.streamErrors)
@@ -2090,6 +2405,9 @@ export class Http3Connection {
2090
2405
 
2091
2406
  /** Resets this side's half of request stream `id` with `code`, unless it is done already. */
2092
2407
  private abandon(k: i32, id: i64, code: i64): void {
2408
+ if (k < 0 || k >= toI32(this.flags.length)) {
2409
+ return // never: every caller holds a slot of the table
2410
+ }
2093
2411
  if ((this.flags[k] & H3_FLAG_SEND_DONE) === 0) {
2094
2412
  this.quic.streamReset(id, code)
2095
2413
  }
@@ -2098,15 +2416,20 @@ export class Http3Connection {
2098
2416
 
2099
2417
  /** A stream error (§8): the request is reset both ways with `code`, and the program told when it knew the request. */
2100
2418
  private streamError(k: i32, id: i64, code: i64): i32 {
2419
+ const phases: i32[] = this.phase
2420
+ if (k < 0 || k >= toI32(phases.length) || k >= toI32(this.flags.length)) {
2421
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2422
+ }
2423
+ phases[k] = H3_PHASE_DISCARD
2424
+ const delivered: boolean = (this.flags[k] & H3_FLAG_DELIVERED) !== 0
2101
2425
  this.streamErrors = h3Count(this.streamErrors)
2102
2426
  this.abandon(k, id, code)
2103
2427
  this.quic.streamStopSending(id, code)
2104
- this.phase[k] = H3_PHASE_DISCARD
2105
2428
  const charged: i32 = this.chargeReset(k)
2106
2429
  if (charged !== H3_NEED_MORE) {
2107
2430
  return charged
2108
2431
  }
2109
- if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0) {
2432
+ if (delivered) {
2110
2433
  return this.resetEvent(id, code, false)
2111
2434
  }
2112
2435
  return this.dropped(id)
@@ -2119,19 +2442,32 @@ export class Http3Connection {
2119
2442
  * is spent the connection error H3_EXCESSIVE_LOAD (§10.5).
2120
2443
  */
2121
2444
  private chargeReset(k: i32): i32 {
2445
+ if (k < 0 || k >= toI32(this.flags.length)) {
2446
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2447
+ }
2122
2448
  if ((this.flags[k] & H3_FLAG_CHARGED) !== 0) {
2123
2449
  return H3_NEED_MORE
2124
2450
  }
2125
2451
  this.flags[k] = this.flags[k] | H3_FLAG_CHARGED
2126
- this.resetCredit = this.resetCredit - 1
2127
- return this.resetCredit < 0 ? this.fail(H3_EXCESSIVE_LOAD) : H3_NEED_MORE
2452
+ // A spent budget fails the connection, so the credit never goes below 0.
2453
+ const credit: i32 = this.resetCredit
2454
+ if (credit <= 0) {
2455
+ return this.fail(H3_EXCESSIVE_LOAD)
2456
+ }
2457
+ this.resetCredit = credit - 1
2458
+ return H3_NEED_MORE
2128
2459
  }
2129
2460
 
2130
2461
  /** This side ended request `k`'s response with a FIN: its send side is done, and a request the program answered earns the reset budget one back. */
2131
2462
  private answered(k: i32): void {
2463
+ if (k < 0 || k >= toI32(this.flags.length)) {
2464
+ return // never: every caller holds a slot of the table
2465
+ }
2466
+ const delivered: boolean = (this.flags[k] & H3_FLAG_DELIVERED) !== 0
2132
2467
  this.mark(k, H3_FLAG_SEND_DONE)
2133
- if ((this.flags[k] & H3_FLAG_DELIVERED) !== 0 && this.resetCredit < this.config.resetBudget) {
2134
- this.resetCredit = this.resetCredit + 1
2468
+ const credit: i32 = this.resetCredit
2469
+ if (delivered && credit < this.config.resetBudget && credit < 2147483647) {
2470
+ this.resetCredit = credit + 1
2135
2471
  }
2136
2472
  }
2137
2473
 
@@ -2171,7 +2507,11 @@ export class Http3Connection {
2171
2507
  this.abandon(k, id, H3_EXCESSIVE_LOAD)
2172
2508
  }
2173
2509
  this.quic.streamStopSending(id, H3_NO_ERROR)
2174
- this.phase[k] = H3_PHASE_DISCARD
2510
+ const phases: i32[] = this.phase
2511
+ if (k < 0 || k >= toI32(phases.length)) {
2512
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2513
+ }
2514
+ phases[k] = H3_PHASE_DISCARD
2175
2515
  const charged: i32 = this.chargeReset(k)
2176
2516
  return charged !== H3_NEED_MORE ? charged : this.dropped(id)
2177
2517
  }
@@ -2182,12 +2522,23 @@ export class Http3Connection {
2182
2522
  * H3_WRITABLE. Answers that, or H3_NEED_MORE.
2183
2523
  */
2184
2524
  private writable(k: i32, id: i64): i32 {
2185
- if ((this.flags[k] & H3_FLAG_BLOCKED) === 0 || (this.flags[k] & H3_FLAG_SEND_DONE) !== 0) {
2525
+ if (k < 0 || k >= toI32(this.flags.length)) {
2526
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2527
+ }
2528
+ const flags: i32 = this.flags[k]
2529
+ if ((flags & H3_FLAG_BLOCKED) === 0 || (flags & H3_FLAG_SEND_DONE) !== 0 || !this.drain(k, id)) {
2186
2530
  return H3_NEED_MORE
2187
2531
  }
2188
- if (!this.drain(k, id) || this.quic.streams.slots[k].room() <= 0) {
2532
+ const slots: QuicStream[] = this.quic.streams.slots
2533
+ if (k >= toI32(slots.length) || k >= toI32(this.flags.length)) {
2534
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2535
+ }
2536
+ if (slots[k].room() <= 0) {
2189
2537
  return H3_NEED_MORE
2190
2538
  }
2539
+ if (k >= toI32(this.flags.length)) {
2540
+ return H3_NEED_MORE // never: every caller holds a slot of the table
2541
+ }
2191
2542
  this.flags[k] = this.flags[k] & ~H3_FLAG_BLOCKED
2192
2543
  this.stream = id
2193
2544
  return H3_WRITABLE
@@ -2204,11 +2555,15 @@ export class Http3Connection {
2204
2555
  if (k < 0 || k >= toI32(this.slotId.length) || this.slotId[k] !== id) {
2205
2556
  return -1
2206
2557
  }
2558
+ const slots: QuicStream[] = this.quic.streams.slots
2559
+ if (k >= toI32(this.flags.length) || k >= toI32(slots.length)) {
2560
+ return -1 // never: slotId, flags and the stream table hold a slot each
2561
+ }
2207
2562
  const flags: i32 = this.flags[k]
2208
2563
  if ((flags & H3_FLAG_DELIVERED) === 0 || (flags & H3_FLAG_SEND_DONE) !== 0) {
2209
2564
  return -1
2210
2565
  }
2211
- return this.quic.streams.slots[k].stopCode >= 0 ? -1 : k
2566
+ return slots[k].stopCode >= 0 ? -1 : k
2212
2567
  }
2213
2568
 
2214
2569
  /**
@@ -2217,34 +2572,65 @@ export class Http3Connection {
2217
2572
  * them. Answers whether none are owed now.
2218
2573
  */
2219
2574
  private drain(k: i32, id: i64): boolean {
2220
- const base: i32 = k * H3_HEAD
2221
- while (this.pendLen[k] > 0) {
2222
- const n: i32 = this.quic.streamWrite(id, this.pend, base + this.pendAt[k], this.pendLen[k], false)
2223
- if (n <= 0) {
2575
+ // 3072 is the most slots and 16 H3_HEAD, spelled out so the prover bounds `base`.
2576
+ if (k < 0 || k >= 3072) {
2577
+ return false // never: every caller holds a slot of the table
2578
+ }
2579
+ const base: i32 = k * 16
2580
+ while (k < toI32(this.pendLen.length) && k < toI32(this.pendAt.length)) {
2581
+ const owed: i32 = this.pendLen[k]
2582
+ const at: i32 = this.pendAt[k]
2583
+ if (owed <= 0) {
2584
+ break
2585
+ }
2586
+ if (owed > 16 || at < 0 || at > 16) {
2587
+ return false // never: owe queues at most H3_HEAD bytes, and a write takes no more than asked
2588
+ }
2589
+ const n: i32 = this.quic.streamWrite(id, this.pend, base + at, owed, false)
2590
+ if (n <= 0 || n > owed) {
2224
2591
  return false
2225
2592
  }
2226
- this.pendAt[k] = this.pendAt[k] + n
2227
- this.pendLen[k] = this.pendLen[k] - n
2593
+ if (k >= toI32(this.pendLen.length) || k >= toI32(this.pendAt.length)) {
2594
+ return false // never: nothing resizes the table
2595
+ }
2596
+ this.pendAt[k] = at + n
2597
+ this.pendLen[k] = owed - n
2228
2598
  }
2229
- while (this.fillLeft[k] > 0) {
2230
- const want: i32 = toI32(http3Min(this.fillLeft[k], toI64(toI32(this.zeros.length))))
2599
+ while (k < toI32(this.fillLeft.length)) {
2600
+ const fill: i64 = this.fillLeft[k]
2601
+ if (fill <= 0) {
2602
+ break
2603
+ }
2604
+ const want: i32 = toI32(http3Min(fill, toI64(toI32(this.zeros.length))))
2231
2605
  const n: i32 = this.quic.streamWrite(id, this.zeros, H3_ZERO, want, false)
2232
2606
  if (n <= 0) {
2233
2607
  return false
2234
2608
  }
2235
- this.fillLeft[k] = this.fillLeft[k] - toI64(n)
2609
+ if (k >= toI32(this.fillLeft.length)) {
2610
+ return false // never: nothing resizes the table
2611
+ }
2612
+ this.fillLeft[k] = fill - toI64(n)
2236
2613
  }
2237
2614
  return true
2238
2615
  }
2239
2616
 
2240
2617
  /** Queues `buf[0 .. n)`, a frame header, as the bytes owed first on slot `k`. */
2241
2618
  private owe(k: i32, buf: u8[], n: i32): void {
2242
- const base: i32 = k * H3_HEAD
2243
- for (let j: i32 = 0; j < n && j < H3_HEAD && j < toI32(buf.length); j++) {
2244
- this.pend[base + j] = buf[j]
2619
+ // 3072 is the most slots and 16 H3_HEAD, spelled out so the prover bounds `base`.
2620
+ if (k < 0 || k >= 3072 || k >= toI32(this.pendAt.length) || k >= toI32(this.pendLen.length)) {
2621
+ return // never: every caller holds a slot of the table
2622
+ }
2623
+ const base: i32 = k * 16
2624
+ const pend: u8[] = this.pend
2625
+ for (let j: i32 = 0; j < n && j < 16 && j < toI32(buf.length); j++) {
2626
+ const at: i32 = base + j
2627
+ if (at >= toI32(pend.length)) {
2628
+ break // never: pend holds H3_HEAD bytes a slot
2629
+ }
2630
+ pend[at] = buf[j]
2245
2631
  }
2246
2632
  this.pendAt[k] = 0
2247
- this.pendLen[k] = n < H3_HEAD ? n : H3_HEAD
2633
+ this.pendLen[k] = n < 16 ? n : 16
2248
2634
  }
2249
2635
 
2250
2636
  /**
@@ -2253,7 +2639,11 @@ export class Http3Connection {
2253
2639
  * so that QUIC names the stream again once acknowledgements free room.
2254
2640
  */
2255
2641
  private arm(k: i32, id: i64): void {
2256
- const stream: QuicStream = this.quic.streams.slots[k]
2642
+ const slots: QuicStream[] = this.quic.streams.slots
2643
+ if (k < 0 || k >= toI32(slots.length)) {
2644
+ return // never: every caller holds a slot of the table
2645
+ }
2646
+ const stream: QuicStream = slots[k]
2257
2647
  const room: i64 = stream.room()
2258
2648
  this.mark(k, H3_FLAG_BLOCKED)
2259
2649
  if (room > 0) {
@@ -2269,9 +2659,16 @@ export class Http3Connection {
2269
2659
  size = 4
2270
2660
  }
2271
2661
  const payload: i64 = room > 1 ? room - toI64(1 + size) : toI64(0)
2272
- this.scratch[0] = toU8(H3_FILLER_TYPE)
2273
- const n: i32 = quicVarintPut(this.scratch, H3_ONE, payload, size)
2274
- this.owe(k, this.scratch, n)
2662
+ const scratch: u8[] = this.scratch
2663
+ if (toI32(scratch.length) < 1) {
2664
+ return // never: the scratch holds 64 bytes
2665
+ }
2666
+ scratch[0] = toU8(H3_FILLER_TYPE)
2667
+ const n: i32 = quicVarintPut(scratch, H3_ONE, payload, size)
2668
+ this.owe(k, scratch, n)
2669
+ if (k >= toI32(this.fillLeft.length)) {
2670
+ return // never: nothing resizes the table
2671
+ }
2275
2672
  this.fillLeft[k] = payload
2276
2673
  if (!this.drain(k, id)) {
2277
2674
  return
@@ -2304,13 +2701,14 @@ export class Http3Connection {
2304
2701
  http3Empty(out)
2305
2702
  this.encoder.beginSection(out)
2306
2703
  let size: i64 = 0
2307
- if (status > 0) {
2308
- const ten: i32 = 10
2309
- this.statusValue[0] = toU8(H3_DIGIT_ZERO + status / (ten * ten))
2310
- this.statusValue[1] = toU8(H3_DIGIT_ZERO + ((status / ten) % ten))
2311
- this.statusValue[2] = toU8(H3_DIGIT_ZERO + (status % ten))
2704
+ const digits: u8[] = this.statusValue
2705
+ // Every caller passes 0 for trailers or a status of three digits.
2706
+ if (status > 0 && status < 1000 && toI32(digits.length) >= 3) {
2707
+ digits[0] = toU8(H3_DIGIT_ZERO + status / 100)
2708
+ digits[1] = toU8(H3_DIGIT_ZERO + ((status / 10) % 10))
2709
+ digits[2] = toU8(H3_DIGIT_ZERO + (status % 10))
2312
2710
  const nameLength: i32 = toI32(this.statusName.length)
2313
- const valueLength: i32 = toI32(this.statusValue.length)
2711
+ const valueLength: i32 = toI32(digits.length)
2314
2712
  this.encoder.encodeField(
2315
2713
  out,
2316
2714
  this.statusName,
@@ -2322,13 +2720,15 @@ export class Http3Connection {
2322
2720
  false,
2323
2721
  true
2324
2722
  )
2325
- size = toI64(nameLength + valueLength + QPACK_FIELD_OVERHEAD)
2723
+ size = toI64(nameLength) + toI64(valueLength) + toI64(QPACK_FIELD_OVERHEAD)
2326
2724
  }
2327
2725
  if (dated && status >= 200 && toI32(this.date.length) > 0 && !http3NamesHoldDate(names)) {
2328
2726
  const nl: i32 = toI32(this.dateName.length)
2329
2727
  const vl: i32 = toI32(this.date.length)
2330
2728
  this.encoder.encodeField(out, this.dateName, H3_ZERO, nl, this.date, H3_ZERO, vl, false, true)
2331
- size = size + toI64(nl + vl + QPACK_FIELD_OVERHEAD)
2729
+ if (size >= 0 && size < 1073741824) {
2730
+ size = size + toI64(nl) + toI64(vl) + toI64(QPACK_FIELD_OVERHEAD)
2731
+ }
2332
2732
  }
2333
2733
  const n: i32 = toI32(names.length)
2334
2734
  for (let j: i32 = 0; j < n && j < toI32(names.length) && j < toI32(values.length); j++) {
@@ -2337,11 +2737,18 @@ export class Http3Connection {
2337
2737
  const nl: i32 = toI32(name.length)
2338
2738
  const vl: i32 = toI32(value.length)
2339
2739
  this.encoder.encodeField(out, name, H3_ZERO, nl, value, H3_ZERO, vl, httpFieldSensitive(name), true)
2340
- size = size + toI64(nl + vl + QPACK_FIELD_OVERHEAD)
2740
+ // The sum stops growing past 2^30 bytes: a section that size is past any stream buffer.
2741
+ if (size >= 0 && size < 1073741824) {
2742
+ size = size + toI64(nl) + toI64(vl) + toI64(QPACK_FIELD_OVERHEAD)
2743
+ }
2341
2744
  }
2342
2745
  const limit: i64 = this.peer.maxFieldSectionSize
2343
- const length: i64 = toI64(toI32(out.length))
2344
- const total: i64 = toI64(h3FrameHeaderSize(H3_FRAME_HEADERS, length)) + length
2746
+ const encoded: i32 = toI32(out.length)
2747
+ const head: i32 = h3FrameHeaderSize(H3_FRAME_HEADERS, toI64(encoded))
2748
+ if (head < 0 || head > 16) {
2749
+ return H3_TOO_LARGE // never: a frame header is two varints, 16 bytes at most
2750
+ }
2751
+ const total: i64 = toI64(head) + toI64(encoded)
2345
2752
  if ((limit >= 0 && size > limit) || total > toI64(this.quic.streams.bufferSize)) {
2346
2753
  return H3_TOO_LARGE
2347
2754
  }
@@ -2349,7 +2756,11 @@ export class Http3Connection {
2349
2756
  this.mark(k, H3_FLAG_BLOCKED)
2350
2757
  return H3_AGAIN
2351
2758
  }
2352
- if (this.quic.streams.slots[k].room() < total) {
2759
+ const slots: QuicStream[] = this.quic.streams.slots
2760
+ if (k < 0 || k >= toI32(slots.length)) {
2761
+ return H3_AGAIN // never: every caller holds a slot of the table
2762
+ }
2763
+ if (slots[k].room() < total) {
2353
2764
  this.arm(k, id)
2354
2765
  return H3_AGAIN
2355
2766
  }
@@ -2358,11 +2769,11 @@ export class Http3Connection {
2358
2769
  H3_ZERO,
2359
2770
  toI32(this.scratch.length),
2360
2771
  H3_FRAME_HEADERS,
2361
- length
2772
+ toI64(encoded)
2362
2773
  )
2363
2774
  this.quic.streamWrite(id, this.scratch, H3_ZERO, hp, false)
2364
- this.quic.streamWrite(id, out, H3_ZERO, toI32(length), fin)
2365
- this.flags[k] = this.flags[k] | H3_FLAG_HEADERS_SENT
2775
+ this.quic.streamWrite(id, out, H3_ZERO, encoded, fin)
2776
+ this.mark(k, H3_FLAG_HEADERS_SENT)
2366
2777
  if (fin) {
2367
2778
  this.answered(k)
2368
2779
  }
@@ -2394,6 +2805,9 @@ export class Http3Connection {
2394
2805
  if (k < 0) {
2395
2806
  return H3_CLOSED
2396
2807
  }
2808
+ if (k >= toI32(this.flags.length)) {
2809
+ return H3_CLOSED // never: writeSlot answers a slot of the table
2810
+ }
2397
2811
  if (
2398
2812
  (this.flags[k] & H3_FLAG_HEADERS_SENT) !== 0 ||
2399
2813
  status < 200 ||
@@ -2423,6 +2837,9 @@ export class Http3Connection {
2423
2837
  if (k < 0) {
2424
2838
  return H3_CLOSED
2425
2839
  }
2840
+ if (k >= toI32(this.flags.length) || k >= toI32(this.dataLeft.length)) {
2841
+ return H3_CLOSED // never: writeSlot answers a slot of the table
2842
+ }
2426
2843
  if (
2427
2844
  (this.flags[k] & H3_FLAG_HEADERS_SENT) === 0 ||
2428
2845
  this.dataLeft[k] > 0 ||
@@ -2462,7 +2879,11 @@ export class Http3Connection {
2462
2879
  if (k < 0) {
2463
2880
  return H3_CLOSED
2464
2881
  }
2465
- if ((this.flags[k] & H3_FLAG_HEADERS_SENT) === 0 || (endStream && toI64(len) < this.dataLeft[k])) {
2882
+ if (k >= toI32(this.flags.length) || k >= toI32(this.dataLeft.length)) {
2883
+ return H3_CLOSED // never: writeSlot answers a slot of the table
2884
+ }
2885
+ const owing: i64 = this.dataLeft[k]
2886
+ if ((this.flags[k] & H3_FLAG_HEADERS_SENT) === 0 || (endStream && toI64(len) < owing)) {
2466
2887
  return H3_INVALID
2467
2888
  }
2468
2889
  if (!this.drain(k, id)) {
@@ -2470,15 +2891,21 @@ export class Http3Connection {
2470
2891
  return H3_AGAIN
2471
2892
  }
2472
2893
  if (len === 0) {
2473
- if (endStream && this.dataLeft[k] === 0) {
2894
+ if (endStream && owing === 0) {
2474
2895
  this.quic.streamWrite(id, this.zeros, H3_ZERO, H3_ZERO, true)
2475
2896
  this.answered(k)
2476
2897
  }
2477
2898
  return 0
2478
2899
  }
2900
+ if (len < 0) {
2901
+ return H3_INVALID // never: h3CheckWindow refused a negative length
2902
+ }
2479
2903
  // DATA frame after DATA frame, until every byte is taken or the send buffer is full.
2480
2904
  let total: i32 = 0
2481
- while (total < len) {
2905
+ while (total >= 0 && total < len) {
2906
+ if (k >= toI32(this.dataLeft.length)) {
2907
+ break // never: nothing resizes the table
2908
+ }
2482
2909
  if (this.dataLeft[k] === 0) {
2483
2910
  const rest: i32 = len - total
2484
2911
  const frame: i64 = toI64(rest < this.config.writeChunk ? rest : this.config.writeChunk)
@@ -2490,20 +2917,42 @@ export class Http3Connection {
2490
2917
  frame
2491
2918
  )
2492
2919
  this.owe(k, this.scratch, n)
2920
+ if (k >= toI32(this.dataLeft.length)) {
2921
+ break // never: nothing resizes the table
2922
+ }
2493
2923
  this.dataLeft[k] = frame
2494
2924
  if (!this.drain(k, id)) {
2495
2925
  this.mark(k, H3_FLAG_BLOCKED)
2496
2926
  break
2497
2927
  }
2928
+ if (k >= toI32(this.dataLeft.length)) {
2929
+ break // never: nothing resizes the table
2930
+ }
2931
+ }
2932
+ const want: i32 = len - total
2933
+ const left: i64 = this.dataLeft[k]
2934
+ if (left <= 0) {
2935
+ break // never: a DATA frame was just begun with writeChunk, at least 1, or fewer bytes owed
2498
2936
  }
2499
- const take: i32 = toI32(http3Min(toI64(len - total), this.dataLeft[k]))
2500
- const fin: boolean = endStream && total + take === len && toI64(take) === this.dataLeft[k]
2501
- const n: i32 = this.quic.streamWrite(id, buf, off + total, take, fin)
2937
+ const take: i32 = left < toI64(want) ? toI32(left) : want
2938
+ const fin: boolean = endStream && take === want && toI64(take) === left
2939
+ const at: i64 = toI64(off) + toI64(total)
2940
+ if (at > 2147483647) {
2941
+ break // never: h3CheckWindow put the window inside `buf`
2942
+ }
2943
+ const n: i32 = this.quic.streamWrite(id, buf, toI32(at), take, fin)
2502
2944
  if (n < 0) {
2503
2945
  return total > 0 ? total : H3_CLOSED
2504
2946
  }
2505
- this.dataLeft[k] = this.dataLeft[k] - toI64(n)
2506
- total = total + n
2947
+ if (k >= toI32(this.dataLeft.length)) {
2948
+ break // never: nothing resizes the table
2949
+ }
2950
+ this.dataLeft[k] = left - toI64(n)
2951
+ const next: i64 = toI64(total) + toI64(n)
2952
+ if (next > 2147483647) {
2953
+ break // never: a write takes no more than `take`
2954
+ }
2955
+ total = toI32(next)
2507
2956
  if (n < take) {
2508
2957
  this.mark(k, H3_FLAG_BLOCKED)
2509
2958
  break
@@ -2541,11 +2990,20 @@ export class Http3Connection {
2541
2990
  if (k < 0) {
2542
2991
  return H3_CLOSED
2543
2992
  }
2993
+ if (k >= toI32(this.flags.length) || k >= toI32(this.dataLeft.length)) {
2994
+ return H3_CLOSED // never: writeSlot answers a slot of the table
2995
+ }
2544
2996
  if ((this.flags[k] & H3_FLAG_HEADERS_SENT) === 0 || this.dataLeft[k] > 0) {
2545
2997
  return H3_INVALID
2546
2998
  }
2547
- const length: i64 = toI64(len)
2548
- const total: i64 = toI64(h3FrameHeaderSize(H3_FRAME_DATA, length)) + length
2999
+ if (len < 0) {
3000
+ return H3_INVALID // never: h3CheckWindow refused a negative length
3001
+ }
3002
+ const head: i32 = h3FrameHeaderSize(H3_FRAME_DATA, toI64(len))
3003
+ if (head < 0 || head > 16) {
3004
+ return H3_TOO_LARGE // never: a frame header is two varints, 16 bytes at most
3005
+ }
3006
+ const total: i64 = toI64(head) + toI64(len)
2549
3007
  if (total > toI64(this.quic.streams.bufferSize)) {
2550
3008
  return H3_TOO_LARGE
2551
3009
  }
@@ -2553,11 +3011,21 @@ export class Http3Connection {
2553
3011
  this.mark(k, H3_FLAG_BLOCKED)
2554
3012
  return H3_AGAIN
2555
3013
  }
2556
- if (this.quic.streams.slots[k].room() < total) {
3014
+ const slots: QuicStream[] = this.quic.streams.slots
3015
+ if (k >= toI32(slots.length)) {
3016
+ return H3_CLOSED // never: writeSlot answers a slot of the table
3017
+ }
3018
+ if (slots[k].room() < total) {
2557
3019
  this.arm(k, id)
2558
3020
  return H3_AGAIN
2559
3021
  }
2560
- const hp: i32 = h3PutFrameHeader(this.scratch, H3_ZERO, toI32(this.scratch.length), H3_FRAME_DATA, length)
3022
+ const hp: i32 = h3PutFrameHeader(
3023
+ this.scratch,
3024
+ H3_ZERO,
3025
+ toI32(this.scratch.length),
3026
+ H3_FRAME_DATA,
3027
+ toI64(len)
3028
+ )
2561
3029
  this.quic.streamWrite(id, this.scratch, H3_ZERO, hp, false)
2562
3030
  this.quic.streamWrite(id, buf, off, len, fin)
2563
3031
  if (fin) {
@@ -2583,10 +3051,17 @@ export class Http3Connection {
2583
3051
  if (k < 0) {
2584
3052
  return H3_CLOSED
2585
3053
  }
3054
+ const phases: i32[] = this.phase
3055
+ if (k >= toI32(phases.length)) {
3056
+ return H3_CLOSED // never: writeSlot answers a slot of the table
3057
+ }
3058
+ const done: boolean = phases[k] === H3_PHASE_DONE
3059
+ if (!done) {
3060
+ phases[k] = H3_PHASE_DISCARD
3061
+ }
2586
3062
  this.abandon(k, id, code)
2587
- if (this.phase[k] !== H3_PHASE_DONE) {
3063
+ if (!done) {
2588
3064
  this.quic.streamStopSending(id, code)
2589
- this.phase[k] = H3_PHASE_DISCARD
2590
3065
  }
2591
3066
  return 0
2592
3067
  }
@@ -2616,6 +3091,9 @@ export class Http3Connection {
2616
3091
  if (this.slotId[k] !== id) {
2617
3092
  return true
2618
3093
  }
3094
+ if (k >= toI32(this.phase.length) || k >= toI32(this.flags.length)) {
3095
+ return false // never: slotId, phase and flags hold a slot each
3096
+ }
2619
3097
  return this.phase[k] === H3_PHASE_HEADERS && (this.flags[k] & H3_FLAG_WT) === 0
2620
3098
  }
2621
3099