
# Setting the browser security headers and the CSP

The application sends security headers by itself on every response, with nothing to set on your side.

## Browser security headers

`SecurityHeadersFilter` adds to **every** response, static SPA files included:

| Header | Value | Why here |
| --- | --- | --- |
| `Content-Security-Policy` | `planning.securite.csp` (see below) | The SPA loads no third-party script except the Cloudflare Web Analytics tag, and only when its token is set: anything injected is refused at run time |
| `Referrer-Policy` | `no-referrer` | The staff-space, ICS-subscription **and** wall-display tokens travel **in the URL**; without this header they would leave in the `Referer` of each outgoing navigation (OpenStreetMap, repository). One exception for the tiles, below |
| `X-Frame-Options` | `DENY` | Nothing is meant to be framed, and hijacking a click in a session that can rewrite the whole schedule has no upside |
| `Cross-Origin-Opener-Policy` | `same-origin` | Isolates the window from any `window.opener` opened from another site |
| `X-Content-Type-Options` | `nosniff` | Exports (PDF, ICS, SQL, CSV) are served with their type |
| `Permissions-Policy` | `geolocation=(), camera=(), microphone=(), payment=()` | None of these APIs is used |
| `Strict-Transport-Security` | `planning.securite.hsts`, **only on an HTTPS visit** | One year, subdomains included. Absent on the local stack, in clear |
| `X-Robots-Tag` | `noindex, nofollow` | The only URL a robot reaches without an account (a staff member's space) **is** their access token. Not adjustable: there is no page to index |

### Not being indexed, said three times

`robots.txt` (copied from `src/main/webui/public/`) forbids everything to every robot, a `<meta name="robots">` tag in `index.html` repeats it in the page, the header above on **every response**. The three are not redundant:

- the **file** is an instruction a robot may ignore, and silent about an already-known URL;
- the **tag** exists only once the page is read as HTML: not an exported PDF, an ICS feed or an API response;
- the **header** is the only one that *de-indexes* an address already reached, the case that matters: a space link travels by e-mail, and pasting it once on a public page is enough.

### `script-src` forbids any `on*=""`

No `'unsafe-inline'`, no `'unsafe-hashes'`: the browser refuses **any `on*=""` attribute**. An Angular optimisation then becomes silently destructive: when the CSP blocks that handler, **the stylesheet stays on `print`**, nothing raises an error and everything extraction did not keep disappears (an icon font, for instance). `inlineCritical` is therefore **disabled**, and `scripts/check-csp-index.js` fails the build if the produced `index.html` contains a single inline handler.

Three deliberate exceptions:

- **the CSP stops at the threshold of `/q/*`**: Swagger UI serves inline scripts there that the project does not control; the other headers apply;
- **HSTS is sent only on an HTTPS visit**: a browser would ignore it in clear, and setting it locally would pin `localhost` to https;
- **map tiles escape `no-referrer`**: `tile.openstreetmap.org` [blocks traffic without a `Referer`](https://osm.wiki/blocked) and the map would stay grey. `MapPicker` sets `referrerPolicy: 'strict-origin-when-cross-origin'` on its tile layer alone; the token does not leak, this policy sending only the origin.

### A single origin

The CORS filter is on and names **no** third-party origin: a request whose `Origin` header is not the application's address is refused with 403, on `/mcp`, `/api` and the sign-in. The application's own pages pass, and a client with no `Origin` (a non-browser MCP client, a subscribed calendar, `curl`) is not concerned. This is the `Origin` validation the MCP specification asks for; it does not protect against *DNS rebinding*, needless here since `/mcp` requires a key or token in a header that a booby-trapped page does not have.

"The same origin" is read from the request's scheme and host, so behind the reverse proxy from `X-Forwarded-Proto` and `Host`, which the admin sign-in already depends on. A proxy that rewrites `Host` needs `QUARKUS_HTTP_PROXY_ENABLE_FORWARDED_HOST=true`; an instance in https on a port other than 443 also needs `X-Forwarded-Port`.

No variable opens a third-party origin, on purpose: do not set `QUARKUS_HTTP_CORS_ORIGINS`, which Quarkus reads by itself, because opening it for an MCP client in a browser would open it to the whole admin API. `QUARKUS_HTTP_CORS_ENABLED=false` switches the filter off and restores 4.0.0 behaviour, while you fix a proxy that does not forward these headers.

### The notifications' service worker

A single file of the application, `/sw.js`, is a service worker: it receives a push notification, shows it, and a tap on it opens the space. It has **no `fetch` handler, no cache and no offline mode**, and is only registered on the gesture of a person turning notifications on from their space, on the `/animateur/` scope. The default CSP allows it without widening anything (`worker-src 'self' blob:`, and `default-src 'self'` for the shortcut's manifest and the icons). The file is served like `index.html`, revalidated at every visit, so a fix reaches it at once.

### Settings

| Variable | Default | Use |
| --- | --- | --- |
| `CSP` | policy above | Full policy. **Empty = header disabled** |
| `HSTS` | `max-age=31536000; includeSubDomains` | **Empty = header disabled**. Add `preload` only after deciding the domain will never serve in clear again: irreversible at browser scale |

The default CSP names only what the application contacts by itself:

| Directive | Third-party origins | When |
| --- | --- | --- |
| `script-src` | `https://static.cloudflareinsights.com` | only if `CLOUDFLARE_WEB_ANALYTICS_TOKEN` is set |
| `connect-src` | `https://cloudflareinsights.com` | only if `CLOUDFLARE_WEB_ANALYTICS_TOKEN` is set |
| `connect-src` | the origin of `SENTRY_DSN` (`https://bugsink.exemple.org`) | only if `SENTRY_DSN` is set |
| `connect-src` | `https://api.github.com` | always: the "new version available" hint, asked once the admin session is confirmed ([`versioning.md`](versioning.md)) |
| `img-src` | `https:` | always: OpenStreetMap tiles and the `BRANDING_IMAGES` logo (`logo` key), specific to each deployment |

The application adds the Cloudflare and Sentry origins at startup, from these variables, also to a `CSP` you write yourself, in the `script-src` and `connect-src` directives it carries (a policy without those directives stays as is). To switch off the new-version check, remove `https://api.github.com` from `connect-src`. `style-src` keeps `'unsafe-inline'`: Angular Material writes its styles into the page.

## Page caching

The browser must ask for the entry page again on each visit: it names the files of the version in place. `index.html` (at the root and on each interface address) and the files under a fixed name (translation catalogues, fonts, favicon, `robots.txt`) go out with `Cache-Control: no-cache`: the browser keeps its copy and revalidates it, one request answered `304` as long as nothing changed. Files whose name carries a fingerprint (`main-*.js`, `chunk-*.js`) keep their long cache.

Without this, an update, a security fix included, could leave the old interface in place for up to a day at a visitor who already came. If your reverse proxy adds its own cache headers, leave it those of the application on these addresses.
