Repository navigation
Lock down inline scripts and the visitor's address - #266
Open
carljmosca wants to merge 1 commit into
Open
carljmosca wants to merge 1 commit into
carljmosca wants to merge 1 commit into
Conversation
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>
|
@carljmosca is attempting to deploy a commit to the Basic Intelligence Team on Vercel. A member of the Team first needs to authorize it. |
[High impact] The PR appears safe to merge; no actionable issues were found. SummaryThis PR tightens script permissions and lets self-hosted installs choose which proxy header supplies the visitor’s address.
No actionable issues were found. Tests were not run during this review. Acknowledged behavior:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsmakes 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.tsxrenders per request. It was the only page built ahead of time, so its scripts would otherwise lack the nonce.style-srckeeps'unsafe-inline': React writesstyleattributes, which a nonce can't cover, and adding a nonce there would switch'unsafe-inline'off.'strict-dynamic', so thevercel.livehost 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 withCLIENT_IP_HEADER=x-forwarded-forto walk the list from its end past the install's own proxies.ipAddressHeaders,trustedProxies), and the app reads the header with Better Auth's owngetIPFromHeader, so sign-in rate limits, the app's rate limits and the audit trail all name the same visitor.@better-auth/corebecomes a direct dependency at the version already locked (a one-line lockfile change).ipv6Subnet: 128). They are written in expanded form, e.g.2001:0db8:….CLIENT_IP_HEADER. The settings are documented indocs/self-hosting.mdand.env.example.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.<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 insrc/lib/security/request.test.tspass. 13 tests in the update-workflow suite fail locally, the same 13 as onmain.npm run typecheck,npm run lintand Prettier are clean on the changed files.🤖 Generated with Claude Code
Note
Add per-request CSP nonces and configurable trusted-proxy client IP resolution
unsafe-inlinefor scripts in the CSP built bybuildContentSecurityPolicywith 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.CLIENT_IP_HEADERandTRUSTED_PROXIESsettings, read byreadClientIpSettingsin 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.@better-auth/coreas a dependency.src/app/not-found.tsxis now async and awaits the Next.js connection per request.<script>tags without the per-request nonce will be blocked by CSP.style-srcstill allowsunsafe-inline.Macroscope summarized d268f0e.