nish 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -119,7 +119,7 @@ const HKDF_LABEL_PREFIX: string = "tls13 "
119
119
  * code says what the RFC says.
120
120
  */
121
121
  const hkdfSalt = (salt: u8[], hashLen: i32): u8[] =>
122
- toI32(salt.length) === 0 ? new Array<u8>(hashLen) : salt
122
+ toI32(salt.length) === 0 ? new Array<u8>(toU16(hashLen)) : salt
123
123
 
124
124
  /**
125
125
  * Copies `block[0 .. n)` to `out[at .. at + n)`, where `n` is as much of the
@@ -128,12 +128,25 @@ const hkdfSalt = (salt: u8[], hashLen: i32): u8[] =>
128
128
  const hkdfAppend = (out: u8[], at: i32, block: u8[]): i32 => {
129
129
  const outLength: i32 = toI32(out.length)
130
130
  const blockLength: i32 = toI32(block.length)
131
- let k: i32 = 0
132
- while (k < blockLength && at + k < outLength) {
133
- out[at + k] = block[k]
134
- k += 1
131
+ let p: i32 = at
132
+ for (let k: i32 = 0; k < blockLength && p >= 0 && p < outLength; k += 1) {
133
+ out[p] = block[k]
134
+ p += 1
135
+ }
136
+ return p
137
+ }
138
+
139
+ /**
140
+ * Writes `byte` to `out[at]` and answers `at + 1`. Every caller writes inside
141
+ * a buffer it sized for the bytes it puts there; the test is the shape that
142
+ * proves the store, not a check a caller can fail.
143
+ */
144
+ const hkdfPut = (out: u8[], at: i32, byte: u8): i32 => {
145
+ if (at >= 0 && at < toI32(out.length)) {
146
+ out[at] = byte
147
+ return at + 1
135
148
  }
136
- return at + k
149
+ return at
137
150
  }
138
151
 
