Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: redirect path parsing (#41966)

Etienne Stalmansa4643c59202f
diff --git a/apps/www/app/api-v2/cms/disable-draft/route.ts b/apps/www/app/api-v2/cms/disable-draft/route.ts
index 9de21ecc4e1..2c1a494ec69 100644
--- a/apps/www/app/api-v2/cms/disable-draft/route.ts
+++ b/apps/www/app/api-v2/cms/disable-draft/route.ts
@@ -1,5 +1,6 @@
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
+import { SITE_ORIGIN } from '~/lib/constants'
export async function GET(request: Request) {
const draft = await draftMode()
@@ -7,9 +8,20 @@ export async function GET(request: Request) {
const slug = searchParams.get('slug')
const path = searchParams.get('path') || 'blog'
+ // Redirect to the path from the fetched post
+ const redir = slug ? `/${path}/${slug}` : `/${path}`
+
// Disable Draft Mode by clearing the cookie
draft.disable()
- // Redirect to the path from the fetched post
- redirect(slug ? `/${path}/${slug}` : `/${path}`)
+ try {
+ const parsed = new URL(redir, SITE_ORIGIN)
+ // Only allow paths that stay on the origin
+ if (parsed.origin === SITE_ORIGIN) {
+ redirect(parsed.pathname)
+ }
+ } catch {
+ // Invalid URL
+ }
+ redirect(SITE_ORIGIN)
}
diff --git a/apps/www/app/api-v2/cms/preview/route.ts b/apps/www/app/api-v2/cms/preview/route.ts
index 46b25c4576c..c0cb489e536 100644
--- a/apps/www/app/api-v2/cms/preview/route.ts
+++ b/apps/www/app/api-v2/cms/preview/route.ts
@@ -1,5 +1,6 @@
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
+import { SITE_ORIGIN } from '~/lib/constants'
export async function GET(request: Request) {
const draft = await draftMode()
@@ -31,5 +32,16 @@ export async function GET(request: Request) {
draft.enable()
// Redirect to the path from the fetched post
- redirect(`/${path}/${slug}`)
+ const redir = `/${path}/${slug}`
+
+ try {
+ const parsed = new URL(redir, SITE_ORIGIN)
+ // Only allow paths that stay on the origin
+ if (parsed.origin === SITE_ORIGIN) {
+ redirect(parsed.pathname)
+ }
+ } catch {
+ // Invalid URL
+ }
+ redirect(SITE_ORIGIN)
}