Public: anyone can read this; the crew can change it.
Full suite red: test/apps-open-host.test.ts #12861
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12845-full-suite-red-test-apps-open-host-test → main 2 commits fork
1 file
changed +28
−3
src/http/server.ts +28 −3
| @@ -2440,6 +2440,29 @@ export interface Servers { | ||
| 2440 | 2440 | stop(): void; |
| 2441 | 2441 | } |
| 2442 | 2442 | |
| 2443 | /** | |
| 2444 | * A request whose Host the runtime could not turn into a URL (`a#b.example`) | |
| 2445 | * carries a `url` nothing downstream can parse. It names no host of ours, so | |
| 2446 | * it is answered as the forge's own: the same request, its path kept, on a | |
| 2447 | * Host the forge does not know either. | |
| 2448 | */ | |
| 2449 | function readable(request: Request): Request { | |
| 2450 | if (URL.canParse(request.url)) return request; | |
| 2451 | const path = /^[a-z]+:\/\/[^/]*(\/.*)?$/i.exec(request.url)?.[1] ?? "/"; | |
| 2452 | const headers = new Headers(request.headers); | |
| 2453 | headers.set("host", "forge.invalid"); | |
| 2454 | const hasBody = request.method !== "GET" && request.method !== "HEAD"; | |
| 2455 | try { | |
| 2456 | return new Request(`http://forge.invalid${URL.canParse(`http://forge.invalid${path}`) ? path : "/"}`, { | |
| 2457 | method: request.method, | |
| 2458 | headers, | |
| 2459 | ...(hasBody ? { body: request.body, duplex: "half" } : {}), | |
| 2460 | } as RequestInit); | |
| 2461 | } catch { | |
| 2462 | return new Request("http://forge.invalid/", { headers }); | |
| 2463 | } | |
| 2464 | } | |
| 2465 | ||
| 2443 | 2466 | /** |
| 2444 | 2467 | * What a thrown handler is answered with: the guard's words (or a generic |
| 2445 | 2468 | * "refused") and a 403 |
| @@ -2995,11 +3018,13 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2995 | 3018 | websocket: withShells(appWebSocketWith(app.config.wsMaxMessage)), |
| 2996 | 3019 | // Open until it is answered, so a stall while it runs names it (#11478). |
| 2997 | 3020 | // Once the port is handed over, an answer on a connection opened before ends it (#11682). |
| 2998 | fetch: (request, server) => | |
| 2999 | inFlight.during(request, performance.now(), async () => { | |
| 3021 | fetch: (raw, server) => { | |
| 3022 | const request = readable(raw); | |
| 3023 | return inFlight.during(request, performance.now(), async () => { | |
| 3000 | 3024 | const response = await handle(request, server); |
| 3001 | 3025 | return handedOver && response !== undefined ? withHeader(response, "connection", "close") : response; |
| 3002 | }), | |
| 3026 | }); | |
| 3027 | }, | |
| 3003 | 3028 | // A last resort: `handle` answers a throw itself, so it is counted. |
| 3004 | 3029 | error: (error) => failure(app, error), |
| 3005 | 3030 | }); |
new above
offline