@amritk/nish-x86_64-linux 0.50.0 → 0.51.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 (54) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +4 -1
  3. package/package.json +1 -1
  4. package/runtime/nish.d.ts +82 -2
  5. package/runtime/nish.h +5 -0
  6. package/runtime/runtime-host.c +21 -5
  7. package/std/README.md +7 -7
  8. package/std/base64.ts +61 -95
  9. package/std/bytes.ts +6 -4
  10. package/std/cli-args.ts +150 -107
  11. package/std/compress/checksum.ts +68 -10
  12. package/std/compress/deflate.ts +193 -100
  13. package/std/compress/gzip.ts +124 -49
  14. package/std/compress/zlib.ts +57 -15
  15. package/std/crypto/aes.ts +51 -9
  16. package/std/crypto/ed25519.ts +12 -10
  17. package/std/crypto/hkdf.ts +405 -283
  18. package/std/crypto/hmac.ts +483 -148
  19. package/std/crypto/jwt.ts +19 -21
  20. package/std/crypto/p256.ts +5 -7
  21. package/std/crypto/pki-roots.ts +28 -8
  22. package/std/crypto/rsa-sign.ts +5 -2
  23. package/std/crypto/sha256.ts +81 -26
  24. package/std/crypto/sha512.ts +135 -49
  25. package/std/json-value.ts +14 -15
  26. package/std/net/buffer-pool.ts +25 -14
  27. package/std/net/fetch.ts +108 -27
  28. package/std/net/handler.ts +11 -22
  29. package/std/net/hpack.ts +38 -19
  30. package/std/net/http-compress.ts +21 -12
  31. package/std/net/http-server.ts +9 -4
  32. package/std/net/http1-client-codec.ts +22 -15
  33. package/std/net/http1-codec.ts +37 -36
  34. package/std/net/http3-server.ts +127 -45
  35. package/std/net/http3.ts +99 -50
  36. package/std/net/postgres-async.ts +5 -10
  37. package/std/net/qpack.ts +83 -17
  38. package/std/net/quic-listener.ts +42 -27
  39. package/std/net/quic-packet.ts +91 -50
  40. package/std/net/quic-stream.ts +29 -22
  41. package/std/net/quic.ts +48 -23
  42. package/std/net/static.ts +5 -3
  43. package/std/net/tls/client.ts +81 -34
  44. package/std/net/tls/codec.ts +3 -4
  45. package/std/net/tls/record.ts +54 -22
  46. package/std/net/tls/schedule.ts +339 -149
  47. package/std/net/tls.ts +42 -12
  48. package/std/net/upstream.ts +18 -19
  49. package/std/net/url.ts +78 -21
  50. package/std/net/websocket.ts +3 -11
  51. package/std/net/webtransport.ts +95 -21
  52. package/std/raster.ts +12 -2
  53. package/std/term-pixels.ts +18 -2
  54. package/std/text.ts +5 -5
@@ -118,6 +118,7 @@ import { chacha20HeaderMask, chacha20Poly1305Open, chacha20Poly1305Seal } from "
118
118
  import { timingSafeEqual } from "nish/crypto/ct"
119
119
  import { tryCopyBytes } from "nish/bytes"
120
120
  import {
121
+ HkdfHash,
121
122
  HkdfScratch,
122
123
  hkdfExpandLabelHeldInto,
123
124
  hkdfExpandLabelInto,
@@ -370,9 +371,10 @@ export class QuicPacket {
370
371
  /**
371
372
  * HKDF-Expand-Label (RFC 8446 §7.1, RFC 9001 §5.1) with an empty context,
372
373
  * under the hash `aead` pairs with: SHA-384 for AES-256-GCM, SHA-256 for the
373
- * others. Every label and length here is a fixed one the expand accepts.
374
+ * others. Answers `null` where the expand does, which no fixed label and
375
+ * length here reaches.
374
376
  */
375
- const quicPacketExpandLabel = (aead: i32, secret: u8[], label: string, length: i32): u8[] =>
377
+ const quicPacketExpandLabel = (aead: i32, secret: u8[], label: string, length: i32): u8[] | null =>
376
378
  aead === QUIC_AEAD_AES_256_GCM
377
379
  ? hkdfExpandLabelSha384(secret, label, [], length)
378
380
  : hkdfExpandLabelSha256(secret, label, [], length)
@@ -1245,10 +1247,12 @@ export const quicInitialSecrets = (dcid: u8[]): QuicInitialSecrets | null => {
1245
1247
  return null
1246
1248
  }
1247
1249
  const initial: u8[] = hkdfExtractSha256(quicPacketInitialSalt(), dcid)
1248
- return new QuicInitialSecrets(
1249
- quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "client in", 32),
1250
- quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "server in", 32)
1251
- )
1250
+ const client: u8[] | null = quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "client in", 32)
1251
+ const server: u8[] | null = quicPacketExpandLabel(QUIC_AEAD_AES_128_GCM, initial, "server in", 32)
1252
+ if (client === null || server === null) {
1253
+ return null
1254
+ }
1255
+ return new QuicInitialSecrets(client, server)
1252
1256
  }
