Files
loki/scripts/site-template/lib/security.ts
CatoandClaude c83c8f473e feat(starter): kickoff and new-site.sh both produce the fleet starter (#1138)
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16,
better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in
as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit,
tests and CI — copied at a pinned fleet commit by
scripts/templates/sync-starter.sh. CI proves the copy is exact
(sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's
starter moves. Never edited here.

The kickoff's "nextjs-tailwind" starter seeds the same app from
src/lib/starter-app.generated.ts (generated from the directory, pinned by
scripts/test/starter-app.ts), minus the workflows register-site.sh adds with
the site's CD. The repo is seeded in one Trees API call with inline contents
instead of a blob POST per file (~85 for this starter). The widget token rides
in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values.
The new-from-scratch picker reads the light template list, so the starter's
file bodies never reach a client bundle.

Both paths give the site its database and auth/seal secrets ON THE BOX
(scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials,
/opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and
record the db in the register row so every deploy applies drizzle/.
register-site.sh seeds the starter's full workflow set to starter repos and
the golden CI floor to anything else, never overwriting another repo's ci.yml.

test-site-template.sh follows the starter (verify SSOT, design tokens instead
of a local palette) and its comment stripper is fixed: the sed range deleted
the page from the doc comment to EOF, so every day-zero check had been passing
against an empty string. check-template-contrast.mjs and the starter-lock
generator are retired with the files they checked.


Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 13:39:14 -07:00

73 lines
3.1 KiB
TypeScript

/**
* The security headers, in one place. Products built from this starter hold
* the most private things people have — legal letters, health, money — so the
* browser is told to trust as little as possible:
*
* - scripts only from this origin, and only those carrying this response's
* nonce (`strict-dynamic` lets those load their own chunks, nothing else);
* - network requests only to this origin (`connect-src 'self'`): a script
* that slipped in still has nowhere to send what it read;
* - no framing (clickjacking), no plugins, no <base> rewriting, forms post
* only here;
* - HTTPS only, for two years, subdomains included;
* - no referrer leaves the site, so a private URL never lands in someone
* else's logs;
* - camera, microphone, location and payment APIs are off for every frame.
* WebAuthn (passkeys) is deliberately left on.
*
* The Loki feedback widget is the one third party, allowed only when this
* deployment turns the widget on. Loosening anything here needs a reason
* written next to the change.
*/
export const LOKI_ORIGIN = "https://loki.orangecat.ch";
export function contentSecurityPolicy(
nonce: string,
{ dev = false, widget = false }: { dev?: boolean; widget?: boolean } = {},
): string {
if (!/^[A-Za-z0-9+/=_-]{16,}$/.test(nonce)) throw new Error("CSP nonce is malformed");
const extra = widget ? ` ${LOKI_ORIGIN}` : "";
return [
"default-src 'self'",
// `unsafe-eval` only in development, where React needs it for its stacks.
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'${dev ? " 'unsafe-eval'" : ""}`,
// Next sets a few inline style attributes; styles cannot run code, and
// blocking them would only break layout.
"style-src 'self' 'unsafe-inline'",
"img-src 'self' blob: data:",
"font-src 'self'",
`connect-src 'self'${extra}${dev ? " ws:" : ""}`,
widget ? `frame-src ${LOKI_ORIGIN}` : "frame-src 'none'",
"worker-src 'self' blob:",
"manifest-src 'self'",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
"frame-ancestors 'none'",
"upgrade-insecure-requests",
].join("; ");
}
/** Headers for every response, pages and API alike. Read by next.config.ts. */
export const SECURITY_HEADERS: { key: string; value: string }[] = [
{ key: "Strict-Transport-Security", value: "max-age=63072000; includeSubDomains; preload" },
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "Referrer-Policy", value: "no-referrer" },
{
key: "Permissions-Policy",
value:
"camera=(), microphone=(), geolocation=(), payment=(), usb=(), serial=(), bluetooth=(), hid=(), interest-cohort=(), browsing-topics=()",
},
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
{ key: "Cross-Origin-Resource-Policy", value: "same-origin" },
{ key: "X-DNS-Prefetch-Control", value: "off" },
];
/** A fresh, unguessable nonce per response. */
export function newNonce(): string {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
return btoa(String.fromCharCode(...bytes));
}