139
152
  /**
@@ -177,17 +190,21 @@ export const hkdfExpandSha256 = (prk: u8[], info: u8[], length: i32): u8[] | nul
177
190
  ) {
178
191
  return null
179
192
  }
180
- const out: u8[] = new Array<u8>(length)
193
+ // At most 255 blocks of 48 bytes, so `toU16` keeps every length exactly.
194
+ const out: u8[] = new Array<u8>(toU16(length))
181
195
  const counter: u8[] = new Array<u8>(1)
182
196
  const counterLength: i32 = 1
183
197
  let previous: u8[] = []
184
198
  let at: i32 = 0
185
- let i: i32 = 1
199
+ // At most 255, so the byte below is the counter itself.
200
+ let i: u32 = 1
186
201
  while (at < length) {
187
202
  const mac = new HmacSha256(prk)
188
203
  mac.update(previous, HKDF_FROM, toI32(previous.length))
189
204
  mac.update(info, HKDF_FROM, toI32(info.length))
190
- counter[0] = toU8(i)
205
+ if (toI32(counter.length) >= 1) {
206
+ counter[0] = toU8(i)
207
+ }
191
208
  mac.update(counter, HKDF_FROM, counterLength)
192
209
  const block: u8[] = mac.digest()
193
210
  wipe(previous)
@@ -220,17 +237,21 @@ export const hkdfExpandSha384 = (prk: u8[], info: u8[], length: i32): u8[] | nul
220
237
  ) {
221
238
  return null
222
239
  }
223
- const out: u8[] = new Array<u8>(length)
240
+ // At most 255 blocks of 48 bytes, so `toU16` keeps every length exactly.
241
+ const out: u8[] = new Array<u8>(toU16(length))
224
242
  const counter: u8[] = new Array<u8>(1)
225
243
  const counterLength: i32 = 1
226
244
  let previous: u8[] = []
227
245
  let at: i32 = 0
228
- let i: i32 = 1
246
+ // At most 255, so the byte below is the counter itself.
247
+ let i: u32 = 1
229
248
  while (at < length) {
230
249
  const mac = new HmacSha384(prk)
231
250
  mac.update(previous, HKDF_FROM, toI32(previous.length))
232
251
  mac.update(info, HKDF_FROM, toI32(info.length))
233
- counter[0] = toU8(i)
252
+ if (toI32(counter.length) >= 1) {
253
+ counter[0] = toU8(i)
254
+ }
234
255
  mac.update(counter, HKDF_FROM, counterLength)
235
256
  const block: u8[] = mac.digest()
236
257
  wipe(previous)
@@ -411,22 +432,18 @@ const hkdfLabel = (caller: string, label: string, context: u8[], length: i32): u
411
432
  const full: string = `${HKDF_LABEL_PREFIX}${label}`
412
433
  const fullLength: i32 = toI32(full.length)
413
434
  const contextLength: i32 = toI32(context.length)
414
- const out: u8[] = new Array<u8>(4 + fullLength + contextLength)
415
- out[0] = toU8(length >> 8)
416
- out[1] = toU8(length & 0xff)
417
- out[2] = toU8(fullLength)
418
- for (let k: i32 = 0; k < fullLength; k += 1) {
419
- // `out` is longer than `3 + fullLength`, but the proof cannot read that
420
- // off the sum it was allocated with; this test is what proves the store.
421
- // It sits in the body rather than the loop condition, which stays the
422
- // plain `k < fullLength` the attribute pass counts as a bounded loop.
423
- if (3 + k >= toI32(out.length)) {
424
- panic("hkdfLabel: the label overran its buffer")
425
- }
426
- out[3 + k] = toU8(toI32(full.charCodeAt(k)))
427
- }
428
- out[3 + fullLength] = toU8(contextLength)
429
- hkdfAppend(out, 4 + fullLength, context)
435
+ // At most 4 + 255 + 255 bytes after the tests above, so the `u32` sum is
436
+ // exact and `toU16` keeps it.
437
+ const out: u8[] = new Array<u8>(toU16(toU32(fullLength) + toU32(contextLength) + 4))
438
+ let p: i32 = hkdfPut(out, 0, toU8(length >> 8))
439
+ p = hkdfPut(out, p, toU8(length & 0xff))
440
+ p = hkdfPut(out, p, toU8(fullLength))
441
+ for (let k: i32 = 0; k < fullLength && p >= 0 && p < toI32(out.length); k += 1) {
442
+ out[p] = toU8(toI32(full.charCodeAt(k)))
443
+ p += 1
444
+ }
445
+ p = hkdfPut(out, p, toU8(contextLength))
446
+ hkdfAppend(out, p, context)
430
447
  return out
431
448
  }
432
449
 
@@ -637,6 +654,15 @@ const hkdfMacUpdate = (s: HkdfScratch, hashLength: i32, data: u8[], off: i32, le
637
654
  }
638
655
  }
639
656
 
657
+ /** Feeds the whole of `data`, at any length, to the scratch's HMAC for `hashLength`. */
658
+ const hkdfMacUpdateAll = (s: HkdfScratch, hashLength: i32, data: u8[]): void => {
659
+ if (hashLength === SHA384_SIZE) {
660
+ s.sha384.inner.engine.absorbAll(data)
661
+ } else {
662
+ s.sha256.inner.updateAll(data)
663
+ }
664
+ }
665
+
640
666
  /** Writes the HMAC for `hashLength`'s tag at `out[at]`. */
