Public: anyone can read this; the crew can change it.
Every fetch to a caller-given host passes one private-address guard, proven by a sweep of the source #14303
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14280-every-fetch-to-a-caller-given-host-passe → main 2 commits fork 2 threads · 2 unresolved
7 files
changed +316
−75
src/kernel/outbound.ts +41 −6
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | import { lookup } from "node:dns/promises"; |
| 16 | 16 | import { isIP } from "node:net"; |
| 17 | 17 | import { err, ok, type Result } from "./result.ts"; |
| 18 | import { addressBytes, inCidrs, parseCidr, type Cidr } from "./cidr.ts"; | |
| 18 | import { addressBytes, inCidr, inCidrs, parseCidr, type Cidr } from "./cidr.ts"; | |
| 19 | 19 | import { cellProxy, UNRESOLVED_WORDS } from "../apps/egress.ts"; |
| 20 | 20 | |
| 21 | 21 | const REFUSED = [ |
| @@ -27,16 +27,37 @@ const REFUSED = [ | ||
| 27 | 27 | "172.16.0.0/12", |
| 28 | 28 | "192.168.0.0/16", |
| 29 | 29 | "224.0.0.0/4", |
| 30 | "::/128", | |
| 31 | "::1/128", | |
| 30 | "240.0.0.0/4", | |
| 31 | // Unspecified, loopback and the old IPv4-compatible form (`::127.0.0.1`). | |
| 32 | "::/96", | |
| 33 | // NAT64 local-use: a site's own translator, nothing public behind it. | |
| 34 | "64:ff9b:1::/48", | |
| 32 | 35 | "fc00::/7", |
| 33 | 36 | "fe80::/10", |
| 34 | 37 | "ff00::/8", |
| 35 | 38 | ].map((c) => parseCidr(c)!) satisfies Cidr[]; |
| 36 | 39 | |
| 40 | const NAT64 = parseCidr("64:ff9b::/96")!; | |
| 41 | const SIX_TO_FOUR = parseCidr("2002::/16")!; | |
| 42 | ||
| 43 | /** | |
| 44 | * The IPv4 address an IPv6 one carries, for the two wrappers a host may | |
| 45 | * route: NAT64 (`64:ff9b::a9fe:a9fe`, the last four bytes) and 6to4 | |
| 46 | * (`2002:a9fe:a9fe::1`, bytes two to five). Judged as that IPv4 address, | |
| 47 | * so the metadata address cannot pass dressed as IPv6. | |
| 48 | */ | |
| 49 | function wrappedIpv4(address: string): string | undefined { | |
| 50 | const bytes = addressBytes(address); | |
| 51 | if (bytes === undefined || bytes.length !== 16) return undefined; | |
| 52 | const at = inCidr(address, NAT64) ? 12 : inCidr(address, SIX_TO_FOUR) ? 2 : -1; | |
| 53 | return at < 0 ? undefined : [...bytes.slice(at, at + 4)].join("."); | |
| 54 | } | |
| 55 | ||
| 37 | 56 | /** Is this address one a public service would never have? Unparseable counts. */ |
| 38 | 57 | export function isRefusedAddress(address: string): boolean { |
| 39 | return addressBytes(address) === undefined || inCidrs(address, REFUSED); | |
| 58 | if (addressBytes(address) === undefined || inCidrs(address, REFUSED)) return true; | |
| 59 | const inner = wrappedIpv4(address); | |
| 60 | return inner !== undefined && inCidrs(inner, REFUSED); | |
| 40 | 61 | } |
| 41 | 62 | |
| 42 | 63 | export type Resolve = (host: string) => Promise<readonly string[]>; |
| @@ -94,6 +115,20 @@ export class GateRefusedError extends Error { | ||
| 94 | 115 | } |
| 95 | 116 | } |
| 96 | 117 | |
| 118 | /** | |
| 119 | * A host as a URL would read it: `2130706433`, `0x7f.1` and `0177.0.0.1` are | |
| 120 | * all 127.0.0.1, and a caller that hands a bare host (git, a webhook row) | |
| 121 | * must not slip one past `isIP` to a resolver that reads it the same way. | |
| 122 | */ | |
| 123 | export function canonicalHost(host: string): string { | |
| 124 | const bare = host.replace(/^\[|\]$/g, ""); | |
| 125 | if (isIP(bare) !== 0) return bare; | |
| 126 | const read = URL.parse(`http://${bare.includes(":") ? `[${bare}]` : bare}/`); | |
| 127 | if (read === null) return bare; | |
| 128 | const name = read.hostname.replace(/^\[|\]$/g, ""); | |
| 129 | return isIP(name) !== 0 ? name : bare; | |
| 130 | } | |
| 131 | ||
| 97 | 132 | /** |
| 98 | 133 | * How to reach a host, or why not. Inside a cell: by name through the gate |
| 99 | 134 | * (an address literal is refused — nothing there is judged by name). Outside |
| @@ -103,7 +138,7 @@ export async function vetHost( | ||
| 103 | 138 | host: string, |
| 104 | 139 | options: OutboundOptions = {}, |
| 105 | 140 | ): Promise<Result<Route, "refused" | "unresolved">> { |
| 106 | const bare = host.replace(/^\[|\]$/g, ""); | |
| 141 | const bare = canonicalHost(host); | |
| 107 | 142 | const proxy = gateOf(options); |
| 108 | 143 | if (proxy !== undefined) return isIP(bare) !== 0 ? err("refused") : ok({ via: "gate", proxy }); |
| 109 | 144 | let found: readonly string[]; |
| @@ -146,7 +181,7 @@ export async function outboundFetch( | ||
| 146 | 181 | if (vetted.error === "refused") throw new RefusedError(); |
| 147 | 182 | throw new Error(UNRESOLVED_SENTENCE); |
| 148 | 183 | } |
| 149 | const name = target.hostname.replace(/^\[|\]$/g, ""); | |
| 184 | const name = canonicalHost(target.hostname); | |
| 150 | 185 | const tls = { ...(options.ca ? { ca: options.ca } : {}) }; |
| 151 | 186 | if (vetted.value.via === "gate") { |
| 152 | 187 | let answer: Response; |
src/origins/discover.ts +4 −64
| @@ -25,6 +25,7 @@ import { | ||
| 25 | 25 | vetHost, |
| 26 | 26 | } from "../kernel/outbound.ts"; |
| 27 | 27 | import { trustedHosts } from "./trusted-hosts.ts"; |
| 28 | import { exemptOrigin, isDev, isLoopback, operatorAllowed } from "./origin_fetch.ts"; | |
| 28 | 29 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 29 | 30 | import { readCapped } from "./providers/http.ts"; |
outdated
Move the re-export below the imports, or point git-mirror.ts at origin_fetch.ts and drop it. Also remove the leftover double blank lines where the helpers were deleted. |
||
| 30 | 31 | import type { Discovered, OriginKind } from "./types.ts"; |
| @@ -54,56 +55,6 @@ export interface DiscoverInput { | ||
| 54 | 55 | kind?: OriginKind; |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | /** | |
| 58 | * Hosts an operator has explicitly vouched for, comma-separated. This is the | |
| 59 | * one way to reach a LAN forge or a plaintext one — an operator decision, | |
| 60 | * never a per-user toggle, because the risk it accepts is the server's. | |
| 61 | */ | |
| 62 | function allowedHosts(env: Record<string, string | undefined>): Set<string> { | |
| 63 | return new Set( | |
| 64 | (env.FORGE_ORIGIN_ALLOW ?? "") | |
| 65 | .split(",") | |
| 66 | .map((h) => h.trim().toLowerCase()) | |
| 67 | .filter((h) => h !== ""), | |
| 68 | ); | |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Whether the operator named this URL's host in `FORGE_ORIGIN_ALLOW`: its | |
| 73 | * `host:port` for that port, or its bare hostname for every port. Discovery | |
| 74 | * reads it, and so does the mirror's git (`origins/git-mirror.ts`), so a | |
| 75 | * host an origin may be connected from is a host it may be mirrored from. | |
| 76 | */ | |
| 77 | export function operatorAllowed( | |
| 78 | href: string, | |
| 79 | env: Record<string, string | undefined> = process.env, | |
| 80 | ): boolean { | |
| 81 | const url = URL.parse(href); | |
| 82 | if (url === null) return false; | |
| 83 | const allowed = allowedHosts(env); | |
| 84 | return ( | |
| 85 | allowed.has(url.host.toLowerCase()) || | |
| 86 | allowed.has(url.hostname.toLowerCase()) | |
| 87 | ); | |
| 88 | } | |
| 89 | ||
| 90 | /** A development instance (`FORGE_DEV=1`, also set for the test suite). */ | |
| 91 | function isDev(env: Record<string, string | undefined>): boolean { | |
| 92 | return env.FORGE_DEV === "1"; | |
| 93 | } | |
| 94 | ||
| 95 | /** Loopback is this machine: reachable in dev, where a development instance | |
| 96 | * and the test suite live, and nowhere else unless an operator names it. */ | |
| 97 | function isLoopback(hostname: string): boolean { | |
| 98 | const host = hostname.replace(/^\[|\]$/g, "").toLowerCase(); | |
| 99 | return ( | |
| 100 | host === "localhost" || | |
| 101 | host === "::1" || | |
| 102 | host === "0:0:0:0:0:0:0:1" || | |
| 103 | /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host) | |
| 104 | ); | |
| 105 | } | |
| 106 | ||
| 107 | 58 | /** |
| 108 | 59 | * A literal address inside a range a server has no business dialling on a |
| 109 | 60 | * user's say-so. Only literals are checked here: a name that *resolves* into |
| @@ -217,7 +168,7 @@ export async function discover( | ||
| 217 | 168 | const web = allowedOrigin(wanted.host, env); |
| 218 | 169 | if (web === undefined) return err("unknown_host"); |
| 219 | 170 | const probe: Probe = { env, options, refused: false }; |
| 220 | if (!exempt(web, env)) { | |
| 171 | if (!exemptOrigin(web, env)) { | |
| 221 | 172 | const vetted = await vetHost(new URL(web).hostname, options); |
| 222 | 173 | if (!vetted.ok && vetted.error === "refused") return err("private_host"); |
| 223 | 174 | } |
| @@ -395,17 +346,6 @@ interface Probe { | ||
| 395 | 346 | status?: number; |
| 396 | 347 | } |
| 397 | 348 | |
| 398 | /** An operator-named host, or loopback in dev: dialled as typed, unvetted. */ | |
| 399 | function exempt( | |
| 400 | href: string, | |
| 401 | env: Record<string, string | undefined>, | |
| 402 | ): boolean { | |
| 403 | return ( | |
| 404 | operatorAllowed(href, env) || | |
| 405 | (isDev(env) && isLoopback(new URL(href).hostname)) | |
| 406 | ); | |
| 407 | } | |
| 408 | ||
| 409 | 349 | function sameOrigin(href: string, web: string): boolean { |
| 410 | 350 | try { |
| 411 | 351 | return new URL(href).origin === web; |
| @@ -428,7 +368,7 @@ async function getJson( | ||
| 428 | 368 | for (let hop = 0; hop < MAX_HOPS; hop++) { |
| 429 | 369 | if (!sameOrigin(target, web)) { |
| 430 | 370 | // Say so when the hop it was sent to is inside the network. |
| 431 | if (!exempt(target, probe.env)) { | |
| 371 | if (!exemptOrigin(target, probe.env)) { | |
| 432 | 372 | const vetted = await vetHost(new URL(target).hostname, probe.options); |
| 433 | 373 | if (!vetted.ok && vetted.error === "refused") probe.refused = true; |
| 434 | 374 | } |
| @@ -442,7 +382,7 @@ async function getJson( | ||
| 442 | 382 | let response: Response; |
| 443 | 383 | try { |
| 444 | 384 | // Vetted, pinned to the judged address, and never auto-followed. |
| 445 | response = exempt(target, probe.env) | |
| 385 | response = exemptOrigin(target, probe.env) | |
| 446 | 386 | ? await fetch(target, init) |
| 447 | 387 | : await outboundFetch(target, init, probe.options); |
| 448 | 388 | } catch (e) { |
src/origins/git-mirror.ts +1 −1
| @@ -25,7 +25,7 @@ import { listRefs } from "../git/read.ts"; | ||
| 25 | 25 | import { ZERO_OID } from "../git/hooks.ts"; |
| 26 | 26 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 27 | 27 | import { trustedHosts } from "./trusted-hosts.ts"; |
| 28 | import { operatorAllowed } from "./discover.ts"; | |
| 28 | import { operatorAllowed } from "./origin_fetch.ts"; | |
| 29 | 29 | import { vetHost, type OutboundOptions } from "../kernel/outbound.ts"; |
| 30 | 30 | import { pinFor, remoteGitEnv } from "../git/remote-env.ts"; |
| 31 | 31 | import { redact, safeRemoteUrl, secretsIn } from "./credentials.ts"; |
src/origins/oauth.ts +4 −3
| @@ -32,6 +32,7 @@ import { id, secret as randomSecret } from "../kernel/id.ts"; | ||
| 32 | 32 | import { open, seal } from "../kernel/secret.ts"; |
| 33 | 33 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 34 | 34 | import type { AuthKind, OriginKind } from "./types.ts"; |
| 35 | import { originFetch } from "./origin_fetch.ts"; | |
| 35 | 36 | |
| 36 | 37 | const TIMEOUT_MS = 10_000; |
| 37 | 38 | const MAX_BYTES = 64 * 1024; |
| @@ -321,7 +322,7 @@ export async function revokeGrant( | ||
| 321 | 322 | }); |
| 322 | 323 | const url = `${app.apiUrl.replace(/\/+$/, "")}/applications/${encodeURIComponent(app.clientId)}/grant`; |
| 323 | 324 | try { |
| 324 | const response = await fetch(url, { | |
| 325 | const response = await originFetch(url, { | |
| 325 | 326 | method: "DELETE", |
| 326 | 327 | headers: { |
| 327 | 328 | accept: "application/vnd.github+json", |
| @@ -906,7 +907,7 @@ export async function registerClient( | ||
| 906 | 907 | ): Promise<Result<string, OAuthFailure>> { |
| 907 | 908 | let response: Response; |
| 908 | 909 | try { |
| 909 | response = await fetch(`${webUrl.replace(/\/+$/, "")}/oauth/register`, { | |
| 910 | response = await originFetch(`${webUrl.replace(/\/+$/, "")}/oauth/register`, { | |
| 910 | 911 | method: "POST", |
| 911 | 912 | headers: { accept: "application/json", "content-type": "application/json", "user-agent": "forge" }, |
| 912 | 913 | body: JSON.stringify({ |
| @@ -992,7 +993,7 @@ async function postForm( | ||
| 992 | 993 | ): Promise<Result<Record<string, unknown>, OAuthFailure>> { |
| 993 | 994 | let response: Response; |
| 994 | 995 | try { |
| 995 | response = await fetch(url, { | |
| 996 | response = await originFetch(url, { | |
| 996 | 997 | method: "POST", |
| 997 | 998 | headers: { |
| 998 | 999 | accept: "application/json", |
src/origins/origin_fetch.ts added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | /** | |
| 2 | * How the forge reaches an origin a member named, after discovery: every | |
| 3 | * request — OAuth registration, token exchange, each API call — goes through | |
| 4 | * the private-address guard again (`kernel/outbound.ts`), so a name that | |
| 5 | * answered public at discovery and private a minute later (DNS rebinding) is | |
| 6 | * refused at the second answer. Hosts an operator vouched for | |
| 7 | * (`FORGE_ORIGIN_ALLOW`) and loopback in dev are dialled as typed. | |
| 8 | */ | |
| 9 | ||
| 10 | import { outboundFetch } from "../kernel/outbound.ts"; | |
| 11 | import { trustedHosts } from "./trusted-hosts.ts"; | |
| 12 | ||
| 13 | /** | |
| 14 | * Hosts an operator has explicitly vouched for, comma-separated. This is the | |
| 15 | * one way to reach a LAN forge or a plaintext one — an operator decision, | |
| 16 | * never a per-user toggle, because the risk it accepts is the server's. | |
| 17 | */ | |
| 18 | function allowedHosts(env: Record<string, string | undefined>): Set<string> { | |
| 19 | return new Set( | |
| 20 | (env.FORGE_ORIGIN_ALLOW ?? "") | |
| 21 | .split(",") | |
| 22 | .map((h) => h.trim().toLowerCase()) | |
| 23 | .filter((h) => h !== ""), | |
| 24 | ); | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * Whether the operator named this URL's host in `FORGE_ORIGIN_ALLOW`: its | |
| 29 | * `host:port` for that port, or its bare hostname for every port. Discovery | |
| 30 | * reads it, and so does the mirror's git (`origins/git-mirror.ts`), so a | |
| 31 | * host an origin may be connected from is a host it may be mirrored from. | |
| 32 | */ | |
| 33 | export function operatorAllowed( | |
| 34 | href: string, | |
| 35 | env: Record<string, string | undefined> = process.env, | |
| 36 | ): boolean { | |
| 37 | const url = URL.parse(href); | |
| 38 | if (url === null) return false; | |
| 39 | const allowed = allowedHosts(env); | |
| 40 | return ( | |
| 41 | allowed.has(url.host.toLowerCase()) || | |
| 42 | allowed.has(url.hostname.toLowerCase()) | |
| 43 | ); | |
| 44 | } | |
| 45 | ||
| 46 | /** A development instance (`FORGE_DEV=1`, also set for the test suite). */ | |
| 47 | export function isDev(env: Record<string, string | undefined>): boolean { | |
| 48 | return env.FORGE_DEV === "1"; | |
| 49 | } | |
| 50 | ||
| 51 | /** Loopback is this machine: reachable in dev, where a development instance | |
| 52 | * and the test suite live, and nowhere else unless an operator names it. */ | |
| 53 | export function isLoopback(hostname: string): boolean { | |
| 54 | const host = hostname.replace(/^\[|\]$/g, "").toLowerCase(); | |
| 55 | return ( | |
| 56 | host === "localhost" || | |
| 57 | host === "::1" || | |
| 58 | host === "0:0:0:0:0:0:0:1" || | |
| 59 | /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host) | |
| 60 | ); | |
| 61 | } | |
| 62 | ||
| 63 | /** An operator-named host, or loopback in dev: dialled as typed, unvetted. */ | |
| 64 | export function exemptOrigin( | |
| 65 | href: string, | |
| 66 | env: Record<string, string | undefined>, | |
| 67 | ): boolean { | |
| 68 | return ( | |
| 69 | operatorAllowed(href, env) || | |
| 70 | (isDev(env) && isLoopback(new URL(href).hostname)) | |
| 71 | ); | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * fetch() to an origin: exempt hosts as typed, every other through the guard with the admin's | |
| 76 | * trusted names. | |
| 77 | */ | |
| 78 | export function originFetch( | |
| 79 | url: string, | |
| 80 | init: RequestInit = {}, | |
| 81 | env: Record<string, string | undefined> = process.env, | |
| 82 | ): Promise<Response> { | |
| 83 | if (exemptOrigin(url, env)) return fetch(url, init); | |
| 84 | return outboundFetch(url, init, { trusted: trustedHosts() }); | |
| 85 | } |
src/origins/providers/http.ts +2 −1
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | import { redact } from "../credentials.ts"; |
| 13 | import { originFetch } from "../origin_fetch.ts"; | |
| 13 | 14 | import type { |
| 14 | 15 | ProviderCredential, |
| 15 | 16 | ProviderFailure, |
| @@ -267,7 +268,7 @@ export class ApiClient { | ||
| 267 | 268 | ); |
| 268 | 269 | let answer: Response; |
| 269 | 270 | try { |
| 270 | answer = await fetch(target, { | |
| 271 | answer = await originFetch(target, { | |
| 271 | 272 | method, |
| 272 | 273 | headers, |
| 273 | 274 | ...(body === undefined ? {} : { body }), |
test/outbound-sweep.test.ts added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | /** | |
| 2 | * Every fetch to a host somebody else chose passes the one private-address | |
| 3 | * guard (`kernel/outbound.ts`, #14280). The sweep reads src for `fetch(` | |
| 4 | * calls whose URL is not a literal (a same-origin path, this machine's | |
| 5 | * loopback, or a named https host); each must sit in the list below with | |
| 6 | * the reason it is not a caller-given host, by file and count, so a new one | |
| 7 | * fails here until it goes through the guard or says why it need not. | |
| 8 | */ | |
| 9 | ||
| 10 | import { describe, expect, test } from "bun:test"; | |
| 11 | import { Glob } from "bun"; | |
| 12 | import { RefusedError, canonicalHost, isRefusedAddress, outboundFetch, vetHost } from "../src/kernel/outbound.ts"; | |
| 13 | import { exemptOrigin, originFetch } from "../src/origins/origin_fetch.ts"; | |
| 14 | ||
| 15 | /** A fetch call: not a method (`net.fetch(`), not a declaration, not the word in a regex. */ | |
| 16 | /** `fetch(`, and the same function behind a qualifier (`globalThis.fetch(`, `Bun.fetch(`). */ | |
| 17 | const CALL = /(?:(?<![\w.$])|(?<=(?<![\w.$])(?:globalThis|Bun|window|self)\.))fetch\(\s*(?![?)])/g; | |
| 18 | /** A URL the code itself names: a same-origin path, loopback, or an https host spelled out. */ | |
| 19 | const LITERAL = /^(["'`])(?:\/|http:\/\/127\.0\.0\.1[:/]|https:\/\/[a-z0-9.-]+\.[a-z]+\/)/; | |
| 20 | ||
| 21 | const BROWSER = "browser script served by the forge: it fetches its own origin's paths"; | |
| 22 | ||
| 23 | /** File → how many non-literal fetches it holds, and why none of them takes a caller-given host. */ | |
| 24 | const ALLOWED: Record<string, { count: number; why: string }> = { | |
| 25 | "src/kernel/outbound.ts": { count: 2, why: "the guard itself: dials the address it judged, or the cell's gate" }, | |
| 26 | "src/origins/origin_fetch.ts": { count: 1, why: "operator-allowed hosts (FORGE_ORIGIN_ALLOW) and dev loopback only; every other through the guard" }, | |
| 27 | "src/origins/discover.ts": { count: 1, why: "the exempt branch of discovery; every other probe is outboundFetch" }, | |
| 28 | "src/git/hook-address.ts": { count: 1, why: "the forge's own internal listener: a unix socket or 127.0.0.1" }, | |
| 29 | "src/cli/api.ts": { count: 1, why: "the workbench CLI on a person's own machine, talking to the forge they configured" }, | |
| 30 | "src/http/server.ts": { count: 1, why: "the forge's own unix socket" }, | |
| 31 | "src/http/rp.ts": { count: 2, why: "the parent forge's issuer, from this forge's configuration" }, | |
| 32 | "src/actions/upstream.ts": { count: 2, why: "the parent forge's API, from the upstream credential the operator set" }, | |
| 33 | "src/actions/cloud.ts": { count: 1, why: "the cloud provider API an admin connected; providers are named in code" }, | |
| 34 | "src/actions/pinned.ts": { count: 2, why: "the cloud provider API and the admin's own pinned forge" }, | |
| 35 | "src/apps/proxy.ts": { count: 1, why: "an app's own socket or loopback port, never a host from a request" }, | |
| 36 | "src/apps/preview_spec.ts": { count: 1, why: "the preview's own loopback address, built by the forge" }, | |
| 37 | "src/apps/toolbar.ts": { count: 1, why: BROWSER }, | |
| 38 | "src/arcade/carts.ts": { count: 1, why: "the app's own loopback page (`http://127.0.0.1:<port>`)" }, | |
| 39 | "src/devices/ipp.ts": { count: 1, why: "a printer is on the owner's LAN by design; core/devices.ts holds it to private literals and never link-local" }, | |
| 40 | "src/perf/github_traffic.ts": { count: 1, why: "api.github.com, named in code" }, | |
| 41 | "src/chaos/report.ts": { count: 1, why: "the forge URL the operator runs the chaos drill against" }, | |
| 42 | "src/system/footing.ts": { count: 1, why: "the Kubernetes API from the pod's service account" }, | |
| 43 | "src/cluster/jobs.ts": { count: 1, why: "the Kubernetes API from the pod's service account" }, | |
| 44 | "src/crew/openai_runner.ts": { count: 1, why: "the model endpoint from operator configuration" }, | |
| 45 | "src/crew/decide.ts": { count: 1, why: "the model endpoint from operator configuration" }, | |
| 46 | "src/spawn/live.ts": { count: 1, why: "a child forge's own URL, minted by this forge" }, | |
| 47 | "src/runner/main.ts": { count: 1, why: "the forge a runner was enrolled with, from its own configuration" }, | |
| 48 | "src/release/smoke.ts": { count: 5, why: "the operator's release target" }, | |
| 49 | "src/release/small_forge.ts": { count: 1, why: "a forge the release check started itself" }, | |
| 50 | "src/snapshot/verdict.ts": { count: 1, why: "the forge URL from operator configuration" }, | |
| 51 | "src/snapshot/drill.ts": { count: 1, why: "the restored forge the drill started on loopback" }, | |
| 52 | "src/snapshot/store.ts": { count: 1, why: "a presigned URL to the operator's bucket" }, | |
| 53 | "src/snapshot/probe.ts": { count: 1, why: "the operator's forge URL" }, | |
| 54 | "src/maintenance/standby.ts": { count: 1, why: "the operator's standby page URL" }, | |
| 55 | "src/maintain/keeper/down.ts": { count: 1, why: "services the operator listed in the keeper's environment" }, | |
| 56 | "src/maintain/duties/wait_split.ts": { count: 1, why: "the forge's own public URL, timed" }, | |
| 57 | }; | |
| 58 | ||
| 59 | /** Every non-literal fetch in src, as `file:line`. */ | |
| 60 | async function sweep(): Promise<Map<string, string[]>> { | |
| 61 | const found = new Map<string, string[]>(); | |
| 62 | for (const file of new Glob("src/**/*.ts").scanSync(".")) { | |
| 63 | const text = await Bun.file(file).text(); | |
| 64 | for (const m of text.matchAll(CALL)) { | |
| 65 | const before = text.slice(text.lastIndexOf("\n", m.index) + 1, m.index); | |
| 66 | if (/^\s*(\/?\*|\/\/)/.test(before) || /function\s+$/.test(before)) continue; | |
| 67 | if (LITERAL.test(text.slice(m.index + m[0].length))) continue; | |
| 68 | const line = text.slice(0, m.index).split("\n").length; | |
| 69 | found.set(file, [...(found.get(file) ?? []), `${file}:${line}`]); | |
| 70 | } | |
| 71 | } | |
| 72 | return found; | |
| 73 | } | |
| 74 | ||
| 75 | describe("the sweep of src", () => { | |
| 76 | test("every fetch to a non-literal host is the guard's, or is listed with its reason", async () => { | |
| 77 | const unlisted: string[] = []; | |
| 78 | const stale: string[] = []; | |
| 79 | for (const [file, sites] of await sweep()) { | |
| 80 | if (file.startsWith("src/web/")) continue; // BROWSER: pages and their scripts | |
| 81 | const allowed = ALLOWED[file]; | |
| 82 | if (allowed === undefined || sites.length > allowed.count) unlisted.push(...sites); | |
| 83 | else if (sites.length < allowed.count) stale.push(`${file}: listed ${allowed.count}, found ${sites.length}`); | |
| 84 | } | |
| 85 | expect(unlisted).toEqual([]); | |
| 86 | expect(stale).toEqual([]); | |
| 87 | for (const [file, { why }] of Object.entries(ALLOWED)) expect([file, why.length > 10]).toEqual([file, true]); | |
| 88 | }); | |
| 89 | ||
| 90 | test("the origin paths a member names go through the guard, not a bare fetch", async () => { | |
| 91 | const found = await sweep(); | |
| 92 | expect(found.get("src/origins/oauth.ts")).toBeUndefined(); | |
| 93 | expect(found.get("src/origins/providers/http.ts")).toBeUndefined(); | |
| 94 | }); | |
| 95 | ||
| 96 | test("the sweep sees a bare fetch to a variable, and passes a literal one", () => { | |
| 97 | const sample = 'await fetch(url, {});\nawait globalThis.fetch(u);\nawait Bun.fetch(u);\nawait app.fetch(req);\nawait fetch("https://registry.npmjs.org/x");\nawait fetch(`http://127.0.0.1:${p}/`);'; | |
| 98 | const hits = [...sample.matchAll(CALL)].filter((m) => !LITERAL.test(sample.slice(m.index + m[0].length))); | |
| 99 | expect(hits.length).toBe(3); | |
| 100 | }); | |
| 101 | }); | |
| 102 | ||
| 103 | /** Ten and more spellings of loopback, link-local, private and the metadata address. */ | |
| 104 | const SPELLINGS = [ | |
| 105 | "2130706433", // 127.0.0.1, decimal | |
| 106 | "0x7f000001", // hex | |
| 107 | "0177.0.0.1", // octal | |
| 108 | "127.1", // short form | |
| 109 | "0x7f.0.0.1", | |
| 110 | "2852039166", // 169.254.169.254, decimal | |
| 111 | "0xa9fea9fe", // the metadata address in hex | |
| 112 | "0xa.0.0.1", // 10.0.0.1 | |
| 113 | "::ffff:127.0.0.1", // mapped IPv6, dotted | |
| 114 | "::ffff:7f00:1", // mapped IPv6, hex | |
| 115 | "[::ffff:a9fe:a9fe]", // mapped metadata, bracketed | |
| 116 | "::127.0.0.1", // IPv4-compatible | |
| 117 | "0:0:0:0:0:0:0:1", | |
| 118 | "[::1]", | |
| 119 | "fe80::1", | |
| 120 | "fd00::1", | |
| 121 | "64:ff9b::a9fe:a9fe", // NAT64 wrapping the metadata address | |
| 122 | "64:ff9b::10.0.0.1", // NAT64, dotted | |
| 123 | "64:ff9b:1::1", // NAT64 local-use | |
| 124 | "2002:a9fe:a9fe::1", // 6to4 wrapping the metadata address | |
| 125 | "2002:7f00:1::", // 6to4 wrapping loopback | |
| 126 | ]; | |
| 127 | ||
| 128 | describe("the guard refuses every spelling", () => { | |
| 129 | // The resolver answers public, so a spelling that reached it would pass: each must be read as an | |
| 130 | // address first. | |
| 131 | const resolve = () => Promise.resolve(["93.184.216.34"]); | |
| 132 | ||
| 133 | for (const host of SPELLINGS) | |
| 134 | test(`${host} is refused`, async () => { | |
| 135 | expect(await vetHost(host, { resolve, proxy: undefined })).toEqual({ ok: false, error: "refused" }); | |
| 136 | expect(isRefusedAddress(canonicalHost(host))).toBe(true); | |
| 137 | }); | |
| 138 | ||
| 139 | test("in a URL too: fetching any of them throws before a socket opens", async () => { | |
| 140 | for (const host of SPELLINGS) { | |
| 141 | const bare = host.replace(/^\[|\]$/g, ""); | |
| 142 | const url = `http://${bare.includes(":") ? `[${bare}]` : bare}/`; | |
| 143 | const failed = await outboundFetch(url, {}, { resolve, plain: true, proxy: undefined }).catch((e: unknown) => e); | |
| 144 | expect([host, failed instanceof RefusedError]).toEqual([host, true]); | |
| 145 | } | |
| 146 | }); | |
| 147 | ||
| 148 | test("a public address and a name answering public still pass", async () => { | |
| 149 | expect((await vetHost("93.184.216.34", { proxy: undefined })).ok).toBe(true); | |
| 150 | expect((await vetHost("example.com", { resolve, proxy: undefined })).ok).toBe(true); | |
| 151 | }); | |
| 152 | ||
| 153 | test("NAT64 and 6to4 wrapping a public address still pass", async () => { | |
| 154 | expect((await vetHost("64:ff9b::5db8:d822", { proxy: undefined })).ok).toBe(true); | |
| 155 | expect((await vetHost("2002:5db8:d822::1", { proxy: undefined })).ok).toBe(true); | |
| 156 | }); | |
| 157 | ||
| 158 | test("a redirect is an answer, never followed to where it points", async () => { | |
| 159 | using server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response(null, { status: 302, headers: { location: "http://169.254.169.254/" } }) }); | |
| 160 | const answer = await outboundFetch(`http://127.0.0.1:${server.port}/`, {}, { allow: ["127.0.0.1"], plain: true, proxy: undefined }); | |
| 161 | expect(answer.status).toBe(302); | |
| 162 | }); | |
| 163 | }); | |
| 164 | ||
| 165 | describe("an origin a member named", () => { | |
| 166 | test("is refused at a private address outside dev, where discovery's one look is not the last", async () => { | |
| 167 | const env = { FORGE_DEV: "0" }; | |
| 168 | for (const url of ["https://10.0.0.1/api/v1/version", "https://169.254.169.254/latest", "https://[::1]/x"]) { | |
| 169 | expect(exemptOrigin(url, env)).toBe(false); | |
| 170 | const failed = await originFetch(url, {}, env).catch((e: unknown) => e); | |
| 171 | expect([url, failed instanceof RefusedError]).toEqual([url, true]); | |
| 172 | } | |
| 173 | }); | |
| 174 | ||
| 175 | test("an operator-allowed host and dev loopback stay exempt", () => { | |
| 176 | expect(exemptOrigin("http://git.lan:3000/", { FORGE_ORIGIN_ALLOW: "git.lan:3000" })).toBe(true); | |
| 177 | expect(exemptOrigin("http://127.0.0.1:3000/", { FORGE_DEV: "1" })).toBe(true); | |
| 178 | }); | |
| 179 | }); |
new above
offline
High: NAT64
64:ff9b::/96and 6to42002::/16are not refused, so a wrapped metadata or private IPv4 passes on a dual-stack or NAT64 host. Refuse them or unwrap the embedded IPv4, and add test spellings.