Skip to content

Open source alternative to Semrush and Ahrefs

fix(security): send frame-ancestors 'self' on app pages (#623)

Ben Senescu7117b7c27ed3
diff --git a/src/server.ts b/src/server.ts
index 4d6b9250..68c93bf1 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -28,7 +28,39 @@ import { maybeSendSelfHostHeartbeat } from "@/server/lib/self-host-telemetry";
import { handleGdprStorageErasure } from "@/server/gdpr/storage-erasure";
import { GDPR_STORAGE_ERASURE_PATH } from "@/shared/gdpr-erasure";
-const appFetch = createStartHandler(defaultStreamHandler);
+const startHandler = createStartHandler(defaultStreamHandler);
+
+// The app ships no security response headers of its own, so any third-party
+// page can frame an app route and UI-redress a one-click action (delete a
+// project, change project settings, buy credits). `frame-ancestors 'self'` on
+// the app's own documents is the whole fix, and deliberately all of it: a
+// script-src policy would need a nonce for the inline bootstrap script in
+// __root.tsx plus allowances for Turnstile and PostHog, which is separate,
+// larger work.
+//
+// Only HTML documents, and only ones that carry no policy of their own, so a
+// route that sets its own stricter CSP keeps it (two CSP headers intersect, so
+// adding a second could only confuse things). Wrapping the handler rather than
+// one call site covers the OAuth-provider path too, which serves app documents
+// through this same function.
+async function appFetch(request: Request): Promise<Response> {
+ const response = await startHandler(request);
+ const contentType = response.headers.get("content-type") ?? "";
+ if (
+ !contentType.startsWith("text/html") ||
+ response.headers.has("content-security-policy")
+ ) {
+ return response;
+ }
+ const headers = new Headers(response.headers);
+ headers.set("Content-Security-Policy", "frame-ancestors 'self'");
+ return new Response(response.body, {
+ status: response.status,
+ statusText: response.statusText,
+ headers,
+ });
+}
+
const openSeoOAuthProvider = createOpenSeoOAuthProvider(appFetch);
// Authorize a SAM agent connection in the Worker, before it reaches the Durable