Skip to content

Commit a072125

Browse files
Use hmac.compare_digest for auth token comparisons (#21072)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent af54e12 commit a072125

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/prefect/server/api/middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hmac
12
from typing import Awaitable, Callable
23

34
from fastapi import status
@@ -63,7 +64,9 @@ async def dispatch(
6364
session=session, client=incoming_client
6465
)
6566

66-
if token is None or token.token != incoming_token:
67+
if token is None or not hmac.compare_digest(
68+
token.token, incoming_token
69+
):
6770
return JSONResponse(
6871
{"detail": "Invalid CSRF token or client identifier."},
6972
status_code=status.HTTP_403_FORBIDDEN,

src/prefect/server/api/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import base64
1010
import contextlib
1111
import gc
12+
import hmac
1213
import logging
1314
import mimetypes
1415
import os
@@ -446,7 +447,7 @@ async def token_validation(request: Request, call_next: Any): # type: ignore[re
446447
status_code=status.HTTP_401_UNAUTHORIZED,
447448
content={"exception_message": "Unauthorized"},
448449
)
449-
if decoded != auth_string:
450+
if not hmac.compare_digest(decoded, auth_string):
450451
return JSONResponse(
451452
status_code=status.HTTP_401_UNAUTHORIZED,
452453
content={"exception_message": "Unauthorized"},

src/prefect/server/utilities/subscriptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import hmac
23
from asyncio import IncompleteReadError as IOError
34
from logging import Logger
45
from typing import Optional
@@ -81,7 +82,7 @@ async def accept_prefect_socket(websocket: WebSocket) -> Optional[WebSocket]:
8182
reason="Auth required but no token provided",
8283
)
8384

84-
if received_token != auth_setting:
85+
if not hmac.compare_digest(received_token, auth_setting):
8586
logger.warning("WebSocket connection closed: Invalid token.")
8687
return await websocket.close(
8788
WS_1008_POLICY_VIOLATION, reason="Invalid token"

0 commit comments

Comments
 (0)