Skip to content

Commit eeb9987

Browse files
authored
fix(app-router): encode returnTo in login redirect to prevent OAuth param injection (#2448)
2 parents 91475c0 + 3bb7956 commit eeb9987

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/server/helpers/with-page-auth-required.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe("with-page-auth-required ssr", () => {
8181
);
8282
await expect(handler({})).rejects.toThrowError("NEXT_REDIRECT");
8383
expect(redirect).toHaveBeenCalledTimes(1);
84-
expect(redirect).toHaveBeenCalledWith("/auth/login?returnTo=/foo");
84+
expect(redirect).toHaveBeenCalledWith("/auth/login?returnTo=%2Ffoo");
8585
});
8686

8787
it("should protect a page and redirect to returnTo fn option", async () => {
@@ -114,7 +114,7 @@ describe("with-page-auth-required ssr", () => {
114114
).rejects.toThrowError("NEXT_REDIRECT");
115115
expect(redirect).toHaveBeenCalledTimes(1);
116116
expect(redirect).toHaveBeenCalledWith(
117-
"/auth/login?returnTo=/foo/bar?foo=bar"
117+
"/auth/login?returnTo=%2Ffoo%2Fbar%3Ffoo%3Dbar"
118118
);
119119
});
120120

src/server/helpers/with-page-auth-required.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const appRouteHandlerFactory =
196196
: opts.returnTo;
197197
const { redirect } = await import("next/navigation.js");
198198
redirect(
199-
`${config.loginUrl}${opts.returnTo ? `?returnTo=${returnTo}` : ""}`
199+
`${config.loginUrl}${returnTo ? `?returnTo=${encodeURIComponent(returnTo)}` : ""}`
200200
);
201201
}
202202
return handler(params);

0 commit comments

Comments
 (0)