feat(portal-bff): helmet + env-driven CORS allowlist + double-submit CSRF
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)
This commit is contained in:
@@ -7,8 +7,10 @@ import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/
|
||||
import { provideRouter } from '@angular/router';
|
||||
import {
|
||||
AUTH_BFF_BASE_URL,
|
||||
AUTH_CSRF_COOKIE_NAME,
|
||||
bffCredentialsInterceptor,
|
||||
bffUnauthorizedInterceptor,
|
||||
csrfInterceptor,
|
||||
} from 'feature-auth';
|
||||
import { environment } from '../environments/environment';
|
||||
import { appRoutes } from './app.routes';
|
||||
@@ -33,13 +35,18 @@ export const appConfig: ApplicationConfig = {
|
||||
// when a BFF route (other than `/auth/me` itself) answers
|
||||
// 401, keeping the SPA's auth state in sync after server-
|
||||
// side session destruction (absolute-timeout, manual revoke).
|
||||
// - `csrfInterceptor` reads the BFF's CSRF cookie and copies
|
||||
// the value into the `X-CSRF-Token` header on mutating BFF
|
||||
// requests (POST / PUT / PATCH / DELETE), per the
|
||||
// double-submit pattern from ADR-0009.
|
||||
provideHttpClient(
|
||||
withFetch(),
|
||||
withInterceptors([bffCredentialsInterceptor, bffUnauthorizedInterceptor]),
|
||||
withInterceptors([bffCredentialsInterceptor, csrfInterceptor, bffUnauthorizedInterceptor]),
|
||||
),
|
||||
// `feature-auth` is environment-agnostic and reads the BFF base
|
||||
// URL from this token — provided once per app from `environment.ts`
|
||||
// (per ADR-0018).
|
||||
// `feature-auth` is environment-agnostic and reads its BFF
|
||||
// config from these tokens — provided once per app from
|
||||
// `environment.ts` (per ADR-0018).
|
||||
{ provide: AUTH_BFF_BASE_URL, useValue: environment.bffApiBaseUrl },
|
||||
{ provide: AUTH_CSRF_COOKIE_NAME, useValue: environment.bffCsrfCookieName },
|
||||
],
|
||||
};
|
||||
|
||||
@@ -25,6 +25,15 @@ export const environment = {
|
||||
*/
|
||||
bffApiBaseUrl: 'http://localhost:3000/api',
|
||||
|
||||
/**
|
||||
* Name of the BFF's CSRF cookie. Mirrors the BFF's
|
||||
* `csrfCookieName()` convention: `__Host-portal_csrf` in
|
||||
* production (Secure-required, hence excluded from HTTP dev),
|
||||
* `portal_csrf` otherwise. Read by the `csrfInterceptor` to
|
||||
* echo the value back in the `X-CSRF-Token` request header.
|
||||
*/
|
||||
bffCsrfCookieName: 'portal_csrf',
|
||||
|
||||
/**
|
||||
* OTLP/HTTP traces endpoint. Targets the Collector in
|
||||
* `infra/local/dev.compose.yml`. CORS is enabled there — see
|
||||
|
||||
Reference in New Issue
Block a user