fix: use constant-time comparison for bearer token validation (#8) #56
No reviewers
Labels
No Label
ai-changes-requested
ai-failed
ai-merged
ai-pr-opened
ai-reviewed
ai-reviewing
ai-reviewing
ai-working
bug
enhancement
evolution
performance
phase-0
phase-1a
phase-1b
phase-1c
phase-1d
security
tech-debt
todo
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/paper-dynasty-database#56
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "ai/paper-dynasty-database#8"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Replaces Python's
==string comparison invalid_token()withhmac.compare_digest()to prevent timing-based side-channel attacks on bearer token validation.Changes
app/dependencies.py: Addedimport hmacand changedreturn token == AUTH_TOKEN→return hmac.compare_digest(token, AUTH_TOKEN)Why
Python's
==operator short-circuits on the first mismatched character, leaking timing information that an attacker could use to brute-force the token one character at a time.hmac.compare_digest()takes constant time regardless of where a mismatch occurs.Tests
No test suite — changes verified by reading the modified file.
Closes #8
AI Code Review
Files Reviewed
app/dependencies.py(modified)Findings
Correctness
import hmacadded (alphabetically ordered betweendatetimeandlogging) andreturn token == AUTH_TOKENreplaced withreturn hmac.compare_digest(token, AUTH_TOKEN).hmac.compare_digest()acceptsstrarguments (since Python 3.3). Bothtoken(from OAuth2 bearer) andAUTH_TOKEN(f-string from env var) are strings — noTypeErrorrisk.valid_token()on line 41.Security
==short-circuits on the first mismatched byte, leaking information about how many leading characters match.hmac.compare_digest()takes constant time regardless of mismatch position.AUTH_TOKEN = f'{os.environ.get("API_TOKEN")}'will produce the string"None"if the env var is unset rather than raising an error or failing loudly. Pre-existing issue, out of scope for this PR.Style & Conventions
Suggestions
Verdict: APPROVED
Correct, minimal security fix.
hmac.compare_digest()is the standard library solution for this exact problem and is used appropriately here. No issues found.Automated review by Claude PR Reviewer
fb9c2d4e85to19ac5ffd0a