116e0468b0c3a00c4b74fa0bf901430371c53661
phase-2 security baseline the main.ts placeholder has been
advertising since the auth track started. three independent
middlewares + their spa counterparts shipped together because
they only become meaningful as a set.
helmet on the bff:
helmet() with three overrides matching our specific shape:
- HSTS only in production (dev runs on plain http)
- crossOriginResourcePolicy: 'cross-origin' (SPA needs to read
bff json from a different origin)
- CSP disabled in non-prod (bff doesn't render html; default CSP
just adds devtools noise)
cors allowlist env-driven:
CORS_ALLOWED_ORIGINS is now mandatory at boot. readCorsAllowlist()
parses + validates (http(s) only, bare origins, no path/query),
same boot-time validator family as assertSessionSecret etc. the
previous hardcoded localhost fallback is removed — silent
cors misconfiguration is exactly the "works in dev breaks in
prod" trap this guard catches.
double-submit CSRF (session-bound):
- bff mints a 256-bit csrfToken at /auth/callback, stored on
req.session.csrfToken AND mirrored to a js-readable cookie
(__Host-portal_csrf prod / portal_csrf dev). cookie is the
spa's read-only view; session is the source of truth.
- createCsrfMiddleware compares X-CSRF-Token header to the
session token via crypto.timingSafeEqual. skips: safe methods,
anonymous requests, /auth/login + /auth/callback.
- mismatch → 403 {"error":"csrf"} + structured pino warn.
- spa csrfInterceptor reads the cookie via document.cookie and
copies into X-CSRF-Token on mutating bff requests. omitted on
GET/HEAD/OPTIONS and on non-bff origins.
- logout + absolute-timeout middleware now clear the csrf cookie
alongside the session cookie.
notable choices:
- session-bound double-submit, not pure cookie-vs-header. an
attacker who plants a cookie via subdomain takeover would
still need to know the server-side session token. tying the
check to req.session.csrfToken instead of the cookie itself
is what makes this stronger than the canonical recipe.
- csrfInterceptor sits between bffCredentialsInterceptor (which
sets withCredentials) and bffUnauthorizedInterceptor (which
handles 401s). forward order, no surprises.
- no CSRF for anonymous mutating routes in v1 — none exist today
and generating tokens for anonymous sessions conflicts with
express-session's saveUninitialized: false.
specs:
- 239 / 239 pass under the clean-env repro (env -u every config
var including the new CORS_ALLOWED_ORIGINS).
- +42 specs across check-cors-allowlist, csrf-cookie, csrf
middleware, csrf interceptor, and extended auth.controller /
absolute-timeout coverage for the csrf cookie mirror/clear.
out of scope, landing in follow-ups:
- rate limiting + structured error filter (remaining phase-2
todos)
- CSP fine-tuning for the portal-shell + portal-admin static
bundles
- csrf token rotation on idle-extension (current token lives
the session lifetime)
Documentation index
This is the entry point to all project documentation. It is maintained automatically: any addition, rename, or removal of a .md file under docs/ must be reflected here in the same change.
Conventions
- Documentation is written in English.
- One topic per file. Group related files into a folder when there are three or more.
- Cross-reference with relative links so they keep working in GitHub, IDEs, and exported sites.
- For architectural decisions, do not add them here — they belong in decisions/ as MADR 4.0.0 ADRs.
Sections
Daily development
- development.md — repo layout, prerequisites, initial setup, daily commands, observability dev-loop (Jaeger UI, log ↔ trace correlation), dependency updates (Renovate), conventional commit cycle. Day-to-day reference for working on the project.
Architecture
- architecture.md — cross-cutting Mermaid diagrams: C4 system context, C4 containers, Nx module boundaries, CI/CD pipeline. Single-decision diagrams (auth sequence, ERD, etc.) live inline in their ADR.
Onboarding & environment
Setup guides for new contributors:
- setup/01-wsl-terminal-setup.md — modern WSL terminal (Zsh + Powerlevel10k + CLI tools)
- setup/02-dev-web-stack.md — Node via nvm, pnpm via corepack, Docker
- setup/03-angular-nx-monorepo.md — Angular + Nx monorepo bootstrap
Operations & runbooks
Empty — to be populated when we deploy.
Security, performance, accessibility
Empty — placeholders to be filled with rationale docs alongside their corresponding ADRs.
Description
Languages
TypeScript
85.3%
JavaScript
5.4%
SCSS
4.3%
HTML
3.9%
Shell
0.8%
Other
0.3%