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>
29 lines
986 B
TypeScript
29 lines
986 B
TypeScript
function getUrlOrigin(url: string) {
|
|
try {
|
|
return new URL(url).origin;
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// Builds the Content-Security-Policy. Reads the environment when called, so it
|
|
// can run at build time (next.config.ts) or per request in the Docker proxy,
|
|
// where ALLOWED_FRAME_URLS and API_URL then apply without rebuilding the image.
|
|
export function getContentSecurityPolicy() {
|
|
const apiUrlOrigin = getUrlOrigin(process.env.API_URL || '');
|
|
const connectSrc = ["'self'", 'https:', apiUrlOrigin].filter(Boolean).join(' ');
|
|
const frameAncestors = ["'self'", process.env.ALLOWED_FRAME_URLS || ''].filter(Boolean).join(' ');
|
|
|
|
const directives = [
|
|
`default-src 'self'`,
|
|
`img-src 'self' https: data: blob:`,
|
|
`script-src 'self' 'unsafe-eval' 'unsafe-inline'`,
|
|
`style-src 'self' 'unsafe-inline'`,
|
|
`connect-src ${connectSrc}`,
|
|
`frame-src 'self' http: https:`,
|
|
`frame-ancestors ${frameAncestors}`,
|
|
];
|
|
|
|
return `${directives.join('; ')};`;
|
|
}
|