# Security headers (Console 2)

Set in `apps/admin-console/server/security-headers.js`, mounted in `server.js`
before routing so they apply to documents, assets and API proxy responses alike.

Express is the right layer here: Console 2 is served directly by its own Node
process (`node server.js`, Dockerfile `EXPOSE 3000`) with no nginx or CDN in
front of it. If an edge is introduced later, move these there and delete the
middleware rather than setting them twice — duplicate CSP headers are
intersected by browsers, which produces breakage that is hard to trace.

## What is sent

| Header | Value | Why |
| --- | --- | --- |
| `X-Frame-Options` | `DENY` | Clickjacking. Belt and braces with `frame-ancestors`, which older browsers ignore. |
| `Content-Security-Policy` | see below | Limits where scripts, styles and data may come from. |
| `Strict-Transport-Security` | `max-age=31536000; includeSubDomains` | HTTPS only — sending it over plain HTTP is meaningless and can lock out a local dev setup. |
| `X-Content-Type-Options` | `nosniff` | Stops a response being executed as a type it did not declare. |
| `Referrer-Policy` | `strict-origin-when-cross-origin` | Admin URLs contain member numbers and filters; do not leak them to third parties. |
| `Permissions-Policy` | camera, mic, geolocation etc. all `()` | The console needs none of them. |
| `Cross-Origin-Opener-Policy` | `same-origin` | Isolates the browsing context from openers. |
| `X-Permitted-Cross-Domain-Policies` | `none` | Legacy Flash/PDF cross-domain policy files. |

`preload` is deliberately absent from HSTS. Submitting to the preload list is
irreversible in practice and commits every subdomain of the registrable domain;
that is an infrastructure decision, not an application one.

## CSP rollout

The policy ships as `Content-Security-Policy-Report-Only` by default. Set
`CSP_ENFORCE=true` to switch to the enforcing header.

Report-only first because a wrong CSP breaks the page silently and completely.
Run it in report-only in staging, watch the console for violations, then enforce.

Current sources, each with a reason — anything not on this list is blocked:

- `script-src … https://www.gstatic.com` — `react-google-charts` loads the
  Google Charts loader from there.
- `style-src … https://fonts.googleapis.com`, `font-src … https://fonts.gstatic.com`
  — the webfonts linked in `root.tsx`.
- `img-src … https://cdn.jsdelivr.net` — country flag images in
  `utils/country.json`.
- `img-src … https://storage.karmagroup.com` — member and campaign assets in GCS.
- `img-src … https://placehold.co` — placeholder images.
- `connect-src … https://api.ipify.org` — client IP lookup.

### `unsafe-inline`

Both `script-src` and `style-src` currently allow it, which weakens the policy
against XSS. Two causes, both fixable but neither trivial:

- React Router's SSR hydration writes an inline bootstrap script.
- Mantine injects inline styles at runtime.

The fix is nonce-based CSP: generate a nonce per request, pass it to the
document via `entry.server.tsx`, and set `MantineProvider`'s emotion nonce.
Worth doing, and worth doing as its own change with its own testing rather than
alongside a header rollout.

## Not covered here

The API (`apps/core`) is a separate origin and sets its own headers. It was
checked at the same time: unauthenticated requests get 401 and no
`Access-Control-Allow-Origin` is returned, so cross-origin reads are already
blocked.
