mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## Problem www sends no `Content-Security-Policy`, `X-Frame-Options`, `X-Content-Type-Options`, or `Referrer-Policy` headers on its pages. Only HSTS is set. The first version of this PR added these headers on `/(.*)`. That turned out to be unsafe, because www is the zone router: `lib/rewrites.js` proxies Studio (`/dashboard`), docs, the UI library, the design system, the knowledge base, and other apps, and the header rules in `next.config.mjs` also apply to those proxied responses. A CSP there would add a second enforced policy on top of each app's own headers, and `X-Frame-Options: DENY` would break the UI library previews, which iframe `/library/example/*`. ## Solution The headers are built in `apps/www/lib/security-headers.js` and spread into `headers()` in `next.config.mjs`. **Scope** - The rules apply only to paths that www serves itself. The proxied path prefixes come from `lib/rewrites.js`, so a new zone rewrite is excluded automatically. - On those paths: `X-Frame-Options: SAMEORIGIN`, `X-Content-Type-Options: nosniff`, `Referrer-Policy: strict-origin-when-cross-origin`, and an enforced CSP. **CSP (script-focused)** - `script-src` allows `'self'`, `'unsafe-inline'`, and an explicit list of the third-party script hosts that www pages load: GTM through Stape, PostHog, Usercentrics, tweet embeds, HubSpot, hCaptcha, Default.com, Buy with AWS, and a path-scoped jsDelivr entry for unicornstudio. Each entry has a comment that names its user. - `'unsafe-inline'` stays, because the Next.js inline scripts need a nonce otherwise, and a nonce needs dynamic rendering. - `object-src 'none'`, `base-uri 'self'`, `form-action 'self'`, `frame-ancestors 'self'`. - Removed from the earlier version: `unpkg.com` (only used inside a blog code block, and anyone can publish scripts there), `*.vercel.app` (any Vercel app), `frontend-assets.supabase.com` (www serves its assets from the same origin), and the duplicate `script-src-elem`. **`'unsafe-eval'`** - Off by default. - Allowed only on the pages that evaluate MDX on the client with `next-mdx-remote-client`: `/changelog`, `/customers/[slug]`, `/events/[slug]`, `/alternatives/[slug]`, and `/partners/catalog/[slug]`. Removing it there would break those pages. - Allowed in development, because Next.js needs it there. **Preview and staging** - Detection matches Studio's CSP: `NEXT_PUBLIC_VERCEL_ENV === 'preview'` or `NEXT_PUBLIC_ENVIRONMENT` of `local` or `staging`. It no longer depends on `NEXT_PUBLIC_IS_PLATFORM`. - On preview and staging, the Vercel toolbar and the staging PostHog host are added. - The PostHog origin also comes from `NEXT_PUBLIC_POSTHOG_HOST`, the same value the client uses. **Tests** `lib/security-headers.test.ts` covers the policy for each environment, the proxied prefixes from the real rewrites, and the header sources. The sources are compiled with the same path-to-regexp build and options that Next.js uses. ## Review instructions 1. Open the preview and accept cookie consent so GTM and PostHog load. 2. Open DevTools → Console and filter on `Content-Security-Policy`. 3. Visit these pages and confirm there are no violations: - home - a blog post with a tweet embed (`/blog/launch-week-5-community-day`) - `/changelog` - a `/customers/...` page - `/state-of-startups` - `/contact/sales` - `/aws-marketplace` - a `/go` page with a HubSpot form 4. Run `curl -sI <preview>/` and check the four headers. Run `curl -sI <preview>/changelog` and check that the CSP includes `'unsafe-eval'`. 5. Run `curl -sI <preview>/dashboard` and `curl -sI <preview>/library` and check that they have no www CSP or `X-Frame-Options`. 6. Confirm `/dashboard` and `/library` still load. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>