Files
immich-folio/middleware.ts
Ralksta 8f0bb94caf fix: keep /admin reachable while setup is required (#392)
The root layout replaced the entire app with the SetupScreen whenever
the config was incomplete (missing gallery.yaml or Immich credentials)
— including /admin, which lives under the same root layout. That locked
users out of the very tool that fixes an incomplete configuration.

The middleware now forwards the request pathname via an x-pathname
header, and the root layout skips the setup screen for admin paths, so
/admin renders normally (login when ADMIN_PASSWORD is set, the
enable-hint otherwise) while every other route keeps showing the setup
instructions.

Fixes #326

Co-authored-by: ralksta <ralksta@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-05 00:27:09 +02:00

66 lines
2.5 KiB
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
// Define CSP directives
const cspDirectives = [
"default-src 'self'",
// No 'unsafe-inline' fallback: CSP3 browsers ignore it next to a nonce, but
// CSP2-only browsers ignore 'strict-dynamic' and would honour it — making
// the fallback strictly worse than having none. There are no inline
// <script> tags in the app; Next.js nonces the ones it injects itself.
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://unpkg.com",
"font-src 'self' https://fonts.gstatic.com",
"img-src 'self' data: blob: https://*.basemaps.cartocdn.com https://*.tile.openstreetmap.org https://unpkg.com",
"connect-src 'self' https://fonts.googleapis.com https://fonts.gstatic.com",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
].join('; ');
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-nonce', nonce);
// Layouts cannot read the request URL; the root layout needs the pathname
// to keep /admin reachable while the app is in setup mode.
requestHeaders.set('x-pathname', request.nextUrl.pathname);
requestHeaders.set('Content-Security-Policy', cspDirectives);
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
// Only the CSP is set here. Every other security header comes from
// next.config.ts, which also covers /api and static assets. Setting a header
// in both places emits it twice with conflicting values.
response.headers.set('Content-Security-Policy', cspDirectives);
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes — JSON/binary responses, no document CSP needed)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico, sitemap.xml, robots.txt (metadata files)
*
* /admin is NOT excluded: it is the highest-privilege surface in the app
* and previously ran with no enforced CSP at all.
*/
{
source: '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
};