@amritk/nish-aarch64-linux 0.39.0 → 0.41.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 (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
@@ -27,7 +27,8 @@
27
27
  * serverAuth, name constraints — and that the leaf's subjectAltName names
28
28
  * the host dialled. Its refusal becomes the alert: `certificate_expired` for
29
29
  * a certificate outside its validity, `unknown_ca` when no path reaches a
30
- * root of the store, and `bad_certificate` for every other reason. There is
30
+ * root of the store, and `bad_certificate` for every other reason, a key
31
+ * distrusted with `tlsClientDistrustSpki` among them. There is
31
32
  * no switch that skips the step and none is to be added: a client that is to
32
33
  * talk to a server it cannot authenticate is given a store holding its
33
34
  * issuing root, never a way around the check (`docs/security/tls.md`, "The
@@ -47,6 +48,7 @@ import {
47
48
  PKI_OK,
48
49
  PkiResult,
49
50
  PkiTrustStore,
51
+ pkiDistrustSpki,
50
52
  pkiTrustStore,
51
53
  pkiVerifyServer,
52
54
  } from "nish/crypto/pki"
@@ -135,6 +137,22 @@ export const tlsClientTrust = (roots: PkiRoots): TlsClientTrust => new TlsClient
135
137
  */
136
138
  export const tlsClientSystemTrust = (): TlsClientTrust => tlsClientTrust(pkiLoadSystemRoots())
137
139
 
140
+ /**
141
+ * Distrusts a key in `trust`: every connection made with it from now on
142
+ * refuses, with `bad_certificate`, a server whose path holds a certificate
143
+ * (leaf, intermediate or root) whose subjectPublicKeyInfo DER hashes to
144
+ * `spkiSha256` under SHA-256 (`pkiDistrustSpki`). It is how a program stops
145
+ * trusting a CA its roots still hold, or a key known to be compromised,
146
+ * before the system bundle drops it. A trust distrusts nothing until this is
147
+ * called.
148
+ *
149
+ * @param trust The trust to change; every connection that shares it sees the change.
150
+ * @param spkiSha256 The SHA-256 of the subjectPublicKeyInfo DER, 32 bytes.
151
+ * @returns Whether the hash was added: false, and nothing added, unless it is 32 bytes long.
152
+ */
153
+ export const tlsClientDistrustSpki = (trust: TlsClientTrust, spkiSha256: u8[]): boolean =>
154
+ pkiDistrustSpki(trust.store, spkiSha256)
155
+
138
156
  /**
139
157
  * Whether `signature`, as a CertificateVerify carries it under `scheme`,
140
158
  * verifies over `content` under `leaf`'s key. `false` for a scheme that
package/std/net/url.ts CHANGED
@@ -861,52 +861,100 @@ const urlReadAuthority = (bytes: u8[], a: i32, b: i32, u: Url): boolean => {
861
861
  }
862
862
  h = at + 1
863
863
  }
864
- let portAt: i32 = b
864
+ const portAt: i32 = urlHostEnd(bytes, h, b)
865
+ if (portAt < 0) {
866
+ return false
867
+ }
865
868
  if (h < b && toI32(bytes[h]) === URL_OPEN_BRACKET) {
866
- const close: i32 = urlFind(bytes, h, b, URL_CLOSE_BRACKET)
867
- if (close === b || !urlIsIpv6(bytes, h + 1, close)) {
868
- return false
869
- }
870
- u.host = urlLowerString(bytes, h + 1, close)
871
- if (close + 1 < b) {
872
- if (toI32(bytes[close + 1]) !== URL_COLON) {
873
- return false
874
- }
875
- portAt = close + 1
876
- }
869
+ u.host = urlLowerString(bytes, h + 1, portAt - 1)
877
870
  } else {
878
- portAt = urlFind(bytes, h, b, URL_COLON)
879
- let i: i32 = h
880
- while (i >= 0 && i < portAt && i < toI32(bytes.length) && urlIsUnreserved(toI32(bytes[i]))) {
881
- i += 1
882
- }
883
- if (i < portAt) {
884
- return false
885
- }
886
- if (urlLooksNumeric(bytes, h, portAt) && !urlIsIpv4(bytes, h, portAt)) {
887
- return false
888
- }
889
871
  u.host = urlLowerString(bytes, h, portAt)
890
872
  }
891
873
  if (portAt + 1 < b) {
892
- let port: i32 = 0
893
- let i: i32 = portAt + 1
894
- while (i >= 0 && i < b && i < toI32(bytes.length)) {
895
- const c: i32 = toI32(bytes[i])
896
- if (!urlIsDigit(c)) {
897
- return false
898
- }
899
- port = port * 10 + c - 48
900
- if (port > URL_MAX_PORT) {
901
- return false
902
- }
903
- i += 1
874
+ const port: i32 = urlPortOf(bytes, portAt + 1, b)
875
+ if (port < 0) {
876
+ return false
904
877
  }
905
878
  u.port = port
906
879
  }
907
880
  return true
908
881
  }
909
882
 
883
+ /**
884
+ * Where the host that starts `bytes[h, b)` ends: the index of the `:` that
885
+ * starts its port, or `b` when there is none; -1 when the host breaks a rule
886
+ * `urlParse` names. The host is a bracketed IPv6 address, or a reg-name of
887
+ * unreserved bytes that is a dotted-decimal IPv4 address if it reads as a
888
+ * number at all. An empty host is allowed.
889
+ */
890
+ const urlHostEnd = (bytes: u8[], h: i32, b: i32): i32 => {
891
+ if (h >= 0 && h < b && h < toI32(bytes.length) && toI32(bytes[h]) === URL_OPEN_BRACKET) {
892
+ const close: i32 = urlFind(bytes, h, b, URL_CLOSE_BRACKET)
893
+ if (close === b || !urlIsIpv6(bytes, h + 1, close)) {
894
+ return -1
895
+ }
896
+ if (close + 1 < b && toI32(bytes[close + 1]) !== URL_COLON) {
897
+ return -1
898
+ }
899
+ return close + 1
900
+ }
901
+ const portAt: i32 = urlFind(bytes, h, b, URL_COLON)
902
+ let i: i32 = h
903
+ while (i >= 0 && i < portAt && i < toI32(bytes.length) && urlIsUnreserved(toI32(bytes[i]))) {
904
+ i += 1
905
+ }
906
+ if (i < portAt) {
907
+ return -1
908
+ }
909
+ if (urlLooksNumeric(bytes, h, portAt) && !urlIsIpv4(bytes, h, portAt)) {
910
+ return -1
911
+ }
912
+ return portAt
913
+ }
914
+
915
+ /** The port `bytes[a, b)` names, digits up to 65535, or -1 when it is not one. */
916
+ const urlPortOf = (bytes: u8[], a: i32, b: i32): i32 => {
917
+ let port: i32 = 0
918
+ let i: i32 = a
919
+ while (i >= 0 && i < b && i < toI32(bytes.length)) {
920
+ const c: i32 = toI32(bytes[i])
921
+ if (!urlIsDigit(c)) {
922
+ return -1
923
+ }
924
+ port = port * 10 + c - 48
925
+ if (port > URL_MAX_PORT) {
926
+ return -1
927
+ }
928
+ i += 1
929
+ }
930
+ return port
931
+ }
932
+
933
+ /**
934
+ * Whether `bytes[a, b)` is `uri-host [ ":" port ]` (RFC 3986 §3.2.2, §3.2.3)
935
+ * by the rules `urlParse` holds a URL's host and port to: a bracketed IPv6
936
+ * address, a dotted-decimal IPv4 address, or a reg-name of unreserved bytes
937
+ * that does not read as a number, then an optional `:` and a port of digits
938
+ * up to 65535. An empty host and an empty port are allowed, as in a URL; a
939
+ * percent-encoded or sub-delims byte in a reg-name is not. It is the rule an
940
+ * HTTP server holds a `Host` field to (RFC 9112 §3.2). Allocates nothing.
941
+ *
942
+ * @param bytes The bytes that hold the value.
943
+ * @param a The index of the value's first byte.
944
+ * @param b The index past the value's last byte.
945
+ * @returns True when the bytes are a host and an optional port; false otherwise, and for a window outside `bytes`.
946
+ */
947
+ export const urlIsHostPort = (bytes: u8[], a: i32, b: i32): boolean => {
948
+ if (a < 0 || b < a || b > toI32(bytes.length)) {
949
+ return false
950
+ }
951
+ const portAt: i32 = urlHostEnd(bytes, a, b)
952
+ if (portAt < 0) {
953
+ return false
954
+ }
955
+ return portAt + 1 >= b || urlPortOf(bytes, portAt + 1, b) >= 0
956
+ }
957
+
910
958
  /**
911
959
  * `s` split into a `Url` whose scheme is empty when `s` is a relative
912
960
  * reference, with its path as written; null when it breaks one of the rules
@@ -8,7 +8,8 @@
8
8
  * the subprotocols it offers, and `websocketUpgradeResponse(key, protocol)`
9
9
  * writes the 101 that accepts it, with `Sec-WebSocket-Accept` from
10
10
  * `websocketAcceptKey` (§4.2.2): the base64 of the SHA-1 of the key and a
11
- * fixed GUID. What `HTTP1_UPGRADE`'s `upgradeBytes()` answers is the first
11
+ * fixed GUID. `websocketOriginAllowed(parser, allowed)` is the opt-in check
12
+ * of `Origin` a server makes first, against a page on another site. What `HTTP1_UPGRADE`'s `upgradeBytes()` answers is the first
12
13
  * input for the decoder.
13
14
  *
14
15
  * **The client's handshake** (§4.1). `websocketClientKey(nonce)` spells 16
@@ -990,6 +991,78 @@ export const websocketOfferedProtocols = (p: Http1Parser): string[] | null => {
990
991
  return out
991
992
  }
992
993
 
994
+ /**
995
+ * Whether the request's `Origin` is one of `allowed`: the check a server makes
996
+ * before `acceptWebSocket` so that a page on another site cannot open a
997
+ * WebSocket that rides the user's cookies (cross-site WebSocket hijacking,
998
+ * RFC 6455 §10.2). A browser always sends `Origin` on the opening handshake
999
+ * and a page cannot change it; the handshake itself is not refused without
1000
+ * one, because a client that is not a browser need not send it, so the check
1001
+ * is the program's to opt into.
1002
+ *
1003
+ * An origin is compared whole, in any ASCII case, as the browser serializes
1004
+ * it: `https://example.com`, or `https://example.com:8443` for a port that is
1005
+ * not the scheme's default, with no path and no trailing slash. Answers false
1006
+ * for a request with no `Origin` or with more than one, so a program that also
1007
+ * serves clients that are not browsers lets those through itself. Allocates
1008
+ * nothing.
1009
+ *
1010
+ * @param p the parser holding the request's head, as `Http1Connection.parser` does at `H1_REQUEST`
1011
+ * @param allowed the origins to accept, each written as a browser sends it; "null" accepts an opaque origin, which any sandboxed page can send
1012
+ * @returns true when exactly one `Origin` field is present and its value is one of `allowed`
1013
+ */
1014
+ export const websocketOriginAllowed = (p: Http1Parser, allowed: string[]): boolean => {
1015
+ const head: u8[] = p.head
1016
+ const spans: i32[] = p.spans
1017
+ let found: i32 = -1
1018
+ for (let i: i32 = 0; i < p.headerCount && 4 * i + 3 < toI32(spans.length); i += 1) {
1019
+ if (websocketSpanIs(head, spans[4 * i], spans[4 * i + 1], "origin")) {
1020
+ if (found >= 0) {
1021
+ return false
1022
+ }
1023
+ found = i
1024
+ }
1025
+ }
1026
+ if (found < 0) {
1027
+ return false
1028
+ }
1029
+ for (let k: i32 = 0; k < toI32(allowed.length); k += 1) {
1030
+ if (websocketSpanFolds(head, spans[4 * found + 2], spans[4 * found + 3], allowed[k])) {
1031
+ return true
1032
+ }
1033
+ }
1034
+ return false
1035
+ }
1036
+
1037
+ /**
1038
+ * Whether `bytes[a .. b)` is `text` with ASCII letters in either case on
1039
+ * either side, as an origin's scheme and host compare. Allocates nothing.
1040
+ */
1041
+ const websocketSpanFolds = (bytes: u8[], a: i32, b: i32, text: string): boolean => {
1042
+ const n: i32 = toI32(text.length)
1043
+ if (a < 0 || b - a !== n || b > toI32(bytes.length)) {
1044
+ return false
1045
+ }
1046
+ for (let k: i32 = 0; k < n; k += 1) {
1047
+ const at: i32 = a + k
1048
+ if (at < 0 || at >= toI32(bytes.length) || k >= toI32(text.length)) {
1049
+ return false
1050
+ }
1051
+ let c: i32 = toI32(bytes[at])
1052
+ if (c >= 65 && c <= 90) {
1053
+ c += 32
1054
+ }
1055
+ let w: i32 = toI32(text.charCodeAt(k))
1056
+ if (w >= 65 && w <= 90) {
1057
+ w += 32
1058
+ }
1059
+ if (c !== w) {
1060
+ return false
1061
+ }
1062
+ }
1063
+ return true
1064
+ }
1065
+
993
1066
  /** Whether `bytes[a .. b)` is `word`, which is lower case, in any ASCII case. Allocates nothing. */
994
1067
  const websocketSpanIs = (bytes: u8[], a: i32, b: i32, word: string): boolean => {
995
1068
  const n: i32 = toI32(word.length)
@@ -266,7 +266,13 @@ export class WebTransportConfig {
266
266
  * event either way, and one this side closed from its close.
267
267
  */
268
268
  idleTimeout: i32 = 60000
269
- /** Datagrams one session may deliver in a second, 1 to 1,048,576; the rest are dropped and counted. */
269
+ /**
270
+ * Datagrams one session may deliver in a second, 1 to 1,048,576; the rest are dropped and counted
271
+ * in `datagramsDropped`. Dropping is the whole defence: no count of drops closes the session.
272
+ * Datagrams are unreliable by contract (RFC 9297), so a dropped one is a loss the application
273
+ * already handles; a drop costs O(1); a close would punish a browser that bursts; and a client
274
+ * past the cap gains nothing but the CPU its datagrams cost to drop.
275
+ */
270
276
  maxDatagramsPerSecond: i32 = 1024
271
277
  }
272
278