Public: anyone can read this; the crew can change it.
Apps never receive the forge's or the edge's credentials: the app proxy strips x-plat-*, cookie and authorization #2935
mason/forge:mason/2919-apps-never-receive-the-forge-s-or-the-ed → main 1 commit fork
6 files
changed +98
−5
docs/THREATS.md +1 −1
| @@ -136,7 +136,7 @@ How each part proves itself to the next, today. | ||
| 136 | 136 | | runner → forge | the run's scoped token (`FORGE_TOKEN`) and a few-minute passport (`FORGE_PASSPORT`) | `test/passport-edge.test.ts` | |
| 137 | 137 | | git hook → forge | per-boot secret in `x-forge-hook-secret` on the internal listener; a call without it is refused and counted | none yet | |
| 138 | 138 | | cell → proxy | the proxy's Unix socket bound into the cell's own network namespace; no credential, the namespace is the identity | `test/contain.test.ts` | |
| 139 | | edge → pod | `X-Plat-Edge-Token` equal to `FORGE_EDGE_TOKEN`, compared as fixed-width digests; without it `X-Plat-User` is ignored; the `signin.edge` setting (admin, refused for an admin with no password or passkey) turns the edge's identity off on every door | `test/edge-auth.test.ts` | | |
| 139 | | edge → pod | `X-Plat-Edge-Token` equal to `FORGE_EDGE_TOKEN`, compared as fixed-width digests; without it `X-Plat-User` is ignored, and so is either header from a socket peer outside `FORGE_TRUSTED_PROXIES` (this machine, when unset); the app proxy strips `x-plat-*`, `authorization` and `forge_session` before an app sees the request; the `signin.edge` setting (admin, refused for an admin with no password or passkey) turns the edge's identity off on every door | `test/edge-auth.test.ts`, `test/apps-identity.test.ts` | | |
| 140 | 140 | | child → parent | none at call time: `parent.json` names the parent's address and the child pulls over public git; the claim link (sha256 kept, 24 hours, single use) hands the child to its person | `test/spawn-first-boot.test.ts` | |
| 141 | 141 | |
| 142 | 142 | Gaps: edge → pod and runner → forge ride a shared secret, not a per-call |
src/apps/proxy.ts +24
| @@ -321,6 +321,29 @@ export function grantUrl( | ||
| 321 | 321 | return `${originFor(runtime, target, host)}/__forge/grant?t=${encodeURIComponent(token)}&to=${encodeURIComponent(path)}`; |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | /** | |
| 325 | * What the forge trusts and an app must never hold: the edge's headers | |
| 326 | * (`x-plat-*`, an edge token and user are an admin of the forge), the | |
| 327 | * caller's `authorization`, and the forge's own session cookie. Tenant code | |
| 328 | * that kept any of them could call the forge as the visitor. | |
| 329 | */ | |
| 330 | export function withoutForgeCredentials(headers: Headers): void { | |
| 331 | for (const name of [...headers.keys()]) | |
| 332 | if (name.startsWith("x-plat-")) headers.delete(name); | |
| 333 | headers.delete("authorization"); | |
| 334 | const cookie = headers.get("cookie"); | |
| 335 | if (cookie === null) return; | |
| 336 | const kept = cookie | |
| 337 | .split(";") | |
| 338 | .filter((part) => part.split("=", 1)[0]!.trim() !== FORGE_SESSION_COOKIE) | |
| 339 | .map((part) => part.trim()) | |
| 340 | .filter((part) => part !== ""); | |
| 341 | if (kept.length === 0) headers.delete("cookie"); | |
| 342 | else headers.set("cookie", kept.join("; ")); | |
| 343 | } | |
| 344 | ||
| 345 | const FORGE_SESSION_COOKIE = "forge_session"; | |
| 346 | ||
| 324 | 347 | const HOP_BY_HOP = [ |
| 325 | 348 | "connection", |
| 326 | 349 | "keep-alive", |
| @@ -615,6 +638,7 @@ export async function proxyApp( | ||
| 615 | 638 | const headers = new Headers(request.headers); |
| 616 | 639 | for (const name of HOP_BY_HOP) headers.delete(name); |
| 617 | 640 | for (const name of IDENTITY_HEADERS) headers.delete(name); |
| 641 | withoutForgeCredentials(headers); | |
| 618 | 642 | headers.set("x-forwarded-host", request.headers.get("host") ?? ""); |
| 619 | 643 | headers.set("x-forwarded-proto", url.protocol.replace(":", "")); |
| 620 | 644 | for (const [name, value] of Object.entries( |
src/http/edge.ts +5 −1
| @@ -4,7 +4,9 @@ | ||
| 4 | 4 | * and `X-Plat-Edge-Token`, a secret only the edge holds. A request carrying |
| 5 | 5 | * the secret the operator configured (`FORGE_EDGE_TOKEN`) is that person, |
| 6 | 6 | * signed in by the platform. Without the secret the headers are a claim |
| 7 | * anyone could write, and they are ignored. Signing in and out are the | |
| 7 | * anyone could write, and they are ignored — as they are from any socket | |
| 8 | * peer that is not a proxy the operator trusts (`http/peer.ts`), secret or | |
| 9 | * not. The app proxy strips them before an app sees them. Signing in and out are the | |
| 8 | 10 | * edge's own entry points on this host. |
| 9 | 11 | */ |
| 10 | 12 | |
| @@ -12,6 +14,7 @@ import { createHash, timingSafeEqual } from "node:crypto"; | ||
| 12 | 14 | import type { User } from "../core/users.ts"; |
| 13 | 15 | import type { App } from "../app.ts"; |
| 14 | 16 | import { edgeSigninOn } from "../core/capacity.ts"; |
| 17 | import { fromTrustedPeer } from "./peer.ts"; | |
| 15 | 18 | |
| 16 | 19 | /** |
| 17 | 20 | * The edge secret in force: the configured one while the `signin.edge` |
| @@ -28,6 +31,7 @@ export function edgeIdentity( | ||
| 28 | 31 | edgeToken: string | undefined, |
| 29 | 32 | ): string | undefined { |
| 30 | 33 | if (edgeToken === undefined || edgeToken === "") return undefined; |
| 34 | if (!fromTrustedPeer(request)) return undefined; | |
| 31 | 35 | const presented = request.headers.get("x-plat-edge-token") ?? ""; |
| 32 | 36 | if (!sameSecret(presented, edgeToken)) return undefined; |
| 33 | 37 | const login = (request.headers.get("x-plat-user") ?? "").trim(); |
src/http/peer.ts +24 −1
| @@ -40,7 +40,7 @@ export function resolveClient( | ||
| 40 | 40 | if (peer === undefined) return "unknown"; |
| 41 | 41 | const listed = trust.proxies.length > 0; |
| 42 | 42 | const trusted = (address: string): boolean => |
| 43 | listed ? inCidrs(address, trust.proxies) : LOOPBACK.has(address); | |
| 43 | trustedPeer(address, trust); | |
| 44 | 44 | if (!trusted(peer)) return peer; |
| 45 | 45 | if (trust.header !== undefined && listed) { |
| 46 | 46 | const named = headers.get(trust.header)?.trim() ?? ""; |
| @@ -59,7 +59,20 @@ export function resolveClient( | ||
| 59 | 59 | return client.slice(0, 64); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | /** | |
| 63 | * Whether a socket peer may speak for the edge: a listed proxy, or with | |
| 64 | * none listed this machine. A peer with no address is the forge's own unix | |
| 65 | * socket, which only the front proxy on this machine opens. | |
| 66 | */ | |
| 67 | export function trustedPeer(peer: string | undefined, trust: ProxyTrust): boolean { | |
| 68 | if (peer === undefined) return true; | |
| 69 | return trust.proxies.length > 0 | |
| 70 | ? inCidrs(peer, trust.proxies) | |
| 71 | : LOOPBACK.has(peer); | |
| 72 | } | |
| 73 | ||
| 62 | 74 | const clients = new WeakMap<Request, string>(); |
| 75 | const strangers = new WeakSet<Request>(); | |
| 63 | 76 | |
| 64 | 77 | /** The listener notes each request's client once, as it arrives. */ |
| 65 | 78 | export const notePeer = ( |
| @@ -68,8 +81,17 @@ export const notePeer = ( | ||
| 68 | 81 | trust: ProxyTrust = NO_TRUST, |
| 69 | 82 | ): void => { |
| 70 | 83 | clients.set(request, resolveClient(address, request.headers, trust)); |
| 84 | if (trustedPeer(address, trust)) strangers.delete(request); | |
| 85 | else strangers.add(request); | |
| 71 | 86 | }; |
| 72 | 87 | |
| 88 | /** | |
| 89 | * Whether this request arrived from a peer that may carry the edge's | |
| 90 | * headers. One the listener never noted was made inside the forge. | |
| 91 | */ | |
| 92 | export const fromTrustedPeer = (request: Request): boolean => | |
| 93 | !strangers.has(request); | |
| 94 | ||
| 73 | 95 | export function clientAddress(request: Request): string { |
| 74 | 96 | return clients.get(request) ?? "unknown"; |
| 75 | 97 | } |
| @@ -96,4 +118,5 @@ export const clientBucket = (request: Request): string => | ||
| 96 | 118 | export const carryPeer = (from: Request, to: Request): void => { |
| 97 | 119 | const client = clients.get(from); |
| 98 | 120 | if (client !== undefined) clients.set(to, client); |
| 121 | if (strangers.has(from)) strangers.add(to); | |
| 99 | 122 | }; |
test/apps-identity.test.ts +22 −2
| @@ -101,6 +101,10 @@ describe("apps: identity, and a deploy that never answers", () => { | ||
| 101 | 101 | return Response.json({ |
| 102 | 102 | identity: request.headers.get("x-forge-identity"), |
| 103 | 103 | signature: request.headers.get("x-forge-signature"), |
| 104 | edgeToken: request.headers.get("x-plat-edge-token"), | |
| 105 | edgeUser: request.headers.get("x-plat-user"), | |
| 106 | authorization: request.headers.get("authorization"), | |
| 107 | cookie: request.headers.get("cookie"), | |
| 104 | 108 | key: process.env.FORGE_APP_IDENTITY_KEY ?? "", |
| 105 | 109 | }); |
| 106 | 110 | }, |
| @@ -130,13 +134,29 @@ describe("apps: identity, and a deploy that never answers", () => { | ||
| 130 | 134 | const heard = (await ( |
| 131 | 135 | await viaHost(`hello.${DOMAIN}`, "/who", { |
| 132 | 136 | headers: { |
| 133 | cookie, | |
| 137 | cookie: `${cookie}; forge_session=the-visitors-session; theme=dark`, | |
| 134 | 138 | // A client's own claim never reaches the app. |
| 135 | 139 | "x-forge-identity": "evil|mallory|admin|9999999999999", |
| 136 | 140 | "x-forge-signature": "AAAA", |
| 141 | // Nor does anything the forge would take as a credential. | |
| 142 | "x-plat-edge-token": "the-edge-secret", | |
| 143 | "x-plat-user": "olive", | |
| 144 | authorization: "Bearer forge_visitorstoken", | |
| 137 | 145 | }, |
| 138 | 146 | }) |
| 139 | ).json()) as { identity: string; signature: string; key: string }; | |
| 147 | ).json()) as { | |
| 148 | identity: string; | |
| 149 | signature: string; | |
| 150 | key: string; | |
| 151 | edgeToken: string | null; | |
| 152 | edgeUser: string | null; | |
| 153 | authorization: string | null; | |
| 154 | cookie: string | null; | |
| 155 | }; | |
| 156 | expect(heard.edgeToken).toBeNull(); | |
| 157 | expect(heard.edgeUser).toBeNull(); | |
| 158 | expect(heard.authorization).toBeNull(); | |
| 159 | expect(heard.cookie).toBe(`${cookie}; theme=dark`); | |
| 140 | 160 | expect(heard.key).toBe(identityKey(dataDir, record.id)); |
| 141 | 161 | const [id, name, role, expires] = heard.identity.split("|"); |
| 142 | 162 | expect(id).toBe(alice.id); |
test/edge-auth.test.ts +22
| @@ -5,6 +5,8 @@ import { join } from "node:path"; | ||
| 5 | 5 | import { createApp, type App } from "../src/app.ts"; |
| 6 | 6 | import { edgeIdentity, edgeLoginUrl, edgeLogoutUrl } from "../src/http/edge.ts"; |
| 7 | 7 | import { serve, type Servers } from "../src/http/server.ts"; |
| 8 | import { notePeer } from "../src/http/peer.ts"; | |
| 9 | import { parseCidrs } from "../src/kernel/cidr.ts"; | |
| 8 | 10 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 9 | 11 | import { mintInvite } from "../src/core/invites.ts"; |
| 10 | 12 | import { setSignup } from "../src/settings/signup.ts"; |
| @@ -40,6 +42,26 @@ describe("edgeIdentity", () => { | ||
| 40 | 42 | ).toBeUndefined(); |
| 41 | 43 | }); |
| 42 | 44 | |
| 45 | test("a stranger's edge headers name no one, even with the secret; the edge's do", () => { | |
| 46 | const listed = parseCidrs("10.42.0.0/16"); | |
| 47 | if (!listed.ok) throw new Error(listed.error); | |
| 48 | const trust = { proxies: listed.value }; | |
| 49 | const claim = { "x-plat-user": "ada", "x-plat-edge-token": TOKEN }; | |
| 50 | const stranger = request(claim); | |
| 51 | notePeer(stranger, "203.0.113.9", trust); | |
| 52 | expect(edgeIdentity(stranger, TOKEN)).toBeUndefined(); | |
| 53 | // With no proxies listed this machine alone is the edge. | |
| 54 | const elsewhere = request(claim); | |
| 55 | notePeer(elsewhere, "203.0.113.9"); | |
| 56 | expect(edgeIdentity(elsewhere, TOKEN)).toBeUndefined(); | |
| 57 | const edge = request(claim); | |
| 58 | notePeer(edge, "10.42.0.7", trust); | |
| 59 | expect(edgeIdentity(edge, TOKEN)).toBe("ada"); | |
| 60 | const local = request(claim); | |
| 61 | notePeer(local, "127.0.0.1"); | |
| 62 | expect(edgeIdentity(local, TOKEN)).toBe("ada"); | |
| 63 | }); | |
| 64 | ||
| 43 | 65 | test("return targets are paths on this host, never URLs", () => { |
| 44 | 66 | expect(edgeLoginUrl("/ada/hello?tab=1#x")).toBe( |
| 45 | 67 | "/_plat/login?rd=%2Fada%2Fhello%3Ftab%3D1", |
new above
offline