diff --git a/src/gateway/relay.test.ts b/src/gateway/relay.test.ts index d3862b10c..88b3e0dd4 100644 --- a/src/gateway/relay.test.ts +++ b/src/gateway/relay.test.ts @@ -770,4 +770,32 @@ describe("GatewayRelay against a worker that breaks the protocol", () => { expect(await pending).toMatchObject({ code: "WORKER_UNREACHABLE", state: "rejected" }); }); + + it("drops every hop-by-hop and framing header of a worker's relay head, in any case, and keeps an ordinary header, on the response it hands the caller", async () => { + const { connection, relay } = await setup(); + const pending = relay.relay(WORKER_ID, call()); + const request = await sentFrame(connection, "relay.request"); + await openedChannel(connection); + + connection.push("relay-head", { + headers: [ + ["connection", "close"], + ["Content-Length", "5"], + ["keep-alive", "timeout=5"], + ["proxy-authenticate", "Basic realm=worker"], + ["proxy-authorization", "Basic worker-secret"], + ["te", "trailers"], + ["Te", "custom-checksum"], + ["trailer", "X-Note"], + ["TRANSFER-ENCODING", "chunked"], + ["upgrade", "h2c"], + ["x-kept", "yes"], + ], + requestId: request.id, + status: 200, + }); + const response = await pending; + + expect([...response.headers.entries()]).toEqual([["x-kept", "yes"]]); + }); }); diff --git a/src/gateway/relay.ts b/src/gateway/relay.ts index 618f31b1e..4d0f2668f 100644 --- a/src/gateway/relay.ts +++ b/src/gateway/relay.ts @@ -2,7 +2,12 @@ // channel wait and the wait for the head, and an idle bound covers the response body, which may be a live stream that // runs as long as someone reads it; the link bounds the channels open to a worker (ADR 0028). -import { isSimlockError, relayHeaderPairs, type AnySimlockError } from "../contract/index.js"; +import { + isRelayFramingHeader, + isSimlockError, + relayHeaderPairs, + type AnySimlockError, +} from "../contract/index.js"; import { DispatchError } from "../daemon/dispatch.js"; import { LinkChannelError, type LinkChannel } from "../link/index.js"; import { type Clock, SystemClock, type TimerHandle } from "../ports/index.js"; @@ -204,7 +209,11 @@ class Exchange { } const responseHeaders = new Headers(); try { - for (const [name, value] of headers) responseHeaders.append(name, value); + for (const [name, value] of headers) { + // why: the gateway frames its own response, so a worker's framing headers must not reach the caller. + if (isRelayFramingHeader(name)) continue; + responseHeaders.append(name, value); + } } catch { // why: `Headers` refuses a value above U+00FF that the push schema lets through. this.#invalid();