fix: hash private API key before lookup to match stored HMAC value#155
Conversation
Private keys are HMAC-hashed with API_KEY_SECRET before storage in createApiKey(), but authenticateWithPrivateKey() was comparing the raw key against the hashed DB value, causing all private key auth to fail. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@seemayr is attempting to deploy a commit to the cossistant Team on Vercel. A member of the Team first needs to authorize it. |
Greptile SummaryThis PR fixes a broken private API key authentication path: The change is minimal, targeted, and correct — no other lookup paths are affected, and public key auth is unchanged. Confidence Score: 5/5Safe to merge — the fix is minimal, correct, and directly addresses a broken authentication path with no side effects. The change is a single-function fix that aligns the lookup path with the storage path. All findings are P2 or lower; no correctness, data-integrity, or security regressions are introduced. No files require special attention.
|
| Filename | Overview |
|---|---|
| apps/api/src/lib/auth-validation.ts | Adds hashApiKey + env imports and hashes the private key before the Redis/DB lookup in authenticateWithPrivateKey, matching the HMAC-SHA256 hash stored by createApiKey. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Client sends Bearer token] --> B{Valid sk_ format?}
B -- No --> C[401 Invalid format]
B -- Yes --> D[Compute HMAC digest of token]
D --> E[Lookup digest in Redis cache]
E --> F{Cache hit?}
F -- Yes --> G[Return ApiKey record]
F -- No --> H[Query DB by digest]
H --> I{Row found?}
I -- Yes --> J[Cache result, return ApiKey]
I -- No --> K[Return null - 401 Invalid API key]
G --> L[Auth success]
J --> L
Reviews (1): Last reviewed commit: "fix: hash private API key before lookup ..." | Re-trigger Greptile
Rieranthony
left a comment
There was a problem hiding this comment.
Perfect, thanks for the fix!
Summary
API_KEY_SECRETbefore storage increateApiKey(), butauthenticateWithPrivateKey()was comparing the raw bearer token against the hashed DB valueRoot Cause
In
apps/api/src/db/queries/api-keys.ts,createApiKey()(line ~209) hashes private keys:But in
apps/api/src/lib/auth-validation.ts,authenticateWithPrivateKey()passed the raw key directly to the lookup, which would never match the hashed value in the database.Change
apps/api/src/lib/auth-validation.ts— hash the private key before lookup:Public key auth is unaffected (public keys are stored raw).
Test plan
Authorization: Bearer sk_...against/v1/websites🤖 Generated with Claude Code