DEV Community

Faizan Ahmad
Faizan Ahmad

Posted on

The open redirect hiding in next.startsWith("/")

Almost every app with a login page has some version of this:

const next = params.next?.startsWith("/") ? params.next : "/dashboard";
router.push(next);
Enter fullscreen mode Exit fullscreen mode

The idea is reasonable: only follow next if it's a path on our own site, never a full URL. We had it in three places. It doesn't do what it looks like it does.

Three ways past the check

All three of these start with /:

next= value What the browser does
//evil.example A protocol-relative URL. It means "same scheme, host evil.example".
/\evil.example The URL parser treats \ as / in http(s) URLs, so it becomes //evil.example.
/\t/evil.example Tabs and newlines are stripped while parsing, which again leaves //evil.example.

So https://yourapp.com/login?next=//evil.example is a link that shows your real login page and, once the person signs in, hands them to someone else's site. That's a convincing phishing setup: the victim checked the domain, and it was yours.

Where it actually bites

It depends on what consumes next:

  • router.push(next) and window.location.href = next follow protocol-relative URLs off-site. Both were in our login flow: the email form used one and the Google sign-in button used the other.
  • A redirect response with Location: //evil.example is followed off-site too.
  • ${origin}${next} happens to be safe, because prefixing your own origin turns //evil.example into a path on your own host. Don't rely on that: the next refactor will remove the prefix.

The fix

One function, used everywhere a next value is followed:

export function safeNextPath(next: string | null | undefined, fallback = "/dashboard"): string {
  if (typeof next !== "string" || next.length === 0 || next.length > 2048) return fallback;
  if (!next.startsWith("/")) return fallback;
  if (next.startsWith("//") || next.startsWith("/\\")) return fallback;
  // Tab/newline would be stripped by the URL parser *after* this check passed.
  if (/[\u0000-\u001f\u007f]/.test(next)) return fallback;
  return next;
}
Enter fullscreen mode Exit fullscreen mode

The test table is the valuable part, because it's the list of attacks:

it.each([
  "//evil.example/login",
  "/\\evil.example",
  "https://evil.example",
  "javascript:alert(1)",
  "/\t/evil.example",
  "/\n/evil.example",
  "",
])("falls back for %s", (next) => {
  expect(safeNextPath(next)).toBe("/dashboard");
});
Enter fullscreen mode Exit fullscreen mode

Then test the paths that should keep working, with their query strings, since those carry real state:

expect(safeNextPath("/dashboard/forms/new?template=buyer-lead-intake"))
  .toBe("/dashboard/forms/new?template=buyer-lead-intake");
Enter fullscreen mode Exit fullscreen mode

The second bug in the same flow

While fixing this we found the opposite problem: a legitimate next was being lost. Our proxy sent signed-out visitors to the login page like this:

url.pathname = "/login";
url.searchParams.set("next", pathname); // ← the query string is gone
Enter fullscreen mode Exit fullscreen mode

Our template pages link to /dashboard/forms/new?template=.... A visitor picked a template, was asked to sign up, and landed in an empty builder, because ?template= never made it into next. Carry the whole thing:

url.search = "";
url.searchParams.set("next", `${pathname}${request.nextUrl.search}`);
Enter fullscreen mode Exit fullscreen mode

The page that receives it also has to read the parameter. Ours didn't, so we fixed that as well.

Check yours

Search the codebase for startsWith("/") near the words next, redirect, returnTo or callbackUrl. Then try ?next=//example.com on your own login page while signed in. It takes a minute, and it's one of the most common bugs in auth flows.


Found and fixed in DodoForm, an AI form builder. We verified the fix against production with a throwaway account: signed in, next=//evil.example now lands on the dashboard.

Top comments (0)