Skip to content

Commit 905b472

Browse files
committed
fix: update get_jwt_user to avoid non-string token_type
similar to other functions. Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent aa1f4de commit 905b472

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: check-added-large-files
1212

1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.14.10
14+
rev: v0.14.11
1515
hooks:
1616
- id: ruff
1717
args: ["--output-format=concise"]
@@ -31,7 +31,7 @@ repos:
3131

3232
- repo: https://github.com/astral-sh/uv-pre-commit
3333
# uv version.
34-
rev: 0.9.18
34+
rev: 0.9.22
3535
hooks:
3636
# Update the uv lockfile
3737
- id: uv-lock

app/managers/auth.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ async def resend_verify_code(
526526
bearer = 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

Comments
 (0)