Skip to content

Commit 568bfd0

Browse files
authored
fix(canvas): serve Content-Length on Canvas document HEAD responses (#117964)
* fix(canvas): serve Content-Length on Canvas document HEAD responses * fix(canvas): measure decoded HTML representation for Content-Length
1 parent f2b3d15 commit 568bfd0

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

‎src/canvas/serve.runtime.test.ts‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Core Canvas document HTTP response coverage.
2-
import { mkdtemp, rm } from "node:fs/promises";
2+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
33
import type { IncomingMessage, ServerResponse } from "node:http";
44
import { tmpdir } from "node:os";
55
import path from "node:path";
66
import { afterEach, describe, expect, it, vi } from "vitest";
7-
import { createCanvasDocument } from "./documents.js";
7+
import { createCanvasDocument, resolveCanvasDocumentsDir } from "./documents.js";
88
import { handleCanvasDocumentHttpRequest } from "./serve.runtime.js";
99

1010
const tempDirs: string[] = [];
@@ -81,6 +81,59 @@ describe("core canvas document host", () => {
8181
expect(response.headers["content-security-policy"]).toBeUndefined();
8282
});
8383

84+
it("serves Content-Length on HEAD responses", async () => {
85+
const stateDir = await createStateDir();
86+
const html = "<html><body>widget</body></html>";
87+
const document = await createCanvasDocument(
88+
{
89+
id: "widget-1",
90+
kind: "html_bundle",
91+
entrypoint: { type: "html", value: html },
92+
cspSandbox: "scripts",
93+
},
94+
{ stateDir },
95+
);
96+
const css = "body { color: red; }";
97+
await writeFile(
98+
path.join(resolveCanvasDocumentsDir(stateDir), "widget-1", "style.css"),
99+
css,
100+
"utf8",
101+
);
102+
const cssUrl = document.entryUrl.replace(/index\.html$/, "style.css");
103+
104+
const getHtml = await capture(document.entryUrl);
105+
const headHtml = await capture(document.entryUrl, "HEAD");
106+
expect(headHtml.statusCode).toBe(200);
107+
expect(headHtml.headers["content-length"]).toBe(String(getHtml.body.byteLength));
108+
expect(headHtml.body.byteLength).toBe(0);
109+
expect(headHtml.headers["content-type"]).toBe("text/html; charset=utf-8");
110+
expect(headHtml.headers["content-security-policy"]).toBe("sandbox allow-scripts");
111+
112+
const getCss = await capture(cssUrl);
113+
const headCss = await capture(cssUrl, "HEAD");
114+
expect(getCss.statusCode).toBe(200);
115+
expect(headCss.statusCode).toBe(200);
116+
expect(headCss.headers["content-length"]).toBe(String(getCss.body.byteLength));
117+
expect(headCss.body.byteLength).toBe(0);
118+
119+
// Invalid UTF-8 in a copied HTML file expands to U+FFFD when served, so the
120+
// header must be measured from the decoded representation, not raw bytes.
121+
const brokenBytes = Buffer.from([
122+
0x3c, 0x68, 0x31, 0x3e, 0xff, 0xfe, 0x3c, 0x2f, 0x68, 0x31, 0x3e,
123+
]);
124+
await writeFile(
125+
path.join(resolveCanvasDocumentsDir(stateDir), "widget-1", "broken.html"),
126+
brokenBytes,
127+
);
128+
const brokenUrl = document.entryUrl.replace(/index\.html$/, "broken.html");
129+
const getBroken = await capture(brokenUrl);
130+
const headBroken = await capture(brokenUrl, "HEAD");
131+
expect(getBroken.statusCode).toBe(200);
132+
expect(getBroken.body.byteLength).toBeGreaterThan(brokenBytes.byteLength);
133+
expect(headBroken.headers["content-length"]).toBe(String(getBroken.body.byteLength));
134+
expect(headBroken.body.byteLength).toBe(0);
135+
});
136+
84137
it("rejects unsupported methods and traversal paths", async () => {
85138
await createStateDir();
86139
const methodResponse = await capture(

‎src/canvas/serve.runtime.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,27 @@ export async function handleCanvasDocumentHttpRequest(
8080
: ((await detectMime({ filePath: opened.realPath })) ?? "application/octet-stream");
8181
res.setHeader("Cache-Control", "no-store");
8282
if (mime === "text/html") {
83+
// Measure the decoded representation: toString("utf8") expands invalid
84+
// bytes to U+FFFD, so the raw file length can differ from the body sent.
85+
const body = opened.data.toString("utf8");
8386
res.setHeader("Content-Type", "text/html; charset=utf-8");
87+
res.setHeader("Content-Length", String(Buffer.byteLength(body)));
8488
if ((await resolveDocumentSandbox(root, relativePath)) === "scripts") {
8589
res.setHeader("Content-Security-Policy", "sandbox allow-scripts");
8690
}
87-
res.end(opened.data.toString("utf8"));
91+
if (req.method === "HEAD") {
92+
res.end();
93+
return true;
94+
}
95+
res.end(body);
8896
return true;
8997
}
9098
res.setHeader("Content-Type", mime);
99+
res.setHeader("Content-Length", String(opened.data.byteLength));
100+
if (req.method === "HEAD") {
101+
res.end();
102+
return true;
103+
}
91104
res.end(opened.data);
92105
return true;
93106
} catch (error) {

0 commit comments

Comments
 (0)