@@ -14,7 +14,7 @@ import { AuthErrorReason, handshake, signedIn, signedOut, signedOutInvalidToken
1414import { createClerkRequest } from './clerkRequest' ;
1515import { getCookieName , getCookieValue } from './cookie' ;
1616import { HandshakeService } from './handshake' ;
17- import { getMachineTokenType , isMachineToken , isTokenTypeAccepted } from './machine' ;
17+ import { getMachineTokenType , isMachineToken , isOAuthJwt , isTokenTypeAccepted } from './machine' ;
1818import { OrganizationMatcher } from './organizationMatcher' ;
1919import type { MachineTokenType , SessionTokenType } from './tokenTypes' ;
2020import { TokenType } from './tokenTypes' ;
@@ -411,6 +411,19 @@ export const authenticateRequest: AuthenticateRequest = (async (
411411 async function authenticateRequestWithTokenInHeader ( ) {
412412 const { tokenInHeader } = authenticateContext ;
413413
414+ // Reject OAuth JWTs that may appear in headers when expecting session tokens.
415+ // OAuth JWTs are valid Clerk-signed JWTs and will pass verifyToken() verification,
416+ // but should not be accepted as session tokens.
417+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
418+ if ( isOAuthJwt ( tokenInHeader ! ) ) {
419+ return signedOut ( {
420+ tokenType : TokenType . SessionToken ,
421+ authenticateContext,
422+ reason : AuthErrorReason . TokenTypeMismatch ,
423+ message : '' ,
424+ } ) ;
425+ }
426+
414427 try {
415428 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
416429 const { data, errors } = await verifyToken ( tokenInHeader ! , authenticateContext ) ;
0 commit comments