Files
CatoandClaude 131bac3475 templates/app: the one starter every new product is created from (#165)
A complete, tested Next.js 16 app — next-intl (en/de/fr, localePrefix
"always", parity test, Swiss spelling), better-auth accounts on Drizzle + pg
(email+password with verification, magic link via mail-kit, passkeys, TOTP
2FA, the shared bitbaum sign-in and OrangeCat as env-gated OIDC with
client_secret_post + PKCE, roles in the app's own users table), nonce CSP with
strict-dynamic and connect-src 'self', limitkit on every auth path keyed on the
proxy's hop, secrets sealed with ai-kit seal, ai-kit complete() example route,
own-model settings, bip-kit roadmap/changelog, /api/health asserting the app
can read its own tables, and CI/deploy/live-check/CodeQL/gitleaks/audit
workflows.

scripts/templates/render-app.sh fills the placeholders;
scripts/templates/test-app-template.sh holds the render to the fleet's own
judges (version-currency gapsFor, verify-floor predicates, cicd-hygiene audit)
on every PR, and the new app-template CI job installs it, runs verify and
drives sign-up/sign-in against a real Postgres.

STACK.md records the owner's 2026-10-11 sign-in decision: every app owns its
accounts and offers every method; it supersedes the 2026-09-11 federate-only
rule. SHARED.md and README point new products at templates/app. The
duplication ratchet skips the starter itself (the one canonical copy).


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

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 12:49:22 -07:00

82 lines
3.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { contentSecurityPolicy, newNonce, SECURITY_HEADERS } from "./security";
const directives = (csp: string) =>
new Map(
csp.split(";").map((part) => {
const [name, ...values] = part.trim().split(/\s+/);
return [name, values.join(" ")] as const;
}),
);
describe("contentSecurityPolicy", () => {
const nonce = newNonce();
const csp = directives(contentSecurityPolicy(nonce));
it("runs only scripts carrying this response's nonce", () => {
expect(csp.get("script-src")).toBe(`'self' 'nonce-${nonce}' 'strict-dynamic'`);
expect(csp.get("script-src")).not.toContain("unsafe-inline");
expect(csp.get("script-src")).not.toContain("unsafe-eval");
});
it("sends data only to this origin", () => {
expect(csp.get("connect-src")).toBe("'self'");
});
it("cannot be framed, embeds nothing, posts forms only here", () => {
expect(csp.get("frame-ancestors")).toBe("'none'");
expect(csp.get("frame-src")).toBe("'none'");
expect(csp.get("object-src")).toBe("'none'");
expect(csp.get("base-uri")).toBe("'self'");
expect(csp.get("form-action")).toBe("'self'");
expect(csp.get("default-src")).toBe("'self'");
});
it("opens only Loki, and only when the widget is on", () => {
const withWidget = directives(contentSecurityPolicy(nonce, { widget: true }));
expect(withWidget.get("connect-src")).toBe("'self' https://loki.orangecat.ch");
expect(withWidget.get("frame-src")).toBe("https://loki.orangecat.ch");
});
it("allows eval only in development", () => {
const dev = directives(contentSecurityPolicy(nonce, { dev: true }));
expect(dev.get("script-src")).toContain("'unsafe-eval'");
});
it("refuses a nonce that could break out of the header", () => {
expect(() => contentSecurityPolicy("abc'; script-src *")).toThrow();
expect(() => contentSecurityPolicy("")).toThrow();
});
});
describe("newNonce", () => {
it("is fresh every time", () => {
const seen = new Set(Array.from({ length: 50 }, newNonce));
expect(seen.size).toBe(50);
});
});
describe("SECURITY_HEADERS", () => {
const headers = new Map(SECURITY_HEADERS.map(({ key, value }) => [key.toLowerCase(), value]));
it("pins HTTPS for two years", () => {
expect(headers.get("strict-transport-security")).toMatch(/max-age=63072000/);
expect(headers.get("strict-transport-security")).toMatch(/includeSubDomains/);
});
it("sends no referrer and refuses framing and sniffing", () => {
expect(headers.get("referrer-policy")).toBe("no-referrer");
expect(headers.get("x-frame-options")).toBe("DENY");
expect(headers.get("x-content-type-options")).toBe("nosniff");
});
it("switches off camera, microphone, location and payment", () => {
const policy = headers.get("permissions-policy") ?? "";
for (const feature of ["camera", "microphone", "geolocation", "payment"]) {
expect(policy).toContain(`${feature}=()`);
}
// Passkeys must keep working.
expect(policy).not.toContain("publickey-credentials");
});
});