Compare admin token as UTF-8 bytes to avoid TypeError on non-ASCII input (B-46)

secrets.compare_digest raises TypeError instead of returning False when a
str argument contains non-ASCII characters, turning a bad admin token into
an unhandled 500 instead of the expected 403. Encode both sides before
comparing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-27 16:17:40 +02:00
co-authored by Claude Sonnet 5
parent 6045c89ed0
commit 31bc9a327f
4 changed files with 20 additions and 14 deletions
+3 -1
View File
@@ -26,7 +26,9 @@ async def require_admin(x_admin_token: str = Header(default="")) -> None:
# anyone on an instance that never configured a token.
if not settings.admin_token:
raise HTTPException(status.HTTP_403_FORBIDDEN, "invalid admin token")
if not secrets.compare_digest(x_admin_token, settings.admin_token):
# compare_digest raises TypeError on a str containing non-ASCII characters
# (B-46) -- comparing the UTF-8 bytes instead accepts any input safely.
if not secrets.compare_digest(x_admin_token.encode(), settings.admin_token.encode()):
raise HTTPException(status.HTTP_403_FORBIDDEN, "invalid admin token")