@buildanagent/harness 0.1.0-alpha.30 → 0.1.0-alpha.31
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/README.md +69 -3
- package/dist/credentials/addresses.d.ts +9 -0
- package/dist/credentials/addresses.js +52 -0
- package/dist/credentials/addresses.js.map +1 -1
- package/dist/credentials/egress-proxy.js +34 -4
- package/dist/credentials/egress-proxy.js.map +1 -1
- package/dist/extensions/load.js +5 -2
- package/dist/extensions/load.js.map +1 -1
- package/dist/extensions/lock.d.ts +16 -2
- package/dist/extensions/lock.js +155 -21
- package/dist/extensions/lock.js.map +1 -1
- package/dist/extensions/mcp-pins-node.d.ts +16 -0
- package/dist/extensions/mcp-pins-node.js +18 -0
- package/dist/extensions/mcp-pins-node.js.map +1 -0
- package/dist/extensions/node.d.ts +4 -2
- package/dist/extensions/node.js +4 -2
- package/dist/extensions/node.js.map +1 -1
- package/dist/harness.d.ts +7 -1
- package/dist/harness.js.map +1 -1
- package/dist/mcp/connection.d.ts +9 -3
- package/dist/mcp/connection.js +192 -25
- package/dist/mcp/connection.js.map +1 -1
- package/dist/mcp/errors.d.ts +5 -2
- package/dist/mcp/errors.js.map +1 -1
- package/dist/mcp/guarded-fetch.d.ts +33 -0
- package/dist/mcp/guarded-fetch.js +391 -0
- package/dist/mcp/guarded-fetch.js.map +1 -0
- package/dist/mcp/http-node.d.ts +15 -0
- package/dist/mcp/http-node.js +179 -0
- package/dist/mcp/http-node.js.map +1 -0
- package/dist/mcp/index.d.ts +6 -3
- package/dist/mcp/index.js +5 -2
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/manager.d.ts +4 -3
- package/dist/mcp/manager.js +432 -47
- package/dist/mcp/manager.js.map +1 -1
- package/dist/mcp/pins.d.ts +9 -0
- package/dist/mcp/pins.js +54 -0
- package/dist/mcp/pins.js.map +1 -0
- package/dist/mcp/record.js +6 -0
- package/dist/mcp/record.js.map +1 -1
- package/dist/mcp/remote-config.d.ts +20 -0
- package/dist/mcp/remote-config.js +208 -0
- package/dist/mcp/remote-config.js.map +1 -0
- package/dist/mcp/remote.d.ts +20 -0
- package/dist/mcp/remote.js +146 -0
- package/dist/mcp/remote.js.map +1 -0
- package/dist/mcp/types.d.ts +198 -8
- package/dist/mcp/types.js.map +1 -1
- package/dist/server/protocol.schema.json +6 -3
- package/dist/session/runner.js +39 -6
- package/dist/session/runner.js.map +1 -1
- package/dist/tools/index.d.ts +1 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/types.d.ts +16 -0
- package/dist/tools/types.js.map +1 -1
- package/llms.txt +18 -2
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -196,7 +196,7 @@ A tool that needs a credential uses a route. `routes` lists, for each one, a nam
|
|
|
196
196
|
|
|
197
197
|
The secret is read for each request, so a rotated key is used at once, and it never enters the sandbox's environment, a log record or a response from the proxy.
|
|
198
198
|
|
|
199
|
-
`proxy.fetch(input, init)` is for tools that run in the harness's own process, which a network sandbox doesn't confine. It applies the same allowlist, address check and pinned connection, and it rejects with `egress_denied` for a host that isn't allowed. It uses HTTPS only and doesn't follow redirects, so each hop is a new call that the allowlist checks. A URL under `proxy.url`, or under `http://egress.proxy`, which also works on a Unix socket, is a route request with the same injection. An aborted signal rejects with its reason.
|
|
199
|
+
`proxy.fetch(input, init)` is for tools that run in the harness's own process, which a network sandbox doesn't confine. It applies the same allowlist, address check and pinned connection, and it rejects with `egress_denied` for a host that isn't allowed. It uses HTTPS only and doesn't follow redirects, so each hop is a new call that the allowlist checks. An upstream status outside 200 to 599, which Node parses and a Web `Response` refuses, rejects with `egress_denied`, closes the upstream connection and is logged with the reason `bad_status`. An answer that breaks after it has begun is logged once, when it closes. A URL under `proxy.url`, or under `http://egress.proxy`, which also works on a Unix socket, is a route request with the same injection. An aborted signal rejects with its reason.
|
|
200
200
|
|
|
201
201
|
`scrubSecrets(entry, secrets)` returns a copy of a session entry with each secret replaced by `[REDACTED]`, in every string and key at any depth, and also when the secret was percent-encoded, JSON-escaped or turned into base64, alone or inside a longer value such as a Basic authorization header. An entry's `type`, `id`, `parentId`, `seq`, `timestamp`, `origin` and `formatVersion` are never rewritten. Apply it before every append, with `await proxy.knownSecrets()` and the tokens and session URLs of any hosted-browser, CDP or OAuth connector tool, because the scrubber can only remove a secret it is told about.
|
|
202
202
|
|
|
@@ -731,6 +731,7 @@ The harness reads the list one page at a time and stops at the page that passes
|
|
|
731
731
|
- `aborted`: the turn's signal fired.
|
|
732
732
|
- `input_required`: the server asked for input from the user, which this harness does not supply yet.
|
|
733
733
|
- `server_error`: the server answered with an error, and its message is cut to 500 characters.
|
|
734
|
+
- `disabled`: the tool is new, changed or removed since it was pinned, and the call never reached the server.
|
|
734
735
|
|
|
735
736
|
A server that fails to start, or to finish its handshake within `connectTimeoutMs` (default 30,000 ms), fails to connect with `mcp_connect_failed`. A stdio server's line over `maxLineBytes` (default 8 MiB) closes the connection, and a line that is not a JSON object is dropped and logged. Only the newest `maxStderrBytes` (default 64 KiB) of its standard error are kept, for `connection.stderr()`, and they never reach the model.
|
|
736
737
|
|
|
@@ -740,9 +741,74 @@ A server that fails to start, or to finish its handshake within `connectTimeoutM
|
|
|
740
741
|
|
|
741
742
|
**One switch turns MCP off.** With `createMcpManager({ ..., mcp: false })`, `connect()` resolves with an empty list, starts no server and registers nothing.
|
|
742
743
|
|
|
743
|
-
**A session records which servers ran.** `manager.records()` gives one `extension_loaded` record of tier `mcp` per connected server, in config order; pass them in `extensionRecords` to `createHarness()`, and a turn writes an entry for each record its session's log does not hold yet. A record holds the server's name, the transport kind, the era, and two hashes: one of the command and its args, for a stdio server, and one of the tools as registered, each tool's name, description and input schema, sorted by name. It never holds an argument or an environment value, because either can carry a token, and a server that failed to connect has no record. The command hash is not salted, so anyone who reads the log can check a guess at an argument, such as a short PIN or a token of a known format, by hashing it; put every secret in `setEnv`, never in `args`. Every hash is `sha256:` and the SHA-256 of canonical JSON, so an unchanged server records the same hashes on every connect.
|
|
744
|
+
**A session records which servers ran.** `manager.records()` gives one `extension_loaded` record of tier `mcp` per connected server, in config order; pass them in `extensionRecords` to `createHarness()`, and a turn writes an entry for each record its session's log does not hold yet. A record holds the server's name, the transport kind, the era, and two hashes: one of the command and its args, for a stdio server, or of the URL, for a remote one, and one of the tools as registered, each tool's name, description and input schema, sorted by name. It never holds an argument or an environment value, because either can carry a token, and a server that failed to connect has no record. The command hash is not salted, so anyone who reads the log can check a guess at an argument, such as a short PIN or a token of a known format, by hashing it; put every secret in `setEnv`, never in `args`. Every hash is `sha256:` and the SHA-256 of canonical JSON, so an unchanged server records the same hashes on every connect.
|
|
744
745
|
|
|
745
|
-
|
|
746
|
+
Two limits remain. A server's request for user input fails the call: a server-supplied prompt is untrusted text a person would read as the harness's own, and the owner chose on 2026-11-22 to fail such calls until the connectors work gives a pause a safe form. And the manager takes configs only from host code; a project's own MCP config, which needs project trust, is todo N54.
|
|
747
|
+
|
|
748
|
+
## A remote server is reached over HTTPS to a public address, and every byte it sends is bounded
|
|
749
|
+
|
|
750
|
+
A server with `transport: { kind: "http", url }` speaks MCP's streamable HTTP transport, and the era is negotiated as for any other server. `kind: "sse"` is the HTTP+SSE transport of the 2024-11-05 revision, which MCP has deprecated since 2025-03-26; it is for legacy servers only, so the harness connects in the legacy era with no probe. A remote server is untrusted code on another machine, and the harness reaches it with its own network identity, so the URL, every address the URL's name resolves to and every byte that comes back are checked. The client library supplies both transports, and the harness's own guarded fetch sends every request they make. This example, `examples/mcp-remote.ts`, reaches a server through the egress proxy with a token the host read from its secret store, and puts the manager at the turn boundary:
|
|
751
|
+
|
|
752
|
+
```ts
|
|
753
|
+
export function trackerHarness(registry: ToolRegistry, model: Model, proxy: McpEgressFetch, token: string) {
|
|
754
|
+
const manager = createMcpManager({
|
|
755
|
+
registry,
|
|
756
|
+
servers: [
|
|
757
|
+
{
|
|
758
|
+
name: "tracker",
|
|
759
|
+
transport: {
|
|
760
|
+
kind: "http",
|
|
761
|
+
url: "https://mcp.tracker.example/mcp",
|
|
762
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
763
|
+
proxy,
|
|
764
|
+
},
|
|
765
|
+
exposure: "direct",
|
|
766
|
+
},
|
|
767
|
+
],
|
|
768
|
+
pins: memoryMcpPinStore(),
|
|
769
|
+
});
|
|
770
|
+
manager.start();
|
|
771
|
+
const pipeline = createPermissionPipeline(
|
|
772
|
+
{ mode: "default", hooks: [manager.permissionHook()], interactive: true, policyVersion: "v1" },
|
|
773
|
+
registry,
|
|
774
|
+
);
|
|
775
|
+
const harness = createHarness({ registry, model, system: "You track issues.", pipeline, turnBoundaries: [manager] });
|
|
776
|
+
return { manager, harness };
|
|
777
|
+
}
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
**The URL and the headers come from host code alone.** The URL must be `https:`, and the harness refuses one with a user name or password, a fragment, a control character, or a query parameter whose name looks like a credential, such as `token` or `api_key`. A credential goes in `headers`, which nothing reads from the environment. A header the transport sets itself, such as Host, Content-Type, Accept-Encoding or Mcp-Session-Id, is refused, and so is a value with a line break. A refusal names the rule and never quotes the value. No log line, error message or `extension_loaded` record holds a header value or the URL: errors and log lines have both replaced with `[redacted]`, and the record holds a hash of the URL. A `401` fails the connect or the call with an `McpError` that says so; the harness runs no OAuth flow and fetches no metadata the server points at.
|
|
781
|
+
|
|
782
|
+
**Every address is checked, and the request connects to the one that was checked.** Without a proxy, on Node, each request resolves the URL's name once, refuses it when any answer is loopback, private, carrier-grade NAT, link-local (with the cloud metadata address `169.254.169.254`), unspecified, multicast or reserved, or an IPv6 form that wraps one, and connects to the first answer. Once the socket connects, the harness checks the address it reached once more, so a name whose answer changes between two lookups can't steer a request to the host's own network. `allowAddresses` names the ranges one server may reach anyway, such as `10.20.0.0/16`; it never allows loopback. A `/0` range is refused, and so is any IPv6 range that holds the base of a form that wraps IPv4: mapped, compatible, translated, NAT64 or 6to4. `::ffff:0:0/96` is all of IPv4, and `::ffff:10.0.0.0/8` is really `::/8`, so both are refused; write the IPv4 range itself. A range in brackets, such as `[10.0.0.0]/8`, is refused too, since brackets belong to a URL. `unsafeAllowLoopback` lets a test reach a server on `127.0.0.1`, over plain `http:` when every answer is loopback, and nothing else does. `resolve` replaces the system resolver, and every address it returns is checked the same way. With `proxy`, every request goes through the egress proxy's `fetch()`, so its allowlist, its own address checks and its log apply, and the URL must be `https:`. On any other runtime, Cloudflare's workerd included, the platform's `fetch` carries the request. workerd has `node:https` under `nodejs_compat` but refuses the custom lookup the pinned connection needs, so the harness tells it apart by its user agent. There the harness checks a literal address, counts `localhost` and names under `.localhost`, with or without a trailing dot, as loopback, and can't resolve any other name, so the platform decides where it goes.
|
|
783
|
+
|
|
784
|
+
**TLS is never weakened.** Certificates are always verified, against the system's authorities or the server's own `ca`, over TLS 1.2 or later, and an `https:` URL never falls back to plain HTTP. The harness asks for no compression and refuses a compressed answer on Node, so a decompression bomb is never decoded; through a platform `fetch`, which decodes on its own, the byte caps count decoded bytes.
|
|
785
|
+
|
|
786
|
+
**No redirect is followed.** The client library would follow a redirect within the server's origin. The harness answers every `3xx` with an error instead, and answers a `304`, which no MCP request asks for, with an error that says so. It never sends a request that leaves the configured origin, such as one to an SSE `endpoint` event on another host.
|
|
787
|
+
|
|
788
|
+
**Every response is bounded.** A JSON body, one server-sent event and one line may each hold at most `maxMessageBytes` (default 8 MiB), and one stream at most `maxStreamBytes` (default 64 MiB). A response must start within `headersTimeoutMs` (default 30,000 ms), and a stream is cut when it sends nothing for `streamIdleMs` (default 120,000 ms) or stays open past `maxStreamMs` (default an hour). At most `maxStreams` responses (default 8) may be open on one connection; a request past that fails at once. A body, event or line past its size, or any bound on the answer to one request, closes the connection and fails what was waiting on it, with the bound in the error. A streamable HTTP GET stream or a `subscriptions/listen` stream that goes quiet, stays open an hour or passes `maxStreamBytes` is cut and opened again at once, and such a cut never counts against the reopen limits below. A legacy SSE server's event stream is the one channel its answers come on and can't be opened again, so it has only the event and line bounds: a quiet SSE server needs no keepalives. A body on that GET that is not an event stream, such as an error page, is bounded like any answer. The server's session id must be 1 to 256 visible ASCII characters, and the harness sends it back and trusts nothing else about it.
|
|
789
|
+
|
|
790
|
+
**The server can't set the reconnect delay.** The harness removes every `retry:` field from an event stream, and every `id:` field the client could not send back as `Last-Event-ID`, before the library reads the stream. A streamable HTTP stream that drops, or whose reopen fails, is opened again after one second, doubling to thirty, and after ten such reopens within ten minutes the connection closes, so a legacy server never keeps a connection with no stream for its change notices. A legacy SSE stream is opened once: when it ends, the connection closes, and nothing reconnects it.
|
|
791
|
+
|
|
792
|
+
## Servers connect in the background, and the first turn waits only for `direct` servers
|
|
793
|
+
|
|
794
|
+
`manager.start()` connects every server in the background and returns at once. Pass the manager to `createHarness({ turnBoundaries: [manager] })`. Each turn then calls `manager.beginTurn()` before it builds the turn's tools. The first turn waits up to `directWaitMs` (default 10,000 ms) for the servers whose `exposure` is `direct`, and for no other. A deferred server that takes thirty seconds to answer never delays the first model call, and a `direct` server that answers within the wait is in the first request's tools. A server that connects later is registered at the next turn's start, never inside a turn, and a `direct` one is registered as `deferred`, so the tools a request sends never change because a server was slow. A server that fails is tried once and reported once, in its status and through `log`. `manager.statuses()` reports each server, `connecting` until its tools are registered. `manager.close()` aborts the connects still running and every open stream, and clears its timers. `connect()` still connects everything and waits, for a host that wants every server before its first turn.
|
|
795
|
+
|
|
796
|
+
## A tool runs only while its definition matches its pin, and a change waits for the next turn
|
|
797
|
+
|
|
798
|
+
The manager pins each tool's definition: `sha256:` and the SHA-256 of the canonical JSON of its `name`, `description` and `inputSchema`. The annotations are left out, because they change nothing the harness does. With `pins`, a store of approved definitions, a tool that has no pin, or whose definition hashes differently, starts disabled, and a server the store holds nothing for has every tool disabled. Without a store, each server's tools as listed at connect are its pins for the life of the manager. `fileMcpPinStore(".agents/extensions.lock.json")` from `@buildanagent/harness/extensions/node` keeps the pins in the project's lock file, under `mcp.<server>.tools`, beside the extension pins, and each writer keeps the other's entries. `readMcpPins(file, server)` and `writeMcpPins(file, server, pins)` from the same entry read and write one server's pins directly, for a host that builds its own store; `writeMcpPins()` refuses a lock file it can't read. Every write to one path in this process, through any store or `writeLock()`, runs after the one before it. A lock file with MCP pins alone pins no extension, so writing it never makes a project's extensions refused, and the loader's notice says the file holds no extension pins. A lock file that is a link, not a regular file, or over 1 MiB is refused. The harness checks and reads it through one open handle, so a file swapped in between the check and the read is never read. The lock file sits in the project, so anyone who can commit to the project can approve definitions for any server the host configures: give a project's `fileMcpPinStore()` to the manager only for a project the person trusts, as project extensions load only then. `pinProjectExtensions()` replaces a lock file it can't read, such as one with merge-conflict markers, and its MCP pins go with it; an MCP pin write refuses such a file, since dropping the extension pins would let unpinned extensions load.
|
|
799
|
+
|
|
800
|
+
**A disabled tool is refused before its server.** `manager.permissionHook()` denies a call to a disabled tool before anything else decides, so a sticky allowance, an allow rule or `bypass` mode can't run it; put it first in the pipeline's `hooks`. A disabled tool's own `execute` refuses the call too, with `failure: "disabled"`, so the server is never reached without the hook either. `manager.pending()` lists the tools waiting for a person, with each one's definition for the person to read and its hash. `manager.approve(server, tool, hash)` writes the pin and then records the approval, so a failed write approves nothing, and the tool may run from the next turn. A hash that is no longer the server's current one is refused, so an approval never covers a definition the person did not see, and a tool that changes after its approval is disabled again. A tool runs only when both its server's current definition and the definition it was registered with match its pin, because the model reads the registered one all session. The registered definition's hash is the one taken from the same list the tool was registered from, so a server that lists another definition between its connect and the turn that registers it can't make the registered one pass. A tool registered with an unapproved definition therefore stays disabled for the session even if the server later lists the approved one, and a new session registers that one.
|
|
801
|
+
|
|
802
|
+
**A changed list is read again and announced at the next turn.** A legacy server's `notifications/tools/list_changed`, and the same notice on the `subscriptions/listen` stream that the harness opens on a 2026-07-28 server that advertises `tools.listChanged`, make the manager read the list again, at most once per `relistIntervalMs` (default 2,000 ms), however many notices come. A dropped listen stream is opened again after one second, doubling to a minute; after eight short-lived streams in a row, it stays closed. At the next turn's start, a changed, new or removed tool is disabled, an approved one is enabled, and the turn writes one notice, a text block of origin `harness` in front of the user's input. The gateway's transcript has no system role, so this block is how an append-only log tells the model that tools changed. The notice names each tool by its canonical name, which keeps only `A-Z`, `a-z`, `0-9`, `_` and `-` of the server's name for it, and says what changed. It never quotes a description, it counts the tools a server added mid-session instead of naming them, it lists at most 20 changes, and a change that is undone before the turn announces nothing. A notice counts as told only once the turn has written it, through `manager.commitNotices()`, so a turn that fails before its input is written tells it again next time.
|
|
803
|
+
|
|
804
|
+
**The tools a request sends never change mid-session.** A changed tool keeps the definition it was registered with, so the model reads the old description until a new session. A tool a server adds mid-session is not registered: it waits in `manager.pending()`, and once approved it joins the next session's tools.
|
|
805
|
+
|
|
806
|
+
Four limits come with the remote transports and the pins:
|
|
807
|
+
|
|
808
|
+
- the post-connect address check and the reconnect delay clamp are second guards, behind the pinned lookup and the removed `retry:` field;
|
|
809
|
+
- the manager's tool state is per manager, so two sessions that share one manager see each other's turn boundaries;
|
|
810
|
+
- a response stream that breaks loses its request, and the harness never sends a tool call again, since every MCP tool is a write;
|
|
811
|
+
- on any runtime but Node, Cloudflare's workerd included, a name is resolved by the platform, which the harness can't check; `localhost` and names under `.localhost` count as loopback there.
|
|
746
812
|
|
|
747
813
|
## A skill is a file the model reads, and only its name, description and path are in the prompt
|
|
748
814
|
|
|
@@ -12,6 +12,15 @@
|
|
|
12
12
|
export declare function isPublicAddress(address: string): boolean;
|
|
13
13
|
/** Whether `text` is an IPv4 or IPv6 address, as a host name that must not be looked up. */
|
|
14
14
|
export declare function isAddressLiteral(text: string): boolean;
|
|
15
|
+
/** Whether `range` is an address or a CIDR range such as `10.0.0.0/8` or `fd00::/8`. */
|
|
16
|
+
export declare function isAddressRange(range: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether `address` lies in `range`, an address or a CIDR range. An IPv4 address and its mapped IPv6 form are the same
|
|
19
|
+
* address here. Text that is not an address, or a range that can't be read, matches nothing.
|
|
20
|
+
*/
|
|
21
|
+
export declare function inAddressRange(address: string, range: string): boolean;
|
|
22
|
+
/** Whether `address` is a loopback address: `127.0.0.0/8`, `::1`, or an IPv6 form that wraps a `127` address. */
|
|
23
|
+
export declare function isLoopbackAddress(address: string): boolean;
|
|
15
24
|
/**
|
|
16
25
|
* The IPv4 address `address` names, as a number: a dotted quad in its canonical form, or an IPv6 address that wraps
|
|
17
26
|
* one (the mapped, compatible, translated, NAT64 and 6to4 forms). `undefined` for anything else.
|
|
@@ -143,6 +143,58 @@ export function isAddressLiteral(text) {
|
|
|
143
143
|
const bare = text.startsWith("[") && text.endsWith("]") ? text.slice(1, -1) : text;
|
|
144
144
|
return parseIpv4(bare) !== undefined || parseIpv6(bare) !== undefined;
|
|
145
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* The address as eight 16-bit groups, with an IPv4 address in its mapped form `::ffff:a.b.c.d`, or `undefined` when
|
|
148
|
+
* `text` is not an address.
|
|
149
|
+
*/
|
|
150
|
+
function groupsOf(text) {
|
|
151
|
+
const bare = text.startsWith("[") && text.endsWith("]") ? text.slice(1, -1) : text;
|
|
152
|
+
const v4 = parseIpv4(bare);
|
|
153
|
+
if (v4 !== undefined)
|
|
154
|
+
return [0, 0, 0, 0, 0, 0xffff, Math.floor(v4 / 65536), v4 % 65536];
|
|
155
|
+
return parseIpv6(bare);
|
|
156
|
+
}
|
|
157
|
+
/** The prefix length of a range in mapped form: an IPv4 prefix counts from bit 96. */
|
|
158
|
+
function rangeOf(range) {
|
|
159
|
+
const [address = "", prefix, extra] = range.split("/");
|
|
160
|
+
// Brackets belong to a URL's host, not to a range: read as written, `[10.0.0.0]/8` would pass as IPv6 `::/8`.
|
|
161
|
+
if (extra !== undefined || address.includes("[") || address.includes("]"))
|
|
162
|
+
return undefined;
|
|
163
|
+
const groups = groupsOf(address);
|
|
164
|
+
if (groups === undefined)
|
|
165
|
+
return undefined;
|
|
166
|
+
const v4 = parseIpv4(address) !== undefined;
|
|
167
|
+
const max = v4 ? 32 : 128;
|
|
168
|
+
if (prefix !== undefined && !/^(0|[1-9][0-9]{0,2})$/.test(prefix))
|
|
169
|
+
return undefined;
|
|
170
|
+
const length = prefix === undefined ? max : Number(prefix);
|
|
171
|
+
if (length > max)
|
|
172
|
+
return undefined;
|
|
173
|
+
return { groups, bits: v4 ? length + 96 : length };
|
|
174
|
+
}
|
|
175
|
+
/** Whether `range` is an address or a CIDR range such as `10.0.0.0/8` or `fd00::/8`. */
|
|
176
|
+
export function isAddressRange(range) {
|
|
177
|
+
return rangeOf(range) !== undefined;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Whether `address` lies in `range`, an address or a CIDR range. An IPv4 address and its mapped IPv6 form are the same
|
|
181
|
+
* address here. Text that is not an address, or a range that can't be read, matches nothing.
|
|
182
|
+
*/
|
|
183
|
+
export function inAddressRange(address, range) {
|
|
184
|
+
const groups = groupsOf(address);
|
|
185
|
+
const parsed = rangeOf(range);
|
|
186
|
+
return groups !== undefined && parsed !== undefined && inPrefix(groups, parsed.groups, parsed.bits);
|
|
187
|
+
}
|
|
188
|
+
/** Whether `address` is a loopback address: `127.0.0.0/8`, `::1`, or an IPv6 form that wraps a `127` address. */
|
|
189
|
+
export function isLoopbackAddress(address) {
|
|
190
|
+
const groups = groupsOf(address);
|
|
191
|
+
if (groups === undefined)
|
|
192
|
+
return false;
|
|
193
|
+
if (inPrefix(groups, [0, 0, 0, 0, 0, 0, 0, 1], 128))
|
|
194
|
+
return true;
|
|
195
|
+
const wrapped = wrappedIpv4(groups);
|
|
196
|
+
return wrapped !== undefined && Math.floor(wrapped / 2 ** 24) === 127;
|
|
197
|
+
}
|
|
146
198
|
const bareOf = (text) => (text.startsWith("[") && text.endsWith("]") ? text.slice(1, -1) : text);
|
|
147
199
|
/**
|
|
148
200
|
* The IPv4 address `address` names, as a number: a dotted quad in its canonical form, or an IPv6 address that wraps
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addresses.js","sourceRoot":"","sources":["../../src/credentials/addresses.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,sEAAsE;AACtE,MAAM,YAAY,GA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AAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC;IAC/B,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;CACf,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3B,IAAI,EAAE,KAAK,SAAS,IAAI,YAAY,CAAC,EAAE,EAAE,UAAU,CAAC;QAAE,OAAO,IAAI,CAAC;IAClE,MAAM,EAAE,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IACtC,OAAO,EAAE,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;AAC7F,CAAC","sourcesContent":["/**\n * Which network addresses an egress proxy may connect to. It refuses every address that isn't a public one: loopback,\n * link-local (which holds the cloud metadata address 169.254.169.254), private, carrier-grade NAT, unspecified,\n * multicast and reserved ranges, for both IPv4 and IPv6, and an IPv6 address that wraps one of the refused IPv4 ones.\n * Names resolve to addresses in the proxy, and each address is checked here, so a name that points at the host's own\n * network is refused even when its name is allowed. It parses addresses itself and imports no Node module.\n */\n\n/** The IPv4 ranges a proxy refuses, as `[address, prefix length]`. */\nconst IPV4_REFUSED: readonly (readonly [number, number])[] = [\n [0x00000000, 8], // this network\n [0x0a000000, 8], // private\n [0x64400000, 10], // carrier-grade NAT, 100.64.0.0/10\n [0x7f000000, 8], // loopback\n [0xa9fe0000, 16], // link-local, which holds the metadata address\n [0xac100000, 12], // private\n [0xc0000000, 24], // IETF protocol assignments\n [0xc0000200, 24], // documentation\n [0xc0a80000, 16], // private\n [0xc6120000, 15], // benchmarking, 198.18.0.0/15\n [0xc6336400, 24], // documentation\n [0xcb007100, 24], // documentation\n [0xe0000000, 4], // multicast\n [0xf0000000, 4], // reserved, and the broadcast address\n];\n\n/** The four parts of an IPv4 address as one number, or `undefined` when `text` is not a dotted quad. */\nfunction parseIpv4(text: string): number | undefined {\n const parts = text.split(\".\");\n if (parts.length !== 4) return undefined;\n let value = 0;\n for (const part of parts) {\n if (!/^(0|[1-9][0-9]{0,2})$/.test(part)) return undefined;\n const octet = Number(part);\n if (octet > 255) return undefined;\n value = value * 256 + octet;\n }\n return value;\n}\n\n/** The 16-bit groups in one side of an IPv6 address, where the last piece may be a dotted IPv4 address. */\nfunction readGroups(side: string): number[] | undefined {\n if (side === \"\") return [];\n const groups: number[] = [];\n const pieces = side.split(\":\");\n for (const [index, piece] of pieces.entries()) {\n const v4 = index === pieces.length - 1 && piece.includes(\".\") ? parseIpv4(piece) : undefined;\n if (v4 !== undefined) {\n groups.push(Math.floor(v4 / 65536), v4 % 65536);\n } else if (/^[0-9a-fA-F]{1,4}$/.test(piece)) {\n groups.push(Number.parseInt(piece, 16));\n } else {\n return undefined;\n }\n }\n return groups;\n}\n\n/** The eight 16-bit groups of an IPv6 address, or `undefined` when `text` is not one. */\nfunction parseIpv6(text: string): number[] | undefined {\n const whole = text.split(\"%\")[0] ?? text;\n const doubles = whole.split(\"::\");\n if (doubles.length > 2) return undefined;\n const head = readGroups(doubles[0] ?? \"\");\n const tail = doubles.length === 2 ? readGroups(doubles[1] ?? \"\") : [];\n if (head === undefined || tail === undefined) return undefined;\n if (doubles.length === 1) return head.length === 8 ? head : undefined;\n const missing = 8 - head.length - tail.length;\n return missing >= 1 ? [...head, ...new Array<number>(missing).fill(0), ...tail] : undefined;\n}\n\n/** Whether `value` falls in one of `ranges`, each an `[address, prefix length]` pair. */\nfunction inIpv4Ranges(value: number, ranges: readonly (readonly [number, number])[]): boolean {\n return ranges.some(([base, bits]) => {\n const size = 2 ** (32 - bits);\n return Math.floor(value / size) === Math.floor(base / size);\n });\n}\n\nconst ipv4Refused = (value: number): boolean => inIpv4Ranges(value, IPV4_REFUSED);\n\n/** The IPv6 prefixes a proxy refuses, as the leading groups and the prefix length in bits. */\nconst IPV6_REFUSED: readonly (readonly [readonly number[], number])[] = [\n [[0, 0, 0, 0, 0, 0, 0, 0], 128], // ::, unspecified\n [[0, 0, 0, 0, 0, 0, 0, 1], 128], // ::1, loopback\n [[0x64, 0xff9b, 1], 48], // 64:ff9b:1::/48, local-use NAT64\n [[0x100], 64], // 100::/64, discard-only\n [[0x2001, 0], 23], // 2001::/23: Teredo, benchmarking, ORCHID and protocol assignments\n [[0x2001, 0x0db8], 32], // documentation\n [[0x3fff], 20], // 3fff::/20, documentation\n [[0x5f00], 16], // segment routing SIDs\n [[0xfc00], 7], // unique local\n [[0xfe80], 10], // link-local\n [[0xfec0], 10], // site-local, deprecated\n [[0xff00], 8], // multicast\n];\n\n/** Whether the first `bits` bits of `groups` equal those of `prefix`. */\nfunction inPrefix(groups: readonly number[], prefix: readonly number[], bits: number): boolean {\n for (let index = 0; index * 16 < bits; index += 1) {\n const take = Math.min(16, bits - index * 16);\n const mask = (0xffff << (16 - take)) & 0xffff;\n if (((groups[index] ?? 0) & mask) !== ((prefix[index] ?? 0) & mask)) return false;\n }\n return true;\n}\n\n/** The IPv4 address an IPv6 one carries, as a number: mapped, compatible, translated, NAT64 and 6to4 forms. */\nfunction wrappedIpv4(groups: readonly number[]): number | undefined {\n const [a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0] = groups;\n const zeroHigh = a === 0 && b === 0 && c === 0 && d === 0;\n const low = g * 65536 + h;\n if (zeroHigh && e === 0 && (f === 0xffff || f === 0)) return low; // ::ffff:a.b.c.d and ::a.b.c.d\n if (zeroHigh && e === 0xffff && f === 0) return low; // ::ffff:0:a.b.c.d, IPv4-translated\n if (a === 0x64 && b === 0xff9b && c === 0 && d === 0 && e === 0 && f === 0) return low; // 64:ff9b::/96\n return a === 0x2002 ? b * 65536 + c : undefined; // 6to4 wraps the address in its next two groups\n}\n\n/** Whether an IPv6 address, as eight groups, is one a proxy refuses. */\nfunction ipv6Refused(groups: readonly number[]): boolean {\n const wrapped = wrappedIpv4(groups);\n if (wrapped !== undefined && ipv4Refused(wrapped)) return true;\n return IPV6_REFUSED.some(([prefix, bits]) => inPrefix(groups, prefix, bits));\n}\n\n/**\n * Whether `address` is a public one a proxy may connect to. Text that isn't an IPv4 or IPv6 address is not public, so a\n * resolver's odd answer is refused instead of passed on.\n */\nexport function isPublicAddress(address: string): boolean {\n const bare = address.startsWith(\"[\") && address.endsWith(\"]\") ? address.slice(1, -1) : address;\n const v4 = parseIpv4(bare);\n if (v4 !== undefined) return !ipv4Refused(v4);\n const v6 = parseIpv6(bare);\n return v6 !== undefined && !ipv6Refused(v6);\n}\n\n/** Whether `text` is an IPv4 or IPv6 address, as a host name that must not be looked up. */\nexport function isAddressLiteral(text: string): boolean {\n const bare = text.startsWith(\"[\") && text.endsWith(\"]\") ? text.slice(1, -1) : text;\n return parseIpv4(bare) !== undefined || parseIpv6(bare) !== undefined;\n}\n\nconst bareOf = (text: string): string => (text.startsWith(\"[\") && text.endsWith(\"]\") ? text.slice(1, -1) : text);\n\n/**\n * The IPv4 address `address` names, as a number: a dotted quad in its canonical form, or an IPv6 address that wraps\n * one (the mapped, compatible, translated, NAT64 and 6to4 forms). `undefined` for anything else.\n */\nexport function ipv4Of(address: string): number | undefined {\n const bare = bareOf(address);\n const v4 = parseIpv4(bare);\n if (v4 !== undefined) return v4;\n const v6 = parseIpv6(bare);\n return v6 === undefined ? undefined : wrappedIpv4(v6);\n}\n\n/** The IPv4 ranges that reach the host itself or its link: this network, loopback and link-local. */\nconst IPV4_LOCAL: readonly (readonly [number, number])[] = [\n [0x00000000, 8],\n [0x7f000000, 8],\n [0xa9fe0000, 16],\n];\n\n/** The IPv6 prefixes that reach the host itself or its link: unspecified, loopback and link-local. */\nconst IPV6_LOCAL: readonly (readonly [readonly number[], number])[] = [\n [[0, 0, 0, 0, 0, 0, 0, 0], 128],\n [[0, 0, 0, 0, 0, 0, 0, 1], 128],\n [[0xfe80], 10],\n];\n\n/**\n * Whether `address` reaches the host itself or its link: unspecified, loopback or link-local (which holds the cloud\n * metadata address 169.254.169.254), for IPv4 and IPv6, and an IPv6 address that wraps one of those IPv4 ones. Text\n * that isn't an address literal is not one.\n */\nexport function isLocalAddress(address: string): boolean {\n const v4 = ipv4Of(address);\n if (v4 !== undefined && inIpv4Ranges(v4, IPV4_LOCAL)) return true;\n const v6 = parseIpv6(bareOf(address));\n return v6 !== undefined && IPV6_LOCAL.some(([prefix, bits]) => inPrefix(v6, prefix, bits));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"addresses.js","sourceRoot":"","sources":["../../src/credentials/addresses.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,sEAAsE;AACtE,MAAM,YAAY,GAA2C;IAC3D,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,eAAe;IAChC,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,UAAU;IAC3B,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,mCAAmC;IACrD,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,WAAW;IAC5B,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,+CAA+C;IACjE,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,UAAU;IAC5B,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,4BAA4B;IAC9C,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,gBAAgB;IAClC,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,UAAU;IAC5B,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,8BAA8B;IAChD,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,gBAAgB;IAClC,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE,gBAAgB;IAClC,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,YAAY;IAC7B,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,sCAAsC;CACxD,CAAC;AAEF,wGAAwG;AACxG,SAAS,SAAS,CAAC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AC;IAC3B,OAAO,EAAE,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;AACxD,CAAC;AAED,qGAAqG;AACrG,MAAM,UAAU,GAA2C;IACzD,CAAC,UAAU,EAAE,CAAC,CAAC;IACf,CAAC,UAAU,EAAE,CAAC,CAAC;IACf,CAAC,UAAU,EAAE,EAAE,CAAC;CACjB,CAAC;AAEF,sGAAsG;AACtG,MAAM,UAAU,GAAsD;IACpE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC;IAC/B,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC;IAC/B,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC;CACf,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3B,IAAI,EAAE,KAAK,SAAS,IAAI,YAAY,CAAC,EAAE,EAAE,UAAU,CAAC;QAAE,OAAO,IAAI,CAAC;IAClE,MAAM,EAAE,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IACtC,OAAO,EAAE,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;AAC7F,CAAC","sourcesContent":["/**\n * Which network addresses an egress proxy may connect to. It refuses every address that isn't a public one: loopback,\n * link-local (which holds the cloud metadata address 169.254.169.254), private, carrier-grade NAT, unspecified,\n * multicast and reserved ranges, for both IPv4 and IPv6, and an IPv6 address that wraps one of the refused IPv4 ones.\n * Names resolve to addresses in the proxy, and each address is checked here, so a name that points at the host's own\n * network is refused even when its name is allowed. It parses addresses itself and imports no Node module.\n */\n\n/** The IPv4 ranges a proxy refuses, as `[address, prefix length]`. */\nconst IPV4_REFUSED: readonly (readonly [number, number])[] = [\n [0x00000000, 8], // this network\n [0x0a000000, 8], // private\n [0x64400000, 10], // carrier-grade NAT, 100.64.0.0/10\n [0x7f000000, 8], // loopback\n [0xa9fe0000, 16], // link-local, which holds the metadata address\n [0xac100000, 12], // private\n [0xc0000000, 24], // IETF protocol assignments\n [0xc0000200, 24], // documentation\n [0xc0a80000, 16], // private\n [0xc6120000, 15], // benchmarking, 198.18.0.0/15\n [0xc6336400, 24], // documentation\n [0xcb007100, 24], // documentation\n [0xe0000000, 4], // multicast\n [0xf0000000, 4], // reserved, and the broadcast address\n];\n\n/** The four parts of an IPv4 address as one number, or `undefined` when `text` is not a dotted quad. */\nfunction parseIpv4(text: string): number | undefined {\n const parts = text.split(\".\");\n if (parts.length !== 4) return undefined;\n let value = 0;\n for (const part of parts) {\n if (!/^(0|[1-9][0-9]{0,2})$/.test(part)) return undefined;\n const octet = Number(part);\n if (octet > 255) return undefined;\n value = value * 256 + octet;\n }\n return value;\n}\n\n/** The 16-bit groups in one side of an IPv6 address, where the last piece may be a dotted IPv4 address. */\nfunction readGroups(side: string): number[] | undefined {\n if (side === \"\") return [];\n const groups: number[] = [];\n const pieces = side.split(\":\");\n for (const [index, piece] of pieces.entries()) {\n const v4 = index === pieces.length - 1 && piece.includes(\".\") ? parseIpv4(piece) : undefined;\n if (v4 !== undefined) {\n groups.push(Math.floor(v4 / 65536), v4 % 65536);\n } else if (/^[0-9a-fA-F]{1,4}$/.test(piece)) {\n groups.push(Number.parseInt(piece, 16));\n } else {\n return undefined;\n }\n }\n return groups;\n}\n\n/** The eight 16-bit groups of an IPv6 address, or `undefined` when `text` is not one. */\nfunction parseIpv6(text: string): number[] | undefined {\n const whole = text.split(\"%\")[0] ?? text;\n const doubles = whole.split(\"::\");\n if (doubles.length > 2) return undefined;\n const head = readGroups(doubles[0] ?? \"\");\n const tail = doubles.length === 2 ? readGroups(doubles[1] ?? \"\") : [];\n if (head === undefined || tail === undefined) return undefined;\n if (doubles.length === 1) return head.length === 8 ? head : undefined;\n const missing = 8 - head.length - tail.length;\n return missing >= 1 ? [...head, ...new Array<number>(missing).fill(0), ...tail] : undefined;\n}\n\n/** Whether `value` falls in one of `ranges`, each an `[address, prefix length]` pair. */\nfunction inIpv4Ranges(value: number, ranges: readonly (readonly [number, number])[]): boolean {\n return ranges.some(([base, bits]) => {\n const size = 2 ** (32 - bits);\n return Math.floor(value / size) === Math.floor(base / size);\n });\n}\n\nconst ipv4Refused = (value: number): boolean => inIpv4Ranges(value, IPV4_REFUSED);\n\n/** The IPv6 prefixes a proxy refuses, as the leading groups and the prefix length in bits. */\nconst IPV6_REFUSED: readonly (readonly [readonly number[], number])[] = [\n [[0, 0, 0, 0, 0, 0, 0, 0], 128], // ::, unspecified\n [[0, 0, 0, 0, 0, 0, 0, 1], 128], // ::1, loopback\n [[0x64, 0xff9b, 1], 48], // 64:ff9b:1::/48, local-use NAT64\n [[0x100], 64], // 100::/64, discard-only\n [[0x2001, 0], 23], // 2001::/23: Teredo, benchmarking, ORCHID and protocol assignments\n [[0x2001, 0x0db8], 32], // documentation\n [[0x3fff], 20], // 3fff::/20, documentation\n [[0x5f00], 16], // segment routing SIDs\n [[0xfc00], 7], // unique local\n [[0xfe80], 10], // link-local\n [[0xfec0], 10], // site-local, deprecated\n [[0xff00], 8], // multicast\n];\n\n/** Whether the first `bits` bits of `groups` equal those of `prefix`. */\nfunction inPrefix(groups: readonly number[], prefix: readonly number[], bits: number): boolean {\n for (let index = 0; index * 16 < bits; index += 1) {\n const take = Math.min(16, bits - index * 16);\n const mask = (0xffff << (16 - take)) & 0xffff;\n if (((groups[index] ?? 0) & mask) !== ((prefix[index] ?? 0) & mask)) return false;\n }\n return true;\n}\n\n/** The IPv4 address an IPv6 one carries, as a number: mapped, compatible, translated, NAT64 and 6to4 forms. */\nfunction wrappedIpv4(groups: readonly number[]): number | undefined {\n const [a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0] = groups;\n const zeroHigh = a === 0 && b === 0 && c === 0 && d === 0;\n const low = g * 65536 + h;\n if (zeroHigh && e === 0 && (f === 0xffff || f === 0)) return low; // ::ffff:a.b.c.d and ::a.b.c.d\n if (zeroHigh && e === 0xffff && f === 0) return low; // ::ffff:0:a.b.c.d, IPv4-translated\n if (a === 0x64 && b === 0xff9b && c === 0 && d === 0 && e === 0 && f === 0) return low; // 64:ff9b::/96\n return a === 0x2002 ? b * 65536 + c : undefined; // 6to4 wraps the address in its next two groups\n}\n\n/** Whether an IPv6 address, as eight groups, is one a proxy refuses. */\nfunction ipv6Refused(groups: readonly number[]): boolean {\n const wrapped = wrappedIpv4(groups);\n if (wrapped !== undefined && ipv4Refused(wrapped)) return true;\n return IPV6_REFUSED.some(([prefix, bits]) => inPrefix(groups, prefix, bits));\n}\n\n/**\n * Whether `address` is a public one a proxy may connect to. Text that isn't an IPv4 or IPv6 address is not public, so a\n * resolver's odd answer is refused instead of passed on.\n */\nexport function isPublicAddress(address: string): boolean {\n const bare = address.startsWith(\"[\") && address.endsWith(\"]\") ? address.slice(1, -1) : address;\n const v4 = parseIpv4(bare);\n if (v4 !== undefined) return !ipv4Refused(v4);\n const v6 = parseIpv6(bare);\n return v6 !== undefined && !ipv6Refused(v6);\n}\n\n/** Whether `text` is an IPv4 or IPv6 address, as a host name that must not be looked up. */\nexport function isAddressLiteral(text: string): boolean {\n const bare = text.startsWith(\"[\") && text.endsWith(\"]\") ? text.slice(1, -1) : text;\n return parseIpv4(bare) !== undefined || parseIpv6(bare) !== undefined;\n}\n\n/**\n * The address as eight 16-bit groups, with an IPv4 address in its mapped form `::ffff:a.b.c.d`, or `undefined` when\n * `text` is not an address.\n */\nfunction groupsOf(text: string): number[] | undefined {\n const bare = text.startsWith(\"[\") && text.endsWith(\"]\") ? text.slice(1, -1) : text;\n const v4 = parseIpv4(bare);\n if (v4 !== undefined) return [0, 0, 0, 0, 0, 0xffff, Math.floor(v4 / 65536), v4 % 65536];\n return parseIpv6(bare);\n}\n\n/** The prefix length of a range in mapped form: an IPv4 prefix counts from bit 96. */\nfunction rangeOf(range: string): { readonly groups: number[]; readonly bits: number } | undefined {\n const [address = \"\", prefix, extra] = range.split(\"/\");\n // Brackets belong to a URL's host, not to a range: read as written, `[10.0.0.0]/8` would pass as IPv6 `::/8`.\n if (extra !== undefined || address.includes(\"[\") || address.includes(\"]\")) return undefined;\n const groups = groupsOf(address);\n if (groups === undefined) return undefined;\n const v4 = parseIpv4(address) !== undefined;\n const max = v4 ? 32 : 128;\n if (prefix !== undefined && !/^(0|[1-9][0-9]{0,2})$/.test(prefix)) return undefined;\n const length = prefix === undefined ? max : Number(prefix);\n if (length > max) return undefined;\n return { groups, bits: v4 ? length + 96 : length };\n}\n\n/** Whether `range` is an address or a CIDR range such as `10.0.0.0/8` or `fd00::/8`. */\nexport function isAddressRange(range: string): boolean {\n return rangeOf(range) !== undefined;\n}\n\n/**\n * Whether `address` lies in `range`, an address or a CIDR range. An IPv4 address and its mapped IPv6 form are the same\n * address here. Text that is not an address, or a range that can't be read, matches nothing.\n */\nexport function inAddressRange(address: string, range: string): boolean {\n const groups = groupsOf(address);\n const parsed = rangeOf(range);\n return groups !== undefined && parsed !== undefined && inPrefix(groups, parsed.groups, parsed.bits);\n}\n\n/** Whether `address` is a loopback address: `127.0.0.0/8`, `::1`, or an IPv6 form that wraps a `127` address. */\nexport function isLoopbackAddress(address: string): boolean {\n const groups = groupsOf(address);\n if (groups === undefined) return false;\n if (inPrefix(groups, [0, 0, 0, 0, 0, 0, 0, 1], 128)) return true;\n const wrapped = wrappedIpv4(groups);\n return wrapped !== undefined && Math.floor(wrapped / 2 ** 24) === 127;\n}\n\nconst bareOf = (text: string): string => (text.startsWith(\"[\") && text.endsWith(\"]\") ? text.slice(1, -1) : text);\n\n/**\n * The IPv4 address `address` names, as a number: a dotted quad in its canonical form, or an IPv6 address that wraps\n * one (the mapped, compatible, translated, NAT64 and 6to4 forms). `undefined` for anything else.\n */\nexport function ipv4Of(address: string): number | undefined {\n const bare = bareOf(address);\n const v4 = parseIpv4(bare);\n if (v4 !== undefined) return v4;\n const v6 = parseIpv6(bare);\n return v6 === undefined ? undefined : wrappedIpv4(v6);\n}\n\n/** The IPv4 ranges that reach the host itself or its link: this network, loopback and link-local. */\nconst IPV4_LOCAL: readonly (readonly [number, number])[] = [\n [0x00000000, 8],\n [0x7f000000, 8],\n [0xa9fe0000, 16],\n];\n\n/** The IPv6 prefixes that reach the host itself or its link: unspecified, loopback and link-local. */\nconst IPV6_LOCAL: readonly (readonly [readonly number[], number])[] = [\n [[0, 0, 0, 0, 0, 0, 0, 0], 128],\n [[0, 0, 0, 0, 0, 0, 0, 1], 128],\n [[0xfe80], 10],\n];\n\n/**\n * Whether `address` reaches the host itself or its link: unspecified, loopback or link-local (which holds the cloud\n * metadata address 169.254.169.254), for IPv4 and IPv6, and an IPv6 address that wraps one of those IPv4 ones. Text\n * that isn't an address literal is not one.\n */\nexport function isLocalAddress(address: string): boolean {\n const v4 = ipv4Of(address);\n if (v4 !== undefined && inIpv4Ranges(v4, IPV4_LOCAL)) return true;\n const v6 = parseIpv6(bareOf(address));\n return v6 !== undefined && IPV6_LOCAL.some(([prefix, bits]) => inPrefix(v6, prefix, bits));\n}\n"]}
|
|
@@ -525,11 +525,13 @@ class EgressGate {
|
|
|
525
525
|
stream?.on("data", (chunk) => {
|
|
526
526
|
bytes += chunk.length;
|
|
527
527
|
});
|
|
528
|
+
// A status `Response` refuses is recorded with its reason, as the fetch records one.
|
|
529
|
+
const reason = reply.status < 200 || reply.status > 599 ? "bad_status" : reply.reason;
|
|
528
530
|
const finished = () => this.record({
|
|
529
531
|
...base,
|
|
530
532
|
status: reply.status,
|
|
531
533
|
bytes,
|
|
532
|
-
...(
|
|
534
|
+
...(reason === undefined ? {} : { reason }),
|
|
533
535
|
});
|
|
534
536
|
if (stream === undefined)
|
|
535
537
|
finished();
|
|
@@ -563,16 +565,38 @@ class EgressGate {
|
|
|
563
565
|
request.signal.removeEventListener("abort", abort);
|
|
564
566
|
});
|
|
565
567
|
upstream.setTimeout(this.idleMs, () => upstream.destroy(new Error("idle")));
|
|
568
|
+
// Once an answer has begun, its record is written when the answer closes, so a socket error after it, such as a
|
|
569
|
+
// 204 with bytes after it, writes no second record.
|
|
570
|
+
let answered = false;
|
|
566
571
|
upstream.once("response", (reply) => {
|
|
572
|
+
answered = true;
|
|
567
573
|
let bytes = 0;
|
|
574
|
+
let reason;
|
|
568
575
|
reply.on("data", (chunk) => {
|
|
569
576
|
bytes += chunk.length;
|
|
570
577
|
});
|
|
571
578
|
// The record is written when the response is done with, which also covers one that is never read to its end.
|
|
572
|
-
reply.once("close", () => this.record({
|
|
573
|
-
|
|
579
|
+
reply.once("close", () => this.record({
|
|
580
|
+
kind: "fetch",
|
|
581
|
+
host,
|
|
582
|
+
port,
|
|
583
|
+
method: request.method,
|
|
584
|
+
status: reply.statusCode ?? 0,
|
|
585
|
+
bytes,
|
|
586
|
+
...(reason === undefined ? {} : { reason }),
|
|
587
|
+
}));
|
|
588
|
+
// A throw here would escape the event handler and end the process, so a status `Response` refuses rejects.
|
|
589
|
+
try {
|
|
590
|
+
resolve(this.toResponse(reply.statusCode ?? 0, reply.headers, reply));
|
|
591
|
+
}
|
|
592
|
+
catch (error) {
|
|
593
|
+
reason = "bad_status";
|
|
594
|
+
reject(error);
|
|
595
|
+
}
|
|
574
596
|
});
|
|
575
597
|
upstream.once("error", (error) => {
|
|
598
|
+
if (answered)
|
|
599
|
+
return;
|
|
576
600
|
if (request.signal.aborted) {
|
|
577
601
|
reject(request.signal.reason);
|
|
578
602
|
return;
|
|
@@ -587,12 +611,18 @@ class EgressGate {
|
|
|
587
611
|
});
|
|
588
612
|
}
|
|
589
613
|
toResponse(status, headers, stream) {
|
|
614
|
+
// Node parses any three-digit status, and `Response` takes only 200 to 599, so any other is refused here and its
|
|
615
|
+
// upstream reply is destroyed, so its socket closes.
|
|
616
|
+
if (status < 200 || status > 599) {
|
|
617
|
+
stream?.destroy();
|
|
618
|
+
throw new HarnessError("egress_denied", `the upstream answered with status ${status}, which HTTP does not define`);
|
|
619
|
+
}
|
|
590
620
|
const out = new Headers();
|
|
591
621
|
for (const [name, value] of Object.entries(headers)) {
|
|
592
622
|
for (const one of Array.isArray(value) ? value : [value])
|
|
593
623
|
out.append(name, one);
|
|
594
624
|
}
|
|
595
|
-
const bodiless = stream === undefined || [
|
|
625
|
+
const bodiless = stream === undefined || [204, 205, 304].includes(status);
|
|
596
626
|
// A response with no body is drained, so its socket and its log record don't wait for a reader that never comes.
|
|
597
627
|
if (bodiless)
|
|
598
628
|
stream?.resume();
|