Skip to content

fix(auth): compare API keys in constant time - #76

Closed
HamzaKhanBUIC wants to merge 2 commits into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/api-key-constant-time-compare
Closed

HamzaKhanBUIC wants to merge 2 commits into
getdownandcode:mainfrom
HamzaKhanBUIC:fix/api-key-constant-time-compare

Conversation

@HamzaKhanBUIC

Copy link
Copy Markdown

Fixes #74

Summary of Changes

  • Constant-Time Verification (agentkit/api/auth.py): Replaced the short-circuiting api_key not in settings.api_keys_list check with constant-time comparison (hmac.compare_digest) across all configured keys to eliminate byte-by-byte timing oracles.
  • Key Normalisation: Normalises presented keys (api_key.strip()) identical to Settings.api_keys_list, ensuring that whitespace-padded keys (e.g. ' key ') resolve identically to unpadded keys and that consistent keys are used for downstream rate-limiting and logging.
  • UTF-8 Byte Safety: Encodes presented and configured keys to UTF-8 bytes before comparing, preventing TypeError crashes when unauthenticated clients or fuzzers submit non-ASCII / Unicode payloads.
  • Tests & Quality:
    • Added unit test cases in tests/unit/test_api_auth.py asserting whitespace-padded keys, whitespace-only keys, invalid keys, and Unicode key support.
    • Added integration test in test_api_key_auth_endpoint_integration verifying identical HTTP 200 responses for padded headers.
    • ruff check ., ruff format --check ., and mypy agentkit/api/auth.py all clean.

Copilot AI balanced review requested due to automatic review settings October 20, 2026 15:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@HamzaKhanBUIC
HamzaKhanBUIC deleted the fix/api-key-constant-time-compare branch October 21, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AUDIT-17 — Low — API key comparison is not constant-time and not normalised

2 participants