Payload's colour-scheme client hints go to the admin only: withSecurityConfig outside withPayload

withPayload adds Accept-CH, Critical-CH and Vary: Sec-CH-Prefers-Color-Scheme for /:path*; Critical-CH makes
Chrome repeat the first document request of a visit to a public page. As the outer wrapper, withSecurityConfig
moves the three headers to /admin/:path* (adminClientHints: { route, sources } | false). Inside withPayload
nothing changes, so an unedited site behaves as before. Exports scopeColorSchemeClientHints,
adminClientHintSources, COLOR_SCHEME_CLIENT_HINT; doctor rule client-hints (info); reference site and starter
use the new order; e2e:built and the starter gate check it.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 11:55:26 +01:00
co-authored by Claude Fable 5.2
parent 2f6a81acbf
commit 9143c132a3
28 changed files with 500 additions and 59 deletions
+53 -5
View File
@@ -2,7 +2,8 @@
Generic HTTP security headers (HSTS, X-Frame-Options, nosniff,
Referrer-Policy, Permissions-Policy, Cross-Origin-Opener-Policy; `X-Robots-Tag` on staging)
as a function for `next.config`, `withSecurityConfig` to hide `X-Powered-By`, and an opt-in
as a function for `next.config`, `withSecurityConfig` to hide `X-Powered-By` and to keep Payload's colour-scheme
client hint headers on the admin panel only, and an opt-in
Content-Security-Policy (`buildCsp`, report-only first, a separate admin policy, optional nonces and a
report collector).
@@ -42,8 +43,9 @@ const nextConfig: NextConfig = {
},
}
// withSecurityConfig: no X-Powered-By (Next's and Payload's)
export default withPayload(withSecurityConfig(nextConfig))
// withSecurityConfig OUTSIDE withPayload: no X-Powered-By (Next's and Payload's), and Payload's colour-scheme
// client hints (Accept-CH, Critical-CH, Vary) for the admin only, not for every page
export default withSecurityConfig(withPayload(nextConfig))
```
## Options
@@ -83,8 +85,9 @@ advisories) to try. Audits flag it. Where it comes from:
Payload's REST/GraphQL handlers add nothing of their own.
`withSecurityConfig(nextConfig, { hidePoweredBy = true })` sets `poweredByHeader: false` AND drops any
`X-Powered-By` entry from `headers()` rules, so both orders work:
`withPayload(withSecurityConfig(nextConfig))` (recommended) or `withSecurityConfig(withPayload(nextConfig))`.
`X-Powered-By` entry from `headers()` rules, so both orders hide the header:
`withSecurityConfig(withPayload(nextConfig))` (recommended: the only order that also narrows the client hints,
next section) or `withPayload(withSecurityConfig(nextConfig))` (the order of sites made before 1.17.0).
Turn off: `withSecurityConfig(nextConfig, { hidePoweredBy: false })`, or do not call it. Without the helper,
`poweredByHeader: false` in the config passed to `withPayload` has the same effect.
@@ -95,6 +98,49 @@ Check: `curl -sI https://<DOMAIN>/pl | grep -i x-powered-by` prints nothing. Beh
Keep the software updated as well — hiding the header does not remove the advisories (the minimum safe
Next.js is listed in the agent guide, §4).
## Client hints — the admin only (`withSecurityConfig`)
**The problem.** Payload's `withPayload` adds three response headers for **every** path (`/:path*`):
`Accept-CH: Sec-CH-Prefers-Color-Scheme`, `Critical-CH: Sec-CH-Prefers-Color-Scheme` and
`Vary: Sec-CH-Prefers-Color-Scheme`. The admin panel needs them: with the hint in the request it renders light or
dark on the server, so the panel does not flash on its first paint. A public page does not read the hint, yet
`Critical-CH` tells Chrome to **repeat the first document request of a visit** with the hint added — one wasted
round trip on every first page view (two requests for `/pl` in the server log; about 600 ms in a mobile Lighthouse
run of a client site, where it shows as a repeated navigation under "Avoid multiple page redirects"). Route
handlers and static files also get `Vary: Sec-CH-Prefers-Color-Scheme`, which splits them in a shared cache.
Neither `withPayload` nor the proxy has an option for it, and a proxy cannot remove a header that `next.config` set.
**The fix.** `withSecurityConfig` as the **outer** wrapper moves the three headers to the panel's routes:
```ts
export default withSecurityConfig(withPayload(nextConfig)) // /admin/:path* only (the default)
```
| Option | Default | Role |
|---|---|---|
| `adminClientHints` | `true` | `true`: the hint headers only for `/admin/:path*`. `false`: leave them on every path (the behaviour before 1.17.0) |
| `adminClientHints.route` | `'/admin'` | Payload's `routes.admin` when the panel has another address: `{ route: '/its' }` |
| `adminClientHints.sources` | `[]` | more Next `source` patterns that keep the headers, e.g. `['/preview/:path*']` for a page of your own that reads `Sec-CH-Prefers-Color-Scheme` on the server |
- **The order matters.** Only the outer wrapper sees the rule `withPayload` adds. `withPayload(withSecurityConfig(…))`
— the order of every site made before 1.17.0 — still builds and still hides `X-Powered-By`, but the hints stay on
every path, as before. Swap the two names (one line); nothing else changes. `iapl-doctor` rule `client-hints`
(info) reports the old order, and with `--url` a public page that still sends `Critical-CH`.
- **What is moved.** Only the token `Sec-CH-Prefers-Color-Scheme` in `Accept-CH`, `Critical-CH` and `Vary` of a
site-wide rule (`/:path*`, `/(.*)`). Other tokens stay (another client hint, another `Vary` name), other headers
of the rule stay (CSP, HSTS, …), and a rule you wrote for a narrower source is left alone. `/admin/:path*` gets
the headers exactly as `withPayload` wrote them. Next's own `Vary` for pages (`rsc, next-router-state-tree, …`)
is not touched.
- **The panel works as before**: on its first request Chrome learns the hint from `/admin` and repeats that one
request, the panel paints in the browser's scheme with no flash; a theme chosen in the account (the
`payload-theme` cookie) still wins.
- **Building blocks:** `scopeColorSchemeClientHints(rules, sources?)` (the same narrowing for a `headers()` array
of your own), `adminClientHintSources(route?)`, `COLOR_SCHEME_CLIENT_HINT`.
Check (a production build): `curl -sI localhost:3000/pl | grep -iE 'accept-ch|critical-ch'` prints nothing;
`curl -sI localhost:3000/admin/login | grep -iE 'accept-ch|critical-ch'` prints both. In Chrome, a first visit to a
public page in a fresh profile shows one document request.
## Content-Security-Policy
### Why CSP is opt-in
@@ -1058,6 +1104,8 @@ the full text, as before. The components take the same as props
- **Turn off:** headers — do not call `buildSecurityHeaders`, or `null` / `false` per header (`frameOptions`,
`referrerPolicy`, `permissionsPolicy`, `coop`), `hsts: false`; CSP — leave out `csp` (the default);
X-Powered-By hiding — `withSecurityConfig(config, { hidePoweredBy: false })` or do not call it;
client hints for the admin only — `withSecurityConfig(config, { adminClientHints: false })` (hints on every path
again), `{ adminClientHints: { route, sources } }` (another panel address, more paths);
2FA — `twoFactor: false` (discouraged).
- **Override:** `additional` (the same key overrides the plugin's header and the `csp` ones),
`buildCsp({ extra, override, mode, strictDynamic })`, `buildAdminCsp({ extra, override })`,