Skip to content

Commit a1bfd19

Browse files
seemayrclaude
andauthored
fix: hash private API key before lookup to match stored HMAC value (#155)
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>
1 parent 4fbb6ac commit a1bfd19

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

apps/api/src/lib/auth-validation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
type ApiKeyWithWebsiteAndOrganization,
44
getApiKeyByKey,
55
} from "@api/db/queries/api-keys";
6+
import { env } from "@api/env";
67
import {
8+
hashApiKey,
79
isValidPublicApiKeyFormat,
810
isValidSecretApiKeyFormat,
911
} from "@api/utils/api-keys";
@@ -313,7 +315,9 @@ export async function authenticateWithPrivateKey(
313315
throw new AuthValidationError(401, "Invalid private API key format");
314316
}
315317

316-
return await getApiKeyFromRedis(privateKey, db);
318+
// Private keys are HMAC-hashed before storage, so hash before lookup
319+
const hashedKey = hashApiKey(privateKey, env.API_KEY_SECRET);
320+
return await getApiKeyFromRedis(hashedKey, db);
317321
}
318322

319323
/**

0 commit comments

Comments
 (0)