@amritk/nish-x86_64-linux 0.49.1 → 0.50.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 (91) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +12 -6
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +16 -9
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
package/std/net/url.ts CHANGED
@@ -117,14 +117,31 @@ const URL_SLASH: i32 = 47
117
117
  const URL_EQUALS: i32 = 61
118
118
  const URL_QUESTION: i32 = 63
119
119
 
120
+ /**
121
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
122
+ * step subtracts a non-negative value from one that is no larger than
123
+ * `bytes.length`, so the bounds prover sees no sum that can overflow.
124
+ */
125
+ const urlWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
126
+ if (off < 0 || len < 0) {
127
+ return -1
128
+ }
129
+ const size: i32 = toI32(bytes.length)
130
+ const room: i32 = size - off
131
+ if (room < 0) {
132
+ return -1
133
+ }
134
+ const gap: i32 = room - len
135
+ return gap < 0 ? -1 : size - gap
136
+ }
137
+
120
138
  /** Panics unless `bytes[off, off + len)` lies inside `bytes`, and answers the window's end, `off + len`. */
121
139
  const urlCheckWindow = (bytes: u8[], off: i32, len: i32, who: string): i32 => {
122
- const size: i32 = toI32(bytes.length)
123
- if (off < 0 || len < 0 || off > size || len > size - off) {
140
+ const end: i32 = urlWindowEnd(bytes, off, len)
141
+ if (end < 0) {
124
142
  panic(who + ": the window is outside the array")
125
143
  }
126
- // The test above bounds the sum by `bytes.length`, so it cannot wrap.
127
- return toI32(toU32(off) + toU32(len))
144
+ return end
128
145
  }
129
146
 
130
147
  /** The value of one hex digit, either case, or -1. */
@@ -186,14 +203,13 @@ const urlDecodeRun = (
186
203
  ): i32 => {
187
204
  const n: i32 = toI32(src.length)
188
205
  const size: i32 = toI32(out.length)
189
- if (off < 0 || len < 0 || off > n || len > n - off) {
206
+ const end: i32 = urlWindowEnd(src, off, len)
207
+ if (end < 0) {
190
208
  return URL_DECODE_ERR_WINDOW
191
209
  }
192
210
  if (at < 0 || at > size) {
193
211
  return URL_DECODE_AT_OUTSIDE
194
212
  }
195
- // The window test bounds the sum by `src.length`, so it cannot wrap.
196
- const end: i32 = toI32(toU32(off) + toU32(len))
197
213
  const form: boolean = (flags & URL_DECODE_FORM) !== 0
198
214
  const lenient: boolean = (flags & URL_DECODE_LENIENT) !== 0
199
215
  const allowNul: boolean = (flags & URL_DECODE_ALLOW_NUL) !== 0
@@ -201,7 +217,7 @@ const urlDecodeRun = (
201
217
  let w: i32 = at
202
218
  while (i >= 0 && i < end && i < n) {
203
219
  let b: i32 = toI32(src[i])
204
- let step: i32 = 1
220
+ let escaped: boolean = false
205
221
  if (b === URL_PERCENT) {
206
222
  const e: i32 = urlEscapeAt(src, i, end)
207
223
  if (e >= 0) {
@@ -209,7 +225,7 @@ const urlDecodeRun = (
209
225
  return wantPos ? i : URL_DECODE_ERR_NUL
210
226
  }
211
227
  b = e
212
- step = 3
228
+ escaped = true
213
229
  } else if (!lenient) {
214
230
  return wantPos ? i : URL_DECODE_ERR_ESCAPE
215
231
  }
@@ -221,12 +237,13 @@ const urlDecodeRun = (
221
237
  }
222
238
  out[w] = toU8(b)
223
239
  w += 1
224
- // An escape is taken only when its digits lie inside `src`, so the step
225
- // ends at most at `src.length` and cannot wrap.
226
- i = toI32(toU32(i) + toU32(step))
240
+ // An escape is taken only when its two digits lie inside `src`, so
241
+ // `i < n - 2` always holds for one; the test is what proves the step
242
+ // cannot overflow.
243
+ i = escaped && i < n - 2 ? i + 3 : i + 1
227
244
  }
228
245
  // `w` starts at `at` and only grows, so the difference cannot wrap.
229
- return wantPos ? -1 : toI32(toU32(w) - toU32(at))
246
+ return wantPos ? -1 : w - at
230
247
  }
231
248
 
232
249
  /** `urlQueryStart` of `bytes[off, end)`, for a caller that built the window from `bytes.length` and so has nothing to check. */
@@ -397,8 +414,8 @@ export class UrlPathSegments {
397
414
  j += 1
398
415
  }
399
416
  this.start = pos
400
- // `j` starts at `pos` and only grows, so the difference cannot wrap.
401
- this.length = toI32(toU32(j) - toU32(pos))
417
+ // `j` starts at `pos` and only grows, so the length is `j - pos` or 0.
418
+ this.length = j > pos && pos >= 0 ? j - pos : 0
402
419
  if (j < end) {
403
420
  this.pos = j + 1
404
421
  } else {
@@ -521,17 +538,18 @@ export class UrlQuery {
521
538
  i += 1
522
539
  }
523
540
  // `start <= eq < i` when `eq` was found, as the loop only moves `i` on,
524
- // so none of the differences below can wrap.
541
+ // so each length is the difference its test checks, never the 0.
525
542
  this.keyStart = start
526
543
  if (eq < 0) {
527
- this.keyLength = toI32(toU32(i) - toU32(start))
544
+ this.keyLength = i > start && start >= 0 ? i - start : 0
528
545
  this.valueStart = i
529
546
  this.valueLength = 0
530
547
  this.hasValue = false
531
548
  } else {
532
- this.keyLength = toI32(toU32(eq) - toU32(start))
533
- this.valueStart = toI32(toU32(eq) + 1)
534
- this.valueLength = toI32(toU32(i) - toU32(eq) - 1)
549
+ const valueStart: i32 = eq < i ? eq + 1 : i
550
+ this.keyLength = eq > start && start >= 0 ? eq - start : 0
551
+ this.valueStart = valueStart
552
+ this.valueLength = i > valueStart && valueStart >= 0 ? i - valueStart : 0
535
553
  this.hasValue = true
536
554
  }
537
555
  this.pos = i
@@ -550,22 +568,21 @@ export class UrlQuery {
550
568
  const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean => {
551
569
  const n: i32 = toI32(name.length)
552
570
  const size: i32 = toI32(bytes.length)
553
- if (off < 0 || len < 0 || off > size || len > size - off) {
571
+ const end: i32 = urlWindowEnd(bytes, off, len)
572
+ if (end < 0) {
554
573
  return false
555
574
  }
556
- // `off + len` is at most `size`, checked above, so it cannot wrap.
557
- const end: i32 = toI32(toU32(off) + toU32(len))
558
575
  let i: i32 = off
559
576
  let o: i32 = 0
560
577
  while (i >= 0 && i < end && i < size) {
561
578
  let b: i32 = toI32(bytes[i])
562
- let step: i32 = 1
579
+ let escaped: boolean = false
563
580
  if (b === URL_PERCENT) {
564
581
  b = urlEscapeAt(bytes, i, end)
565
582
  if (b <= 0) {
566
583
  return false
567
584
  }
568
- step = 3
585
+ escaped = true
569
586
  } else if (b === URL_PLUS) {
570
587
  b = URL_SPACE
571
588
  }
@@ -573,9 +590,10 @@ const urlKeyEquals = (bytes: u8[], off: i32, len: i32, name: string): boolean =>
573
590
  return false
574
591
  }
575
592
  o += 1
576
- // An escape is taken only when its digits lie inside `bytes`, so the step
577
- // ends at most at `bytes.length` and cannot wrap.
578
- i = toI32(toU32(i) + toU32(step))
593
+ // An escape is taken only when its two digits lie inside `bytes`, so
594
+ // `i < size - 2` always holds for one; the test is what proves the step
595
+ // cannot overflow.
596
+ i = escaped && i < size - 2 ? i + 3 : i + 1
579
597
  }
580
598
  return o === n
581
599
  }
@@ -639,7 +657,7 @@ const urlIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
639
657
  } else {
640
658
  return false
641
659
  }
642
- if (end - i - 1 < need || i + 1 >= n) {
660
+ if (end - i - 1 < need || i >= n - 1) {
643
661
  return false
644
662
  }
645
663
  const second: i32 = toI32(buf[i + 1])
@@ -647,27 +665,31 @@ const urlIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
647
665
  return false
648
666
  }
649
667
  // The test on `end` above already keeps the sequence inside `buf`; each
650
- // byte's own guard is spelled out for the bounds proof.
651
- if (need >= 2) {
652
- if (i >= n - 2) {
653
- return false
654
- }
655
- const c: i32 = toI32(buf[i + 2])
656
- if (c < 0x80 || c > 0xbf) {
657
- return false
658
- }
668
+ // byte's own guard is spelled out for the bounds proof, and the step
669
+ // past the sequence is taken under the guard that proves it fits.
670
+ if (need === 1) {
671
+ i = i + 2
672
+ continue
659
673
  }
660
- if (need >= 3) {
661
- if (i >= n - 3) {
662
- return false
663
- }
664
- const c: i32 = toI32(buf[i + 3])
665
- if (c < 0x80 || c > 0xbf) {
666
- return false
667
- }
674
+ if (i >= n - 2) {
675
+ return false
676
+ }
677
+ const third: i32 = toI32(buf[i + 2])
678
+ if (third < 0x80 || third > 0xbf) {
679
+ return false
680
+ }
681
+ if (need === 2) {
682
+ i = i + 3
683
+ continue
668
684
  }
669
- // Every byte of the sequence was read inside `buf`, so the step cannot wrap.
670
- i = toI32(toU32(i) + toU32(need) + 1)
685
+ if (i >= n - 3) {
686
+ return false
687
+ }
688
+ const fourth: i32 = toI32(buf[i + 3])
689
+ if (fourth < 0x80 || fourth > 0xbf) {
690
+ return false
691
+ }
692
+ i = i + 4
671
693
  }
672
694
  return true
673
695
  }
@@ -762,8 +784,6 @@ const URL_AT: i32 = 64
762
784
  const URL_OPEN_BRACKET: i32 = 91
763
785
  const URL_CLOSE_BRACKET: i32 = 93
764
786
  const URL_BACKSLASH: i32 = 92
765
- /** The largest port a URL may name (RFC 6335 §6: a port is 16 bits). */
766
- const URL_MAX_PORT: i32 = 65535
767
787
 
768
788
  /**
769
789
  * A URL split into its parts (RFC 3986 §3). Each part is held as it was
@@ -794,19 +814,21 @@ export class Url {
794
814
 
795
815
  /** `bytes[a, b)` as a string with A to Z lower-cased. */
796
816
  const urlLowerString = (bytes: u8[], a: i32, b: i32): string => {
797
- // `a <= b` is the caller's contract: a window the other way round is a
798
- // negative length, and `new Array` refuses it.
799
- const out: u8[] = new Array<u8>(toI32(toU32(b) - toU32(a)))
817
+ // Callers pass `0 <= a <= b`; a window the other way round is empty.
818
+ let width: i32 = 0
819
+ if (b > a && a >= 0) {
820
+ width = b - a
821
+ }
822
+ const out: u8[] = new Array<u8>(width)
823
+ const n: i32 = toI32(bytes.length)
800
824
  let j: i32 = 0
801
- while (j < toI32(out.length)) {
802
- const i: i32 = toI32(toU32(a) + toU32(j))
803
- if (i < 0 || i >= toI32(bytes.length)) {
804
- break
805
- }
825
+ let i: i32 = a
826
+ while (j < toI32(out.length) && i >= 0 && i < n) {
806
827
  const c: i32 = toI32(bytes[i])
807
828
  // `c | 32` is `c + 32` for A to Z, whose bit 5 is clear.
808
829
  out[j] = c >= 65 && c <= 90 ? toU8(c | 32) : toU8(c)
809
830
  j += 1
831
+ i += 1
810
832
  }
811
833
  return String.fromBytes(out, 0, toI32(out.length))
812
834
  }
@@ -1060,7 +1082,10 @@ const urlHostEnd = (bytes: u8[], h: i32, b: i32): i32 => {
1060
1082
 
1061
1083
  /** The port `bytes[a, b)` names, digits up to 65535, or -1 when it is not one. */
1062
1084
  const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
1063
- let port: i32 = 0
1085
+ // At most 65535, the largest port (RFC 6335 §6: a port is 16 bits), held
1086
+ // as a range the proof reads, so with a digit added the sum is at most
1087
+ // 655359.
1088
+ let port: integer<0, 65535> = 0
1064
1089
  let i: i32 = a
1065
1090
  while (i >= 0 && i < b && i < toI32(bytes.length)) {
1066
1091
  const c: i32 = toI32(bytes[i])
@@ -1068,12 +1093,11 @@ const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
1068
1093
  if (c < 48 || c > 57) {
1069
1094
  return -1
1070
1095
  }
1071
- // `port` is at most 65535 here and `c` a digit, so the sum is at most
1072
- // 655359 and cannot wrap.
1073
- port = toI32(toU32(port) * 10 + toU32(c) - 48)
1074
- if (port > URL_MAX_PORT) {
1096
+ const next: i32 = port * 10 + (c - 48)
1097
+ if (next > 65535) {
1075
1098
  return -1
1076
1099
  }
1100
+ port = next
1077
1101
  i += 1
1078
1102
  }
1079
1103
  return port
@@ -1177,16 +1201,16 @@ const urlReadReference = (s: string): Url | null => {
1177
1201
  return u
1178
1202
  }
1179
1203
 
1180
- /** The width of a `.` (1) or `%2e` (3) at `s[i]`, or 0. */
1204
+ /** The index just past a `.` or `%2e` at `s[i]`, or -1 when there is neither. */
1181
1205
  const urlDotAt = (s: string, i: i32, end: i32): i32 => {
1182
1206
  // Callers pass `0 <= i` and `end <= s.length`, so only `i >= end` can
1183
1207
  // answer here; the other bounds are spelled out for the proof.
1184
1208
  const n: i32 = toI32(s.length)
1185
1209
  if (i < 0 || i >= end || i >= n) {
1186
- return 0
1210
+ return -1
1187
1211
  }
1188
1212
  if (toI32(s.charCodeAt(i)) === URL_DOT) {
1189
- return 1
1213
+ return i + 1
1190
1214
  }
1191
1215
  if (
1192
1216
  i < n - 2 &&
@@ -1195,28 +1219,23 @@ const urlDotAt = (s: string, i: i32, end: i32): i32 => {
1195
1219
  toI32(s.charCodeAt(i + 1)) === 50 &&
1196
1220
  (toI32(s.charCodeAt(i + 2)) | 32) === 101
1197
1221
  ) {
1198
- return 3
1222
+ return i + 3
1199
1223
  }
1200
- return 0
1224
+ return -1
1201
1225
  }
1202
1226
 
1203
1227
  /** 1 when the segment `s[a, b)` is `.`, 2 when it is `..`, either written with `%2e`, else 0. */
1204
1228
  const urlDotSegment = (s: string, a: i32, b: i32): i32 => {
1205
- const first: i32 = urlDotAt(s, a, b)
1206
- if (first === 0) {
1229
+ // `urlDotAt` answers the index past the dot, which it steps to under the
1230
+ // bound that proves the step fits, so no sum is taken here.
1231
+ const next: i32 = urlDotAt(s, a, b)
1232
+ if (next < 0) {
1207
1233
  return 0
1208
1234
  }
1209
- // `urlDotAt` answers a width only for characters inside `s`, so each sum
1210
- // is at most `s.length` and cannot wrap.
1211
- const next: i32 = toI32(toU32(a) + toU32(first))
1212
1235
  if (next === b) {
1213
1236
  return 1
1214
1237
  }
1215
- const second: i32 = urlDotAt(s, next, b)
1216
- if (second > 0 && toI32(toU32(next) + toU32(second)) === b) {
1217
- return 2
1218
- }
1219
- return 0
1238
+ return urlDotAt(s, next, b) === b ? 2 : 0
1220
1239
  }
1221
1240
 
1222
1241
  /**
@@ -251,6 +251,18 @@ const websocketUtf8Run = (state: WsUtf8, buf: u8[], off: i32, len: i32): boolean
251
251
  return true
252
252
  }
253
253
 
254
+ /**
255
+ * Panics unless `buf[off .. off + len)` is inside `buf`: a window past its
256
+ * buffer is the caller's mistake. A window guard, so a caller that proves
257
+ * the window in range removes the panic site.
258
+ */
259
+ const websocketCheckWindow = (what: string, buf: readonly u8[], off: i32, len: i32): void => {
260
+ const size: i32 = toI32(buf.length)
261
+ if (off < 0 || len < 0 || off > size || len > size - off) {
262
+ panic(`${what}: the window is outside the buffer`)
263
+ }
264
+ }
265
+
254
266
  /**
255
267
  * Whether `buf[off .. off + len)` is well-formed UTF-8, no character cut short
256
268
  * at the end. The window is the caller's, already checked against `buf`, as
@@ -272,10 +284,7 @@ const websocketUtf8Whole = (buf: u8[], off: i32, len: i32): boolean => {
272
284
  * @returns True when the bytes are UTF-8 with no overlong form, surrogate or code point past U+10FFFF, and no character cut short at the end.
273
285
  */
274
286
  export const websocketIsUtf8 = (buf: u8[], off: i32, len: i32): boolean => {
275
- const size: i32 = toI32(buf.length)
276
- if (off < 0 || len < 0 || off > size || len > size - off) {
277
- panic("websocketIsUtf8: the window is outside the buffer")
278
- }
287
+ websocketCheckWindow("websocketIsUtf8", buf, off, len)
279
288
  return websocketUtf8Whole(buf, off, len)
280
289
  }
281
290
 
@@ -360,10 +369,7 @@ export class WsDecoder {
360
369
  * @param len How many bytes to append.
361
370
  */
362
371
  feed(data: u8[], off: i32, len: i32): void {
363
- const size: i32 = toI32(data.length)
364
- if (off < 0 || len < 0 || off > size || len > size - off) {
365
- panic("WsDecoder: the window is outside the buffer")
366
- }
372
+ websocketCheckWindow("WsDecoder", data, off, len)
367
373
  if (this.state !== WS_S_FRAMES) {
368
374
  return
369
375
  }
@@ -421,7 +427,8 @@ const websocketReserve = (d: WsDecoder, len: i32): boolean => {
421
427
  return true
422
428
  }
423
429
  const live: i32 = end - start
424
- if (toI64(len) > toI64(2147483647) - toI64(live)) {
430
+ const most: i32 = 2147483647
431
+ if (toI64(len) > toI64(most) - toI64(live)) {
425
432
  return false
426
433
  }
427
434
  const need: i32 = websocketAdd(live, len)
@@ -141,7 +141,6 @@ import {
141
141
  H3_REQUEST_REJECTED,
142
142
  H3_WT_SESSION_GONE,
143
143
  h3Count,
144
- h3CheckWindow,
145
144
  h3Greased,
146
145
  h3PutFrameHeader,
147
146
  h3PutVarint,
@@ -1400,16 +1399,20 @@ export class WebTransport {
1400
1399
  * @param id The session's ID.
1401
1400
  * @param code The application's code; only its low 32 bits are sent.
1402
1401
  * @param buf The buffer holding the reason; only read, and copied into the capsule. A window outside
1403
- * it panics, whatever the session's state.
1402
+ * it answers H3_INVALID, whatever the session's state.
1404
1403
  * @param off Where the reason starts in `buf`.
1405
1404
  * @param len How many bytes the reason has, at most 1024.
1406
- * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes;
1405
+ * @returns 0 once the capsule and the FIN are written; H3_INVALID for a reason past 1024 bytes or a
1406
+ * window outside `buf`;
1407
1407
  * H3_AGAIN while the CONNECT stream has no room for the capsule; H3_TOO_LARGE for a capsule past
1408
1408
  * the stream's buffer; or H3_CLOSED for a session not accepted or already gone, or a CONNECT
1409
1409
  * stream that can no longer be written.
1410
1410
  */
1411
1411
  close(id: i64, code: i64, buf: u8[], off: i32, len: i32): i32 {
1412
- h3CheckWindow("WebTransport.close", buf, off, len)
1412
+ const size: i32 = toI32(buf.length)
1413
+ if (off < 0 || len < 0 || off > size || len > size - off) {
1414
+ return H3_INVALID
1415
+ }
1413
1416
  const w: WebTransportSession | null = this.openSession(id)
1414
1417
  if (w === null) {
1415
1418
  return H3_CLOSED
@@ -1603,7 +1606,12 @@ export class WebTransport {
1603
1606
  head[have] = bytes[at]
1604
1607
  this.capAt = wtAdd(at, 1)
1605
1608
  w.capHeadLen = have + 1
1606
- if (h3ReadFrameHeader(this.header, head, WT_ZERO, have + 1) === 0) {
1609
+ const filled: i32 = have + 1
1610
+ if (filled > toI32(head.length)) {
1611
+ return this.malformed(w, H3_DATAGRAM_ERROR) // never: `have` was below `head.length`
1612
+ }
1613
+ const start: i32 = 0
1614
+ if (h3ReadFrameHeader(this.header, head, start, filled) === 0) {
1607
1615
  continue
1608
1616
  }
1609
1617
  w.capHeadLen = 0
@@ -1850,17 +1858,22 @@ export class WebTransport {
1850
1858
  /**
1851
1859
  * Writes up to `len` bytes of `buf` from `off` to session stream `id`, with
1852
1860
  * its FIN when `fin` and all were taken. Answers how many it took, or
1853
- * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`).
1861
+ * H3_AGAIN, or H3_CLOSED (see `Http3Connection.writeStream`). A window
1862
+ * outside `buf` answers H3_INVALID, whatever stream `id` names.
1854
1863
  * @param id The stream's ID, one of a session this layer took or opened.
1855
1864
  * @param buf The buffer holding the bytes; only read, and what is taken is copied.
1856
1865
  * @param off Where the bytes start in `buf`.
1857
1866
  * @param len How many bytes to write.
1858
1867
  * @param fin Whether to end this side of the stream once every byte is taken.
1859
1868
  * @returns How many bytes it took, fewer than `len` only when the send buffer filled (0 for a bare
1860
- * FIN); H3_AGAIN when it took none; or H3_CLOSED for a stream this layer does not hold or with no
1861
- * side here to write.
1869
+ * FIN); H3_AGAIN when it took none; H3_CLOSED for a stream this layer does not hold or with no
1870
+ * side here to write; or H3_INVALID for a window outside `buf`.
1862
1871
  */
1863
1872
  write(id: i64, buf: u8[], off: i32, len: i32, fin: boolean): i32 {
1873
+ const size: i32 = toI32(buf.length)
1874
+ if (off < 0 || len < 0 || off > size || len > size - off) {
1875
+ return H3_INVALID
1876
+ }
1864
1877
  this.current()
1865
1878
  const t: WebTransportStream | null = this.streamOf(id)
1866
1879
  if (t === null) {
@@ -1943,15 +1956,18 @@ export class WebTransport {
1943
1956
  * for a session not accepted.
1944
1957
  * @param id The session's ID.
1945
1958
  * @param buf The buffer holding the payload; only read, and copied into the datagram. A window
1946
- * outside it panics, whatever the session's state.
1959
+ * outside it answers H3_INVALID, whatever the session's state.
1947
1960
  * @param off Where the payload starts in `buf`.
1948
1961
  * @param len How many bytes the payload has.
1949
1962
  * @returns 0 once the datagram is queued; H3_AGAIN while QUIC's queue is full; H3_TOO_LARGE past
1950
- * `maxDatagramPayload`, or when QUIC refuses it for any other reason; or H3_CLOSED for a session
1951
- * not accepted, or one already gone.
1963
+ * `maxDatagramPayload`, or when QUIC refuses it for any other reason; H3_CLOSED for a session
1964
+ * not accepted, or one already gone; or H3_INVALID for a window outside `buf`.
1952
1965
  */
1953
1966
  sendDatagram(id: i64, buf: u8[], off: i32, len: i32): i32 {
1954
- h3CheckWindow("WebTransport.sendDatagram", buf, off, len)
1967
+ const size: i32 = toI32(buf.length)
1968
+ if (off < 0 || len < 0 || off > size || len > size - off) {
1969
+ return H3_INVALID
1970
+ }
1955
1971
  const w: WebTransportSession | null = this.openSession(id)
1956
1972
  if (w === null) {
1957
1973
  return H3_CLOSED