fix(auth): key rate-limit bucket by key_id/hash, never plaintext API key (S-6)#209
Merged
Merged
Conversation
…key (S-6) `_rate_limit_key` returned the plaintext API key, which the rate limiter uses verbatim as a Redis sorted-set key NAME (`zadd(key, ...)`). Anyone with Redis read access (SCAN/MONITOR/a backup/a shared cache) could recover live plaintext API keys straight from the bucket names (audit S-6, P3, secret-at-rest). Key the bucket by the stable non-secret `key_id`, then the stored `key_hash`, then a SHA-256 of the plaintext (never the plaintext itself), finally the non-secret name. Each distinct key still maps to its own bucket, so per-key rate-limit isolation is unchanged. Two prior tests encoded the plaintext-keying contract (asserted the bucket == the plaintext); rewritten to assert the secure contract, plus new coverage that the bucket never contains the plaintext. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
DORA Metrics
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
_rate_limit_keyreturned the plaintext API key, whichrate_limiter.check()uses verbatim as a Redis sorted-set key name (zadd(key, ...),expire(key, ...)). Anyone with Redis read access — a compromise,SCAN/MONITOR, a backup, a shared cache — could recover live plaintext API keys straight from the bucket names (audit S-6, P3, secret-at-rest disclosure).How
Key the bucket by a stable non-secret identifier instead:
key_id(stable, non-secret) →key_hash→name(last resort).Each distinct key still maps to its own bucket, so per-key rate-limit isolation is unchanged; only a one-time bucket-namespace reset happens on deploy (60s windows, expire immediately). No migration needed.
Tests
bucket == plaintext) — rewritten to assert the secure contract.key_idis preferred over secret material; the key is deterministic and per-key distinct (isolation preserved).test_auth_manager_mutation) + 62 (broader auth/rate-limit suites) pass;ruff check+format+mypyclean.🤖 Generated with Claude Code