Public: anyone can read this; the crew can change it.
A cell child never receives the edge token or x-plat-* headers: forward and forwardSocket strip them #15236
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15234-a-cell-child-never-receives-the-edge-tok → main 1 commit fork
3 files
changed +80
−11
docs/threats/child-forge.md +5
| @@ -110,3 +110,8 @@ What it can reach: everything a child forge can reach, no more: its own data dir | ||
| 110 | 110 | child that writes another repository into its `parent.json` is refused and the refusal |
| 111 | 111 | logged; no token is minted for the repository it named (#14759). |
| 112 | 112 | Enforced by: `test/proposal-handout.test.ts` |
| 113 | - A cell child never hears the forge's own headers: `forward` and `forwardSocket` | |
| 114 | (`src/spawn/cell.ts`) drop every `x-plat-*` header, the edge token and the user the edge | |
| 115 | vouched for among them, on a plain request and a websocket handshake alike. The visitor's | |
| 116 | `authorization` and cookies pass, so the child's own sign-in still works (#15234). | |
| 117 | Enforced by: `test/spawn-cell.test.ts` |
src/spawn/cell.ts +33 −10
| @@ -812,6 +812,37 @@ const WHO = new Set(["x-forwarded-host", "x-forwarded-proto"]); | ||
| 812 | 812 | |
| 813 | 813 | const HOP = new Set(["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade", "content-length"]); |
| 814 | 814 | |
| 815 | /** | |
| 816 | * The forge's own words between its edge and itself (`x-plat-*`: the edge token, the user it | |
| 817 | * vouched for) never reach a child, as `stripForgeCredentials` keeps them from an app | |
| 818 | * (`apps/proxy.ts`). The visitor's `authorization` and cookies are the child's own sign-in, and | |
| 819 | * pass. | |
| 820 | */ | |
| 821 | function forgeOnly(name: string): boolean { | |
| 822 | return name.startsWith("x-plat-"); | |
| 823 | } | |
| 824 | ||
| 825 | /** The headers a plain request carries to a child. */ | |
| 826 | export function forwardHeaders(request: Request, clientHeader?: string): Headers { | |
| 827 | const headers = new Headers(); | |
| 828 | for (const [k, v] of request.headers) if (!HOP.has(k) && !WHO.has(k) && !forgeOnly(k)) headers.set(k, v); | |
| 829 | // The address the forge resolved (`http/peer.ts`), the only one the child hears. | |
| 830 | nameClient(headers, request, clientHeader); | |
| 831 | return headers; | |
| 832 | } | |
| 833 | ||
| 834 | /** The headers a websocket handshake carries to a child. */ | |
| 835 | export function socketHeaders(request: Request, clientHeader?: string): Record<string, string> { | |
| 836 | const headers: Record<string, string> = {}; | |
| 837 | for (const [k, v] of request.headers) { | |
| 838 | // The client writes its own handshake; the visitor's key, version and extensions end here. | |
| 839 | if (HOP.has(k) || WHO.has(k) || forgeOnly(k) || namesClient(k, clientHeader) || k.startsWith("sec-websocket-")) continue; | |
| 840 | headers[k] = v; | |
| 841 | } | |
| 842 | headers["x-forwarded-for"] = clientAddress(request); | |
| 843 | return headers; | |
| 844 | } | |
| 845 | ||
| 815 | 846 | /** Carry a request to the child's loopback port and its answer back, streaming. */ |
| 816 | 847 | export async function forward( |
| 817 | 848 | child: Pick<CellChild, "port">, |
| @@ -819,10 +850,7 @@ export async function forward( | ||
| 819 | 850 | clientHeader?: string, |
| 820 | 851 | ): Promise<Response> { |
| 821 | 852 | const at = new URL(request.url); |
| 822 | const headers = new Headers(); | |
| 823 | for (const [k, v] of request.headers) if (!HOP.has(k) && !WHO.has(k)) headers.set(k, v); | |
| 824 | // The address the forge resolved (`http/peer.ts`), the only one the child hears. | |
| 825 | nameClient(headers, request, clientHeader); | |
| 853 | const headers = forwardHeaders(request, clientHeader); | |
| 826 | 854 | const hasBody = request.method !== "GET" && request.method !== "HEAD"; |
| 827 | 855 | try { |
| 828 | 856 | const res = await fetch(`http://127.0.0.1:${child.port}${at.pathname}${at.search}`, { |
| @@ -856,12 +884,7 @@ export async function forwardSocket( | ||
| 856 | 884 | clientHeader?: string, |
| 857 | 885 | ): Promise<Response | undefined> { |
| 858 | 886 | const at = new URL(request.url); |
| 859 | const headers: Record<string, string> = {}; | |
| 860 | for (const [k, v] of request.headers) { | |
| 861 | // The client writes its own handshake; the visitor's key, version and extensions end here. | |
| 862 | if (!HOP.has(k) && !WHO.has(k) && !namesClient(k, clientHeader) && !k.startsWith("sec-websocket-")) headers[k] = v; | |
| 863 | } | |
| 864 | headers["x-forwarded-for"] = clientAddress(request); | |
| 887 | const headers = socketHeaders(request, clientHeader); | |
| 865 | 888 | const url = new URL(`ws://127.0.0.1:${child.port}${at.pathname}${at.search}`); |
| 866 | 889 | const opened = await connectUpstream(url, headers, offeredProtocols(request)); |
| 867 | 890 | if (!opened.ok) return new Response("this forge is not answering", { status: 502 }); |
test/spawn-cell.test.ts +42 −1
| @@ -10,7 +10,7 @@ import { createRun, queueDepth } from "../src/core/runs.ts"; | ||
| 10 | 10 | import { createApp } from "../src/app.ts"; |
| 11 | 11 | import { childEnv } from "../src/apps/process.ts"; |
| 12 | 12 | import { watchedLimits } from "../src/apps/contain.ts"; |
| 13 | import { childLog, plainLine, cellChildren, crewAtWork, nextRunDue, type CellChild, stopGently, cellEnv, childEnvFor, cellLimits, cellPlacement, cellSpecFor, forward, launchSpecFor, ownsBody } from "../src/spawn/cell.ts"; | |
| 13 | import { childLog, plainLine, cellChildren, crewAtWork, nextRunDue, type CellChild, stopGently, cellEnv, childEnvFor, cellLimits, cellPlacement, cellSpecFor, forward, forwardHeaders, launchSpecFor, ownsBody, socketHeaders } from "../src/spawn/cell.ts"; | |
| 14 | 14 | import { fallbackPlacement, hostedPlacement, memoryBodies } from "../src/spawn/placement.ts"; |
| 15 | 15 | import type { SpawnPlan } from "../src/spawn/plan.ts"; |
| 16 | 16 | import { SIZES } from "../src/spawn/sizes.ts"; |
| @@ -548,6 +548,47 @@ describe("who the child hears", () => { | ||
| 548 | 548 | }); |
| 549 | 549 | }); |
| 550 | 550 | |
| 551 | describe("the forge's own headers stop at the cell", () => { | |
| 552 | const carrying = (headers: Record<string, string>) => { | |
| 553 | const request = new Request("http://kid--f.test/", { | |
| 554 | headers: { | |
| 555 | host: "kid--f.test", | |
| 556 | "x-plat-edge-token": "edge-secret", | |
| 557 | "x-plat-user": "alice", | |
| 558 | authorization: "Bearer visitor", | |
| 559 | cookie: "kid_session=s1", | |
| 560 | ...headers, | |
| 561 | }, | |
| 562 | }); | |
| 563 | notePeer(request, "203.0.113.9"); | |
| 564 | return request; | |
| 565 | }; | |
| 566 | ||
| 567 | test("a plain request reaches the child without x-plat-*, with the visitor's sign-in", async () => { | |
| 568 | let seen: Headers | undefined; | |
| 569 | const server = Bun.serve({ port: 0, fetch(req) { seen = req.headers; return new Response("ok"); } }); | |
| 570 | try { | |
| 571 | await forward({ port: server.port! }, carrying({})); | |
| 572 | expect(seen?.get("x-plat-edge-token")).toBeNull(); | |
| 573 | expect(seen?.get("x-plat-user")).toBeNull(); | |
| 574 | expect(seen?.get("authorization")).toBe("Bearer visitor"); | |
| 575 | expect(seen?.get("cookie")).toBe("kid_session=s1"); | |
| 576 | const built = forwardHeaders(carrying({})); | |
| 577 | expect([...built.keys()].filter((k) => k.startsWith("x-plat-"))).toEqual([]); | |
| 578 | } finally { | |
| 579 | server.stop(true); | |
| 580 | } | |
| 581 | }); | |
| 582 | ||
| 583 | test("a websocket handshake reaches the child without x-plat-*, with the visitor's sign-in", () => { | |
| 584 | const built = socketHeaders(carrying({ upgrade: "websocket", connection: "Upgrade", "sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==", "sec-websocket-version": "13" })); | |
| 585 | expect(Object.keys(built).filter((k) => k.startsWith("x-plat-"))).toEqual([]); | |
| 586 | expect(built.authorization).toBe("Bearer visitor"); | |
| 587 | expect(built.cookie).toBe("kid_session=s1"); | |
| 588 | expect(built["x-forwarded-for"]).toBe("203.0.113.9"); | |
| 589 | }); | |
| 590 | }); | |
| 591 | ||
| 551 | 592 | describe("a child that will not start", () => { |
| 552 | 593 | test.skipIf(cells.ok)("is not relaunched on every request", async () => { |
| 553 | 594 | const warned: string[] = []; |
new above
offline