Skip to content

Lock down inline scripts and the visitor's address - #266

Open
carljmosca wants to merge 1 commit into
NYLLON-SOFTWARE:mainfrom
carljmosca:security/csp-nonce-trusted-proxy-upstream
Open

carljmosca wants to merge 1 commit into
NYLLON-SOFTWARE:mainfrom
carljmosca:security/csp-nonce-trusted-proxy-upstream

Conversation

@carljmosca

@carljmosca carljmosca commented Nov 24, 2026 •

Copy link
Copy Markdown

Two hardening changes, in the Content-Security-Policy and in how a self-hosted install learns the visitor's address.

Inline scripts need a nonce

The production policy allowed script-src 'unsafe-inline', so it gave no protection against an injected inline script.

  • src/proxy.ts makes a random nonce for each request and uses 'nonce-…' in place of 'unsafe-inline'. The policy is also set on the request, which is where Next.js reads the nonce and adds it to the scripts it writes (guide).
  • src/app/not-found.tsx renders per request. It was the only page built ahead of time, so its scripts would otherwise lack the nonce.
  • style-src keeps 'unsafe-inline': React writes style attributes, which a nonce can't cover, and adding a nonce there would switch 'unsafe-inline' off.
  • There is no 'strict-dynamic', so the vercel.live host allowed on preview deployments still works. The scripts Next.js and BotID load come from 'self'.

Say which header carries the visitor's address

Away from Vercel, the app now lets an install name the header its own reverse proxy writes, rather than guessing from several.

  • CLIENT_IP_HEADER: the one header the install's proxy writes the visitor's address to. Once it is set, no other header is read.
  • TRUSTED_PROXIES (addresses or CIDR ranges): used with CLIENT_IP_HEADER=x-forwarded-for to walk the list from its end past the install's own proxies.
  • Both are passed to Better Auth (ipAddressHeaders, trustedProxies), and the app reads the header with Better Auth's own getIPFromHeader, so sign-in rate limits, the app's rate limits and the audit trail all name the same visitor. @better-auth/core becomes a direct dependency at the version already locked (a one-line lockfile change).
  • IPv6 addresses are kept whole for the audit trail (ipv6Subnet: 128). They are written in expanded form, e.g. 2001:0db8:….
  • A value that can't be right (a malformed header name, or a proxy entry that isn't an address or range) stops the app with a message.
  • Installs that set neither behave as before and log a warning once in production. A release note should ask self-hosters to set CLIENT_IP_HEADER. The settings are documented in docs/self-hosting.md and .env.example.
  • Vercel deployments are unchanged.

Testing

  • npm run test:e2e: 24/24 pass in Chromium against a production build, with no CSP violations. This covers embedded signing framed from another site, uploads with the pdf.js worker, and signing through the provider.
  • Checked a production build with curl: every <script> on the pages checked carries the request's nonce, and the nonce changes on each request.
  • npm run test:db: 410/410 pass.
  • npm test: the new tests in src/lib/security/request.test.ts pass. 13 tests in the update-workflow suite fail locally, the same 13 as on main.
  • npm run typecheck, npm run lint and Prettier are clean on the changed files.

🤖 Generated with Claude Code

Note

Add per-request CSP nonces and configurable trusted-proxy client IP resolution

  • Replaces unsafe-inline for scripts in the CSP built by buildContentSecurityPolicy with a random 16-byte nonce generated per request in src/proxy.ts. The nonce-bearing CSP is set on both the forwarded request and the response.
  • Adds CLIENT_IP_HEADER and TRUSTED_PROXIES settings, read by readClientIpSettings in src/lib/security/request.ts. Self-hosted installs resolve the visitor address from the configured header only, using Better Auth's IP helper with the trusted proxy list; Vercel keeps its own address headers and ignores these settings.
  • Invalid header names or proxy entries throw at startup; proxy entries without a configured header are rejected. Unconfigured self-hosted installs keep the existing fallback headers and log a one-time production warning.
  • Documents both settings in docs/self-hosting.md and .env.example, and adds @better-auth/core as a dependency. src/app/not-found.tsx is now async and awaits the Next.js connection per request.
  • Behavioral Change: self-hosted installs with a configured header now ignore all other fallback IP headers, and inline <script> tags without the per-request nonce will be blocked by CSP. style-src still allows unsafe-inline.

Macroscope summarized d268f0e.

Scripts: the Content-Security-Policy no longer allows inline scripts. The
proxy makes a nonce for each request and passes the policy on the
request, so Next.js puts it on the scripts it writes; anything else
inline is refused. The not-found page renders per request so its scripts
carry the nonce too. Styles keep 'unsafe-inline' for React's style
attributes.

Address: away from Vercel, CLIENT_IP_HEADER names the one header the
install's reverse proxy writes, and TRUSTED_PROXIES the proxies an
X-Forwarded-For list is walked past. The app and Better Auth read the
same settings with the same parser, so the address that rate limits
count and the audit trail records is the one the install's proxy saw. A
setting that cannot be right stops the app; left unset, the old reading
stays and a warning is logged once in production.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown

@carljmosca is attempting to deploy a commit to the Basic Intelligence Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge; no actionable issues were found.

Summary

This PR tightens script permissions and lets self-hosted installs choose which proxy header supplies the visitor’s address.

  • Pages allow inline scripts only when they carry a request nonce.
  • Self-hosted installs can choose the header that identifies each visitor.

No actionable issues were found. Tests were not run during this review.

Acknowledged behavior:

  • carljmosca explicitly intends and documents configuration errors stopping requests.
  • carljmosca explicitly preserves unconfigured self-hosted installs trusting visitor-supplied headers and adds a production warning.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Request[Incoming request] --> Proxy[Proxy creates fresh nonce]
  Proxy --> RequestPolicy[Request policy]
  RequestPolicy --> Next[Next.js renders scripts with nonce]
  Proxy --> ResponsePolicy[Matching response policy]
  Next --> Browser[Browser checks script permissions]
  ResponsePolicy --> Browser
  Config[Client address settings] --> App[App limits and audit trail]
  Config --> Auth[Better Auth]
Loading

Reviews (1) · Last reviewed commit: "Lock down inline scripts and the visitor..." · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant