nish 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/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/package.json +5 -5
  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
@@ -167,6 +167,9 @@ const TLS_CLIENT_FROM: i32 = 0
167
167
  /** The legacy_session_id the client sends (§4.1.2, §D.4): 32 bytes, so that middleboxes see a resumption. */
168
168
  const TLS_CLIENT_SESSION_ID_SIZE: i32 = 32
169
169
 
170
+ /** The client's longest Finished: its header (4) and a SHA-384 verify_data. */
171
+ const TLS_CLIENT_FINISHED_MAX: i32 = 4 + SHA384_SIZE
172
+
170
173
  /** The longest `server_name` a ClientHello carries: a DNS name's 253 characters, under the extension's own bound. */
171
174
  const TLS_CLIENT_SERVER_NAME_MAX: i32 = 253
172
175
 
@@ -397,7 +400,7 @@ export class TlsClient {
397
400
  this.firstHello = []
398
401
  this.input = new Array<u8>(1024)
399
402
  this.outputInitial = []
400
- this.outputHandshake = new Array<u8>(4 + SHA384_SIZE)
403
+ this.outputHandshake = new Array<u8>(TLS_CLIENT_FINISHED_MAX)
401
404
  this.secrets256 = new TlsSecretPool(SHA256_SIZE)
402
405
  this.secrets384 = new TlsSecretPool(SHA384_SIZE)
403
406
  this.view = new TlsServerFlightView()
@@ -433,7 +436,8 @@ export class TlsClient {
433
436
  }
434
437
  const at: i32 = this.outputInitialLength
435
438
  const end: i32 = this.writeHello()
436
- if (end < 0) {
439
+ // writeHello ends a hello at or below 2^30 - 1 and after `at`, so these never fail.
440
+ if (at < 0 || at > 1073741823 || end < 0 || end > 1073741823) {
437
441
  return this.fail(TLS_ALERT_INTERNAL_ERROR)
438
442
  }
439
443
  this.firstHello = tlsCopyWindow(this.outputInitial, at, end - at)
@@ -471,9 +475,15 @@ export class TlsClient {
471
475
  this.config.alpn,
472
476
  this.cookie
473
477
  )
474
- const before: i32 =
475
- TLS_CLIENT_HELLO_FIXED + toI32(this.sessionId.length) + 2 * toI32(this.config.suites.length)
476
- if (length - before > 65535) {
478
+ // The writer answers -1 past 2^30 - 1, and the Initial output never grows past it.
479
+ if (length < 0 || length > 1073741823 || at < 0 || at > 1073741823) {
480
+ return -1
481
+ }
482
+ const before: i64 =
483
+ toI64(TLS_CLIENT_HELLO_FIXED) +
484
+ toI64(toI32(this.sessionId.length)) +
485
+ toI64(2) * toI64(toI32(this.config.suites.length))
486
+ if (toI64(length) - before > toI64(65535)) {
477
487
  return -1
478
488
  }
479
489
  this.reserveOutput(at + length)
@@ -490,7 +500,9 @@ export class TlsClient {
490
500
  this.config.alpn,
491
501
  this.cookie
492
502
  )
493
- this.sent(TLS_LEVEL_INITIAL, at, end)
503
+ if (end < 0 || !this.sent(TLS_LEVEL_INITIAL, at, end)) {
504
+ return -1
505
+ }
494
506
  return end
495
507
  }
496
508
 
@@ -591,14 +603,18 @@ export class TlsClient {
591
603
  if (need <= capacity) {
592
604
  return
593
605
  }
594
- let size: i32 = capacity
595
- while (size < need) {
606
+ // `most` is 65,540, so the doubling stops long before it could overflow.
607
+ const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
608
+ let size: i32 = capacity < 1 ? 1 : capacity
609
+ while (size < need && size < most && size >= 1 && size <= 1073741823) {
596
610
  size = size * 2
597
611
  }
598
- const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
599
- const grown: u8[] = new Array<u8>(size > most ? most : size)
600
- for (let k: i32 = 0; k < this.inputLength && k < toI32(grown.length) && k < capacity; k++) {
601
- grown[k] = this.input[k]
612
+ const target: i32 = size > most ? most : size
613
+ const old: u8[] = this.input
614
+ const held: i32 = this.inputLength
615
+ const grown: u8[] = []
616
+ for (let k: i32 = 0; k < target; k++) {
617
+ grown.push(k < held && k < toI32(old.length) ? old[k] : 0)
602
618
  }
603
619
  this.input = grown
604
620
  }
@@ -609,9 +625,11 @@ export class TlsClient {
609
625
  if (need <= capacity) {
610
626
  return
611
627
  }
612
- const grown: u8[] = new Array<u8>(need)
613
- for (let k: i32 = 0; k < toI32(grown.length) && k < this.outputInitialLength && k < capacity; k++) {
614
- grown[k] = this.outputInitial[k]
628
+ const old: u8[] = this.outputInitial
629
+ const held: i32 = this.outputInitialLength
630
+ const grown: u8[] = []
631
+ for (let k: i32 = 0; k < need; k++) {
632
+ grown.push(k < held && k < toI32(old.length) ? old[k] : 0)
615
633
  }
616
634
  this.outputInitial = grown
617
635
  }
@@ -651,28 +669,42 @@ export class TlsClient {
651
669
  return this.fail(TLS_ALERT_UNEXPECTED_MESSAGE)
652
670
  }
653
671
  const most: i32 = TLS_MAX_HANDSHAKE_MESSAGE + 4
654
- let taken: i32 = 0
655
- while (taken < len) {
672
+ // `off + len` is within `data`, so stepping `from` through it never wraps.
673
+ let from: i32 = off
674
+ let rest: i32 = len
675
+ while (rest > 0) {
656
676
  // A level's bytes may hold several messages, so they are taken a
657
677
  // largest message at a time, each read before the next is copied in.
658
678
  if (level !== this.expectedLevel()) {
659
679
  return this.fail(TLS_ALERT_UNEXPECTED_MESSAGE)
660
680
  }
661
- const room: i32 = most - this.inputLength
662
- if (room <= 0) {
681
+ const held: i32 = this.inputLength
682
+ // `drain` keeps `input` below one largest message, so `held` is never out of range.
683
+ if (held < 0 || held > 65540 || from < 0 || rest < 1) {
684
+ return this.fail(TLS_ALERT_INTERNAL_ERROR)
685
+ }
686
+ const room: i32 = most - held
687
+ if (room <= 0 || room > 65540) {
663
688
  return this.fail(TLS_ALERT_DECODE_ERROR)
664
689
  }
665
- const take: i32 = len - taken < room ? len - taken : room
666
- this.reserveInput(this.inputLength + take)
690
+ let take: i32 = room
691
+ if (rest < room) {
692
+ take = rest
693
+ }
694
+ this.reserveInput(held + take)
695
+ const input: u8[] = this.input
696
+ let at: i32 = held
667
697
  for (
668
- let k: i32 = 0;
669
- k < take && off + taken + k < toI32(data.length) && this.inputLength < toI32(this.input.length);
670
- k++
698
+ let j: i32 = from;
699
+ j < toI32(data.length) && j - from < take && at >= 0 && at < toI32(input.length);
700
+ j++
671
701
  ) {
672
- this.input[this.inputLength] = data[off + taken + k]
673
- this.inputLength = this.inputLength + 1
702
+ input[at] = data[j]
703
+ at++
674
704
  }
675
- taken = taken + take
705
+ this.inputLength = at
706
+ from = toI32(toI64(from) + toI64(take))
707
+ rest = rest - take
676
708
  const alert: i32 = this.drain()
677
709
  if (alert !== 0) {
678
710
  return this.fail(alert)
@@ -688,20 +720,28 @@ export class TlsClient {
688
720
  */
689
721
  private drain(): i32 {
690
722
  while (this.state !== TLS_CLIENT_STATE_WAIT_AUTH && this.inputLength >= 4) {
691
- const length: i32 = (toI32(this.input[1]) << 16) | (toI32(this.input[2]) << 8) | toI32(this.input[3])
692
- if (length > TLS_MAX_HANDSHAKE_MESSAGE) {
723
+ const input: u8[] = this.input
724
+ // `input` holds at least `inputLength` bytes, so this never answers.
725
+ if (toI32(input.length) < 4) {
726
+ return TLS_ALERT_INTERNAL_ERROR
727
+ }
728
+ const length: i32 = (toI32(input[1]) << 16) | (toI32(input[2]) << 8) | toI32(input[3])
729
+ // A 24-bit length is never negative; the second bound is the prover's copy of the first.
730
+ if (length > TLS_MAX_HANDSHAKE_MESSAGE || length < 0 || length > 65536) {
693
731
  return TLS_ALERT_DECODE_ERROR
694
732
  }
695
733
  const whole: i32 = 4 + length
696
734
  if (this.inputLength < whole) {
697
735
  return 0
698
736
  }
699
- const type: i32 = toI32(this.input[0])
737
+ const type: i32 = toI32(input[0])
700
738
  const alert: i32 = this.handleMessage(type, length)
701
739
  if (alert !== 0) {
702
740
  return alert
703
741
  }
704
- this.consume(whole)
742
+ if (!this.consume(whole)) {
743
+ return TLS_ALERT_INTERNAL_ERROR
744
+ }
705
745
  const endsFlight: boolean = type === TLS_HANDSHAKE_SERVER_HELLO || type === TLS_HANDSHAKE_FINISHED
706
746
  if (endsFlight && this.inputLength > 0) {
707
747
  return TLS_ALERT_UNEXPECTED_MESSAGE
@@ -710,13 +750,33 @@ export class TlsClient {
710
750
  return 0
711
751
  }
712
752
 
713
- /** Drops the first `count` bytes of `input`, moving the rest to its front. */
714
- private consume(count: i32): void {
715
- const rest: i32 = this.inputLength - count
716
- for (let k: i32 = 0; k < rest && count + k < toI32(this.input.length); k++) {
717
- this.input[k] = this.input[count + k]
753
+ /**
754
+ * Drops the first `count` bytes of `input`, moving the rest to its front;
755
+ * false, dropping nothing, for a `count` past what is held, which `drain`
756
+ * then answers with `internal_error`.
757
+ */
758
+ private consume(count: i32): boolean {
759
+ const held: i32 = this.inputLength
760
+ // `drain` drops a whole message it holds, so `count` is never past `held`.
761
+ if (count < 0 || count > 65540 || held < 0 || held > 65540 || count > held) {
762
+ return false
763
+ }
764
+ const rest: i32 = held - count
765
+ const input: u8[] = this.input
766
+ // What is held lies inside `input`, so the move below copies all of it;
767
+ // the guards inside the loop are the prover's.
768
+ if (held > toI32(input.length)) {
769
+ return false
770
+ }
771
+ for (let k: i32 = 0; k < rest && k < toI32(input.length); k++) {
772
+ const at: i32 = count + k
773
+ if (at >= toI32(input.length)) {
774
+ break
775
+ }
776
+ input[k] = input[at]
718
777
  }
719
778
  this.inputLength = rest
779
+ return true
720
780
  }
721
781
 
722
782
  /** Dispatches one whole message, `input[0 .. 4 + length)`, of type `type`, by state. */
@@ -794,6 +854,19 @@ export class TlsClient {
794
854
  return 0
795
855
  }
796
856
 
857
+ /**
858
+ * Hashes the message `input[0 .. 4 + length)` into the transcript; false,
859
+ * hashing nothing, for a length `drain` refuses (past 65,536), so a caller
860
+ * fails the handshake rather than go on with a transcript short a message.
861
+ */
862
+ private hashMessage(length: i32): boolean {
863
+ if (length < 0 || length > 65536) {
864
+ return false
865
+ }
866
+ this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
867
+ return true
868
+ }
869
+
797
870
  /** A ServerHello or HelloRetryRequest, `input[0 .. 4 + length)`. */
798
871
  private handleServerHello(length: i32): i32 {
799
872
  const view: TlsServerFlightView = this.view
@@ -822,7 +895,9 @@ export class TlsClient {
822
895
  if (!this.retried) {
823
896
  this.startTranscript(view.suite)
824
897
  }
825
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
898
+ if (!this.hashMessage(length)) {
899
+ return TLS_ALERT_INTERNAL_ERROR
900
+ }
826
901
  const alert: i32 = this.exchangeKeys()
827
902
  if (alert !== 0) {
828
903
  return alert
@@ -871,7 +946,9 @@ export class TlsClient {
871
946
  this.retried = true
872
947
  this.startTranscript(view.suite)
873
948
  this.transcript.restartWithMessageHash()
874
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
949
+ if (!this.hashMessage(length)) {
950
+ return TLS_ALERT_INTERNAL_ERROR
951
+ }
875
952
  if (view.hasCookie) {
876
953
  this.cookie = tlsCopyWindow(view.data, view.cookieAt, view.cookieLength)
877
954
  }
@@ -957,7 +1034,9 @@ export class TlsClient {
957
1034
  }
958
1035
  this.alpnChosen = chosen
959
1036
  }
960
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
1037
+ if (!this.hashMessage(length)) {
1038
+ return TLS_ALERT_INTERNAL_ERROR
1039
+ }
961
1040
  this.state = TLS_CLIENT_STATE_WAIT_CERTIFICATE
962
1041
  return 0
963
1042
  }
@@ -976,7 +1055,9 @@ export class TlsClient {
976
1055
  return TLS_ALERT_ILLEGAL_PARAMETER
977
1056
  }
978
1057
  this.certificates = tlsCopyCertificates(view)
979
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
1058
+ if (!this.hashMessage(length)) {
1059
+ return TLS_ALERT_INTERNAL_ERROR
1060
+ }
980
1061
  this.content = tlsCertificateVerifyContent(this.transcript.hash())
981
1062
  this.state = TLS_CLIENT_STATE_WAIT_CERTIFICATE_VERIFY
982
1063
  return 0
@@ -994,7 +1075,9 @@ export class TlsClient {
994
1075
  }
995
1076
  this.scheme = view.scheme
996
1077
  this.signature = tlsCopyWindow(view.data, view.signatureAt, view.signatureLength)
997
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
1078
+ if (!this.hashMessage(length)) {
1079
+ return TLS_ALERT_INTERNAL_ERROR
1080
+ }
998
1081
  this.state = TLS_CLIENT_STATE_WAIT_AUTH
999
1082
  return 0
1000
1083
  }
@@ -1018,7 +1101,9 @@ export class TlsClient {
1018
1101
  if (!matches) {
1019
1102
  return TLS_ALERT_DECRYPT_ERROR
1020
1103
  }
1021
- this.transcript.update(this.input, TLS_CLIENT_FROM, 4 + length)
1104
+ if (!this.hashMessage(length)) {
1105
+ return TLS_ALERT_INTERNAL_ERROR
1106
+ }
1022
1107
  this.transcript.hashInto(this.hashes, TLS_CLIENT_FROM)
1023
1108
  // The master secret is derived over the handshake secret, which has no
1024
1109
  // other use left.
@@ -1045,7 +1130,9 @@ export class TlsClient {
1045
1130
  const at: i32 = this.outputHandshakeLength
1046
1131
  const end: i32 = tlsWriteFinished(this.outputHandshake, at, verifyData, TLS_CLIENT_FROM, h)
1047
1132
  secureZero(verifyData)
1048
- this.sent(TLS_LEVEL_HANDSHAKE, at, end)
1133
+ if (!this.sent(TLS_LEVEL_HANDSHAKE, at, end)) {
1134
+ return TLS_ALERT_INTERNAL_ERROR
1135
+ }
1049
1136
  this.held =
1050
1137
  (this.held |
1051
1138
  (1 << TLS_SECRET_CLIENT_APPLICATION) |
@@ -1158,8 +1245,15 @@ export class TlsClient {
1158
1245
  return this.fail(alert >= 1 && alert <= 255 ? alert : TLS_ALERT_BAD_CERTIFICATE)
1159
1246
  }
1160
1247
 
1161
- /** A message the client wrote at `level`, `output[at .. end)`, goes into the transcript once its hash is known and stays on the output. */
1162
- private sent(level: i32, at: i32, end: i32): void {
1248
+ /**
1249
+ * A message the client wrote at `level`, `output[at .. end)`, goes into the transcript once its hash
1250
+ * is known and stays on the output. Answers false, taking nothing, for a window past 2^30 - 1.
1251
+ */
1252
+ private sent(level: i32, at: i32, end: i32): boolean {
1253
+ // Every writer ends at or below 2^30 - 1, so this never refuses.
1254
+ if (at < 0 || at > 1073741823 || end < 0 || end > 1073741823) {
1255
+ return false
1256
+ }
1163
1257
  if (level === TLS_LEVEL_INITIAL) {
1164
1258
  if (this.hashLength !== 0) {
1165
1259
  this.transcript.update(this.outputInitial, at, end - at)
@@ -1169,6 +1263,7 @@ export class TlsClient {
1169
1263
  this.transcript.update(this.outputHandshake, at, end - at)
1170
1264
  this.outputHandshakeLength = end
1171
1265
  }
1266
+ return true
1172
1267
  }
1173
1268
 
1174
1269
  /**