-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nextjs): Detect middlware usage to avoid infinite loop on Keyless…
… mode (#4879)
- Loading branch information
1 parent
72d2953
commit 2e505ca
Showing
8 changed files
with
85 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/nextjs': patch | ||
--- | ||
|
||
Bug fix: Avoid infinite redirect loop on Keyless mode by detecting if `clerkMiddleware()` is used in the application. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { constants } from '@clerk/backend/internal'; | ||
import type { NextRequest } from 'next/server'; | ||
|
||
import type { RequestLike } from './types'; | ||
|
||
export function getCustomAttributeFromRequest(req: RequestLike, key: string): string | null | undefined { | ||
// @ts-expect-error - TS doesn't like indexing into RequestLike | ||
return key in req ? req[key] : undefined; | ||
} | ||
|
||
export function getAuthKeyFromRequest( | ||
req: RequestLike, | ||
key: keyof typeof constants.Attributes, | ||
): string | null | undefined { | ||
return getCustomAttributeFromRequest(req, constants.Attributes[key]) || getHeader(req, constants.Headers[key]); | ||
} | ||
|
||
export function getHeader(req: RequestLike, name: string): string | null | undefined { | ||
if (isNextRequest(req) || isRequestWebAPI(req)) { | ||
return req.headers.get(name); | ||
} | ||
|
||
// If no header has been determined for IncomingMessage case, check if available within private `socket` headers | ||
// When deployed to vercel, req.headers for API routes is a `IncomingHttpHeaders` key-val object which does not follow | ||
// the Headers spec so the name is no longer case-insensitive. | ||
return req.headers[name] || req.headers[name.toLowerCase()] || (req.socket as any)?._httpMessage?.getHeader(name); | ||
} | ||
|
||
export function detectClerkMiddleware(req: RequestLike): boolean { | ||
return Boolean(getAuthKeyFromRequest(req, 'AuthStatus')); | ||
} | ||
|
||
export function isNextRequest(val: unknown): val is NextRequest { | ||
try { | ||
const { headers, nextUrl, cookies } = (val || {}) as NextRequest; | ||
return ( | ||
typeof headers?.get === 'function' && | ||
typeof nextUrl?.searchParams.get === 'function' && | ||
typeof cookies?.get === 'function' | ||
); | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export function isRequestWebAPI(val: unknown): val is Request { | ||
try { | ||
const { headers } = (val || {}) as Request; | ||
return typeof headers?.get === 'function'; | ||
} catch (e) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters