Skip to content

fix(security): send clickjacking, HSTS and nosniff headers on every response - #90

Open
lorenzocorallo wants to merge 1 commit into
mainfrom
fix/security-headers
Open

lorenzocorallo wants to merge 1 commit into
mainfrom
fix/security-headers

Conversation

@lorenzocorallo

Copy link
Copy Markdown
Member

What was missing

The app sent none of the usual security headers. curl -sI https://auth.polinetwork.org/ shows no X-Frame-Options, no CSP frame-ancestors, no Strict-Transport-Security, no X-Content-Type-Options and no Referrer-Policy.

What an attacker could do

The session cookie is SameSite=Lax. A page on any other *.polinetwork.org subdomain counts as the same site, so it could load our pages in a hidden frame with the person still signed in. It could then trick them into clicking on:

  • /consent, which can give an MCP agent the person's rights, admin rights included;
  • /logout;
  • the admin pages.

What this PR sends now

Every response gets:

  • X-Frame-Options: DENY
  • Content-Security-Policy: frame-ancestors 'none'; base-uri 'self'; object-src 'none'
  • X-Content-Type-Options: nosniff
  • Referrer-Policy: strict-origin-when-cross-origin
  • Strict-Transport-Security: max-age=31536000; includeSubDomains, only when BETTER_AUTH_URL is https. Local development runs on http, so it does not get it. No preload.

If a response already has one of these headers, it is left as it is. Cache-Control and the CORS headers on .well-known do not change.

The list lives in one small function, src/lib/security-headers.ts. Two places use it:

  • A request middleware in src/start.ts. It runs first, so it covers pages, server functions and server routes (/api/auth/*, /api/mcp, /.well-known/*). If a response's headers cannot be changed, it adds them to a copy of the response.
  • A Nitro plugin, src/nitro/security-headers.ts. Files from public/ (/assets/*, /favicon.ico) and Nitro's own error pages never reach the middleware, so the plugin covers them.

What the CSP leaves out, on purpose

  • No form-action. The sign-out form posts and is then redirected to the application's own address. form-action would block that redirect.
  • No script-src or default-src. The page has an inline theme script and hydration scripts, which these would block.

How it was tested

  • Unit tests for the header function: https vs http origin, an existing CSP is kept, other headers are not touched, and a response with headers that cannot change gets copied.
  • Built with vp run build and ran the production server against a local Postgres. Checked with curl that these all carry the headers: /, /consent, /logout (307), /users (401), /api/auth/get-session, /api/mcp (401), /.well-known/oauth-authorization-server (CORS and Cache-Control unchanged), a 404 page, a refused server function call (403), /favicon.ico, a built JS file and a missing asset.
  • Ran it again with an https BETTER_AUTH_URL: HSTS shows up. With http it does not.
  • The sign-in page still renders, in the build and in vp dev.
  • In vp dev, Vite serves /favicon.ico itself, so it has no headers there. This only happens in development.
  • vp check passes. vp test passes: 372 tests ran with a local database, including the database integration tests. 117 were skipped because they need a running app and OAuth client settings.
  • Not checked: the real deployment behind the proxy. After deploy, run curl -sI https://auth.polinetwork.org/ to confirm.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 58 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9fe232e and 95f800c.

Files selected for processing: 5

  • src/lib/security-headers.test.ts
  • src/lib/security-headers.ts
  • src/nitro/security-headers.ts
  • src/start.ts
  • vite.config.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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