@amritk/nish-x86_64-linux 0.42.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.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/outbound.ts
CHANGED
|
@@ -44,13 +44,16 @@
|
|
|
44
44
|
* Written from RFC 6890, RFC 4291, RFC 6052, RFC 3056, RFC 4380 and the IANA
|
|
45
45
|
* special-purpose address registries, not ported from another implementation.
|
|
46
46
|
*/
|
|
47
|
-
import {
|
|
47
|
+
import { tryCopyBytes } from "nish/bytes"
|
|
48
48
|
import { DNS_ADDRESS_BYTES } from "nish/net/dns-message"
|
|
49
49
|
import { dnsParseAddress } from "nish/net/dns-config"
|
|
50
50
|
|
|
51
51
|
/** The bytes of one `permit` entry: sixteen of address, then the prefix length in bits. */
|
|
52
52
|
const OUTBOUND_PERMIT_BYTES: i32 = 17
|
|
53
53
|
|
|
54
|
+
/** The longest address text `permit` reads; an IPv6 address with an IPv4 tail takes 45 bytes. */
|
|
55
|
+
const OUTBOUND_TEXT_MAX: i32 = 64
|
|
56
|
+
|
|
54
57
|
/** Where an IPv4 address starts in its sixteen-byte mapped form. */
|
|
55
58
|
const OUTBOUND_V4_AT: i32 = 12
|
|
56
59
|
|
|
@@ -108,9 +111,12 @@ export class OutboundPolicy {
|
|
|
108
111
|
const n: i32 = toI32(cidr.length)
|
|
109
112
|
const slash: i32 = toI32(cidr.indexOf("/"))
|
|
110
113
|
const end: i32 = slash >= 0 ? slash : n
|
|
111
|
-
|
|
114
|
+
if (end <= 0 || end > OUTBOUND_TEXT_MAX) {
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
117
|
+
const text: u8[] = new Array<u8>(OUTBOUND_TEXT_MAX)
|
|
112
118
|
let v4: boolean = true
|
|
113
|
-
for (let i: i32 = 0; i <
|
|
119
|
+
for (let i: i32 = 0; i < end && i < n && i < toI32(text.length); i += 1) {
|
|
114
120
|
const c: i32 = toI32(cidr.charCodeAt(i))
|
|
115
121
|
if (c > 127) {
|
|
116
122
|
return false
|
|
@@ -119,64 +125,51 @@ export class OutboundPolicy {
|
|
|
119
125
|
text[i] = toU8(c)
|
|
120
126
|
}
|
|
121
127
|
const address: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
|
|
122
|
-
if (
|
|
128
|
+
if (!dnsParseAddress(text, 0, end, address, 0, 0)) {
|
|
123
129
|
return false
|
|
124
130
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (slash >= 0) {
|
|
131
|
+
let bits: i32 = v4 ? 32 : 128
|
|
132
|
+
if (slash >= 0 && slash < n) {
|
|
128
133
|
bits = outboundDecimal(cidr, slash + 1, n)
|
|
129
|
-
|
|
134
|
+
}
|
|
135
|
+
if (bits < 0) {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
// An IPv4 prefix is kept in its ::ffff: form, 96 bits longer.
|
|
139
|
+
let stored: i32 = bits
|
|
140
|
+
if (v4) {
|
|
141
|
+
if (bits > 32) {
|
|
130
142
|
return false
|
|
131
143
|
}
|
|
144
|
+
stored = bits + 96
|
|
145
|
+
} else if (bits > 128) {
|
|
146
|
+
return false
|
|
132
147
|
}
|
|
133
148
|
for (let i: i32 = 0; i < 16 && i < toI32(address.length); i += 1) {
|
|
134
149
|
this.permitted.push(address[i])
|
|
135
150
|
}
|
|
136
|
-
this.permitted.push(toU8(
|
|
151
|
+
this.permitted.push(toU8(stored))
|
|
137
152
|
return true
|
|
138
153
|
}
|
|
139
154
|
}
|
|
140
155
|
|
|
141
156
|
/**
|
|
142
157
|
* Whether the sixteen address bytes at `addr[off]` are public, as the module
|
|
143
|
-
* comment defines it.
|
|
158
|
+
* comment defines it. False when sixteen bytes do not fit there.
|
|
144
159
|
*
|
|
145
160
|
* @param addr The array holding the address; an IPv4 one in its `::ffff:` form, as nish:net writes it.
|
|
146
161
|
* @param off Where the address's sixteen bytes start in `addr`.
|
|
147
162
|
* @returns True for a public address. An IPv4 one carried by NAT64 or 6to4 is judged as that IPv4 address.
|
|
148
163
|
*/
|
|
149
164
|
export const outboundAddressPublic = (addr: u8[], off: i32): boolean => {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
const carried: i32 = outboundCarriedV4(addr, off)
|
|
154
|
-
if (carried >= 0) {
|
|
155
|
-
return outboundV4Public(addr, carried)
|
|
156
|
-
}
|
|
157
|
-
const b0: i32 = toI32(addr[off])
|
|
158
|
-
const b1: i32 = toI32(addr[off + 1])
|
|
159
|
-
const b2: i32 = toI32(addr[off + 2])
|
|
160
|
-
const b3: i32 = toI32(addr[off + 3])
|
|
161
|
-
if ((b0 & 0xe0) !== 0x20) {
|
|
162
|
-
// Outside 2000::/3: loopback, unspecified, unique local, link-local, multicast, the rest.
|
|
163
|
-
return false
|
|
164
|
-
}
|
|
165
|
-
if (b0 === 0x20 && b1 === 0x01 && (b2 & 0xfe) === 0) {
|
|
166
|
-
// 2001::/23, IETF protocol assignments, Teredo's 2001::/32 among them.
|
|
167
|
-
return false
|
|
168
|
-
}
|
|
169
|
-
if (b0 === 0x20 && b1 === 0x01 && b2 === 0x0d && b3 === 0xb8) {
|
|
170
|
-
// 2001:db8::/32, documentation.
|
|
171
|
-
return false
|
|
172
|
-
}
|
|
173
|
-
// 3fff::/20, documentation (RFC 9637).
|
|
174
|
-
return !(b0 === 0x3f && b1 === 0xff && (b2 & 0xf0) === 0)
|
|
165
|
+
const a: u8[] | null = outboundSixteen(addr, off)
|
|
166
|
+
return a !== null && outboundPublic(a)
|
|
175
167
|
}
|
|
176
168
|
|
|
177
169
|
/**
|
|
178
170
|
* Whether `policy` lets a connection go to the sixteen address bytes at
|
|
179
|
-
* `addr[off]`: a prefix it `permit`s, or else a public address.
|
|
171
|
+
* `addr[off]`: a prefix it `permit`s, or else a public address. False when
|
|
172
|
+
* sixteen bytes do not fit there.
|
|
180
173
|
*
|
|
181
174
|
* @param policy The policy whose permits are checked first.
|
|
182
175
|
* @param addr The array holding the address; an IPv4 one in its `::ffff:` form, as nish:net writes it.
|
|
@@ -184,24 +177,27 @@ export const outboundAddressPublic = (addr: u8[], off: i32): boolean => {
|
|
|
184
177
|
* @returns True when a connection may go there. An IPv4 address carried by NAT64 or 6to4 is judged as that address, by a permit too.
|
|
185
178
|
*/
|
|
186
179
|
export const outboundAddressAllowed = (policy: OutboundPolicy, addr: u8[], off: i32): boolean => {
|
|
187
|
-
|
|
180
|
+
const a: u8[] | null = outboundSixteen(addr, off)
|
|
181
|
+
if (a === null) {
|
|
182
|
+
return false
|
|
183
|
+
}
|
|
184
|
+
if (outboundPermitted(policy, a)) {
|
|
188
185
|
return true
|
|
189
186
|
}
|
|
190
|
-
const carried: i32 = outboundCarriedV4(
|
|
187
|
+
const carried: i32 = outboundCarriedV4(a)
|
|
191
188
|
if (carried < 0) {
|
|
192
|
-
return
|
|
189
|
+
return outboundPublic(a)
|
|
193
190
|
}
|
|
194
|
-
if (!outboundMapped(
|
|
191
|
+
if (!outboundMapped(a) && toI32(policy.permitted.length) > 0) {
|
|
195
192
|
// A permit names an IPv4 prefix in its ::ffff: form, so try the address a NAT64 or 6to4 one carries in that form too.
|
|
196
193
|
const mapped: u8[] = new Array<u8>(16)
|
|
197
194
|
mapped[10] = toU8(255)
|
|
198
195
|
mapped[11] = toU8(255)
|
|
199
|
-
|
|
200
|
-
if (outboundPermitted(policy, mapped, 0)) {
|
|
196
|
+
if (tryCopyBytes(mapped, OUTBOUND_V4_AT, a, carried, 4) && outboundPermitted(policy, mapped)) {
|
|
201
197
|
return true
|
|
202
198
|
}
|
|
203
199
|
}
|
|
204
|
-
return outboundV4Public(
|
|
200
|
+
return outboundV4Public(a, carried)
|
|
205
201
|
}
|
|
206
202
|
|
|
207
203
|
/**
|
|
@@ -211,29 +207,35 @@ export const outboundAddressAllowed = (policy: OutboundPolicy, addr: u8[], off:
|
|
|
211
207
|
*
|
|
212
208
|
* @param policy The policy each address is checked against, as `outboundAddressAllowed` checks it.
|
|
213
209
|
* @param addresses The records, each sixteen address bytes and a two-byte port, reordered in place.
|
|
214
|
-
* @param count How many records `addresses` holds from its start;
|
|
210
|
+
* @param count How many records `addresses` holds from its start; records past `addresses`' end are not read, and a negative count keeps none.
|
|
215
211
|
* @returns How many records were kept, now `addresses`' first ones; 0 when every one was refused.
|
|
216
212
|
*/
|
|
217
213
|
export const outboundFilter = (policy: OutboundPolicy, addresses: u8[], count: i32): i32 => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
let
|
|
222
|
-
for (let i: i32 = 0; i < count; i += 1) {
|
|
223
|
-
if (
|
|
224
|
-
continue
|
|
225
|
-
}
|
|
226
|
-
if (i !== kept) {
|
|
214
|
+
const held: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
|
|
215
|
+
// Where the next kept record goes, and where the record checked now starts.
|
|
216
|
+
let keptAt: i32 = 0
|
|
217
|
+
let at: i32 = 0
|
|
218
|
+
for (let i: i32 = 0; i < count && at <= toI32(addresses.length) - DNS_ADDRESS_BYTES; i += 1) {
|
|
219
|
+
if (outboundAddressAllowed(policy, addresses, at)) {
|
|
227
220
|
// Swap, so the refused record stays in the list for an error to name.
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
221
|
+
// Both records fit (`keptAt` is at most `at`), so no copy can refuse.
|
|
222
|
+
if (
|
|
223
|
+
keptAt !== at &&
|
|
224
|
+
!(
|
|
225
|
+
tryCopyBytes(held, 0, addresses, keptAt, DNS_ADDRESS_BYTES) &&
|
|
226
|
+
tryCopyBytes(addresses, keptAt, addresses, at, DNS_ADDRESS_BYTES) &&
|
|
227
|
+
tryCopyBytes(addresses, at, held, 0, DNS_ADDRESS_BYTES)
|
|
228
|
+
)
|
|
229
|
+
) {
|
|
230
|
+
break
|
|
232
231
|
}
|
|
232
|
+
// At most `at`, which is at most the length less a record: unsigned
|
|
233
|
+
// arithmetic only so the prover need not relate the two.
|
|
234
|
+
keptAt = toI32(toU32(keptAt) + toU32(DNS_ADDRESS_BYTES))
|
|
233
235
|
}
|
|
234
|
-
|
|
236
|
+
at = toI32(toU32(at) + toU32(DNS_ADDRESS_BYTES))
|
|
235
237
|
}
|
|
236
|
-
return
|
|
238
|
+
return keptAt / DNS_ADDRESS_BYTES
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
/**
|
|
@@ -259,7 +261,7 @@ export const outboundHostAllowed = (policy: OutboundPolicy, host: string): boole
|
|
|
259
261
|
if (
|
|
260
262
|
!literal &&
|
|
261
263
|
entry.startsWith(".") &&
|
|
262
|
-
(name.endsWith(entry) || (toI32(
|
|
264
|
+
(name.endsWith(entry) || (toI32(entry.length) - 1 === toI32(name.length) && entry.endsWith(name)))
|
|
263
265
|
) {
|
|
264
266
|
return true
|
|
265
267
|
}
|
|
@@ -272,128 +274,190 @@ export const outboundHostAllowed = (policy: OutboundPolicy, host: string): boole
|
|
|
272
274
|
*
|
|
273
275
|
* @param addr The array holding the address.
|
|
274
276
|
* @param off Where the address's sixteen bytes start in `addr`.
|
|
275
|
-
* @returns Dotted decimal for an IPv4 address in its `::ffff:` form, otherwise all eight lower-case hex groups with no `::` shortening.
|
|
277
|
+
* @returns Dotted decimal for an IPv4 address in its `::ffff:` form, otherwise all eight lower-case hex groups with no `::` shortening; empty when sixteen bytes do not fit there.
|
|
276
278
|
*/
|
|
277
279
|
export const outboundAddressText = (addr: u8[], off: i32): string => {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
280
|
+
const a: u8[] | null = outboundSixteen(addr, off)
|
|
281
|
+
return a === null ? "" : outboundText(a)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The address in `a`, `outboundSixteen`'s, as `outboundAddressText` writes it. */
|
|
285
|
+
const outboundText = (a: u8[]): string => {
|
|
286
|
+
const mapped: boolean = outboundMapped(a)
|
|
287
|
+
if (toI32(a.length) < 16) {
|
|
288
|
+
return ""
|
|
281
289
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
+
if (mapped) {
|
|
291
|
+
return `${a[12]}.${a[13]}.${a[14]}.${a[15]}`
|
|
292
|
+
}
|
|
293
|
+
const parts: string[] = []
|
|
294
|
+
let high: u8 = toU8(0)
|
|
295
|
+
for (let i: i32 = 0; i < 16 && i < toI32(a.length); i += 1) {
|
|
296
|
+
if ((i & 1) === 0) {
|
|
297
|
+
high = a[i]
|
|
298
|
+
continue
|
|
299
|
+
}
|
|
300
|
+
const v: i32 = toI32(high) * 256 + toI32(a[i])
|
|
301
|
+
if (i > 1) {
|
|
302
|
+
parts.push(":")
|
|
290
303
|
}
|
|
291
304
|
for (let shift: i32 = 12; shift >= 0; shift -= 4) {
|
|
292
305
|
const d: i32 = (v >> shift) & 15
|
|
293
306
|
if (v >> shift !== 0 || shift === 0) {
|
|
294
|
-
|
|
295
|
-
out[at] = toU8(d < 10 ? 48 + d : 87 + d)
|
|
296
|
-
at += 1
|
|
297
|
-
}
|
|
307
|
+
parts.push(String.fromCharCode(toU8(d < 10 ? 48 + d : 87 + d)))
|
|
298
308
|
}
|
|
299
309
|
}
|
|
300
310
|
}
|
|
301
|
-
return
|
|
311
|
+
return parts.join("")
|
|
302
312
|
}
|
|
303
313
|
|
|
304
314
|
// --- helpers -----------------------------------------------------------------
|
|
315
|
+
// Each takes an address as an array of its own, `outboundSixteen`'s, so its
|
|
316
|
+
// reads are proven against that array's length rather than a caller's offset.
|
|
317
|
+
|
|
318
|
+
/** The sixteen address bytes at `addr[off]` in an array of their own, or null when they do not fit there. */
|
|
319
|
+
const outboundSixteen = (addr: u8[], off: i32): u8[] | null => {
|
|
320
|
+
const a: u8[] = new Array<u8>(16)
|
|
321
|
+
return tryCopyBytes(a, 0, addr, off, 16) ? a : null
|
|
322
|
+
}
|
|
305
323
|
|
|
306
|
-
/** Whether
|
|
307
|
-
const
|
|
324
|
+
/** Whether the address in `a`, `outboundSixteen`'s, is public, as `outboundAddressPublic` answers. */
|
|
325
|
+
const outboundPublic = (a: u8[]): boolean => {
|
|
326
|
+
if (toI32(a.length) < 16) {
|
|
327
|
+
return false
|
|
328
|
+
}
|
|
329
|
+
const carried: i32 = outboundCarriedV4(a)
|
|
330
|
+
if (carried >= 0) {
|
|
331
|
+
return outboundV4Public(a, carried)
|
|
332
|
+
}
|
|
333
|
+
const b0: i32 = toI32(a[0])
|
|
334
|
+
const b1: i32 = toI32(a[1])
|
|
335
|
+
const b2: i32 = toI32(a[2])
|
|
336
|
+
const b3: i32 = toI32(a[3])
|
|
337
|
+
if ((b0 & 0xe0) !== 0x20) {
|
|
338
|
+
// Outside 2000::/3: loopback, unspecified, unique local, link-local, multicast, the rest.
|
|
339
|
+
return false
|
|
340
|
+
}
|
|
341
|
+
if (b0 === 0x20 && b1 === 0x01 && (b2 & 0xfe) === 0) {
|
|
342
|
+
// 2001::/23, IETF protocol assignments, Teredo's 2001::/32 among them.
|
|
343
|
+
return false
|
|
344
|
+
}
|
|
345
|
+
if (b0 === 0x20 && b1 === 0x01 && b2 === 0x0d && b3 === 0xb8) {
|
|
346
|
+
// 2001:db8::/32, documentation.
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
// 3fff::/20, documentation (RFC 9637).
|
|
350
|
+
return !(b0 === 0x3f && b1 === 0xff && (b2 & 0xf0) === 0)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Whether one of `policy`'s permits covers the address in `a`. */
|
|
354
|
+
const outboundPermitted = (policy: OutboundPolicy, a: u8[]): boolean => {
|
|
308
355
|
const p: u8[] = policy.permitted
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
356
|
+
const entry: u8[] = new Array<u8>(OUTBOUND_PERMIT_BYTES)
|
|
357
|
+
let at: i32 = 0
|
|
358
|
+
while (at >= 0 && at <= toI32(p.length) - OUTBOUND_PERMIT_BYTES) {
|
|
359
|
+
if (tryCopyBytes(entry, 0, p, at, OUTBOUND_PERMIT_BYTES) && outboundPrefixMatch(entry, a)) {
|
|
312
360
|
return true
|
|
313
361
|
}
|
|
362
|
+
// At most the length less a permit before, so the sum fits.
|
|
363
|
+
at = toI32(toU32(at) + toU32(OUTBOUND_PERMIT_BYTES))
|
|
314
364
|
}
|
|
315
365
|
return false
|
|
316
366
|
}
|
|
317
367
|
|
|
318
|
-
/** Whether the
|
|
319
|
-
const outboundMapped = (
|
|
320
|
-
outboundZeros(
|
|
368
|
+
/** Whether the address in `a` is an IPv4-mapped address, `::ffff:0:0/96`. */
|
|
369
|
+
const outboundMapped = (a: u8[]): boolean =>
|
|
370
|
+
toI32(a.length) >= 16 && outboundZeros(a, 0, 10) && a[10] === toU8(255) && a[11] === toU8(255)
|
|
321
371
|
|
|
322
372
|
/**
|
|
323
|
-
* Where the IPv4 address that the
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
373
|
+
* Where the IPv4 address that the address in `a` carries starts, or -1 for
|
|
374
|
+
* none: `::ffff:a.b.c.d`, how nish:net writes every IPv4 address;
|
|
375
|
+
* `64:ff9b::/96`, the NAT64 well-known prefix (RFC 6052), in its last four
|
|
376
|
+
* bytes; `2002::/16`, 6to4 (RFC 3056), in the four after the prefix.
|
|
327
377
|
*/
|
|
328
|
-
const outboundCarriedV4 = (
|
|
329
|
-
if (
|
|
330
|
-
return
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
outboundZeros(addr, off + 4, 8)
|
|
340
|
-
) {
|
|
341
|
-
return off + OUTBOUND_V4_AT
|
|
378
|
+
const outboundCarriedV4 = (a: u8[]): i32 => {
|
|
379
|
+
if (toI32(a.length) < 16) {
|
|
380
|
+
return -1
|
|
381
|
+
}
|
|
382
|
+
if (outboundMapped(a)) {
|
|
383
|
+
return OUTBOUND_V4_AT
|
|
384
|
+
}
|
|
385
|
+
const b0: i32 = toI32(a[0])
|
|
386
|
+
const b1: i32 = toI32(a[1])
|
|
387
|
+
if (b0 === 0 && b1 === 0x64 && a[2] === toU8(0xff) && a[3] === toU8(0x9b) && outboundZeros(a, 4, 12)) {
|
|
388
|
+
return OUTBOUND_V4_AT
|
|
342
389
|
}
|
|
343
390
|
if (b0 === 0x20 && b1 === 0x02) {
|
|
344
|
-
return
|
|
391
|
+
return 2
|
|
345
392
|
}
|
|
346
393
|
return -1
|
|
347
394
|
}
|
|
348
395
|
|
|
349
|
-
/** Whether the IPv4 address in the four bytes at `
|
|
350
|
-
const outboundV4Public = (
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const
|
|
396
|
+
/** Whether the IPv4 address in the four bytes at `a[at]` is public; false when four do not fit there. */
|
|
397
|
+
const outboundV4Public = (a: u8[], at: i32): boolean => {
|
|
398
|
+
if (at < 0 || at > toI32(a.length) - 4) {
|
|
399
|
+
return false
|
|
400
|
+
}
|
|
401
|
+
const x: i32 = toI32(a[at])
|
|
402
|
+
const b: i32 = toI32(a[at + 1])
|
|
403
|
+
const c: i32 = toI32(a[at + 2])
|
|
404
|
+
const d: i32 = toI32(a[at + 3])
|
|
355
405
|
return !(
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
(
|
|
361
|
-
(
|
|
362
|
-
(
|
|
363
|
-
(
|
|
364
|
-
(
|
|
365
|
-
(
|
|
366
|
-
(
|
|
367
|
-
(
|
|
368
|
-
(
|
|
369
|
-
(
|
|
406
|
+
x === 0 ||
|
|
407
|
+
x === 10 ||
|
|
408
|
+
x === 127 ||
|
|
409
|
+
x >= 224 ||
|
|
410
|
+
(x === 100 && b >= 64 && b <= 127) ||
|
|
411
|
+
(x === 169 && b === 254) ||
|
|
412
|
+
(x === 172 && b >= 16 && b <= 31) ||
|
|
413
|
+
(x === 192 && b === 0 && (c === 0 || c === 2)) ||
|
|
414
|
+
(x === 192 && b === 88 && c === 99) ||
|
|
415
|
+
(x === 192 && b === 168) ||
|
|
416
|
+
(x === 198 && (b === 18 || b === 19)) ||
|
|
417
|
+
(x === 198 && b === 51 && c === 100) ||
|
|
418
|
+
(x === 203 && b === 0 && c === 113) ||
|
|
419
|
+
(x === 168 && b === 63 && c === 129 && d === 16)
|
|
370
420
|
)
|
|
371
421
|
}
|
|
372
422
|
|
|
373
|
-
/** Whether the
|
|
374
|
-
const outboundZeros = (b: u8[],
|
|
375
|
-
|
|
376
|
-
|
|
423
|
+
/** Whether the bytes `b[from..to)` are all zero; a range past `b`'s end is not. */
|
|
424
|
+
const outboundZeros = (b: u8[], from: i32, to: i32): boolean => {
|
|
425
|
+
if (from < 0 || to > toI32(b.length)) {
|
|
426
|
+
return false
|
|
427
|
+
}
|
|
428
|
+
for (let i: i32 = from; i < to; i += 1) {
|
|
429
|
+
if (b[i] !== toU8(0)) {
|
|
377
430
|
return false
|
|
378
431
|
}
|
|
379
432
|
}
|
|
380
433
|
return true
|
|
381
434
|
}
|
|
382
435
|
|
|
383
|
-
/**
|
|
384
|
-
|
|
436
|
+
/**
|
|
437
|
+
* Whether the address in `a` is under the permit `entry`, one
|
|
438
|
+
* `OUTBOUND_PERMIT_BYTES` record: its first prefix-length bits agree with
|
|
439
|
+
* the entry's sixteen address bytes. Never for a length past 128.
|
|
440
|
+
*/
|
|
441
|
+
const outboundPrefixMatch = (entry: u8[], a: u8[]): boolean => {
|
|
442
|
+
if (toI32(entry.length) <= 16 || toI32(a.length) < 16) {
|
|
443
|
+
return false
|
|
444
|
+
}
|
|
445
|
+
const bits: i32 = toI32(entry[16])
|
|
446
|
+
if (bits > 128) {
|
|
447
|
+
return false
|
|
448
|
+
}
|
|
385
449
|
const whole: i32 = bits >> 3
|
|
386
|
-
for (let i: i32 = 0; i < whole; i += 1) {
|
|
387
|
-
if (
|
|
450
|
+
for (let i: i32 = 0; i < whole && i < 16; i += 1) {
|
|
451
|
+
if (entry[i] !== a[i]) {
|
|
388
452
|
return false
|
|
389
453
|
}
|
|
390
454
|
}
|
|
391
455
|
const rest: i32 = bits & 7
|
|
392
|
-
if (rest === 0) {
|
|
456
|
+
if (rest === 0 || whole >= 16) {
|
|
393
457
|
return true
|
|
394
458
|
}
|
|
395
459
|
const mask: i32 = (toI32(0xff) << (8 - rest)) & toI32(0xff)
|
|
396
|
-
return (toI32(
|
|
460
|
+
return (toI32(entry[whole]) & mask) === (toI32(a[whole]) & mask)
|
|
397
461
|
}
|
|
398
462
|
|
|
399
463
|
/** `s[from..to)` as a decimal of one to three digits, or -1. */
|
|
@@ -401,15 +465,17 @@ const outboundDecimal = (s: string, from: i32, to: i32): i32 => {
|
|
|
401
465
|
if (to <= from || to - from > 3) {
|
|
402
466
|
return -1
|
|
403
467
|
}
|
|
404
|
-
|
|
468
|
+
// Unsigned, so the prover need not carry `v`'s bound round the loop: three
|
|
469
|
+
// digits make at most 999.
|
|
470
|
+
let v: u32 = toU32(0)
|
|
405
471
|
for (let i: i32 = from; i < to; i += 1) {
|
|
406
472
|
const c: i32 = toI32(s.charCodeAt(i))
|
|
407
473
|
if (c < 48 || c > 57) {
|
|
408
474
|
return -1
|
|
409
475
|
}
|
|
410
|
-
v = v * 10 + c - 48
|
|
476
|
+
v = v * toU32(10) + toU32(c - 48)
|
|
411
477
|
}
|
|
412
|
-
return v
|
|
478
|
+
return toI32(v)
|
|
413
479
|
}
|
|
414
480
|
|
|
415
481
|
/** Whether `name` is an address literal: it holds a colon, or its last label is all digits. */
|
|
@@ -427,13 +493,12 @@ const outboundIsLiteral = (name: string): boolean => {
|
|
|
427
493
|
|
|
428
494
|
/** `s` with A to Z lower-cased. */
|
|
429
495
|
const outboundLower = (s: string): string => {
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
for (let i: i32 = 0; i < toI32(out.length) && i < n; i += 1) {
|
|
496
|
+
const parts: string[] = []
|
|
497
|
+
for (let i: i32 = 0; i < toI32(s.length); i += 1) {
|
|
433
498
|
const c: i32 = toI32(s.charCodeAt(i))
|
|
434
|
-
|
|
499
|
+
parts.push(String.fromCharCode(toU8(c >= 65 && c <= 90 ? c + 32 : c)))
|
|
435
500
|
}
|
|
436
|
-
return
|
|
501
|
+
return parts.join("")
|
|
437
502
|
}
|
|
438
503
|
|
|
439
504
|
/** `s` without one trailing dot. */
|