1253
1257
 
1254
1258
  /** The key length of `aead` in bytes, or 0 for no AEAD this module knows. */
@@ -1262,6 +1266,10 @@ const quicPacketKeyLength = (aead: i32): i32 => {
1262
1266
  return 0
1263
1267
  }
1264
1268
 
1269
+ /** The HKDF hash `aead` pairs with: SHA-384 for AES-256-GCM, SHA-256 for the others. */
1270
+ const quicPacketHkdfHash = (aead: i32): HkdfHash =>
1271
+ aead === QUIC_AEAD_AES_256_GCM ? HkdfHash.Sha384 : HkdfHash.Sha256
1272
+
1265
1273
  /** The length of `aead`'s hash, which is its traffic secret's length: 48 for SHA-384, else 32. */
1266
1274
  const quicPacketSecretLength = (aead: i32): i32 => (aead === QUIC_AEAD_AES_256_GCM ? 48 : 32)
1267
1275
 
@@ -1282,9 +1290,15 @@ const quicPacketDup = (bytes: u8[]): u8[] => quicPacketSlice(bytes, 0, toI32(byt
1282
1290
 
1283
1291
  /**
1284
1292
  * `quicPacketExpandLabel` of a held secret, answered held: the
1285
- * `hkdfExpandLabelSha*Secret` form, which wipes its own intermediates.
1293
+ * `hkdfExpandLabelSha*Secret` form, which wipes its own intermediates, and
1294
+ * `null` where it answers `null`.
1286
1295
  */
1287
- const quicPacketExpandHeld = (aead: i32, prk: Secret<u8[]>, label: string, length: i32): Secret<u8[]> => {
1296
+ const quicPacketExpandHeld = (
1297
+ aead: i32,
1298
+ prk: Secret<u8[]>,
1299
+ label: string,
1300
+ length: i32
1301
+ ): Secret<u8[]> | null => {
1288
1302
  const none: u8[] = []
1289
1303
  return aead === QUIC_AEAD_AES_256_GCM
1290
1304
  ? hkdfExpandLabelSha384Secret(prk, label, none, length)
@@ -1340,13 +1354,14 @@ export const quicKeys = (slot: QuicKeysSlot, aead: i32, trafficSecret: Secret<u8
1340
1354
  return null
1341
1355
  }
1342
1356
  const scratch: HkdfScratch = new HkdfScratch()
1357
+ let derived: boolean = false
1343
1358
  {
1344
1359
  using _scope = arena()
1345
1360
  const plain: u8[] = expose(trafficSecret, quicPacketDup)
1346
- quicKeysInto(scratch, slot, aead, plain)
1361
+ derived = quicKeysInto(scratch, slot, aead, plain) !== null
1347
1362
  secureZero(plain)
1348
1363
  }
1349
- return slot.keys
1364
+ return derived ? slot.keys : null
1350
1365
  }
1351
1366
 
1352
1367
  /**
@@ -1401,6 +1416,7 @@ export const quicKeysUpdate = (
1401
1416
  return null
1402
1417
  }
1403
1418
  const scratch: HkdfScratch = new HkdfScratch()
1419
+ let derived: boolean = false
1404
1420
  {
1405
1421
  using _scope = arena()
1406
1422
  const hpAes: AesKey | null = next.hpAes
@@ -1408,10 +1424,10 @@ export const quicKeysUpdate = (
1408
1424
  aesKeyInto(next.hp, hpAes)
1409
1425
  }
1410
1426
  const plain: u8[] = expose(nextSecret, quicPacketDup)
1411
- quicPacketDeriveInto(scratch, next, plain)
1427
+ derived = quicPacketDeriveInto(scratch, next, plain)
1412
1428
  secureZero(plain)
1413
1429
  }
1414
- return next
1430
+ return derived ? next : null
1415
1431
  }
1416
1432
 
1417
1433
  /**
@@ -1524,19 +1540,28 @@ const quicPacketPoint = (slot: QuicKeysSlot, aead: i32): void => {
1524
1540
  * `quicPacketDerive` in place: the packet key and IV from `secret` into
1525
1541
  * `keys`' own arrays, and for the AES suites the key's schedule expanded
1526
1542
  * into its own `AesKey`. HKDF runs in `kdf`, which wipes its secret-bearing fields, and
1527
- * the expansion's temporaries go with the arena block.
1543
+ * the expansion's temporaries go with the arena block. Answers whether both
1544
+ * expands derived; on `false` the key and IV are zeroed and the schedule is
1545
+ * not expanded.
1528
1546
  */
1529
- const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): void => {
1547
+ const quicPacketDeriveInto = (kdf: HkdfScratch, keys: QuicKeys, secret: u8[]): boolean => {
1530
1548
  const aead: i32 = keys.aead
1531
- const hashLength: i32 = quicPacketSecretLength(aead)
1549
+ const hash: HkdfHash = quicPacketHkdfHash(aead)
1532
1550
  using _scope = arena()
1533
1551
  const none: u8[] = []
1534
- hkdfExpandLabelInto(kdf, hashLength, secret, "quic key", none, 0, 0, keys.key, 0, quicPacketKeyLength(aead))
1535
- hkdfExpandLabelInto(kdf, hashLength, secret, "quic iv", none, 0, 0, keys.iv, 0, QUIC_IV_SIZE)
1552
+ const derived: boolean =
1553
+ hkdfExpandLabelInto(kdf, hash, secret, "quic key", none, 0, 0, keys.key, 0, quicPacketKeyLength(aead)) &&
1554
+ hkdfExpandLabelInto(kdf, hash, secret, "quic iv", none, 0, 0, keys.iv, 0, QUIC_IV_SIZE)
1555
+ if (!derived) {
1556
+ secureZero(keys.key)
1557
+ secureZero(keys.iv)
1558
+ return false
1559
+ }
1536
1560
  const packetAes: AesKey | null = keys.packetAes
1537
1561
  if (packetAes !== null) {
1538
1562
  aesKeyInto(keys.key, packetAes)
1539
1563
  }
1564
+ return true
1540
1565
  }
1541
1566
 
1542
1567
  /**
@@ -1565,22 +1590,29 @@ export const quicKeysInto = (
1565
1590
  }
1566
1591
  quicPacketPoint(slot, aead)
1567
1592
  const keys: QuicKeys = slot.keys
1568
- quicPacketDeriveInto(kdf, keys, secret)
1593
+ if (!quicPacketDeriveInto(kdf, keys, secret)) {
1594
+ return null
1595
+ }
1569
1596
  {
1570
1597
  using _scope = arena()
1571
1598
  const none: u8[] = []
1572
- hkdfExpandLabelInto(
1573
- kdf,
1574
- quicPacketSecretLength(aead),
1575
- secret,
1576
- "quic hp",
1577
- none,
1578
- 0,
1579
- 0,
1580
- keys.hp,
1581
- 0,
1582
- quicPacketKeyLength(aead)
1583
- )
1599
+ if (
1600
+ !hkdfExpandLabelInto(
1601
+ kdf,
1602
+ quicPacketHkdfHash(aead),
1603
+ secret,
1604
+ "quic hp",
1605
+ none,
1606
+ 0,
1607
+ 0,
1608
+ keys.hp,
1609
+ 0,
1610
+ quicPacketKeyLength(aead)
1611
+ )
1612
+ ) {
1613
+ quicKeysWipe(keys)
1614
+ return null
1615
+ }
1584
1616
  const hpAes: AesKey | null = keys.hpAes
1585
1617
  if (hpAes !== null) {
1586
1618
  aesKeyInto(keys.hp, hpAes)
@@ -1618,8 +1650,7 @@ export const quicKeysUpdateInto = (
1618
1650
  const next: QuicKeys = slot.keys
1619
1651
  next.hp = keys.hp
1620
1652
  next.hpAes = keys.hpAes
1621
- quicPacketDeriveInto(kdf, next, nextSecret)
1622
- return next
1653
+ return quicPacketDeriveInto(kdf, next, nextSecret) ? next : null
1623
1654
  }
1624
1655
 
1625
1656
  /**
@@ -1632,7 +1663,8 @@ export const quicKeysUpdateInto = (
1632
1663
  * @param aead The negotiated suite, one of the `QUIC_AEAD_*` constants.
1633
1664
  * @param secret The current traffic secret as a plain array; only read, and not wiped.
1634
1665
  * @param out The array the next secret is written over, 32 bytes (48 for AES-256-GCM).
1635
- * @returns True when `out` holds the next secret; false, `out` untouched, for a wrong length or `aead`.
1666
+ * @returns True when `out` holds the next secret; false, `out` untouched, for a wrong length or `aead`, or
1667
+ * false, `out` zeroed, where the expand refuses an HMAC step, which a checked length never makes it do.
1636
1668
  */
1637
1669
  export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[], out: u8[]): boolean => {
1638
1670
  const length: i32 = quicPacketSecretLength(aead)
@@ -1641,8 +1673,7 @@ export const quicKeyUpdateSecretInto = (kdf: HkdfScratch, aead: i32, secret: u8[
1641
1673
  }
1642
1674
  using _scope = arena()
1643
1675
  const none: u8[] = []
1644
- hkdfExpandLabelInto(kdf, length, secret, "quic ku", none, 0, 0, out, 0, length)
1645
- return true
1676
+ return hkdfExpandLabelInto(kdf, quicPacketHkdfHash(aead), secret, "quic ku", none, 0, 0, out, 0, length)
1646
1677
  }
1647
1678
 
1648
1679
  /**
@@ -1663,12 +1694,17 @@ export const quicKeysHeldInto = (slot: QuicKeysSlot, aead: i32, secret: Secret<u
1663
1694
  }
1664
1695
  quicPacketPoint(slot, aead)
1665
1696
  const keys: QuicKeys = slot.keys
1666
- const hashLength: i32 = quicPacketSecretLength(aead)
1697
+ const hash: HkdfHash = quicPacketHkdfHash(aead)
1667
1698
  const keyLength: i32 = quicPacketKeyLength(aead)
1668
1699
  const none: u8[] = []
1669
- hkdfExpandLabelHeldInto(hashLength, secret, "quic key", none, keys.key, 0, keyLength)
1670
- hkdfExpandLabelHeldInto(hashLength, secret, "quic iv", none, keys.iv, 0, QUIC_IV_SIZE)
1671
- hkdfExpandLabelHeldInto(hashLength, secret, "quic hp", none, keys.hp, 0, keyLength)
1700
+ if (
1701
+ !hkdfExpandLabelHeldInto(hash, secret, "quic key", none, keys.key, 0, keyLength) ||
1702
+ !hkdfExpandLabelHeldInto(hash, secret, "quic iv", none, keys.iv, 0, QUIC_IV_SIZE) ||
1703
+ !hkdfExpandLabelHeldInto(hash, secret, "quic hp", none, keys.hp, 0, keyLength)
1704
+ ) {
1705
+ quicKeysWipe(keys)
1706
+ return null
1707
+ }
1672
1708
  using _scope = arena()
1673
1709
  const packetAes: AesKey | null = keys.packetAes
1674
1710
  if (packetAes !== null) {
@@ -1698,12 +1734,9 @@ export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>
1698
1734
  return false
1699
1735
  }
1700
1736
  const none: u8[] = []
1701
- if (length === 48) {
1702
- hkdfExpandLabelSha384Held(dst, src, "quic ku", none)
1703
- } else {
1704
- hkdfExpandLabelSha256Held(dst, src, "quic ku", none)
1705
- }
1706
- return true
1737
+ return length === 48
1738
+ ? hkdfExpandLabelSha384Held(dst, src, "quic ku", none)
1739
+ : hkdfExpandLabelSha256Held(dst, src, "quic ku", none)
1707
1740
  }
1708
1741
 
1709
1742
  /**
@@ -1719,7 +1752,7 @@ export const quicKeyUpdateSecretHeldInto = (dst: Secret<u8[]>, src: Secret<u8[]>
1719
1752
  * @param length The ID's length in bytes, 0 to 20.
1720
1753
  * @param client The 32-byte array `client_initial_secret` is written over.
1721
1754
  * @param server The 32-byte array `server_initial_secret` is written over.
1722
- * @returns True when both secrets were written; false, both untouched, otherwise.
1755
+ * @returns True when both secrets were written; false, both untouched, for a refused length or window, or false, `client` possibly still written, if a derive refuses (none in range does).
1723
1756
  */
1724
1757
  export const quicInitialSecretsInto = (
1725
1758
  kdf: HkdfScratch,
@@ -1742,11 +1775,19 @@ export const quicInitialSecretsInto = (
1742
1775
  using _scope = arena()
1743
1776
  const none: u8[] = []
1744
1777
  const initial: u8[] = new Array<u8>(32)
1745
- hkdfExtractInto(kdf, 32, quicPacketInitialSalt(), quicPacketTake(buf, at, length), initial, 0)
1746
- hkdfExpandLabelInto(kdf, 32, initial, "client in", none, 0, 0, client, 0, 32)
1747
- hkdfExpandLabelInto(kdf, 32, initial, "server in", none, 0, 0, server, 0, 32)
1778
+ const derived: boolean =
1779
+ hkdfExtractInto(
1780
+ kdf,
1781
+ HkdfHash.Sha256,
1782
+ quicPacketInitialSalt(),
1783
+ quicPacketTake(buf, at, length),
1784
+ initial,
1785
+ 0
1786
+ ) &&
1787
+ hkdfExpandLabelInto(kdf, HkdfHash.Sha256, initial, "client in", none, 0, 0, client, 0, 32) &&
1788
+ hkdfExpandLabelInto(kdf, HkdfHash.Sha256, initial, "server in", none, 0, 0, server, 0, 32)
1748
1789
  secureZero(initial)
1749
- return true
1790
+ return derived
1750
1791
  }
1751
1792
 
1752
1793
  /** The bits of the first byte header protection covers: four in a long header, five in a short one (RFC 9001 §5.4.1). */
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * const pools = new QuicStreamPools(65536, 111, 64) // 64 connections of 100 + 3 + 8 slots
13
13
  * const streams = new QuicStreams(100, 3, 8, 65536, 1048576) // client bidi, client uni, own; bytes
14
- * streams.share(pools) // before any stream has borrowed
14
+ * streams.share(pools) // before any stream has borrowed; false on a size mismatch
15
15
  * streams.setPeerLimits(maxData, bidiLocal, bidiRemote, uni, maxStreamsBidi, maxStreamsUni)
16
16
  * streams.prime(packetLength) // per packet, before its frames: fills the reserve
17
17
  * const error: i64 = streams.onStream(frame.streamId, frame.offset, payload, frame.dataStart,
@@ -117,7 +117,7 @@ import {
117
117
  quicStreamOverhead,
118
118
  } from "nish/net/quic-frame"
119
119
  import { BufferPool } from "nish/net/buffer-pool"
120
- import { copyBytes } from "nish/bytes"
120
+ import { tryCopyBytes } from "nish/bytes"
121
121
  import { wrappingAdd, wrappingSub } from "nish:unsafe"
122
122
 
123
123
  // ---- What the stream calls answer -----------------------------------------------
@@ -355,28 +355,28 @@ const quicStreamRingRun = (bits: u8[], size: i32, slot: i32, count: i32, on: boo
355
355
  /**
356
356
  * Copies `count` bytes of `src` from `from` into the ring `ring` at index `slot`, wrapping at its end: at most two slices.
357
357
  * Every caller passes `slot` from `QuicStream.at`, so below the ring's length, and a `count` of a ring at most:
358
- * `onStream` keeps a frame's last ring's worth, and `write` clamps to `room()`. So both slices are in range,
359
- * and `copyBytes` panicking on one that is not marks a broken invariant, not a byte to skip.
358
+ * `onStream` keeps a frame's last ring's worth, and `write` clamps to `room()`. So both slices are in range, and
359
+ * the `false` a slice out of range answers marks a broken invariant, which each caller turns into its internal error.
360
360
  */
361
- const quicStreamRingIn = (ring: u8[], slot: i32, src: u8[], from: i32, count: i32): void => {
361
+ const quicStreamRingIn = (ring: u8[], slot: i32, src: u8[], from: i32, count: i32): boolean => {
362
362
  const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
363
- copyBytes(ring, slot, src, from, first)
364
- if (count > first) {
365
- copyBytes(ring, 0, src, quicStreamAdd(from, first), quicStreamSub(count, first))
363
+ if (!tryCopyBytes(ring, slot, src, from, first)) {
364
+ return false
366
365
  }
366
+ return count <= first || tryCopyBytes(ring, 0, src, quicStreamAdd(from, first), quicStreamSub(count, first))
367
367
  }
368
368
 
369
369
  /**
370
370
  * Copies `count` bytes of the ring `ring` from index `slot`, wrapping at its end, into `dst` at `at`: at most two slices.
371
371
  * `read` passes a `count` of at most what is contiguous past `recvRead`, never more than a ring, so, as for
372
- * `quicStreamRingIn`, both slices are in range.
372
+ * `quicStreamRingIn`, both slices are in range, and `false` marks a broken invariant.
373
373
  */
374
- const quicStreamRingOut = (dst: u8[], at: i32, ring: u8[], slot: i32, count: i32): void => {
374
+ const quicStreamRingOut = (dst: u8[], at: i32, ring: u8[], slot: i32, count: i32): boolean => {
375
375
  const first: i32 = quicStreamFirstRun(toI32(ring.length), slot, count)
376
- copyBytes(dst, at, ring, slot, first)
377
- if (count > first) {
378
- copyBytes(dst, quicStreamAdd(at, first), ring, 0, quicStreamSub(count, first))
376
+ if (!tryCopyBytes(dst, at, ring, slot, first)) {
377
+ return false
379
378
  }
379
+ return count <= first || tryCopyBytes(dst, quicStreamAdd(at, first), ring, 0, quicStreamSub(count, first))
380
380
  }
381
381
 
382
382
  /** The bytes of a bitmap holding a bit for each of `bytes` bytes: `bytes` over 8, rounded up. */
@@ -1154,19 +1154,20 @@ export class QuicStreams {
1154
1154
  /**
1155
1155
  * Borrows the stream buffers from `pools` from now on, in place of the
1156
1156
  * table's own: how a server's connections share one set. Call it before
1157
- * any stream has borrowed, or after `reset`. Panics when the pools' buffers
1158
- * are not `bufferSize` bytes.
1157
+ * any stream has borrowed, or after `reset`. Pools whose buffers are not
1158
+ * `bufferSize` bytes are refused: the table keeps the pools it had and
1159
+ * answers `false`.
1159
1160
  *
1160
1161
  * @param pools The pools to borrow from, kept by reference; usually shared with other connections.
1162
+ * @returns `true` when the table borrows from `pools` from now on, `false` when their buffer size differs.
1161
1163
  */
1162
- share(pools: QuicStreamPools): void {
1164
+ share(pools: QuicStreamPools): boolean {
1163
1165
  if (pools.data.size !== this.bufferSize) {
1164
- panic(
1165
- `QuicStreams: pools of ${pools.data.size}-byte buffers shared with streams of ${this.bufferSize} bytes`
1166
- )
1166
+ return false
1167
1167
  }
1168
1168
  this.giveBack()
1169
1169
  this.pools = pools
1170
+ return true
1170
1171
  }
1171
1172
 
1172
1173
  /**
@@ -1839,7 +1840,9 @@ export class QuicStreams {
1839
1840
  if (low >= 0 && low < high) {
1840
1841
  const slot: i32 = stream.at(quicStreamSum(offset, toI64(low)))
1841
1842
  // `from + low` is at most `from + length`, inside `buf`: exact.
1842
- quicStreamRingIn(stream.recvBuf, slot, buf, quicStreamAdd(from, low), high - low)
1843
+ if (!quicStreamRingIn(stream.recvBuf, slot, buf, quicStreamAdd(from, low), high - low)) {
1844
+ return QUIC_ERROR_INTERNAL
1845
+ }
1843
1846
  quicStreamMarkRing(stream.recvHave, size, slot, high - low, true)
1844
1847
  }
1845
1848
  if (contiguous < highest) {
@@ -2087,7 +2090,9 @@ export class QuicStreams {
2087
2090
  }
2088
2091
  // No buffer (length 0) is the one way this fails: a pool buffer is always `bufferSize` bytes.
2089
2092
  if (n > 0 && toI32(stream.sendBuf.length) === stream.size) {
2090
- quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)
2093
+ if (!quicStreamRingIn(stream.sendBuf, stream.at(stream.sendEnd), buf, from, n)) {
2094
+ return QUIC_STREAM_ERR_UNKNOWN
2095
+ }
2091
2096
  }
2092
2097
  stream.sendEnd = quicStreamSum(stream.sendEnd, toI64(n))
2093
2098
  this.writtenTotal = quicStreamSum(this.writtenTotal, toI64(n))
@@ -2144,7 +2149,9 @@ export class QuicStreams {
2144
2149
  // With bytes owed, the buffer is held (the test above), and a held buffer is `bufferSize` bytes.
2145
2150
  if (n > 0 && size === stream.size) {
2146
2151
  const slot: i32 = stream.at(stream.recvRead)
2147
- quicStreamRingOut(buf, at, stream.recvBuf, slot, n)
2152
+ if (!quicStreamRingOut(buf, at, stream.recvBuf, slot, n)) {
2153
+ return QUIC_STREAM_ERR_UNKNOWN
2154
+ }
2148
2155
  quicStreamMarkRing(stream.recvHave, size, slot, n, false)
2149
2156
  }
2150
2157
  stream.recvRead = quicStreamSum(stream.recvRead, toI64(n))
package/std/net/quic.ts CHANGED
@@ -682,20 +682,38 @@ export interface QuicServerConfig {
682
682
  }
683
683
 
684
684
  /**
685
- * Panics, with the pair in the message, unless `config`'s worker fields are
686
- * both unset (`workerIndex` -1 and `workerCount` 0) or name a worker of a
687
- * steered group (0 <= k < N <= 255). `QuicListener` calls it when it is built,
688
- * before it reads a datagram, so an `Http3Server` panics through its listener.
685
+ * Why `config`'s worker fields are not a pair a server can run under, or ""
686
+ * when they are both unset (`workerIndex` -1 and `workerCount` 0) or name a
687
+ * worker of a steered group (0 <= k < N <= 255). A caller that ran it and
688
+ * left on a fault has made `quicWorkerCheck`'s check (the fault contract,
689
+ * `src/fault-contract.ts`).
689
690
  *
690
- * @param config The configuration whose `workerIndex` and `workerCount` are checked.
691
+ * @param config The configuration whose `workerIndex` and `workerCount` are read.
692
+ * @returns "" for a valid pair, else the message naming it.
691
693
  */
692
- export const quicWorkerCheck = (config: QuicServerConfig): void => {
694
+ export const quicWorkerFault = (config: QuicServerConfig): string => {
693
695
  const k: i32 = config.workerIndex
694
696
  const n: i32 = config.workerCount
695
697
  if ((k === -1 && n === 0) || (k >= 0 && k < n && n <= QUIC_WORKER_MAX)) {
696
- return
698
+ return ""
699
+ }
700
+ return `nish/net/quic: workerIndex ${k} and workerCount ${n}, not 0 <= k < n <= ${QUIC_WORKER_MAX}`
701
+ }
702
+
703
+ /**
704
+ * Panics, with the pair in the message, when `quicWorkerFault` finds one.
705
+ * `QuicListener` calls it when it is built, before it reads a datagram, so
706
+ * an `Http3Server` panics through its listener; a caller that ran
707
+ * `quicWorkerFault` on the same configuration and left on a fault has
708
+ * proved it cannot.
709
+ *
710
+ * @param config The configuration whose `workerIndex` and `workerCount` are checked.
711
+ */
712
+ export const quicWorkerCheck = (config: QuicServerConfig): void => {
713
+ const fault: string = quicWorkerFault(config)
714
+ if (fault.length > 0) {
715
+ panic(fault)
697
716
  }
698
- panic(`nish/net/quic: workerIndex ${k} and workerCount ${n}, not 0 <= k < n <= ${QUIC_WORKER_MAX}`)
699
717
  }
700
718
 
701
719
  /**
@@ -1424,6 +1442,8 @@ export const quicStatelessResetToken = (key: u8[], cid: u8[]): u8[] => {
1424
1442
  * @param length The ID's length in bytes, at most 20: the caller's to keep, as nothing checks it.
1425
1443
  * @param out Where the 16-byte token goes; bytes that would fall past its end are not written.
1426
1444
  * @param outAt Where in `out` the token starts.
1445
+ * @returns `true` once the token is written; `false`, writing nothing, when
1446
+ * `cid[at .. at + length)` lies outside `cid`.
1427
1447
  */
1428
1448
  export const quicConnResetTokenInto = (
1429
1449
  mac: HmacSha256Scratch,
@@ -1433,12 +1453,17 @@ export const quicConnResetTokenInto = (
1433
1453
  length: i32,
1434
1454
  out: u8[],
1435
1455
  outAt: i32
1436
- ): void => {
1456
+ ): boolean => {
1437
1457
  using _scope = arena()
1438
1458
  const tag: u8[] = new Array<u8>(32)
1439
- mac.begin(key, QUIC_CONN_FROM, toI32(key.length))
1440
- mac.update(cid, at, length)
1441
- mac.finishInto(tag, QUIC_CONN_FROM)
1459
+ if (
1460
+ !mac.begin(key, QUIC_CONN_FROM, toI32(key.length)) ||
1461
+ !mac.update(cid, at, length) ||
1462
+ !mac.finishInto(tag, QUIC_CONN_FROM)
1463
+ ) {
1464
+ mac.wipe()
1465
+ return false
1466
+ }
1442
1467
  for (let k: i32 = 0; k < QUIC_RESET_TOKEN_SIZE && k < toI32(tag.length); k += 1) {
1443
1468
  const i: i32 = quicConnAt(outAt, k, toI32(out.length))
1444
1469
  if (i < 0 || i >= toI32(out.length)) {
@@ -1448,6 +1473,7 @@ export const quicConnResetTokenInto = (
1448
1473
  }
1449
1474
  secureZero(tag)
1450
1475
  mac.wipe()
1476
+ return true
1451
1477
  }
1452
1478
 
1453
1479
  /** `base + k` when it indexes an array of `length` elements, else -1; the sum is taken in `i64`, so it cannot overflow. */
@@ -1457,22 +1483,19 @@ const quicConnAt = (base: i32, k: i32, length: i32): i32 => {
1457
1483
  }
1458
1484
 
1459
1485
  /**
1460
- * A new array of `count` zero bytes: none for a count of 0 or less. A count
1461
- * that fits a `u16` is sized at once; a larger one is pushed byte by byte,
1462
- * so no `i32` length is computed for `new Array`.
1486
+ * A new array of `count` zero bytes: none for a count of 0 or less, or for
1487
+ * one past an `i32`, which no array can hold. One allocation, its length
1488
+ * proven by the guards.
1463
1489
  */
1464
1490
  const quicConnBytes = (count: i64): u8[] => {
1465
- if (count <= 0) {
1491
+ if (count <= 0 || count > 2147483647) {
1466
1492
  return []
1467
1493
  }
1468
- if (count <= 65535) {
1469
- return new Array<u8>(toU16(count))
1470
- }
1471
- const out: u8[] = []
1472
- while (toI64(out.length) < count) {
1473
- out.push(toU8(0))
1494
+ const n: i32 = toI32(count)
1495
+ if (n <= 0) {
1496
+ return []
1474
1497
  }
1475
- return out
1498
+ return new Array<u8>(n)
1476
1499
  }
1477
1500
 
1478
1501
  /**
@@ -3940,6 +3963,8 @@ export class QuicConnection {
3940
3963
  counter[k] = toU8(toI32((sequence >> (toI64(7 - k) * 8)) & 255))
3941
3964
  }
3942
3965
  const tag: u8[] = new Array<u8>(32)
3966
+ // Every window is a whole array (the seed, the 8-byte counter, the
3967
+ // 32-byte tag), so none of the three answers `false`.
3943
3968
  mac.begin(this.cidSeed, QUIC_CONN_FROM, toI32(this.cidSeed.length))
3944
3969
  mac.update(counter, QUIC_CONN_FROM, toI32(counter.length))
3945
3970
  mac.finishInto(tag, QUIC_CONN_FROM)
package/std/net/static.ts CHANGED
@@ -539,10 +539,12 @@ const staticQ = (b: u8[], start: i32, end: i32): i32 => {
539
539
 
540
540
  /** A new array of `src[0 .. length)`. */
541
541
  const staticCut = (src: u8[], length: i32): u8[] => {
542
- const out: u8[] = []
543
- for (let k: i32 = 0; k < length && k < toI32(src.length); k++) {
544
- out.push(src[k])
542
+ const n: i32 = length < toI32(src.length) ? length : toI32(src.length)
543
+ if (n <= 0) {
544
+ return []
545
545
  }
546
+ const out: u8[] = new Array<u8>(n)
547
+ tryCopyBytes(out, 0, src, 0, n)
546
548
  return out
547
549
  }
548
550