Skip to content

Commit b121219

Browse files
authored
fix(gateway): preserve sandbox HEAD response metadata (#118553)
Punchcard-Session: coral-summit-orchard-93
1 parent 3b3c540 commit b121219

2 files changed

Lines changed: 74 additions & 8 deletions

File tree

‎src/gateway/mcp-app-sandbox-http.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { IncomingMessage } from "node:http";
2+
import type { AddressInfo } from "node:net";
23
import { describe, expect, it } from "vitest";
34
import { buildMcpAppSandboxPath } from "../agents/mcp-app-sandbox.js";
45
import { createSandboxHostHttpServer } from "./mcp-app-sandbox-http.js";
@@ -12,6 +13,25 @@ function request(url: string, method: "GET" | "HEAD" | "POST" = "GET") {
1213
return { res, end, setHeader };
1314
}
1415

16+
async function withSandboxHost(run: (origin: string) => Promise<void>): Promise<void> {
17+
const server = createSandboxHostHttpServer();
18+
await new Promise<void>((resolve, reject) => {
19+
server.once("error", reject);
20+
server.listen(0, "127.0.0.1", () => {
21+
server.off("error", reject);
22+
resolve();
23+
});
24+
});
25+
const address = server.address() as AddressInfo;
26+
try {
27+
await run(`http://127.0.0.1:${address.port}`);
28+
} finally {
29+
await new Promise<void>((resolve, reject) => {
30+
server.close((error) => (error ? reject(error) : resolve()));
31+
});
32+
}
33+
}
34+
1535
describe("MCP App sandbox HTTP origin", () => {
1636
it("serves only the proxy endpoint with metadata-derived CSP", () => {
1737
const result = request(
@@ -80,4 +100,50 @@ describe("MCP App sandbox HTTP origin", () => {
80100
expect.stringContaining("connect-src https://xn--bcher-kva.example"),
81101
);
82102
});
103+
104+
it.each([
105+
{
106+
label: "sandbox HTML",
107+
path: buildMcpAppSandboxPath(),
108+
statusCode: 200,
109+
},
110+
{
111+
label: "missing path",
112+
path: "/missing",
113+
statusCode: 404,
114+
},
115+
{
116+
label: "malformed policy",
117+
path: `${buildMcpAppSandboxPath()}?csp=not-json`,
118+
statusCode: 400,
119+
},
120+
])(
121+
"keeps GET and HEAD representation metadata aligned for $label",
122+
async ({ path, statusCode }) => {
123+
await withSandboxHost(async (origin) => {
124+
const get = await fetch(`${origin}${path}`);
125+
const head = await fetch(`${origin}${path}`, { method: "HEAD" });
126+
const getBody = await get.text();
127+
128+
expect(get.status).toBe(statusCode);
129+
expect(head.status).toBe(statusCode);
130+
expect(getBody).not.toBe("");
131+
expect(await head.text()).toBe("");
132+
expect(get.headers.get("content-length")).toBe(String(Buffer.byteLength(getBody)));
133+
for (const header of [
134+
"content-type",
135+
"content-length",
136+
"cache-control",
137+
"content-security-policy",
138+
"permissions-policy",
139+
"cross-origin-resource-policy",
140+
"origin-agent-cluster",
141+
"referrer-policy",
142+
"x-content-type-options",
143+
]) {
144+
expect(head.headers.get(header), header).toBe(get.headers.get(header));
145+
}
146+
});
147+
},
148+
);
83149
});

‎src/gateway/mcp-app-sandbox-http.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
decodeSandboxHostCsp,
1313
SANDBOX_HOST_PATH,
1414
} from "../agents/sandbox-host.js";
15+
import { respondPlainText } from "./control-ui-http-utils.js";
1516

1617
const MCP_APP_PERMISSIONS_POLICY = "camera=(), microphone=(), geolocation=(), clipboard-write=()";
1718

@@ -20,23 +21,19 @@ function handleMcpAppSandboxHttpRequest(req: IncomingMessage, res: ServerRespons
2021
try {
2122
url = new URL(req.url ?? "/", "http://localhost");
2223
} catch {
23-
res.statusCode = 400;
24-
res.end("Bad Request");
24+
respondPlainText(res, 400, "Bad Request");
2525
return;
2626
}
2727
if (url.pathname !== SANDBOX_HOST_PATH || (req.method !== "GET" && req.method !== "HEAD")) {
28-
res.statusCode = 404;
29-
res.end("Not Found");
28+
respondPlainText(res, 404, "Not Found");
3029
return;
3130
}
3231

3332
let csp;
3433
try {
3534
csp = decodeSandboxHostCsp(url.searchParams.get("csp"));
3635
} catch {
37-
res.statusCode = 400;
38-
res.setHeader("Content-Type", "text/plain; charset=utf-8");
39-
res.end("invalid MCP App sandbox policy");
36+
respondPlainText(res, 400, "invalid MCP App sandbox policy");
4037
return;
4138
}
4239

@@ -49,7 +46,10 @@ function handleMcpAppSandboxHttpRequest(req: IncomingMessage, res: ServerRespons
4946
res.setHeader("Origin-Agent-Cluster", "?1");
5047
res.setHeader("Referrer-Policy", "no-referrer");
5148
res.setHeader("X-Content-Type-Options", "nosniff");
52-
res.end(req.method === "HEAD" ? undefined : buildSandboxHostProxyHtml(csp));
49+
const html = buildSandboxHostProxyHtml(csp);
50+
// Keep GET and HEAD representation metadata aligned while suppressing the HEAD body.
51+
res.setHeader("Content-Length", String(Buffer.byteLength(html)));
52+
res.end(req.method === "HEAD" ? undefined : html);
5353
}
5454

5555
/** Dedicated listener: this origin must never serve Control UI or authenticated Gateway data. */

0 commit comments

Comments
 (0)