Repository navigation
Hash per-user API keys at rest (phase 1: additive) - #1555
Merged
Merged
Conversation
Store HMAC-SHA256 of each mcp_ key, keyed on a pepper derived from ENCRYPTION_KEY with HKDF, plus the last 8 characters for the masked display. New keys write the hash; the backend backfills existing rows at boot and hashes any unhashed row on first use. Lookups match the hash and still fall back to the plaintext column, which stays until a later phase drops it, so older containers and rollbacks keep working. Also corrects the key format comment (64 hex chars, not 32).
helpcodeai
force-pushed
the
fix/hash-api-keys-at-rest
branch
from
November 6, 2026 12:23
73b1917 to
1315ccf
Compare
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
Per-user
mcp_API keys are now stored as a keyed hash as well as in the existingkeycolumn. This is phase 1 of moving them off plaintext at rest. It only adds columns, so older containers and image rollbacks keep working. A later PR drops the plaintext column.HMAC-SHA256(pepper, key), hex. The pepper is derived fromENCRYPTION_KEYwith HKDF-SHA256 (infoamcp:mcp-api-key:v1). No new secret to configure.ENCRYPTION_KEYalready must never change.key_suffixfor the maskedmcp_••••<suffix>display the dashboard already uses.Changes
20261106130000_add_mcp_api_key_hash: nullablekey_hash(unique) andkey_suffixonmcp_api_keys.keyis untouched.common/crypto/api-key-hash.util.ts: pepper derivation, hash, suffix, and the lookupwhere(hash OR plaintext during the transition).McpApiKeysService:generate()writes hash and suffix;resolveUserByKey()looks up by hash with a plaintext fallback;JwtOrManagementKeyGuard: same lookup.No API response changes.
listByUser()and the dashboard still behave as before; they move tokey_suffixin a later phase.Cloud and self-hosted
Same behaviour in both modes. The migration runs at container start as usual and the backfill runs on boot. Clients keep sending the same key in
X-API-KeyorAuthorization: Bearer, so nothing changes for users. Self-hosted instances derive the pepper from their existingENCRYPTION_KEYand need no configuration.Type
Testing
Commands run in
packages/backend, against an isolated local database on the dev Postgres (port 5433), afternpx prisma migrate deployandnpx prisma generate:npx prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma --script→ empty migration (schema and migrations agree)npx tsc --noEmit -p tsconfig.json→ no errorsnpm run lint→ 0 errors, 11 warnings, all in files this PR does not touch;eslint --fixchanged nothingnpx jest --maxWorkers=4→ Test Suites: 1032 passed, 7 skipped; Tests: 31785 passed, 1871 skipped; 0 failedNew tests:
api-key-hash.util.spec.ts: HKDF/HMAC values, stability, per-key and per-secret differences, not a plain SHA-256, weak secret refused.mcp-api-keys.service.spec.ts: hash and suffix on create, hash-or-plaintext lookup, hash on first use, batched backfill, no infinite loop.jwt-or-management-key.guard.spec.ts(real DB): a key matched by its hash alone authenticates; the stored hash is not accepted as a key; a plaintext-only row is hashed on first use.Manual check, upgrade path with real builds of
main(old) and this branch (new) on one local database (MCP_AUTH_MODE=oauth2). Each step probesPOST /mcp/:serverId(initialize) with an MCP key,GET /api/mcp-keyswith a read key, and random keys:/mcp200,/api200, wrong keys 401, MCP key on/api401.pg_dumpofmcp_api_keys, brings everything back to 200. A fullpg_restoreinto a scratch DB also works.No key appears in either backend log.
Checklist
npx jest)Related Issues
The schema change is tracked privately by the maintainers. Next phases:
expiresAt;key_suffix;