Skip to content

Commit 91b12ed

Browse files
committed
chore: precise check using isOAuthJWT
1 parent af9df3e commit 91b12ed

1 file changed

Lines changed: 5 additions & 19 deletions

File tree

packages/backend/src/tokens/request.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AuthErrorReason, handshake, signedIn, signedOut, signedOutInvalidToken
1414
import { createClerkRequest } from './clerkRequest';
1515
import { getCookieName, getCookieValue } from './cookie';
1616
import { HandshakeService } from './handshake';
17-
import { getMachineTokenType, isMachineToken, isTokenTypeAccepted } from './machine';
17+
import { getMachineTokenType, isMachineToken, isOAuthJwt, isTokenTypeAccepted } from './machine';
1818
import { OrganizationMatcher } from './organizationMatcher';
1919
import type { MachineTokenType, SessionTokenType } from './tokenTypes';
2020
import { TokenType } from './tokenTypes';
@@ -411,12 +411,11 @@ export const authenticateRequest: AuthenticateRequest = (async (
411411
async function authenticateRequestWithTokenInHeader() {
412412
const { tokenInHeader } = authenticateContext;
413413

414-
// SECURITY: Reject machine tokens (M2M, OAuth, API keys) when expecting session tokens.
415-
// OAuth JWTs (RFC 9068) are valid JWTs signed by Clerk and will pass verifyToken() verification,
416-
// but they should not be accepted as session tokens. We must explicitly check the token type
417-
// before verification to prevent machine tokens from being incorrectly authenticated as sessions.
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.
418417
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
419-
if (isMachineToken(tokenInHeader!)) {
418+
if (isOAuthJwt(tokenInHeader!)) {
420419
return signedOut({
421420
tokenType: TokenType.SessionToken,
422421
authenticateContext,
@@ -622,19 +621,6 @@ export const authenticateRequest: AuthenticateRequest = (async (
622621
return handleSessionTokenError(decodedErrors[0], 'cookie');
623622
}
624623

625-
// SECURITY: Defense-in-depth check to reject machine tokens in cookies.
626-
// While machine tokens should only be in headers, this prevents potential security issues
627-
// if a machine token somehow ends up in the session cookie.
628-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
629-
if (isMachineToken(authenticateContext.sessionTokenInCookie!)) {
630-
return signedOut({
631-
tokenType: TokenType.SessionToken,
632-
authenticateContext,
633-
reason: AuthErrorReason.TokenTypeMismatch,
634-
message: '',
635-
});
636-
}
637-
638624
if (decodeResult.payload.iat < authenticateContext.clientUat) {
639625
return handleMaybeHandshakeStatus(authenticateContext, AuthErrorReason.SessionTokenIATBeforeClientUAT, '');
640626
}

0 commit comments

Comments
 (0)