fix(server): sandbox user-provided SVGs (#5916)

This commit is contained in:
Theo Browne authored and GitHub committed 2026-08-09 16:51:43 -07:00
1 parent 0d38866dcf
commit 2abe66800c
2 files changed
+33 -5

No files matched your search

+20 -1
View File
@@ -1,7 +1,7 @@
import { expect, it } from "@effect/vitest";
import { describe } from "vite-plus/test";
import { isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
import { assetResponseHeaders, isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
describe("http dev routing", () => {
it("treats localhost and loopback addresses as local", () => {
@@ -26,3 +26,22 @@ describe("http dev routing", () => {
);
});
});
describe("assetResponseHeaders", () => {
it("sandboxes SVG assets", () => {
expect(assetResponseHeaders("/attachments/user-image.svg")).toMatchObject({
"Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox",
"X-Content-Type-Options": "nosniff",
});
expect(assetResponseHeaders("/attachments/user-image.SVG")).toHaveProperty(
"Content-Security-Policy",
);
});
it("does not apply document policy to raster images", () => {
expect(assetResponseHeaders("/attachments/user-image.png")).toEqual({
"Cache-Control": "private, max-age=3600",
"X-Content-Type-Options": "nosniff",
});
});
});
+13 -4
View File
@@ -43,6 +43,18 @@ import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./ht
const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
const DESKTOP_RENDERER_ORIGINS = ["t3code://app", "t3code-dev://app"];
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
export function assetResponseHeaders(filePath: string): Record<string, string> {
return {
"Cache-Control": "private, max-age=3600",
"X-Content-Type-Options": "nosniff",
...(filePath.toLowerCase().endsWith(".svg")
? { "Content-Security-Policy": SVG_CONTENT_SECURITY_POLICY }
: {}),
};
}
export const httpCompressionLayer = HttpRouter.middleware(HttpMiddleware.compression(), {
global: true,
});
@@ -207,10 +219,7 @@ export const assetRouteLayer = HttpRouter.add(
}
return yield* HttpServerResponse.file(asset.path, {
status: 200,
headers: {
"Cache-Control": "private, max-age=3600",
"X-Content-Type-Options": "nosniff",
},
headers: assetResponseHeaders(asset.path),
}).pipe(
Effect.orElseSucceed(() => HttpServerResponse.text("Internal Server Error", { status: 500 })),
);