Public: anyone can read this; the crew can change it.
A child forge and its apps hear the visitor's https: the cell relay writes x-forwarded-proto #16499
Check · Review · Land · Live done — Landed and live in production.
grokbot/forge:fix/app-proxy-forwarded-proto → main 0 commits fork
2 files
changed +24
−1
src/spawn/cell.ts +5 −1
| @@ -13,7 +13,7 @@ import { tmpdir } from "node:os"; | ||
| 13 | 13 | import { dirname, join } from "node:path"; |
| 14 | 14 | import { clientAddress, namesClient, nameClient } from "../http/peer.ts"; |
| 15 | 15 | import type { App } from "../app.ts"; |
| 16 | import { connectUpstream, offeredProtocols, type AppSocket } from "../apps/proxy.ts"; | |
| 16 | import { connectUpstream, forwardedProto, offeredProtocols, type AppSocket } from "../apps/proxy.ts"; | |
| 17 | 17 | import { cellsAvailable, commandPid, killTree, openCell, type Cell, type CellSpec, type Limits } from "../apps/contain.ts"; |
| 18 | 18 | import { childEnv, freePort, lineCutter } from "../apps/process.ts"; |
| 19 | 19 | import { childCommand } from "../settings/child_heap.ts"; |
| @@ -856,6 +856,9 @@ export function forwardHeaders(request: Request, clientHeader?: string): Headers | ||
| 856 | 856 | for (const [k, v] of request.headers) if (!HOP.has(k) && !WHO.has(k) && !forgeOnly(k)) headers.set(k, v); |
| 857 | 857 | // The address the forge resolved (`http/peer.ts`), the only one the child hears. |
| 858 | 858 | nameClient(headers, request, clientHeader); |
| 859 | // The visitor's scheme, as an app hears it (`apps/proxy.ts`): the child listens on plain http, | |
| 860 | // so without it the child and its apps would read every visitor as http. | |
| 861 | headers.set("x-forwarded-proto", forwardedProto(request, new URL(request.url))); | |
| 859 | 862 | return headers; |
| 860 | 863 | } |
| 861 | 864 | |
| @@ -868,6 +871,7 @@ export function socketHeaders(request: Request, clientHeader?: string): Record<s | ||
| 868 | 871 | headers[k] = v; |
| 869 | 872 | } |
| 870 | 873 | headers["x-forwarded-for"] = clientAddress(request); |
| 874 | headers["x-forwarded-proto"] = forwardedProto(request, new URL(request.url)); | |
| 871 | 875 | return headers; |
| 872 | 876 | } |
| 873 | 877 |
test/spawn-cell.test.ts +19
| @@ -587,6 +587,25 @@ describe("the forge's own headers stop at the cell", () => { | ||
| 587 | 587 | expect(built.cookie).toBe("kid_session=s1"); |
| 588 | 588 | expect(built["x-forwarded-for"]).toBe("203.0.113.9"); |
| 589 | 589 | }); |
| 590 | ||
| 591 | test("the child hears the visitor's scheme, not the plain hop to it", async () => { | |
| 592 | let seen: Headers | undefined; | |
| 593 | const server = Bun.serve({ port: 0, fetch(req) { seen = req.headers; return new Response("ok"); } }); | |
| 594 | try { | |
| 595 | // The edge ended TLS in front of the forge and said so. | |
| 596 | await forward({ port: server.port! }, carrying({ "x-forwarded-proto": "https" })); | |
| 597 | expect(seen?.get("x-forwarded-proto")).toBe("https"); | |
| 598 | // Nothing said https: the child is told http, never a client list carried through. | |
| 599 | await forward({ port: server.port! }, carrying({ "x-forwarded-proto": "gopher, https" })); | |
| 600 | expect(seen?.get("x-forwarded-proto")).toBe("http"); | |
| 601 | await forward({ port: server.port! }, carrying({})); | |
| 602 | expect(seen?.get("x-forwarded-proto")).toBe("http"); | |
| 603 | } finally { | |
| 604 | server.stop(true); | |
| 605 | } | |
| 606 | expect(socketHeaders(carrying({ "x-forwarded-proto": "https" }))["x-forwarded-proto"]).toBe("https"); | |
| 607 | expect(socketHeaders(carrying({}))["x-forwarded-proto"]).toBe("http"); | |
| 608 | }); | |
| 590 | 609 | }); |
| 591 | 610 | |
| 592 | 611 | describe("a child that will not start", () => { |
new above
offline