diff --git a/apps/docs/content/_partials/oauth_pkce_flow.mdx b/apps/docs/content/_partials/oauth_pkce_flow.mdx index a6ce865398b..f529a83e551 100644 --- a/apps/docs/content/_partials/oauth_pkce_flow.mdx +++ b/apps/docs/content/_partials/oauth_pkce_flow.mdx @@ -152,12 +152,20 @@ export const GET = async (event) => { locals: { supabase }, } = event const code = url.searchParams.get('code') - const next = url.searchParams.get('next') ?? '/' + // Keep "next" only when it points to this site + const target = URL.parse(url.searchParams.get('next') ?? '/', url.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === url.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (code) { const { error } = await supabase.auth.exchangeCodeForSession(code) if (!error) { - redirect(303, `/${next.slice(1)}`) + redirect(303, next) } } @@ -179,7 +187,15 @@ import { type APIRoute } from 'astro' export const GET: APIRoute = async ({ request, cookies, redirect }) => { const requestUrl = new URL(request.url) const code = requestUrl.searchParams.get('code') - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (code) { const supabase = createServerClient( @@ -224,7 +240,15 @@ import { createServerClient, parseCookieHeader, serializeCookieHeader } from '@s export async function loader({ request }: LoaderFunctionArgs) { const requestUrl = new URL(request.url) const code = requestUrl.searchParams.get('code') - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' const responseHeaders = new Headers() if (code) { @@ -267,7 +291,16 @@ Create a new route in your express app and populate with the following: ... app.get("/auth/callback", async function (req, res) { const code = req.query.code - const next = req.query.next ?? "/" + // Keep "next" only when it points to this site + const site = URL.parse(`${req.protocol}://${req.get("host")}`) + const target = site && URL.parse(typeof req.query.next === "string" ? req.query.next : "/", site) + const next = + target && + ["http:", "https:"].includes(target.protocol) && + target.host === site.host && + !target.pathname.startsWith("//") + ? `${target.pathname}${target.search}${target.hash}` + : "/" if (code) { const supabase = createServerClient( @@ -290,7 +323,7 @@ app.get("/auth/callback", async function (req, res) { await supabase.auth.exchangeCodeForSession(code) } - res.redirect(303, `/${next.slice(1)}`) + res.redirect(303, next) }) ``` diff --git a/apps/docs/content/guides/auth/passwords.mdx b/apps/docs/content/guides/auth/passwords.mdx index 25a52bb0a2b..f2503d591be 100644 --- a/apps/docs/content/guides/auth/passwords.mdx +++ b/apps/docs/content/guides/auth/passwords.mdx @@ -206,10 +206,18 @@ import { type NextRequest } from 'next/server' import { createClient } from '@/utils/supabase/server' export async function GET(request: NextRequest) { - const { searchParams } = new URL(request.url) + const { searchParams, origin, host } = new URL(request.url) const token_hash = searchParams.get('token_hash') const type = searchParams.get('type') as EmailOtpType | null - const next = searchParams.get('next') ?? '/' + // Keep "next" only when it points to this site + const target = URL.parse(searchParams.get('next') ?? '/', origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const supabase = await createClient() @@ -245,29 +253,25 @@ export const GET = async (event) => { } = event const token_hash = url.searchParams.get('token_hash') as string const type = url.searchParams.get('type') as EmailOtpType | null - const next = url.searchParams.get('next') ?? '/' - - /** - * Clean up the redirect URL by deleting the Auth flow parameters. - * - * `next` is preserved for now, because it's needed in the error case. - */ - const redirectTo = new URL(url) - redirectTo.pathname = next - redirectTo.searchParams.delete('token_hash') - redirectTo.searchParams.delete('type') + // Keep "next" only when it points to this site + const target = URL.parse(url.searchParams.get('next') ?? '/', url.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === url.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const { error } = await supabase.auth.verifyOtp({ token_hash, type }) if (!error) { - redirectTo.searchParams.delete('next') - redirect(303, redirectTo) + redirect(303, next) } } // return the user to an error page with some instructions - redirectTo.pathname = '/auth/error' - redirect(303, redirectTo) + redirect(303, '/auth/error') } ``` @@ -286,7 +290,15 @@ export const GET: APIRoute = async ({ request, cookies, redirect }) => { const requestUrl = new URL(request.url) const token_hash = requestUrl.searchParams.get('token_hash') const type = requestUrl.searchParams.get('type') as EmailOtpType | null - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const supabase = createServerClient( @@ -334,7 +346,15 @@ export async function loader({ request }: LoaderFunctionArgs) { const requestUrl = new URL(request.url) const token_hash = requestUrl.searchParams.get('token_hash') const type = requestUrl.searchParams.get('type') as EmailOtpType | null - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' const headers = new Headers() if (token_hash && type) { @@ -380,7 +400,16 @@ const { createClient } = require("./lib/supabase") app.get("/auth/confirm", async function (req, res) { const token_hash = req.query.token_hash const type = req.query.type - const next = req.query.next ?? "/" + // Keep "next" only when it points to this site + const site = URL.parse(`${req.protocol}://${req.get("host")}`) + const target = site && URL.parse(typeof req.query.next === "string" ? req.query.next : "/", site) + const next = + target && + ["http:", "https:"].includes(target.protocol) && + target.host === site.host && + !target.pathname.startsWith("//") + ? `${target.pathname}${target.search}${target.hash}` + : "/" if (token_hash && type) { const supabase = createClient({ req, res }) @@ -389,7 +418,7 @@ app.get("/auth/confirm", async function (req, res) { token_hash, }) if (!error) { - res.redirect(303, `/${next.slice(1)}`) + res.redirect(303, next) } } @@ -765,19 +794,25 @@ Create a new file at `app/auth/confirm/route.ts` and populate with the following ```ts app/auth/confirm/route.ts import { type EmailOtpType } from '@supabase/supabase-js' -import { cookies } from 'next/headers' -import { NextRequest, NextResponse } from 'next/server' +import { redirect } from 'next/navigation' +import { type NextRequest } from 'next/server' // The client you created from the Server-Side Auth instructions import { createClient } from '@/utils/supabase/server' export async function GET(request: NextRequest) { - const { searchParams } = new URL(request.url) + const { searchParams, origin, host } = new URL(request.url) const token_hash = searchParams.get('token_hash') const type = searchParams.get('type') as EmailOtpType | null - const next = searchParams.get('next') ?? '/' - const redirectTo = request.nextUrl.clone() - redirectTo.pathname = next + // Keep "next" only when it points to this site + const target = URL.parse(searchParams.get('next') ?? '/', origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const supabase = await createClient() @@ -787,13 +822,12 @@ export async function GET(request: NextRequest) { token_hash, }) if (!error) { - return NextResponse.redirect(redirectTo) + redirect(next) } } // return the user to an error page with some instructions - redirectTo.pathname = '/auth/auth-code-error' - return NextResponse.redirect(redirectTo) + redirect('/auth/auth-code-error') } ``` @@ -813,29 +847,25 @@ export const GET = async (event) => { } = event const token_hash = url.searchParams.get('token_hash') as string const type = url.searchParams.get('type') as EmailOtpType | null - const next = url.searchParams.get('next') ?? '/' - - /** - * Clean up the redirect URL by deleting the Auth flow parameters. - * - * `next` is preserved for now, because it's needed in the error case. - */ - const redirectTo = new URL(url) - redirectTo.pathname = next - redirectTo.searchParams.delete('token_hash') - redirectTo.searchParams.delete('type') + // Keep "next" only when it points to this site + const target = URL.parse(url.searchParams.get('next') ?? '/', url.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === url.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const { error } = await supabase.auth.verifyOtp({ token_hash, type }) if (!error) { - redirectTo.searchParams.delete('next') - redirect(303, redirectTo) + redirect(303, next) } } // return the user to an error page with some instructions - redirectTo.pathname = '/auth/error' - redirect(303, redirectTo) + redirect(303, '/auth/error') } ``` @@ -854,7 +884,15 @@ export const GET: APIRoute = async ({ request, cookies, redirect }) => { const requestUrl = new URL(request.url) const token_hash = requestUrl.searchParams.get('token_hash') const type = requestUrl.searchParams.get('type') as EmailOtpType | null - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const supabase = createServerClient( @@ -902,7 +940,15 @@ export async function loader({ request }: LoaderFunctionArgs) { const requestUrl = new URL(request.url) const token_hash = requestUrl.searchParams.get('token_hash') const type = requestUrl.searchParams.get('type') as EmailOtpType | null - const next = requestUrl.searchParams.get('next') || '/' + // Keep "next" only when it points to this site + const target = URL.parse(requestUrl.searchParams.get('next') || '/', requestUrl.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === requestUrl.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' const headers = new Headers() if (token_hash && type) { @@ -948,7 +994,16 @@ const { createClient } = require("./lib/supabase") app.get("/auth/confirm", async function (req, res) { const token_hash = req.query.token_hash const type = req.query.type - const next = req.query.next ?? "/" + // Keep "next" only when it points to this site + const site = URL.parse(`${req.protocol}://${req.get("host")}`) + const target = site && URL.parse(typeof req.query.next === "string" ? req.query.next : "/", site) + const next = + target && + ["http:", "https:"].includes(target.protocol) && + target.host === site.host && + !target.pathname.startsWith("//") + ? `${target.pathname}${target.search}${target.hash}` + : "/" if (token_hash && type) { const supabase = createClient({ req, res }) @@ -957,7 +1012,7 @@ app.get("/auth/confirm", async function (req, res) { token_hash, }) if (!error) { - res.redirect(303, `/${next.slice(1)}`) + res.redirect(303, next) } } diff --git a/examples/auth/nextjs-full/app/auth/confirm/route.ts b/examples/auth/nextjs-full/app/auth/confirm/route.ts index c3cc7d6c181..f559fa17051 100644 --- a/examples/auth/nextjs-full/app/auth/confirm/route.ts +++ b/examples/auth/nextjs-full/app/auth/confirm/route.ts @@ -4,10 +4,18 @@ import { createClient } from '@/lib/supabase/server' import { redirect } from 'next/navigation' export async function GET(request: NextRequest) { - const { searchParams } = new URL(request.url) + const { searchParams, origin, host } = new URL(request.url) const token_hash = searchParams.get('token_hash') const type = searchParams.get('type') as EmailOtpType | null - const next = searchParams.get('next') ?? '/' + // Keep "next" only when it points to this site + const target = URL.parse(searchParams.get('next') ?? '/', origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const supabase = await createClient() diff --git a/examples/auth/nextjs-full/package.json b/examples/auth/nextjs-full/package.json index 32cbba35173..d6f3ab0d363 100644 --- a/examples/auth/nextjs-full/package.json +++ b/examples/auth/nextjs-full/package.json @@ -15,7 +15,7 @@ "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.4.1", - "typescript": "5.3.3" + "typescript": "5.9.3" }, "devDependencies": { "@types/node": "20.11.5", diff --git a/examples/auth/sveltekit-full/src/routes/auth/confirm/+server.ts b/examples/auth/sveltekit-full/src/routes/auth/confirm/+server.ts index 209cb13fba1..c8870e8097c 100644 --- a/examples/auth/sveltekit-full/src/routes/auth/confirm/+server.ts +++ b/examples/auth/sveltekit-full/src/routes/auth/confirm/+server.ts @@ -6,26 +6,23 @@ import type { RequestHandler } from './$types' export const GET: RequestHandler = async ({ url, locals: { supabase } }) => { const token_hash = url.searchParams.get('token_hash') const type = url.searchParams.get('type') as EmailOtpType | null - const next = url.searchParams.get('next') ?? '/' - - /** - * Clean up the redirect URL by deleting the Auth flow parameters. - * - * `next` is preserved for now, because it's needed in the error case. - */ - const redirectTo = new URL(url) - redirectTo.pathname = next - redirectTo.searchParams.delete('token_hash') - redirectTo.searchParams.delete('type') + // Keep "next" only when it points to this site + const target = URL.parse(url.searchParams.get('next') ?? '/', url.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === url.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/' if (token_hash && type) { const { error } = await supabase.auth.verifyOtp({ type, token_hash }) if (!error) { - redirectTo.searchParams.delete('next') - redirect(303, redirectTo) + redirect(303, next) } } - redirectTo.pathname = '/auth/error' - redirect(303, redirectTo) + // return the user to an error page with some instructions + redirect(303, '/auth/error') } diff --git a/examples/user-management/sveltekit-user-management/src/routes/auth/confirm/+server.ts b/examples/user-management/sveltekit-user-management/src/routes/auth/confirm/+server.ts index 39d2d211e09..1f6bb394016 100644 --- a/examples/user-management/sveltekit-user-management/src/routes/auth/confirm/+server.ts +++ b/examples/user-management/sveltekit-user-management/src/routes/auth/confirm/+server.ts @@ -7,26 +7,23 @@ import type { RequestHandler } from './$types' export const GET: RequestHandler = async ({ url, locals: { supabase } }) => { const token_hash = url.searchParams.get('token_hash') const type = url.searchParams.get('type') as EmailOtpType | null - const next = url.searchParams.get('next') ?? '/account' - - /** - * Clean up the redirect URL by deleting the Auth flow parameters. - * - * `next` is preserved for now, because it's needed in the error case. - */ - const redirectTo = new URL(url) - redirectTo.pathname = next - redirectTo.searchParams.delete('token_hash') - redirectTo.searchParams.delete('type') + // Keep "next" only when it points to this site + const target = URL.parse(url.searchParams.get('next') ?? '/account', url.origin) + const next = + target && + ['http:', 'https:'].includes(target.protocol) && + target.host === url.host && + !target.pathname.startsWith('//') + ? `${target.pathname}${target.search}${target.hash}` + : '/account' if (token_hash && type) { const { error } = await supabase.auth.verifyOtp({ type, token_hash }) if (!error) { - redirectTo.searchParams.delete('next') - redirect(303, redirectTo) + redirect(303, next) } } - redirectTo.pathname = '/auth/error' - redirect(303, redirectTo) + // return the user to an error page with some instructions + redirect(303, '/auth/error') }