@amritk/nish-aarch64-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
@@ -18,7 +18,8 @@
18
18
  * what preallocating every slot would have cost, so a full server behaves as
19
19
  * it did before and a `take` can never fail for want of memory. Reaching it
20
20
  * therefore means some buffer was not given back, and `take` panics rather
21
- * than allocating past it.
21
+ * than allocating past it; `tryTake` answers `null` there, for a borrower
22
+ * that can refuse its own work.
22
23
  * - **Give-back zeroes what was used.** The borrower says how far into the
23
24
  * buffer it wrote, and `give` zeroes `[0, used)` before the buffer goes on
24
25
  * the free list, so nothing one connection wrote is readable by the next one
@@ -72,21 +73,15 @@ export class BufferPool {
72
73
  /**
73
74
  * An empty pool: it makes nothing until the first `take`. `name` is what
74
75
  * its panics print (the owner's type and field reads best,
75
- * `"Http2TlsServer.output"`). Panics, naming the pool, unless `size` is
76
- * above 0 and `ceiling` at least 1: a pool of neither makes a buffer
77
- * nobody can use or none at all.
76
+ * `"Http2TlsServer.output"`). A `size` of 0 lends empty buffers. A
77
+ * `ceiling` at or below 0, or a `size` below 0, lends none: every
78
+ * `tryTake` answers `null`, and `take` panics as it does at the ceiling.
78
79
  *
79
80
  * @param name the label every panic of this pool prints, to say whose buffer leaked
80
81
  * @param size how many bytes each buffer holds, the same for every buffer the pool lends
81
82
  * @param ceiling the most buffers the pool may ever make, sized to what every slot holding one at once would need
82
83
  */
83
84
  constructor(name: string, size: i32, ceiling: i32) {
84
- if (size <= 0) {
85
- panic(`BufferPool ${name}: a buffer size of ${size} bytes is not above 0`)
86
- }
87
- if (ceiling < 1) {
88
- panic(`BufferPool ${name}: a ceiling of ${ceiling} buffers is below 1`)
89
- }
90
85
  this.name = name
91
86
  this.size = size
92
87
  this.ceiling = ceiling
@@ -101,11 +96,26 @@ export class BufferPool {
101
96
  *
102
97
  * Panics when all `ceiling` buffers are lent: the ceiling is what every
103
98
  * slot holding a buffer at once would need, so one more means a buffer was
104
- * never given back.
99
+ * never given back. `tryTake` answers `null` there instead.
105
100
  *
106
101
  * @returns a buffer of `size` bytes the caller now borrows until it hands it back through `give` or `giveSecret`
107
102
  */
108
103
  take(): u8[] {
104
+ const buf: u8[] | null = this.tryTake()
105
+ if (buf === null) {
106
+ panic(`BufferPool ${this.name}: all ${this.ceiling} buffers are lent, so one was never given back`)
107
+ }
108
+ return buf
109
+ }
110
+
111
+ /**
112
+ * `take` for a borrower that can refuse its own work: a buffer as `take`
113
+ * lends it, or `null` when all `ceiling` buffers are lent or `size` is
114
+ * below 0, with nothing made.
115
+ *
116
+ * @returns a buffer of `size` bytes the caller now borrows, as from `take`, or `null` when the pool has none to lend
117
+ */
118
+ tryTake(): u8[] | null {
109
119
  const free: u8[][] = this.free
110
120
  const count: i32 = toI32(free.length)
111
121
  // The same test twice: the prover reads the first as covering the index
@@ -119,11 +129,12 @@ export class BufferPool {
119
129
  // the ceiling once the test has passed.
120
130
  const made: i32 = this.made
121
131
  const ceiling: i32 = this.ceiling
122
- if (made >= ceiling) {
123
- panic(`BufferPool ${this.name}: all ${ceiling} buffers are lent, so one was never given back`)
132
+ const size: i32 = this.size
133
+ if (made >= ceiling || size < 0) {
134
+ return null
124
135
  }
125
136
  this.made = made + 1
126
- return new Array<u8>(this.size)
137
+ return new Array<u8>(size)
127
138
  }
128
139
 
129
140
  /**
package/std/net/fetch.ts CHANGED
@@ -82,8 +82,8 @@
82
82
  *
83
83
  * Written from RFC 9110 and RFC 9112, not ported from another implementation.
84
84
  */
85
- import { base64Encode } from "nish/base64"
86
- import { copyBytes } from "nish/bytes"
85
+ import { base64EncodeIfFits } from "nish/base64"
86
+ import { tryCopyBytes } from "nish/bytes"
87
87
  import {
88
88
  HTTP1_BODY,
89
89
  HTTP1_CLOSED,
@@ -901,14 +901,26 @@ export class FetchSyncClient {
901
901
  */
902
902
  private readRaw(want: i32): i32 {
903
903
  const until: i64 = this.readDeadline()
904
- let n: i32 = netRead(this.fd, this.rbuf, 0, want)
904
+ const rbuf: u8[] = this.rbuf
905
+ // Every caller asks for at most `HTTP_CLIENT_READ_BYTES`, the buffer's
906
+ // size; tested before each read, since a call between takes what the
907
+ // prover knew of the length.
908
+ if (want < 0 || want > toI32(rbuf.length)) {
909
+ this.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
910
+ return -1
911
+ }
912
+ let n: i32 = netRead(this.fd, rbuf, 0, want)
905
913
  while (n === HTTP_CLIENT_WOULD_BLOCK) {
906
914
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_READABLE, until, HTTP_E_READ_TIMEOUT)
907
915
  if (waited !== HTTP_E_NONE) {
908
916
  this.failWait(waited, `the server sent nothing for ${this.options.readTimeoutMs} ms`)
909
917
  return -1
910
918
  }
911
- n = netRead(this.fd, this.rbuf, 0, want)
919
+ if (want < 0 || want > toI32(rbuf.length)) {
920
+ this.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
921
+ return -1
922
+ }
923
+ n = netRead(this.fd, rbuf, 0, want)
912
924
  }
913
925
  if (n >= 0) {
914
926
  return n
@@ -1005,27 +1017,33 @@ export class FetchSyncClient {
1005
1017
 
1006
1018
  /** Writes `bytes[off, off + len)` to `fd`, waiting up to `readTimeoutMs` each time it is full. */
1007
1019
  private writeRaw(bytes: u8[], off: i32, len: i32): boolean {
1008
- if (off < 0 || len < 0 || len > toI32(bytes.length) - off) {
1020
+ // Where the window ends, or -1 when it does not lie inside `bytes`.
1021
+ const end: i32 = httpClientWindowEnd(bytes, off, len)
1022
+ if (end < 0) {
1009
1023
  this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1010
1024
  return false
1011
1025
  }
1012
1026
  let at: i32 = off
1013
- let left: i32 = len
1014
1027
  // `at >= 0` is the loop's own, since the prover does not carry it round.
1015
- while (left > 0 && at >= 0) {
1028
+ while (at < end && at >= 0) {
1016
1029
  if (this.overdue()) {
1017
1030
  return false
1018
1031
  }
1019
- const n: i32 = netWrite(this.fd, bytes, at, left)
1020
- if (n > 0 && n <= left) {
1021
- // At most `off + len`, inside `bytes`, so the window always has an
1022
- // end; the helper measures it so that the sum is proven to fit.
1032
+ // The window's test again, after the calls that took what the prover
1033
+ // knew of it; it passes wherever the first one did.
1034
+ if (at < 0 || at > end || end > toI32(bytes.length)) {
1035
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1036
+ return false
1037
+ }
1038
+ const n: i32 = netWrite(this.fd, bytes, at, end - at)
1039
+ if (n > 0 && n <= end - at) {
1040
+ // Short of `end`, inside `bytes`; the helper measures it so that the
1041
+ // sum is proven to fit.
1023
1042
  at = httpClientWindowEnd(bytes, at, n)
1024
1043
  if (at < 0) {
1025
1044
  this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1026
1045
  return false
1027
1046
  }
1028
- left -= n
1029
1047
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
1030
1048
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_WRITABLE, this.readDeadline(), HTTP_E_READ_TIMEOUT)
1031
1049
  if (waited !== HTTP_E_NONE) {
@@ -1074,7 +1092,10 @@ export class FetchSyncClient {
1074
1092
  if (i > 15) {
1075
1093
  break
1076
1094
  }
1077
- copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
1095
+ // An address the list does not hold all of is one past its end.
1096
+ if (!tryCopyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)) {
1097
+ break
1098
+ }
1078
1099
  const fd: i32 = tcpConnect(one)
1079
1100
  if (fd < 0) {
1080
1101
  last = fd
@@ -1270,7 +1291,12 @@ export class FetchSyncClient {
1270
1291
  }
1271
1292
  }
1272
1293
  const take: i32 = n < this.pendLen ? n : this.pendLen
1273
- copyBytes(buf, off, this.parser.body, this.pendOff, take)
1294
+ // `read` checked the caller's window and the parser keeps its body
1295
+ // window inside its buffer, so the copy fits; one that did not would be
1296
+ // a client out of step with its parser, answered as the test below is.
1297
+ if (!tryCopyBytes(buf, off, this.parser.body, this.pendOff, take)) {
1298
+ return -HTTP_E_IO
1299
+ }
1274
1300
  // The copy above fit `take` bytes at `pendOff` inside the parser's body,
1275
1301
  // so the window has an end, and `take` is at most `pendLen`. The helper
1276
1302
  // and the test restate both for the bounds proof.
@@ -1789,7 +1815,7 @@ const httpClientProxyUrl = (text: string): Url | null => {
1789
1815
  /**
1790
1816
  * The `Proxy-Authorization` value for a proxy URL's userinfo, `Basic` and
1791
1817
  * the decoded `user:password` in base64; "" when it has none; null when
1792
- * either half is not valid percent-encoding.
1818
+ * either half is not valid percent-encoding or the pair is too long to encode.
1793
1819
  */
1794
1820
  const httpClientProxyAuth = (proxy: Url): string | null => {
1795
1821
  if (proxy.username === "" && proxy.password === "") {
@@ -1800,7 +1826,26 @@ const httpClientProxyAuth = (proxy: Url): string | null => {
1800
1826
  if (user === null || password === null) {
1801
1827
  return null
1802
1828
  }
1803
- return "Basic " + base64Encode(httpFieldBytes(user + ":" + password))
1829
+ // Encoded here, not by `base64Encode`, so that a credential too long to
1830
+ // encode, past 1,610,612,733 bytes, answers null instead of panicking.
1831
+ const field: u8[] = httpFieldBytes(user + ":" + password)
1832
+ if (field.length > 1610612733) {
1833
+ return null
1834
+ }
1835
+ const n: i32 = toI32(field.length)
1836
+ if (n < 0 || n > 1610612733) {
1837
+ return null
1838
+ }
1839
+ // Not negative for that `n`; the test is what lets the compiler see it.
1840
+ const length: i32 = toI32((n + 2) / 3) * 4
1841
+ if (length < 0) {
1842
+ return null
1843
+ }
1844
+ const out: u8[] = new Array<u8>(length)
1845
+ if (base64EncodeIfFits(field, 0, n, out, 0) < 0) {
1846
+ return null
1847
+ }
1848
+ return "Basic " + String.fromBytes(out, 0, toI32(out.length))
1804
1849
  }
1805
1850
 
1806
1851
  /** The first value of the field `name`, in any case, from parallel name and value lists; null when there is none. */
@@ -1946,9 +1991,13 @@ const httpClientTlsVerify = (
1946
1991
  */
1947
1992
  const httpClientTlsClose = (conn: TlsRecordClient, fd: i32): void => {
1948
1993
  conn.close()
1949
- const len: i32 = httpClientTlsPending(conn)
1950
- if (fd >= 0 && len > 0) {
1951
- netWrite(fd, conn.output, conn.outputStart, len)
1994
+ const output: u8[] = conn.output
1995
+ const start: i32 = conn.outputStart
1996
+ const end: i32 = conn.outputEnd
1997
+ // The pending window, tested as `httpClientTlsPending` does; one outside
1998
+ // the buffer is nothing to send.
1999
+ if (fd >= 0 && start >= 0 && start < end && end <= toI32(output.length)) {
2000
+ netWrite(fd, output, start, end - start)
1952
2001
  }
1953
2002
  conn.wipe()
1954
2003
  }
@@ -1960,7 +2009,10 @@ const httpClientConnectHead = (host: string, port: i32, proxy: Url, fault: HttpC
1960
2009
  const values: string[] = [authority]
1961
2010
  const auth: string | null = httpClientProxyAuth(proxy)
1962
2011
  if (auth === null) {
1963
- fault.fail(HTTP_E_PROXY, "the proxy's user name or password is not valid percent-encoding")
2012
+ fault.fail(
2013
+ HTTP_E_PROXY,
2014
+ "the proxy's user name or password is not valid percent-encoding, or too long to encode"
2015
+ )
1964
2016
  return null
1965
2017
  }
1966
2018
  if (auth !== "") {
@@ -2193,7 +2245,10 @@ const httpClientRequestHead = (
2193
2245
  const proxy: Url | null = httpClientProxyUrl(httpProxy)
2194
2246
  const auth: string | null = proxy !== null ? httpClientProxyAuth(proxy) : ""
2195
2247
  if (auth === null) {
2196
- fault.fail(HTTP_E_PROXY, "the proxy's user name or password is not valid percent-encoding")
2248
+ fault.fail(
2249
+ HTTP_E_PROXY,
2250
+ "the proxy's user name or password is not valid percent-encoding, or too long to encode"
2251
+ )
2197
2252
  return null
2198
2253
  }
2199
2254
  if (auth !== "" && !hasProxyAuth) {
@@ -2496,7 +2551,8 @@ export class FetchPool {
2496
2551
  if (fd < 0 || routes[i] !== route) {
2497
2552
  continue
2498
2553
  }
2499
- const quiet: boolean = netRead(fd, this.probe, HTTP_CLIENT_ZERO, 1) === HTTP_CLIENT_WOULD_BLOCK
2554
+ const probe: u8[] = this.probe
2555
+ const quiet: boolean = probe.length > 0 && netRead(fd, probe, 0, 1) === HTTP_CLIENT_WOULD_BLOCK
2500
2556
  // Re-said after the read, which the prover takes as a write to any array.
2501
2557
  if (quiet && i < toI32(fds.length) && i < toI32(routes.length)) {
2502
2558
  fds[i] = -1
@@ -2769,7 +2825,14 @@ const httpClientReadRawAsync = async (conn: HttpClientConn, want: i32): Promise<
2769
2825
  if (conn.overdue()) {
2770
2826
  return -1
2771
2827
  }
2772
- let n: i32 = netRead(conn.fd, conn.rbuf, HTTP_CLIENT_ZERO, want)
2828
+ // Every caller asks for at most `HTTP_CLIENT_READ_BYTES`, the buffer's
2829
+ // size; tested before each read, as `readRaw` does, since an `await`
2830
+ // takes what the prover knew of the length.
2831
+ if (want < 0 || want > toI32(conn.rbuf.length)) {
2832
+ conn.fault.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
2833
+ return -1
2834
+ }
2835
+ let n: i32 = netRead(conn.fd, conn.rbuf, 0, want)
2773
2836
  while (n === HTTP_CLIENT_WOULD_BLOCK) {
2774
2837
  const ready = await httpClientWaitPeerAsync(conn, false)
2775
2838
  if (!ready) {
@@ -2778,7 +2841,11 @@ const httpClientReadRawAsync = async (conn: HttpClientConn, want: i32): Promise<
2778
2841
  if (conn.overdue()) {
2779
2842
  return -1
2780
2843
  }
2781
- n = netRead(conn.fd, conn.rbuf, HTTP_CLIENT_ZERO, want)
2844
+ if (want < 0 || want > toI32(conn.rbuf.length)) {
2845
+ conn.fault.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
2846
+ return -1
2847
+ }
2848
+ n = netRead(conn.fd, conn.rbuf, 0, want)
2782
2849
  }
2783
2850
  if (n < 0) {
2784
2851
  conn.lost = true
@@ -2806,7 +2873,14 @@ const httpClientWriteRawAsync = async (
2806
2873
  if (conn.overdue()) {
2807
2874
  return false
2808
2875
  }
2809
- const n: i32 = netWrite(conn.fd, bytes, at, left)
2876
+ // The window's test again, after the calls that took what the prover
2877
+ // knew of it; it passes wherever the first one did.
2878
+ const end: i32 = httpClientWindowEnd(bytes, at, left)
2879
+ if (end < 0 || at > end || end > toI32(bytes.length)) {
2880
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
2881
+ return false
2882
+ }
2883
+ const n: i32 = netWrite(conn.fd, bytes, at, end - at)
2810
2884
  if (n > 0 && n <= left) {
2811
2885
  // At most `off + len`, inside `bytes`, so the window always has an
2812
2886
  // end; the helper measures it so that the sum is proven to fit.
@@ -3203,7 +3277,10 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
3203
3277
  if (i > 15) {
3204
3278
  break
3205
3279
  }
3206
- copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
3280
+ // An address the list does not hold all of is one past its end.
3281
+ if (!tryCopyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)) {
3282
+ break
3283
+ }
3207
3284
  const fd: i32 = tcpConnect(one)
3208
3285
  if (fd < 0) {
3209
3286
  last = fd
@@ -3377,7 +3454,11 @@ const httpClientBodyAsync = async (conn: HttpClientConn, res: FetchResponse, lim
3377
3454
  while (toI32(buf.length) < want) {
3378
3455
  buf.push(0)
3379
3456
  }
3380
- copyBytes(buf, len, conn.parser.body, conn.parser.bodyOff, take)
3457
+ // The parser lent take bytes at bodyOff; a span its body does not hold is its fault, not the peer's.
3458
+ if (!tryCopyBytes(buf, len, conn.parser.body, conn.parser.bodyOff, take)) {
3459
+ conn.fault.fail(HTTP_E_IO, "the body bytes lie outside the parser's buffer")
3460
+ return -1
3461
+ }
3381
3462
  len = want
3382
3463
  }
3383
3464
  event = await httpClientPumpAsync(conn)
@@ -2663,45 +2663,34 @@ const handlerAllowAppend = (value: u8[], bytes: u8[], start: i32, end: i32): voi
2663
2663
  }
2664
2664
 
2665
2665
  /**
2666
- * The first `n` elements' length as a `u16`, at most 65535, and 0 when `n` is
2667
- * not positive: an allocation of a `u16` length is not an `array-length`
2668
- * site, so the helpers below allocate once up to it and push only past it.
2666
+ * `n` copies of `fill`, none when `n` is not positive: one allocation, its
2667
+ * length proven non-negative by the guard.
2669
2668
  */
2670
- const handlerHead = (n: i32): u16 => {
2669
+ const handlerInts = (n: i32, fill: i32): i32[] => {
2671
2670
  if (n <= 0) {
2672
- return 0
2671
+ return []
2673
2672
  }
2674
- return toU16(n < 65535 ? n : toI32(65535))
2675
- }
2676
-
2677
- /** `n` copies of `fill`, none when `n` is not positive: one allocation up to 65535, pushed past it. */
2678
- const handlerInts = (n: i32, fill: i32): i32[] => {
2679
- const out: i32[] = new Array<i32>(handlerHead(n))
2673
+ const out: i32[] = new Array<i32>(n)
2680
2674
  if (fill !== 0) {
2681
2675
  out.fill(fill)
2682
2676
  }
2683
- for (let k: i32 = 65535; k < n; k++) {
2684
- out.push(fill)
2685
- }
2686
2677
  return out
2687
2678
  }
2688
2679
 
2689
2680
  /** `n` flags, each false, made as `handlerInts` makes its numbers. */
2690
2681
  const handlerFlags = (n: i32): boolean[] => {
2691
- const out: boolean[] = new Array<boolean>(handlerHead(n))
2692
- for (let k: i32 = 65535; k < n; k++) {
2693
- out.push(false)
2682
+ if (n <= 0) {
2683
+ return []
2694
2684
  }
2695
- return out
2685
+ return new Array<boolean>(n)
2696
2686
  }
2697
2687
 
2698
2688
  /** `n` zero bytes, made as `handlerInts` makes its numbers. */
2699
2689
  const handlerBytes = (n: i32): u8[] => {
2700
- const out: u8[] = new Array<u8>(handlerHead(n))
2701
- for (let k: i32 = 65535; k < n; k++) {
2702
- out.push(0)
2690
+ if (n <= 0) {
2691
+ return []
2703
2692
  }
2704
- return out
2693
+ return new Array<u8>(n)
2705
2694
  }
2706
2695
 
2707
2696
  /** The length of `Router.match`'s scratch for routes of up to `maxParams` parameters: two a parameter and two more. */
package/std/net/hpack.ts CHANGED
@@ -426,20 +426,22 @@ const hpackWindowEnd = (buf: u8[], off: i32, len: i32): i32 => {
426
426
  }
427
427
 
428
428
  /**
429
- * The panic of a window outside `buf`, `hpackCheckedWindowEnd`'s, kept apart from its test so that the
430
- * test stays small enough to inline; the window is the caller's.
429
+ * Panics with `message` unless `[off, off + len)` lies inside `buf`. In the window contract's shape
430
+ * (`src/window-contract.ts`), so a caller that proved the window has proved this. The message is
431
+ * the caller's constant rather than one built from the window: building it would open an arena scope
432
+ * on every call, failing or not, wherever this inlines.
431
433
  */
432
- const hpackCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
433
- panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
434
+ const hpackCheckWindow = (message: string, buf: u8[], off: i32, len: i32): void => {
435
+ const size: i32 = toI32(buf.length)
436
+ if (off < 0 || len < 0 || off > size || len > size - off) {
437
+ panic(message)
438
+ }
434
439
  }
435
440
 
436
441
  /** Panics unless `[off, off + len)` lies inside `buf`, and answers the window's end, `off + len`. */
437
- const hpackCheckedWindowEnd = (what: string, buf: u8[], off: i32, len: i32): i32 => {
438
- const end: i32 = hpackWindowEnd(buf, off, len)
439
- if (end < 0) {
440
- hpackCheckWindow(what, buf, off, len)
441
- }
442
- return end
442
+ const hpackCheckedWindowEnd = (message: string, buf: u8[], off: i32, len: i32): i32 => {
443
+ hpackCheckWindow(message, buf, off, len)
444
+ return hpackWindowEnd(buf, off, len)
443
445
  }
444
446
 
445
447
  /**
@@ -581,7 +583,7 @@ export class HpackHuffman {
581
583
  * internal fields).
582
584
  */
583
585
  export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i32): i32 => {
584
- hpackCheckedWindowEnd("hpackHuffmanLength", src, off, len)
586
+ hpackCheckWindow("hpackHuffmanLength: the window is outside its buffer", src, off, len)
585
587
  // Unsigned: at most 30 bits for each of fewer than 2^31 octets, far below 2^64.
586
588
  let bits: u64 = 0
587
589
  // Widened, so `off + len` cannot wrap. The window check has already proved
@@ -618,7 +620,23 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
618
620
  * @param out Where the coded bytes go: appended after what it holds, `hpackHuffmanLength` of them.
619
621
  */
620
622
  export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
621
- hpackCheckedWindowEnd("hpackHuffmanEncode", src, off, len)
623
+ hpackCheckWindow("hpackHuffmanEncode: the window is outside its buffer", src, off, len)
624
+ hpackHuffmanAppend(h, src, off, len, out)
625
+ }
626
+
627
+ /**
628
+ * `hpackHuffmanEncode` with no window check, for a caller that checked the
629
+ * window before it wrote anything and has since pushed to an array the
630
+ * window may share: it codes only the octets of `src[off, off + len)` that
631
+ * lie inside `src`, so it cannot panic, and for a window inside `src` it
632
+ * appends what `hpackHuffmanEncode` does.
633
+ * @param h The code to encode with.
634
+ * @param src The octets to encode; only read.
635
+ * @param off Where the window starts, an index into `src`; a negative one codes nothing.
636
+ * @param len How many octets from `off` on to encode.
637
+ * @param out Where the coded bytes go, appended after what it holds.
638
+ */
639
+ export const hpackHuffmanAppend = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
622
640
  let acc: u64 = 0
623
641
  // Unsigned: fewer than eight waiting bits plus one code of at most 31, so it never wraps.
624
642
  let bits: u32 = 0
@@ -665,7 +683,7 @@ export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i3
665
683
  * which only a table cut short (by writing its internal fields) allows.
666
684
  */
667
685
  export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): i32 => {
668
- hpackCheckedWindowEnd("hpackHuffmanDecode", src, off, len)
686
+ hpackCheckWindow("hpackHuffmanDecode: the window is outside its buffer", src, off, len)
669
687
  // Unsigned, and `bits` never passes 30: the code is complete, so thirty bits always end a symbol,
670
688
  // and the row test below gives up before a 31st.
671
689
  let code: u32 = 0
@@ -743,7 +761,7 @@ export class HpackCursor {
743
761
  constructor(buf: u8[], off: i32, len: i32) {
744
762
  this.buf = buf
745
763
  this.pos = off
746
- this.end = hpackCheckedWindowEnd("HpackCursor", buf, off, len)
764
+ this.end = hpackCheckedWindowEnd("HpackCursor: the window is outside its buffer", buf, off, len)
747
765
  }
748
766
 
749
767
  /**
@@ -753,7 +771,7 @@ export class HpackCursor {
753
771
  reset(buf: u8[], off: i32, len: i32): void {
754
772
  this.buf = buf
755
773
  this.pos = off
756
- this.end = hpackCheckedWindowEnd("HpackCursor", buf, off, len)
774
+ this.end = hpackCheckedWindowEnd("HpackCursor: the window is outside its buffer", buf, off, len)
757
775
  }
758
776
  }
759
777
 
@@ -1356,11 +1374,12 @@ export class HpackWork {
1356
1374
  }
1357
1375
  // `classOf` answers below `HPACK_FIELD_CLASSES`, so the room is at most 2^23 and an unsigned shift holds it.
1358
1376
  const room: u32 = toU32(1) << toU32(j)
1359
- // A length is made at most 65535 at once; a larger class's room is filled out by pushes, which leave the same room.
1360
- const made: u8[] = new Array<u8>(toU16(room < toU32(65535) ? room : toU32(65535)))
1361
- while (toU32(made.length) < room) {
1362
- made.push(0)
1377
+ // One allocation of the class's room; the guard proves its length.
1378
+ const size: i32 = room < toU32(8388609) ? toI32(room) : 0
1379
+ if (size <= 0) {
1380
+ return []
1363
1381
  }
1382
+ const made: u8[] = new Array<u8>(size)
1364
1383
  hpackEmpty(made)
1365
1384
  return made
1366
1385
  }
@@ -80,7 +80,12 @@
80
80
  * output buffer of `compressBound(maxLength)` bytes and
81
81
  * the field arrays `addFields` fills, all built once.
82
82
  */
83
- import { DEFLATE_ERR_OUTPUT_LIMIT, DEFLATE_FAST, compressBound } from "nish/compress/deflate"
83
+ import {
84
+ DEFLATE_ERR_OUTPUT_LIMIT,
85
+ DEFLATE_ERR_WINDOW,
86
+ DEFLATE_FAST,
87
+ compressBound,
88
+ } from "nish/compress/deflate"
84
89
  import { adler32 } from "nish/compress/checksum"
85
90
  import { GzipCompressor, GzipDecompressor, GzipFixedCompressor } from "nish/compress/gzip"
86
91
  import { ZlibDecompressor } from "nish/compress/zlib"
@@ -519,7 +524,11 @@ const httpGzipToZlib = (out: u8[], n: i32, level: i32, src: u8[], off: i32, len:
519
524
  for (let i: i32 = 2; i < end && i >= 0 && i + 8 < toI32(out.length); i += 1) {
520
525
  out[i] = out[i + 8]
521
526
  }
522
- const a: u32 = adler32(src, off, len)
527
+ const a64: i64 = adler32(src, off, len)
528
+ if (a64 < 0) {
529
+ return HTTP_ZLIB_FAILED
530
+ }
531
+ const a: u32 = toU32(a64)
523
532
  out[end] = toU8(a >>> 24)
524
533
  out[end + 1] = toU8((a >>> 16) & 255)
525
534
  out[end + 2] = toU8((a >>> 8) & 255)
@@ -613,15 +622,9 @@ export class HttpCompressor {
613
622
  }
614
623
  this.gzip = new GzipCompressor(level)
615
624
  this.fixed = new GzipFixedCompressor()
616
- // Grown a byte at a time, once: a length the compiler cannot see is not
617
- // negative would cost `new Array` a check. No bench/instructions.json row
618
- // builds a compressor, so no counted figure includes this loop.
619
- const output: u8[] = []
625
+ // One zeroed allocation, sized once; the guard proves its length.
620
626
  const bound: i32 = compressBound(maxLength)
621
- while (toI32(output.length) < bound) {
622
- output.push(0)
623
- }
624
- this.output = output
627
+ this.output = bound > 0 ? new Array<u8>(bound) : []
625
628
  this.names = []
626
629
  this.values = []
627
630
  this.contentEncodingName = httpCompressBytes("content-encoding")
@@ -1250,6 +1253,7 @@ export class HttpBodyDecoder {
1250
1253
  if (off < 0 || len < 0 || off > toI32(src.length) - len) {
1251
1254
  return HTTP_DECODE_BAD_CALL
1252
1255
  }
1256
+ const stop: i32 = toI32(toI64(off) + toI64(len))
1253
1257
  const limit: i32 = this.limit
1254
1258
  // A first guess of four times the input, so most bodies decode at once.
1255
1259
  // Under the test, `len` is at most a quarter of `limit`, so four times it
@@ -1269,13 +1273,18 @@ export class HttpBodyDecoder {
1269
1273
  while (toI32(output.length) < size) {
1270
1274
  output.push(0)
1271
1275
  }
1276
+ // The range decoders answer a misuse rather than panicking, so a body
1277
+ // decoded here reaches no panic; the test above has ruled it out.
1272
1278
  const end: i32 =
1273
1279
  coding === HTTP_CODING_GZIP
1274
- ? this.gunzip.decompress(src, off, len, this.output, toI32(0), limit)
1275
- : this.unzlib.decompress(src, off, len, this.output, toI32(0), limit)
1280
+ ? this.gunzip.decompressRange(src, off, stop, output, toI32(0), limit)
1281
+ : this.unzlib.decompressRange(src, off, stop, output, toI32(0), limit)
1276
1282
  if (end >= 0) {
1277
1283
  return end
1278
1284
  }
1285
+ if (end === DEFLATE_ERR_WINDOW) {
1286
+ return HTTP_DECODE_BAD_CALL
1287
+ }
1279
1288
  if (end !== DEFLATE_ERR_OUTPUT_LIMIT) {
1280
1289
  return HTTP_DECODE_MALFORMED
1281
1290
  }
@@ -123,7 +123,7 @@ import {
123
123
  H3_REQUEST_CANCELLED,
124
124
  H3_REQUEST_REJECTED,
125
125
  } from "nish/net/http3-frame"
126
- import { H3_SERVER_RECEIVE_BURST, Http3Server } from "nish/net/http3-server"
126
+ import { H3_SERVER_RECEIVE_BURST, Http3Server, openHttp3Server } from "nish/net/http3-server"
127
127
  import { QUIC_CONN_CID_LENGTH, QuicServerConfig } from "nish/net/quic"
128
128
  import { QUIC_MAX_CID_LENGTH } from "nish/net/quic-packet"
129
129
  import {
@@ -1530,8 +1530,9 @@ export class HttpServer {
1530
1530
  * field on every HTTP/1.1 and HTTP/2 response. Each connection holds at most
1531
1531
  * `quic.maxStreamsBidi` requests and a quarter of `limits.requests`; a
1532
1532
  * stream past that is rejected with H3_REQUEST_REJECTED. Panics on a second
1533
- * HTTP/3 listener, a `maxFieldSectionSize` past `limits.maxHead`, or
1534
- * `webtransportSessions` above 0. Serve it with `waitSigned`.
1533
+ * HTTP/3 listener, a `maxFieldSectionSize` past `limits.maxHead`,
1534
+ * `webtransportSessions` above 0, or a `slots` or timeout `openHttp3Server`
1535
+ * refuses (slots 1 to 65536, timeouts at least 1). Serve it with `waitSigned`.
1535
1536
  * @param fd the UDP socket's descriptor, still the program's to close
1536
1537
  * @param quic the QUIC and TLS settings
1537
1538
  * @param config the HTTP/3 caps
@@ -1553,12 +1554,16 @@ export class HttpServer {
1553
1554
  if (slots < 1) {
1554
1555
  panic(`HttpServer.listenHttp3: slots ${slots} is below 1`)
1555
1556
  }
1557
+ const opened = openHttp3Server(quic, config, fd, slots, entropy)
1558
+ if (!opened.ok) {
1559
+ panic(`HttpServer.listenHttp3: ${opened.error}`)
1560
+ }
1556
1561
  let per: i64 = toI64(toI32(this.requests.length) / 4)
1557
1562
  if (quic.maxStreamsBidi < per) {
1558
1563
  per = quic.maxStreamsBidi
1559
1564
  }
1560
1565
  this.perConnection3 = per < toI64(1) ? 1 : toI32(per)
1561
- this.h3 = new Http3Server(quic, config, fd, slots, entropy)
1566
+ this.h3 = opened.value
1562
1567
  this.h3Fd = fd
1563
1568
  this.h3Gen = httpServerZeros(toI64(slots), toI32(0))
1564
1569
  this.h3Count = httpServerZeros(toI64(slots), toI32(0))