Resolve ALLOWED_FRAME_URLS from the runtime environment in Docker

The Content-Security-Policy is defined in next.config.ts headers(), which Next
serializes at build time, so ALLOWED_FRAME_URLS only takes effect when it is set
before `next build`. The published Docker images are built without it, so
frame-ancestors cannot be configured at runtime and iframe embedding requires a
custom image (#3793, #2407).

Move the policy into a shared getContentSecurityPolicy() and set the header from
the Docker proxy (docker/proxy.ts) on every response, where runtime env such as
CORS_MAX_AGE and DISABLE_LOGIN is already handled. next.config.ts skips the
build-time CSP when the proxy is present, so exactly one header is sent.

Non-Docker builds are unchanged: the build-time CSP still applies and
ALLOWED_FRAME_URLS keeps working as a build-time variable there.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Niklas LauschandClaude Opus 4.8 committed 2026-06-26 13:31:37 +02:00
1 parent 91b31634e1
commit ecf9b3b0a2
3 files changed
+45 -26

No files matched your search

+15 -3
View File
@@ -1,4 +1,5 @@
import { type NextRequest, NextResponse } from 'next/server';
import { getContentSecurityPolicy } from '@/lib/csp';
import { matchesConfiguredPath } from '@/lib/match-configured-path';
export const config = {
@@ -23,6 +24,9 @@ const trackerHeaders = {
'Cache-Control': 'public, max-age=86400, must-revalidate',
};
// Resolved once at startup — env vars don't change after the process starts.
const contentSecurityPolicy = getContentSecurityPolicy();
function customCollectEndpoint(request: NextRequest) {
const collectEndpoint = process.env.COLLECT_API_ENDPOINT;
@@ -69,12 +73,20 @@ function disableLogin(request: NextRequest) {
export default function middleware(req: NextRequest) {
const fns = [customCollectEndpoint, customScriptName, customScriptUrl, disableLogin];
let res: NextResponse | undefined;
for (const fn of fns) {
const res = fn(req);
res = fn(req);
if (res) {
return res;
break;
}
}
return NextResponse.next();
res ??= NextResponse.next();
// Set the CSP here, not only at build time in next.config.ts, so
// ALLOWED_FRAME_URLS is resolved from the runtime environment.
res.headers.set('Content-Security-Policy', contentSecurityPolicy);
return res;
}