Skip to content

Open-source status page, incident response and uptime monitoring

fix: preserve redirect URL for invited users through onboarding (#2175)

Maximilian Kaske81b7eab72e9a
diff --git a/apps/dashboard/src/app/onboarding/page.tsx b/apps/dashboard/src/app/onboarding/page.tsx
index 692eb93dd..371c3901a 100644
--- a/apps/dashboard/src/app/onboarding/page.tsx
+++ b/apps/dashboard/src/app/onboarding/page.tsx
@@ -1,9 +1,33 @@
import { getQueryClient, trpc } from "@/lib/trpc/server";
+import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import type { SearchParams } from "nuqs";
import { Client } from "./client";
import { searchParamsCache } from "./search-params";
+function tryRedirectToCallback(callbackUrl: string | null): void {
+ if (!callbackUrl) return;
+ let safePath: string | null = null;
+ try {
+ const target = new URL(callbackUrl, "http://_");
+ const safe =
+ (target.protocol === "http:" || target.protocol === "https:") &&
+ target.pathname.startsWith("/") &&
+ !target.pathname.startsWith("//") &&
+ target.pathname !== "/" &&
+ target.pathname !== "/login";
+ if (safe) {
+ safePath = `${target.pathname}${target.search}${target.hash}`;
+ }
+ } catch {
+ // malformed — fall through
+ }
+ // redirect() throws NEXT_REDIRECT; must be outside the try/catch above.
+ if (safePath) {
+ redirect(safePath);
+ }
+}
+
export default async function Page({
searchParams,
}: {
@@ -13,21 +37,16 @@ export default async function Page({
// Same-origin-only redirect: strip host, reject non-HTTP and `//…`
// (protocol-relative). `/` and `/login` are app-specific UX skips.
- if (params.callbackUrl) {
- try {
- const target = new URL(params.callbackUrl, "http://_");
- const safe =
- (target.protocol === "http:" || target.protocol === "https:") &&
- target.pathname.startsWith("/") &&
- !target.pathname.startsWith("//") &&
- target.pathname !== "/" &&
- target.pathname !== "/login";
- if (safe) {
- redirect(`${target.pathname}${target.search}${target.hash}`);
- }
- } catch {
- // malformed — fall through
- }
+ // First check query params, then fallback to cookie set by middleware.
+ tryRedirectToCallback(params.callbackUrl);
+
+ // Fallback: check auth-redirect cookie for invite flows where Auth.js
+ // didn't pass the callbackUrl properly. The cookie has a 10-minute TTL
+ // and will expire naturally.
+ const cookieStore = await cookies();
+ const authRedirect = cookieStore.get("auth-redirect")?.value;
+ if (authRedirect) {
+ tryRedirectToCallback(authRedirect);
}
const queryClient = getQueryClient();
diff --git a/apps/dashboard/src/lib/auth/index.ts b/apps/dashboard/src/lib/auth/index.ts
index 2d1852978..53dbf225d 100644
--- a/apps/dashboard/src/lib/auth/index.ts
+++ b/apps/dashboard/src/lib/auth/index.ts
@@ -20,6 +20,24 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
? [GitHubProvider, GoogleProvider, ResendProvider]
: [GitHubProvider, GoogleProvider],
callbacks: {
+ async redirect({ url, baseUrl }) {
+ // Allow relative URLs, but not protocol-relative `//evil.com` which the
+ // browser would resolve off-origin.
+ if (url.startsWith("/") && !url.startsWith("//")) {
+ return `${baseUrl}${url}`;
+ }
+ // Same-origin absolute URLs only — compare parsed origins, not a string
+ // prefix, so `https://<baseUrl>.evil.com` cannot pass as trusted.
+ // Preserves the original callback URL so invite links keep working.
+ try {
+ if (new URL(url).origin === new URL(baseUrl).origin) {
+ return url;
+ }
+ } catch {
+ // malformed url — fall through to baseUrl
+ }
+ return baseUrl;
+ },
async signIn(params) {
// We keep updating the user info when we loggin in
diff --git a/apps/dashboard/src/proxy.ts b/apps/dashboard/src/proxy.ts
index 36187e9f2..b7d90a8f0 100644
--- a/apps/dashboard/src/proxy.ts
+++ b/apps/dashboard/src/proxy.ts
@@ -22,23 +22,56 @@ export default auth(async (req) => {
}
if (!req.auth && url.pathname !== "/login") {
- console.log("User not authenticated, redirecting to login");
+ if (process.env.NODE_ENV === "development") {
+ console.log("User not authenticated, redirecting to login");
+ }
const newURL = new URL("/login", req.url);
- const encodedSearchParams = `${url.pathname}${url.search}`;
+ // Only worth preserving a non-root destination; tryRedirectToCallback
+ // rejects "/" anyway, so storing it would be a no-op redirect.
+ const encodedSearchParams =
+ url.pathname === "/" ? null : `${url.pathname}${url.search}`;
if (encodedSearchParams) {
newURL.searchParams.append("redirectTo", encodedSearchParams);
}
- return NextResponse.redirect(newURL);
+ const response = NextResponse.redirect(newURL);
+ // Store the redirect URL in a cookie for new users who go through onboarding.
+ // Auth.js may not reliably pass callbackUrl to pages.newUser, so we use a
+ // cookie as a fallback to ensure invite links work correctly.
+ if (encodedSearchParams) {
+ response.cookies.set("auth-redirect", encodedSearchParams, {
+ path: "/",
+ httpOnly: true,
+ secure: process.env.NODE_ENV === "production",
+ sameSite: "lax",
+ maxAge: 60 * 10, // 10 minutes
+ });
+ }
+ return response;
}
if (req.auth && url.pathname === "/login") {
const redirectTo = url.searchParams.get("redirectTo");
- console.log("User authenticated, redirecting to", redirectTo);
if (redirectTo) {
- const redirectToUrl = new URL(redirectTo, req.url);
- return NextResponse.redirect(redirectToUrl);
+ // Same-origin only: rebuild from path/search/hash so an absolute
+ // redirectTo (e.g. https://evil.com) can't become an open redirect.
+ const target = new URL(redirectTo, req.nextUrl.origin);
+ if (
+ (target.protocol === "http:" || target.protocol === "https:") &&
+ !target.pathname.startsWith("//")
+ ) {
+ const safe = new URL(
+ `${target.pathname}${target.search}${target.hash}`,
+ req.nextUrl.origin,
+ );
+ if (process.env.NODE_ENV === "development") {
+ console.log("User authenticated, redirecting to", safe);
+ }
+ const res = NextResponse.redirect(safe);
+ res.cookies.delete("auth-redirect");
+ return res;
+ }
}
}
@@ -64,6 +97,20 @@ export default auth(async (req) => {
response.cookies.delete("workspace-slug");
}
+ // auth-redirect is single-use and consumed by the /onboarding Server
+ // Component. Middleware response-cookie writes are reflected into the same
+ // request's cookies(), so deleting it on the /onboarding request itself
+ // would race (and lose) that read. Clear it on the next authenticated
+ // request instead: by then onboarding has redirected to the target, the
+ // cookie's job is done, and the stale back-button re-redirect is killed.
+ if (
+ req.auth &&
+ url.pathname !== "/onboarding" &&
+ req.cookies.has("auth-redirect")
+ ) {
+ response.cookies.delete("auth-redirect");
+ }
+
return response;
});