Files
EpicNext-Cms/src/proxy.ts
T
openhands 3c8abb6fbf
CI / check (push) Successful in 39s
CI / tests-imaging-browsers (push) Successful in 2m9s
CI / tests-integration (push) Successful in 2m47s
CI / tests-unit (push) Failing after 3m22s
CI / tests-ui (push) Failing after 4m14s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
feat(assets): choose furniture bundle ext via FURNI_BUNDLE_EXT
Bundle the remaining imaging hardening and the host/CI fixes from the
"Doe alles" pass into one release.

Furniture:
- Resolve the local bundle extension from FURNI_BUNDLE_EXT (".hab" default,
  ".nitro" for a stock Nitro client). The configured extension is written by
  imports/repairs and preferred on lookup; the other stays a legacy fallback.

Imaging:
- Avatar proxy sends s-maxage=86400 + stale-while-revalidate (300/3600 for the
  fallback path) so the edge can hold a render a day.
- Cloudflare cache rule now covers /api/imaging/ (verified MISS->HIT live).
- Document that bare /imaging is shadowed by the edge proxy to the renderer.

Ops/CI:
- docker-update.sh skips a CI-managed host (exit 0) instead of failing cron.
- ci-deploy.sh runs the cross-browser imaging gate (chromium+firefox+webkit)
  against the candidate before cutover; DEPLOY_SKIP_CROSS_BROWSER=1 as escape.
- scripts/imager-health-alert.sh alerts (Discord) on imager up/down transitions;
  runs from cron every 5 minutes.

Tests:
- Furniture bundle-ext resolver + FURNI_BUNDLE_EXT wiring.
- Imaging performance budget (bytes + warm latency).
- classifyDdos: /api/imaging/* stays in the api bucket.
2026-10-14 21:46:56 +02:00

116 lines
4.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import { env } from "@/env";
import { buildContentSecurityPolicy, createCspNonce } from "@/lib/csp";
import { ddosReject, enforceDdosRateLimit } from "@/lib/ddos-guard";
import {
isCacheableAssetPath,
isEdgeCacheablePublicPath,
shouldRedirectAdminRequest,
} from "@/lib/proxy-access";
const SECURITY_HEADERS: Record<string, string> = {
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"X-XSS-Protection": "0",
"Referrer-Policy": "strict-origin-when-cross-origin",
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
"Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload",
};
export const proxy = async (req: import("next/server").NextRequest) => {
const decision = await enforceDdosRateLimit(req);
if (decision.outcome === "suspect") {
return ddosReject(403);
}
if (decision.outcome === "block") {
return ddosReject(429, decision.retryAfterSeconds);
}
const pathname = req.nextUrl.pathname;
// Only /admin needs a real session token for the redirect guard; skipping
// JWT decoding on every other request keeps the proxy cheap under load.
const adminPath = pathname === "/admin" || pathname.startsWith("/admin/");
const token = adminPath
? await getToken({
req,
secret: env.AUTH_SECRET,
secureCookie: true,
})
: null;
if (shouldRedirectAdminRequest(pathname, token)) {
// Carry the destination along so the sign-in form can send the visitor
// back to the page they asked for instead of dumping them on /me.
const login = new URL("/login", req.url);
login.searchParams.set("from", `${pathname}${req.nextUrl.search}`);
return NextResponse.redirect(login);
}
const nonce = createCspNonce();
const csp = buildContentSecurityPolicy(nonce);
const headers = new Headers(req.headers);
headers.set("x-pathname", pathname);
headers.set("x-nonce", nonce);
// Next derives its render nonce from the *request* `content-security-policy`
// header, not from `x-nonce`, so the policy has to travel on the request as
// well — otherwise every inline bootstrap/flight script Next emits ships
// without a nonce and is blocked by the very CSP we are building here.
headers.set("Content-Security-Policy", csp);
// A client may supply this legacy derived header; no consumer should trust it.
headers.delete("x-real-client-ip");
const response = NextResponse.next({ request: { headers } });
// HTML is never cached (browser/CDN/edge) so that after a deploy the page
// always references the current build's chunks. Static assets are
// content-hashed + immutable and can be cached aggressively; a stale HTML
// document would reference chunk URLs that no longer exist after a rebuild.
// Rendered avatars and uploaded media are immutable per key and already
// carry their own long-lived Cache-Control, so they keep it here.
if (isCacheableAssetPath(pathname)) {
// Keep the asset headers as-is.
} else if (
isEdgeCacheablePublicPath(pathname) &&
!req.cookies.get("authjs.session-token") &&
!req.cookies.get("__Secure-authjs.session-token") &&
!req.nextUrl.searchParams.has("from")
) {
// Anonymous, low-variance public pages get a short shared cache with
// stale-while-revalidate, so a burst of visitors triggers one render
// instead of one per visitor. Anything session-dependent still falls
// through to the private header below.
response.headers.set(
"Cache-Control",
"public, max-age=0, s-maxage=30, stale-while-revalidate=300",
);
} else {
response.headers.set(
"Cache-Control",
"private, no-cache, no-store, max-age=0, must-revalidate",
);
}
for (const [key, value] of Object.entries(SECURITY_HEADERS)) {
response.headers.set(key, value);
}
response.headers.set("Content-Security-Policy", csp);
return response;
};
export const config = {
// `/imaging` and `/images` are excluded: they are raw image responses that
// carry their own long-lived Cache-Control and must not be rewritten by the
// HTML/asset cache logic. In production the edge proxy also fronts
// `/imaging` straight to the renderer, so the CMS route never sees that
// traffic; `/api/imaging/*` (avatars, badges) is NOT excluded and therefore
// still passes through the anti-DDoS gate and keeps its asset headers.
matcher: [
"/((?!_next/static|_next/image|assets|favicon.ico|swf|nitro-assets|imaging|images).*)",
],
};