Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Security: a stored file opened on its own runs no script

Adarsh7af83b446aa8
diff --git a/src/server/file-response.ts b/src/server/file-response.ts
index 04154faa..2e769670 100644
--- a/src/server/file-response.ts
+++ b/src/server/file-response.ts
@@ -6,7 +6,7 @@ import { absolutePath, contentTypeFor } from "./storage";
/**
* A stored file as an HTTP response, with byte ranges (video seeking), a UTF-8 charset for text,
- * `nosniff`, and a script-free CSP for SVG. `relPath` is under the files folder; traversal is
+ * `nosniff`, a sandbox CSP (and a script-free one for SVG). `relPath` is under the files folder; traversal is
* refused. Used by /api/files (signed in) and the share routes (/s/<token>/f/…). `clean` serves the
* file without the generation data embedded in it (src/server/media-clean.ts): same size and byte
* ranges, metadata blanked as it streams; a file whose metadata can't be read isn't served.
@@ -57,9 +57,11 @@ export function fileResponse(request: Request, relPath: string, opts: { cache?:
"Accept-Ranges": "bytes",
"Cache-Control": opts.cache ?? "private, max-age=31536000, immutable",
"X-Content-Type-Options": "nosniff",
+ // Opened on its own, a stored file runs no script as this site (one could drive a ComfyUI that lets the site in).
+ "Content-Security-Policy": "sandbox",
...opts.headers,
};
- if (type === "image/svg+xml") headers["Content-Security-Policy"] = "default-src 'none'; style-src 'unsafe-inline'";
+ if (type === "image/svg+xml") headers["Content-Security-Policy"] = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
const download = new URL(request.url).searchParams.get("download");
if (download) headers["Content-Disposition"] = `attachment; filename="${download.replace(/[^\w.-]/g, "_")}"`;
const range = request.headers.get("range");
diff --git a/tests/computer-security.test.ts b/tests/computer-security.test.ts
index e44d53a2..0b87baa5 100644
--- a/tests/computer-security.test.ts
+++ b/tests/computer-security.test.ts
@@ -353,6 +353,24 @@ describe("results are checked like uploads", () => {
expect((await put("a", svg.id, 0, svg.lease, Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"/>`.padEnd(80, " ")))).status).toBe(415);
});
+ it("a stored file opened on its own can't run scripts as the site, whatever its type", async () => {
+ // A script on the site's own address could drive the person's ComfyUI: stored files never get to run one.
+ const { saveFile } = await import("@/server/storage");
+ const { fileResponse } = await import("@/server/file-response");
+ for (const [name, contentType, data] of [
+ ["a.png", "image/png", FAKE_PNG],
+ ["a.svg", "image/svg+xml", Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg"/>`)],
+ ["a.txt", "text/plain", Buffer.from("hello")],
+ ] as const) {
+ const url = saveFile({ data, contentType, name, source: "upload" }).url;
+ const res = fileResponse(new Request(`${SITE}${url}`), url.replace(/^\/api\/files\//, ""));
+ expect(res.status).toBe(200);
+ expect(res.headers.get("x-content-type-options")).toBe("nosniff");
+ expect(res.headers.get("content-security-policy")?.split(/;\s*/)).toContain("sandbox");
+ await res.body?.cancel();
+ }
+ });
+
it("a real picture sent as a web page is kept as the picture its bytes say", async () => {
const c = await collecting(ONE_PNG);
expect((await put("a", c.id, 0, c.lease, FAKE_PNG, 0, FAKE_PNG.length, { "content-type": "text/html" })).status).toBe(200);