Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/server/src/http.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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",
});
});
});
17 changes: 13 additions & 4 deletions apps/server/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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 })),
);
Expand Down
Loading