Skip to content

Commit edbd2bf

Browse files
committed
Ensure we check the full origin of callback URLs
This ensures that someone can't just register a domain and make it start with the same URL as ours.
1 parent 06f69a1 commit edbd2bf

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/app/api/auth/[...nextauth]/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const handler = async (req: NextRequest, res: unknown) => {
2424
const cookieStore = req.cookies;
2525
const callbackUrl = cookieStore.get("next-auth.callback-url")?.value;
2626
const redirectUrl =
27-
callbackUrl && callbackUrl.startsWith(process.env.SERVER_URL as string)
27+
callbackUrl && isValidCallbackUrl(callbackUrl)
2828
? callbackUrl
2929
: (process.env.SERVER_URL as string);
3030

@@ -39,4 +39,10 @@ const handler = async (req: NextRequest, res: unknown) => {
3939
) as Promise<Response>;
4040
};
4141

42+
function isValidCallbackUrl(callbackUrlString: string): boolean {
43+
const serverUrl = new URL(process.env.SERVER_URL!);
44+
const callbackUrl = new URL(callbackUrlString);
45+
return serverUrl.origin === callbackUrl.origin;
46+
}
47+
4248
export { handler as GET, handler as POST };

0 commit comments

Comments
 (0)