diff --git a/apps/web/src/app/(auth)/sign-in/[[...sign-in]]/components/SignInForm/SignInForm.tsx b/apps/web/src/app/(auth)/sign-in/[[...sign-in]]/components/SignInForm/SignInForm.tsx index 0b192e0b52b..3e10b6ad1ab 100644 --- a/apps/web/src/app/(auth)/sign-in/[[...sign-in]]/components/SignInForm/SignInForm.tsx +++ b/apps/web/src/app/(auth)/sign-in/[[...sign-in]]/components/SignInForm/SignInForm.tsx @@ -55,6 +55,7 @@ export function SignInForm({ redirect, webUrl: env.NEXT_PUBLIC_WEB_URL, trustedOrigins: [env.NEXT_PUBLIC_API_URL], + adminUrl: env.NEXT_PUBLIC_ADMIN_URL, }); const callbackError = diff --git a/apps/web/src/app/(auth)/sign-up/[[...sign-up]]/page.tsx b/apps/web/src/app/(auth)/sign-up/[[...sign-up]]/page.tsx index 706d79cdb0f..4ade1651492 100644 --- a/apps/web/src/app/(auth)/sign-up/[[...sign-up]]/page.tsx +++ b/apps/web/src/app/(auth)/sign-up/[[...sign-up]]/page.tsx @@ -19,6 +19,7 @@ export default function SignUpPage() { redirect: searchParams.get("redirect"), webUrl: env.NEXT_PUBLIC_WEB_URL, trustedOrigins: [env.NEXT_PUBLIC_API_URL], + adminUrl: env.NEXT_PUBLIC_ADMIN_URL, }); const [isLoadingGoogle, setIsLoadingGoogle] = useState(false); const [isLoadingGithub, setIsLoadingGithub] = useState(false); diff --git a/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.test.ts b/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.test.ts index ecb16af4e3b..78a5f8803f3 100644 --- a/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.test.ts +++ b/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.test.ts @@ -58,3 +58,37 @@ test.each([ }), ).toBe(webUrl); }); + +describe("the admin origin", () => { + const adminUrl = "https://admin.example.com"; + const resolveAdmin = (redirect: string) => + resolveCallbackUrl({ + redirect, + webUrl, + trustedOrigins: [apiUrl], + adminUrl, + }); + + test("is a valid return target, path and query kept", () => { + expect(resolveAdmin(`${adminUrl}/hiring?tab=x`)).toBe( + `${adminUrl}/hiring?tab=x`, + ); + }); + + test.each([ + "https://evil.example/x", + "//admin.example.com/x", + "https://admin.example.com.evil.example/x", + "https://sub.admin.example.com/x", + "https://evil.example@admin.example.com/x", + "https://admin.example.com@evil.example/x", + "https://admin.example.com:8443/x", + "http://admin.example.com/x", + ])("does not open anything else: %s", (redirect) => { + expect(resolveAdmin(redirect)).toBe(webUrl); + }); + + test("is not trusted when no admin URL is set", () => { + expect(resolve(`${adminUrl}/x`)).toBe(webUrl); + }); +}); diff --git a/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.ts b/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.ts index b7d7b5ca75a..24b46deb10f 100644 --- a/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.ts +++ b/apps/web/src/app/(auth)/utils/resolveCallbackUrl/resolveCallbackUrl.ts @@ -1,15 +1,20 @@ import { withOwnDomainTwins } from "@superset/shared/own-domains"; +import { adminReturnUrl } from "@/proxy-routes"; export function resolveCallbackUrl({ redirect, webUrl, trustedOrigins, + adminUrl, }: { redirect: string | null; webUrl: string; trustedOrigins: string[]; + adminUrl?: string; }): string { if (!redirect) return webUrl; + const admin = adminReturnUrl(redirect, adminUrl); + if (admin) return admin.href; let url: URL; try { url = new URL(redirect, webUrl); diff --git a/apps/web/src/env.ts b/apps/web/src/env.ts index 4948240b2a6..b2d98ae3e59 100644 --- a/apps/web/src/env.ts +++ b/apps/web/src/env.ts @@ -34,6 +34,7 @@ export const env = createEnv({ NEXT_PUBLIC_WEB_URL: z.string().url(), NEXT_PUBLIC_MARKETING_URL: z.string().url(), NEXT_PUBLIC_DOCS_URL: z.string().url(), + NEXT_PUBLIC_ADMIN_URL: z.string().url().optional(), NEXT_PUBLIC_POSTHOG_KEY: z.string(), NEXT_PUBLIC_POSTHOG_HOST: z.string().url(), NEXT_PUBLIC_SENTRY_DSN_WEB: z.string().optional(), @@ -50,6 +51,7 @@ export const env = createEnv({ NEXT_PUBLIC_WEB_URL: process.env.NEXT_PUBLIC_WEB_URL, NEXT_PUBLIC_MARKETING_URL: process.env.NEXT_PUBLIC_MARKETING_URL, NEXT_PUBLIC_DOCS_URL: process.env.NEXT_PUBLIC_DOCS_URL, + NEXT_PUBLIC_ADMIN_URL: process.env.NEXT_PUBLIC_ADMIN_URL, NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_HOST: process.env.NEXT_PUBLIC_POSTHOG_HOST, NEXT_PUBLIC_SENTRY_DSN_WEB: process.env.NEXT_PUBLIC_SENTRY_DSN_WEB, diff --git a/apps/web/src/proxy-routes.test.ts b/apps/web/src/proxy-routes.test.ts index 1faa9252fb2..d4b93e0192a 100644 --- a/apps/web/src/proxy-routes.test.ts +++ b/apps/web/src/proxy-routes.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "bun:test"; import { + adminReturnUrl, agentsRewriteUrl, isAgentsRoute, isAuthPageRoute, @@ -154,3 +155,59 @@ describe("agentsRewriteUrl", () => { ); }); }); + +describe("adminReturnUrl", () => { + const admin = "https://admin.example.com"; + + test("keeps a URL on exactly the admin origin, path and query", () => { + expect(adminReturnUrl(`${admin}/hiring/1?tab=x`, admin)?.href).toBe( + `${admin}/hiring/1?tab=x`, + ); + expect( + adminReturnUrl(`${admin}/hiring`, `${admin}/ignored/path`), + ).not.toBeNull(); + }); + + test.each([ + null, + "", + "/hiring", + "//admin.example.com/x", + "https://evil.example/x", + "https://evil.example@admin.example.com/x", + "https://admin.example.com@evil.example/x", + "https://admin.example.com:8443/x", + "http://admin.example.com/x", + "https://admin.example.com.evil.example/x", + "https://evil-admin.example.com/x", + "https://sub.admin.example.com/x", + "https://other.example.com/x", + "https:\\\\evil.example\\x", + "https:/\\evil.example/x", + "javascript:alert(1)", + "data:text/html,x", + ])("rejects %s", (redirect) => { + expect(adminReturnUrl(redirect, admin)).toBeNull(); + }); + + test("a backslash after the host stays on the admin origin", () => { + const url = adminReturnUrl( + "https://admin.example.com\\@evil.example/x", + admin, + ); + expect(url?.href).toBe(`${admin}/@evil.example/x`); + }); + + test("matches nothing when the admin URL is malformed", () => { + expect(adminReturnUrl(`${admin}/x`, "not a url")).toBeNull(); + }); + + test("matches nothing when the admin URL is not http or https", () => { + expect(adminReturnUrl("data:text/html,x", "data:text/html,y")).toBeNull(); + expect(adminReturnUrl("file:///etc/passwd", "file:///tmp")).toBeNull(); + }); + + test("matches nothing when no admin URL is configured", () => { + expect(adminReturnUrl(`${admin}/x`, undefined)).toBeNull(); + }); +}); diff --git a/apps/web/src/proxy-routes.ts b/apps/web/src/proxy-routes.ts index 1e092d85c2a..be6d99b5a54 100644 --- a/apps/web/src/proxy-routes.ts +++ b/apps/web/src/proxy-routes.ts @@ -52,6 +52,25 @@ export function safeRedirectUrl( return url; } +/** An absolute URL on exactly the admin origin: no other host, port, scheme or userinfo. */ +export function adminReturnUrl( + redirect: string | null, + adminUrl: string | undefined, +): URL | null { + if (!redirect || !adminUrl) return null; + let url: URL; + let admin: URL; + try { + url = new URL(redirect); + admin = new URL(adminUrl); + } catch { + return null; + } + const web = admin.protocol === "https:" || admin.protocol === "http:"; + const sameOrigin = web && url.origin === admin.origin; + return sameOrigin && !url.username && !url.password ? url : null; +} + export function isAgentsRoute(pathname: string): boolean { return matchesRouteOrChild(pathname, "/agents"); } diff --git a/apps/web/src/proxy.ts b/apps/web/src/proxy.ts index 82858d2f0e1..eb5aa9fceb7 100644 --- a/apps/web/src/proxy.ts +++ b/apps/web/src/proxy.ts @@ -6,6 +6,7 @@ import { headers } from "next/headers"; import { type NextRequest, NextResponse } from "next/server"; import { + adminReturnUrl, agentsRewriteUrl, isAgentsRoute, isAuthPageRoute, @@ -56,10 +57,10 @@ async function legacyProxy( pathname.startsWith("/trpc/"); if (session && isAuthPageRoute(pathname)) { - const target = safeRedirectUrl( - req.nextUrl.searchParams.get("redirect"), - req.nextUrl.origin, - ); + const redirect = req.nextUrl.searchParams.get("redirect"); + const target = + safeRedirectUrl(redirect, req.nextUrl.origin) ?? + adminReturnUrl(redirect, process.env.NEXT_PUBLIC_ADMIN_URL); return NextResponse.redirect(target ?? new URL("/", req.url)); }