mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem Several auth callback and email confirmation examples redirect to the `next` query parameter without checking it. A reader who copies one gets an open redirect: anyone can send a sign-in link with `next` set to another site, and the app sends the signed-in user there. The affected examples are the SvelteKit, Astro, Remix, and Express tabs of the PKCE flow partial, which all social sign-in pages include, and the Next.js, Astro, Remix, and Express email confirmation handlers in the password guide. The SvelteKit and Express tabs had a partial check, but `//evil.com` and `/\evil.com` still got through. The SvelteKit confirmation tabs and the Next.js reset tab in the password guide copy `next` into the URL path. That keeps the user on the same site, but turns a full URL into a broken path. The example apps under `examples/` have the same problem. The Next.js email confirmation route in `examples/auth/nextjs-full` has the same open redirect. Two SvelteKit example apps copy `next` into the URL path, which keeps the user on the same site but turns a full URL into a broken path. Resolves [DOCS-1836](https://linear.app/supabase/issue/DOCS-1836/make-the-auth-callback-examples-redirect-only-within-the-app), [DOCS-1840](https://linear.app/supabase/issue/DOCS-1840/make-the-auth-examples-under-examples-redirect-only-within-the-app), and [DOCS-1844](https://linear.app/supabase/issue/DOCS-1844/handle-full-url-and-query-string-next-values-in-the-confirmation). ## Solution Each example now reads `next` as a URL against the request's own origin. It keeps `next` only when the result uses `http` or `https`, has the request's host, and its path doesn't start with `//`, then redirects to that path. Anything else falls back to `/`. The examples accept same-site full URLs as well as paths. The signup email template on the password page sends `next={{ .RedirectTo }}`, which is always a full URL, so a path-only check would send every new user to `/`. #51567 proposed a path-only check for the same examples, and this PR builds on it with full-URL support. The redirect itself stays relative. An absolute URL built from the request origin can point to an internal host when the app runs behind a proxy. `URL.parse()` returns `null` for malformed input, so a bad `next` value or `Host` header falls back to `/` instead of throwing. Each check compares the host of `next` with the request's host. Behind a proxy that terminates HTTPS, Express reports the request as `http`, so comparing full origins would send every full-URL `next` to `/`. The redirect carries only the path, so the scheme doesn't affect where the user lands. Other schemes are rejected, because a URL such as `custom://example.com/\evil.com` keeps the backslash in its path, and browsers read `/\evil.com` as another site. The Express examples also read `next` only when it's a string, because the Express 4 query parser can return an object. The Next.js tab of the PKCE partial already stays on the same site, so it's unchanged. The SvelteKit confirmation tabs and the Next.js reset tab in the password guide get the same check as the other tabs. On success they redirect to the checked value. On failure, the SvelteKit tabs redirect to a fixed error page. The Next.js reset tab now matches the signup tab, so its redirect no longer carries `token_hash` and `type`. The email confirmation routes in three example apps get the same check: `examples/auth/nextjs-full`, `examples/auth/sveltekit-full`, and `examples/user-management/sveltekit-user-management`. The user management example falls back to `/account`, the page its tutorial sends users to. In the SvelteKit routes, a successful confirmation redirects straight to the checked value, and a failed one redirects to a fixed error page. Other query parameters on the confirmation link no longer carry over. The Next.js example pins TypeScript 5.9.3, because TypeScript 5.3 has no type for `URL.parse` and `next build` failed its type check. The SvelteKit tutorial embeds the user management route, so the tutorial shows the new code. ## Verification (`/test-the-docs`) Each handler was extracted by script from the MDX on this branch, on `master`, and on #51567, then mounted in a minimal app for its framework. Each app ran against a local Supabase stack in the Compose sandbox. Confirm handlers got a fresh `generateLink` token hash per case. PKCE callbacks got a real code from a PKCE magic-link sign-in through Mailpit, with the code verifier cookie. Each route ran 12 `next` cases: none, `/welcome`, `/account?tab=1`, a same-site full URL, `//evil.com`, `/\evil.com`, `https://evil.com/x`, `javascript:alert(1)`, `http://`, `/<tab>/evil.com`, `/.//evil.com`, and `/a/..//evil.com`. The same-site full URL uses the origin each server reports for itself. | Example | Framework | This branch | `master` | | --- | --- | --- | --- | | PKCE partial, SvelteKit tab | SvelteKit 2.70 + adapter-node | pass 12/12 | 2 cases off-site | | PKCE partial, Astro tab | Astro 7.4 + @astrojs/node | pass 12/12 with `Astro.*` patched (see below) | 5 cases off-site | | PKCE partial, Remix tab | Remix 2.17 | pass 12/12 | 5 cases off-site | | PKCE partial, Express tab | Express 5.3 | pass 12/12 | 2 cases off-site | | Password signup, Next.js tab | Next.js 16.5 | pass 12/12 | 5 cases off-site | | Password signup and reset, Astro tabs | Astro 7.4 | pass 12/12 each | 5 cases off-site each | | Password signup and reset, Remix tabs | Remix 2.17 | pass 12/12 each | 5 cases off-site each | | Password signup and reset, Express tabs | Express 5.3 | pass 12/12 each | 2 cases off-site each | - `next build` type-checks the Next.js handler. `tsc --strict` reports the same errors for the Remix and Astro handlers as on `master`. - Express falls back to `/` on a malformed `Host` header without crashing. - #51567 also kept every route on the same site, and sent the same-site full URL case to `/`. - The unchanged examples stay on the same site in every case. - After review, each of the 14 changed blocks ran in Node 22 against 32 `next` values. The cases include Express behind an HTTPS proxy, an object `next` from the Express 4 query parser, and custom-scheme URLs with a backslash in the path. - The SvelteKit signup and reset tabs and the Next.js reset tab also ran end to end in Node 22, with `redirect` and `verifyOtp` stubbed. A same-site full URL lands on its path, `/account?tab=1` keeps its query, and the error page URL has no `token_hash` or `type`. On `master`, the same cases redirect to `/https://…`, to `/account%3Ftab=1`, and, in the Next.js reset tab, to a URL that still carries `token_hash`. ### Example apps Each example app was built and run in the Compose sandbox against a local Supabase stack. Every case used a fresh signup token, so each request ran a real `verifyOtp` and set a session cookie. The `sveltekit-full` example has no build setup of its own, so its route ran inside the user management app. Each route ran the 12 cases above, plus the same host on the other scheme and an `ftp://` URL on the same host, plus an invalid token to check the error redirect. | Example | Framework | This branch | `master` | | --- | --- | --- | --- | | `auth/nextjs-full` | Next.js 16.5 | pass 14/14, error path passes | 5 cases off-site | | `user-management/sveltekit-user-management` | SvelteKit 2.70 | pass 14/14, error path passes | on-site, but 9 of 12 cases land on a broken path | | `auth/sveltekit-full` | SvelteKit 2.70 | pass 14/14, error path passes | on-site, but 9 of 12 cases land on a broken path | - `next build` passes. With TypeScript 5.3.3, it fails with `Property 'parse' does not exist`. - `svelte-check` reports the same 7 errors on this branch and on `master`, all in the database types and the account form. None are in the confirm route. - Behind a proxy without forwarded-host handling, `next start` sees `localhost:3000`, so a full-URL `next` falls back to the default page there. ### Found while testing, not changed here These problems are on `master` too, and they're outside the scope of this fix: - PKCE partial, Astro tab: `Astro.request` and `Astro.cookies` don't exist in an API route, so the callback returns a 500 error. Tracked in [DOCS-1841](https://linear.app/supabase/issue/DOCS-1841/make-the-astro-oauth-callback-example-run-in-an-api-route). - Password guide, Remix confirm tabs: `setAll(key, value, options)` doesn't match the `@supabase/ssr` signature, so no session cookie is set after confirming. Tracked in [DOCS-1842](https://linear.app/supabase/issue/DOCS-1842/make-the-remix-email-confirmation-examples-set-the-session-cookie). - Password guide, Express confirm tabs: no `return` after the success redirect, so the error redirect also runs. Express 5 logs `ERR_HTTP_HEADERS_SENT`, and Express 4 exits the process. Tracked in [DOCS-1843](https://linear.app/supabase/issue/DOCS-1843/stop-the-express-email-confirmation-examples-from-redirecting-twice). - PKCE partial, Express tab: the callback ignores the result of `exchangeCodeForSession`, so a missing code or a failed exchange still redirects to `next`. Tracked in [DOCS-1845](https://linear.app/supabase/issue/DOCS-1845/send-failed-code-exchanges-to-the-error-page-in-the-express-oauth). - PKCE partial and password guide, Astro tabs: `setAll` drops the cache headers that `@supabase/ssr` passes with the session cookies. Tracked in [DOCS-1846](https://linear.app/supabase/issue/DOCS-1846/apply-the-cache-headers-from-supabasessr-in-the-astro-auth-callback). - Email templates guide, server-side endpoint example: it redirects to `redirect_to` without checking it. Tracked in [DOCS-1847](https://linear.app/supabase/issue/DOCS-1847/check-redirect-to-before-redirecting-in-the-server-side-email-template). - Next.js examples in the PKCE partial, the password guide, and the WorkOS and Google sign-in guides: they import the Supabase clients from `@/utils/supabase/`, but the client guide now creates them in `lib/supabase/`. Tracked in [DOCS-1848](https://linear.app/supabase/issue/DOCS-1848/import-the-nextjs-supabase-clients-from-libsupabase-in-the-auth). ## Review category (docs PRs only) - [ ] Critical: needs a review as soon as possible - [ ] Urgent: the feature ships soon, or it's blocking something else - [ ] Moderate: good to have soon, not urgent - [x] Low: get to it when you can ## Review instructions 1. Check that each changed example only touches the `next` handling and its redirect. 2. Check the changed tabs on the preview: - [GitHub sign-in page on the preview](https://docs-git-alexisa-docs-1836-supabase.vercel.app/docs/guides/auth/social-sign-in/auth-github): the callback code for the SvelteKit, Astro, Remix, and Express tabs. Every social sign-in page includes this partial. - [Password guide on the preview](https://docs-git-alexisa-docs-1836-supabase.vercel.app/docs/guides/auth/passwords): under PKCE flow, every framework tab for signup confirmation and password reset. - [SvelteKit tutorial on the preview](https://docs-git-alexisa-docs-1836-supabase.vercel.app/docs/guides/getting-started/tutorials/with-sveltekit#confirmation-endpoint): the confirmation endpoint embeds the user management example's route. 3. Check the three confirm routes under `examples/` and the `typescript` pin in `examples/auth/nextjs-full/package.json`. ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide). Not used: only code inside existing snippets and example apps changed, no prose. The snippets were verified with `/test-the-docs`.