@@ -526,7 +526,7 @@ async def resend_verify_code(
526526bearer = HTTPBearer (auto_error = False )
527527
528528
529- async def get_jwt_user (
529+ async def get_jwt_user ( # noqa: C901
530530 request : Request ,
531531 db : AsyncSession = Depends (get_database ),
532532 credentials : HTTPAuthorizationCredentials | None = Depends (bearer ),
@@ -561,7 +561,7 @@ async def get_jwt_user(
561561 )
562562 # Use constant-time comparison to prevent timing attacks
563563 token_type = payload .get ("typ" )
564- if token_type is None or not secrets .compare_digest (
564+ if not isinstance ( token_type , str ) or not secrets .compare_digest (
565565 token_type , "access"
566566 ):
567567 increment_auth_failure ("invalid_token" , "jwt" )
@@ -575,10 +575,13 @@ async def get_jwt_user(
575575 )
576576
577577 user_id = payload .get ("sub" )
578- if user_id is None :
578+ # Accept int-like strings but reject weird types early
579+ if isinstance (user_id , str ) and user_id .isdigit ():
580+ user_id = int (user_id )
581+ if not isinstance (user_id , int ):
579582 increment_auth_failure ("invalid_token" , "jwt" )
580583 category_logger .warning (
581- "Authentication attempted with token missing 'sub' claim" ,
584+ "Authentication attempted with invalid 'sub' claim" ,
582585 LogCategory .AUTH ,
583586 )
584587 raise HTTPException (
0 commit comments