641
667
  const hkdfMacFinish = (s: HkdfScratch, hashLength: i32, out: u8[], at: i32): void => {
642
668
  if (hashLength === SHA384_SIZE) {
@@ -650,8 +676,8 @@ const hkdfMacFinish = (s: HkdfScratch, hashLength: i32, out: u8[], at: i32): voi
650
676
  * HKDF-Extract (RFC 5869 §2.2) over the hash `hashLength` names, the PRK's
651
677
  * HashLen bytes written at `out[at]`. An empty `salt` keys HMAC with no bytes,
652
678
  * which HMAC pads to the block with zeros, exactly as the HashLen zeros §2.2
653
- * asks for would be. An `ikm` longer than 2^31 - 1 bytes panics, as
654
- * `hmacSha256` does.
679
+ * asks for would be. The whole `ikm` is hashed, at any length, as
680
+ * `hmacSha256` hashes its message.
655
681
  *
656
682
  * @param s The scratch to compute in; what the call leaves in it is wiped.
657
683
  * @param hashLength 32 for HMAC-SHA-256, 48 for HMAC-SHA-384.
@@ -671,11 +697,8 @@ export const hkdfExtractInto = (
671
697
  ): void => {
672
698
  hkdfCheckHash("hkdfExtractInto", hashLength)
673
699
  hkdfCheckOutput("hkdfExtractInto", out, at, hashLength)
674
- if (ikm.length > 2147483647) {
675
- panic("hkdfExtractInto: input keying material longer than 2^31 - 1 bytes")
676
- }
677
700
  hkdfMacBegin(s, hashLength, salt, HKDF_FROM, toI32(salt.length))
678
- hkdfMacUpdate(s, hashLength, ikm, HKDF_FROM, toI32(ikm.length))
701
+ hkdfMacUpdateAll(s, hashLength, ikm)
679
702
  hkdfMacFinish(s, hashLength, out, at)
680
703
  hkdfWipe(s, hashLength)
681
704
  }
@@ -696,19 +719,30 @@ const hkdfExpandWindow = (
696
719
  length: i32
697
720
  ): void => {
698
721
  const counterLength: i32 = 1
722
+ // `p` runs through the window `out[at .. at + length)`; the bounds on it in
723
+ // the loops only let the prover see what the callers checked. The counter is
724
+ // a `u32`, which wraps rather than overflows; it never passes 255.
725
+ let p: i32 = at
699
726
  let done: i32 = 0
700
- let i: i32 = 1
701
- while (done < length) {
727
+ let i: u32 = 1
728
+ while (done < length && p >= 0 && p < toI32(out.length)) {
702
729
  hkdfMacBegin(s, hashLength, prk, HKDF_FROM, toI32(prk.length))
703
730
  if (i > 1) {
704
731
  hkdfMacUpdate(s, hashLength, s.previous, HKDF_FROM, hashLength)
705
732
  }
706
733
  hkdfMacUpdate(s, hashLength, info, HKDF_FROM, infoLen)
707
- s.counter[0] = toU8(i)
734
+ if (toI32(s.counter.length) >= 1) {
735
+ s.counter[0] = toU8(i)
736
+ }
708
737
  hkdfMacUpdate(s, hashLength, s.counter, HKDF_FROM, counterLength)
709
738
  hkdfMacFinish(s, hashLength, s.previous, HKDF_FROM)
710
- for (let k: i32 = 0; k < hashLength && done < length && k < toI32(s.previous.length); k += 1) {
711
- out[at + done] = s.previous[k]
739
+ for (
740
+ let k: i32 = 0;
741
+ k < hashLength && done < length && p >= 0 && p < toI32(out.length) && k < toI32(s.previous.length);
742
+ k += 1
743
+ ) {
744
+ out[p] = s.previous[k]
745
+ p += 1
712
746
  done += 1
713
747
  }
714
748
  i += 1
@@ -747,7 +781,7 @@ export const hkdfExpandInto = (
747
781
  hkdfCheckHash("hkdfExpandInto", hashLength)
748
782
  if (
749
783
  length < 0 ||
750
- length > HKDF_MAX_BLOCKS * hashLength ||
784
+ length > (hashLength === SHA384_SIZE ? HKDF_MAX_BLOCKS * SHA384_SIZE : HKDF_MAX_BLOCKS * SHA256_SIZE) ||
751
785
  info.length > 2147483647 ||
752
786
  toI32(prk.length) < hashLength
753
787
  ) {
@@ -800,8 +834,8 @@ export const hkdfExpandLabelInto = (
800
834
  if (
801
835
  contextOff < 0 ||
802
836
  contextLen < 0 ||
803
- contextOff > toI32(context.length) - contextLen ||
804
- contextLen > 255
837
+ contextLen > 255 ||
838
+ contextOff > toI32(context.length) - contextLen
805
839
  ) {
806
840
  panic("hkdfExpandLabelInto: the context window is outside its array or longer than 255 bytes")
807
841
  }
@@ -809,26 +843,36 @@ export const hkdfExpandLabelInto = (
809
843
  panic("hkdfExpandLabelInto: a secret shorter than HashLen")
810
844
  }
811
845
  hkdfCheckOutput("hkdfExpandLabelInto", out, at, length)
812
- // The `HkdfLabel` struct, as `hkdfLabel` builds it, into the scratch.
813
- const prefixLength: i32 = toI32(HKDF_LABEL_PREFIX.length)
846
+ // The `HkdfLabel` struct, as `hkdfLabel` builds it, into the scratch, whose
847
+ // HKDF_LABEL_MAX bytes hold the longest one the tests above let through.
848
+ const prefix: string = HKDF_LABEL_PREFIX
849
+ const prefixLength: i32 = toI32(prefix.length)
814
850
  const labelLength: i32 = toI32(label.length)
815
- const fullLength: i32 = prefixLength + labelLength
816
851
  const info: u8[] = s.label
817
- info[0] = toU8(length >> 8)
818
- info[1] = toU8(length & 0xff)
819
- info[2] = toU8(fullLength)
820
- for (let k: i32 = 0; k < prefixLength && 3 + k < toI32(info.length); k += 1) {
821
- info[3 + k] = toU8(toI32(HKDF_LABEL_PREFIX.charCodeAt(k)))
822
- }
823
- for (let k: i32 = 0; k < labelLength && 3 + prefixLength + k < toI32(info.length); k += 1) {
824
- info[3 + prefixLength + k] = toU8(toI32(label.charCodeAt(k)))
825
- }
826
- const contextAt: i32 = 3 + fullLength
827
- info[contextAt] = toU8(contextLen)
828
- for (let k: i32 = 0; k < contextLen && contextAt + 1 + k < toI32(info.length); k += 1) {
829
- info[contextAt + 1 + k] = context[contextOff + k]
852
+ const infoLength: i32 = toI32(info.length)
853
+ let p: i32 = hkdfPut(info, 0, toU8(length >> 8))
854
+ p = hkdfPut(info, p, toU8(length & 0xff))
855
+ p = hkdfPut(info, p, toU8(toU32(prefixLength) + toU32(labelLength)))
856
+ for (let k: i32 = 0; k < prefixLength && p >= 0 && p < infoLength; k += 1) {
857
+ info[p] = toU8(toI32(prefix.charCodeAt(k)))
858
+ p += 1
859
+ }
860
+ for (let k: i32 = 0; k < labelLength && p >= 0 && p < infoLength; k += 1) {
861
+ info[p] = toU8(toI32(label.charCodeAt(k)))
862
+ p += 1
863
+ }
864
+ p = hkdfPut(info, p, toU8(contextLen))
865
+ let q: i32 = contextOff
866
+ for (
867
+ let k: i32 = 0;
868
+ k < contextLen && q >= 0 && q < toI32(context.length) && p >= 0 && p < infoLength;
869
+ k += 1
870
+ ) {
871
+ info[p] = context[q]
872
+ p += 1
873
+ q += 1
830
874
  }
831
- hkdfExpandWindow(s, hashLength, prk, info, contextAt + 1 + contextLen, out, at, length)
875
+ hkdfExpandWindow(s, hashLength, prk, info, p, out, at, length)
832
876
  }
833
877
 
834
878
  // ---- From and into a held `Secret` -----------------------------------------
@@ -868,7 +912,7 @@ const hkdfHeldDerive = (
868
912
  : exposeWith(prk, request, hkdfExpandSha384Exposed)
869
913
  if (okm === null) {
870
914
  panic(
871
- `${caller}: a secret shorter than ${hashLength} bytes, or more than ${255 * hashLength} bytes asked`
915
+ `${caller}: a secret shorter than ${hashLength} bytes, or more than ${toU32(hashLength) * 255} bytes asked`
872
916
  )
873
917
  }
874
918
  return okm
@@ -965,8 +1009,6 @@ export const hkdfExpandLabelHeldInto = (
965
1009
  ): void => {
966
1010
  hkdfCheckOutput("hkdfExpandLabelHeldInto", out, at, length)
967
1011
  const okm: u8[] = hkdfHeldDerive("hkdfExpandLabelHeldInto", hashLength, prk, label, context, length)
968
- for (let k: i32 = 0; k < length && k < toI32(okm.length); k++) {
969
- out[at + k] = okm[k]
970
- }
1012
+ hkdfAppend(out, at, okm)
971
1013
  wipe(okm)
972
1014
  }