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

Parked opened this in answer to
Closed without merging

mason/forge:mason/2919-apps-never-receive-the-forge-s-or-the-ed → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +98 −5
docs/THREATS.md +1 −1
@@ -136,7 +136,7 @@ How each part proves itself to the next, today.
136136 | runner → forge | the run's scoped token (`FORGE_TOKEN`) and a few-minute passport (`FORGE_PASSPORT`) | `test/passport-edge.test.ts` |
137137 | 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 |
138138 | 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` |
140140 | 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` |
141141
142142 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(
321321 return `${originFor(runtime, target, host)}/__forge/grant?t=${encodeURIComponent(token)}&to=${encodeURIComponent(path)}`;
322322 }
323323
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
324347 const HOP_BY_HOP = [
325348 "connection",
326349 "keep-alive",
@@ -615,6 +638,7 @@ export async function proxyApp(
615638 const headers = new Headers(request.headers);
616639 for (const name of HOP_BY_HOP) headers.delete(name);
617640 for (const name of IDENTITY_HEADERS) headers.delete(name);
641 withoutForgeCredentials(headers);
618642 headers.set("x-forwarded-host", request.headers.get("host") ?? "");
619643 headers.set("x-forwarded-proto", url.protocol.replace(":", ""));
620644 for (const [name, value] of Object.entries(
src/http/edge.ts +5 −1
@@ -4,7 +4,9 @@
44 * and `X-Plat-Edge-Token`, a secret only the edge holds. A request carrying
55 * the secret the operator configured (`FORGE_EDGE_TOKEN`) is that person,
66 * 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
810 * edge's own entry points on this host.
911 */
1012
@@ -12,6 +14,7 @@ import { createHash, timingSafeEqual } from "node:crypto";
1214 import type { User } from "../core/users.ts";
1315 import type { App } from "../app.ts";
1416 import { edgeSigninOn } from "../core/capacity.ts";
17 import { fromTrustedPeer } from "./peer.ts";
1518
1619 /**
1720 * The edge secret in force: the configured one while the `signin.edge`
@@ -28,6 +31,7 @@ export function edgeIdentity(
2831 edgeToken: string | undefined,
2932 ): string | undefined {
3033 if (edgeToken === undefined || edgeToken === "") return undefined;
34 if (!fromTrustedPeer(request)) return undefined;
3135 const presented = request.headers.get("x-plat-edge-token") ?? "";
3236 if (!sameSecret(presented, edgeToken)) return undefined;
3337 const login = (request.headers.get("x-plat-user") ?? "").trim();
src/http/peer.ts +24 −1
@@ -40,7 +40,7 @@ export function resolveClient(
4040 if (peer === undefined) return "unknown";
4141 const listed = trust.proxies.length > 0;
4242 const trusted = (address: string): boolean =>
43 listed ? inCidrs(address, trust.proxies) : LOOPBACK.has(address);
43 trustedPeer(address, trust);
4444 if (!trusted(peer)) return peer;
4545 if (trust.header !== undefined && listed) {
4646 const named = headers.get(trust.header)?.trim() ?? "";
@@ -59,7 +59,20 @@ export function resolveClient(
5959 return client.slice(0, 64);
6060 }
6161
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
6274 const clients = new WeakMap<Request, string>();
75 const strangers = new WeakSet<Request>();
6376
6477 /** The listener notes each request's client once, as it arrives. */
6578 export const notePeer = (
@@ -68,8 +81,17 @@ export const notePeer = (
6881 trust: ProxyTrust = NO_TRUST,
6982 ): void => {
7083 clients.set(request, resolveClient(address, request.headers, trust));
84 if (trustedPeer(address, trust)) strangers.delete(request);
85 else strangers.add(request);
7186 };
7287
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
7395 export function clientAddress(request: Request): string {
7496 return clients.get(request) ?? "unknown";
7597 }
@@ -96,4 +118,5 @@ export const clientBucket = (request: Request): string =>
96118 export const carryPeer = (from: Request, to: Request): void => {
97119 const client = clients.get(from);
98120 if (client !== undefined) clients.set(to, client);
121 if (strangers.has(from)) strangers.add(to);
99122 };
test/apps-identity.test.ts +22 −2
@@ -101,6 +101,10 @@ describe("apps: identity, and a deploy that never answers", () => {
101101 return Response.json({
102102 identity: request.headers.get("x-forge-identity"),
103103 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"),
104108 key: process.env.FORGE_APP_IDENTITY_KEY ?? "",
105109 });
106110 },
@@ -130,13 +134,29 @@ describe("apps: identity, and a deploy that never answers", () => {
130134 const heard = (await (
131135 await viaHost(`hello.${DOMAIN}`, "/who", {
132136 headers: {
133 cookie,
137 cookie: `${cookie}; forge_session=the-visitors-session; theme=dark`,
134138 // A client's own claim never reaches the app.
135139 "x-forge-identity": "evil|mallory|admin|9999999999999",
136140 "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",
137145 },
138146 })
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`);
140160 expect(heard.key).toBe(identityKey(dataDir, record.id));
141161 const [id, name, role, expires] = heard.identity.split("|");
142162 expect(id).toBe(alice.id);
test/edge-auth.test.ts +22
@@ -5,6 +5,8 @@ import { join } from "node:path";
55 import { createApp, type App } from "../src/app.ts";
66 import { edgeIdentity, edgeLoginUrl, edgeLogoutUrl } from "../src/http/edge.ts";
77 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";
810 import { createUser, getUserByName } from "../src/core/users.ts";
911 import { mintInvite } from "../src/core/invites.ts";
1012 import { setSignup } from "../src/settings/signup.ts";
@@ -40,6 +42,26 @@ describe("edgeIdentity", () => {
4042 ).toBeUndefined();
4143 });
4244
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
4365 test("return targets are paths on this host, never URLs", () => {
4466 expect(edgeLoginUrl("/ada/hello?tab=1#x")).toBe(
4567 "/_plat/login?rd=%2Fada%2Fhello%3Ftab%3D1",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs