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

Merged opened this From outside

Check · Review · Land · Live done — Landed and live in production.

Merged

grokbot/forge:fix/app-proxy-forwarded-proto → main 0 commits fork

  • Merged. fix/app-proxy-forwarded-proto was merged into main as 0c32275.

Review

Sign in to review this change.
2 files changed +24 −1
src/spawn/cell.ts +5 −1
@@ -13,7 +13,7 @@ import { tmpdir } from "node:os";
1313 import { dirname, join } from "node:path";
1414 import { clientAddress, namesClient, nameClient } from "../http/peer.ts";
1515 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";
1717 import { cellsAvailable, commandPid, killTree, openCell, type Cell, type CellSpec, type Limits } from "../apps/contain.ts";
1818 import { childEnv, freePort, lineCutter } from "../apps/process.ts";
1919 import { childCommand } from "../settings/child_heap.ts";
@@ -856,6 +856,9 @@ export function forwardHeaders(request: Request, clientHeader?: string): Headers
856856 for (const [k, v] of request.headers) if (!HOP.has(k) && !WHO.has(k) && !forgeOnly(k)) headers.set(k, v);
857857 // The address the forge resolved (`http/peer.ts`), the only one the child hears.
858858 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)));
859862 return headers;
860863 }
861864
@@ -868,6 +871,7 @@ export function socketHeaders(request: Request, clientHeader?: string): Record<s
868871 headers[k] = v;
869872 }
870873 headers["x-forwarded-for"] = clientAddress(request);
874 headers["x-forwarded-proto"] = forwardedProto(request, new URL(request.url));
871875 return headers;
872876 }
873877
test/spawn-cell.test.ts +19
@@ -587,6 +587,25 @@ describe("the forge's own headers stop at the cell", () => {
587587 expect(built.cookie).toBe("kid_session=s1");
588588 expect(built["x-forwarded-for"]).toBe("203.0.113.9");
589589 });
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 });
590609 });
591610
592611 describe("a child that will not start", () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs