Repository navigation
fix(api-keys)!: use salted, peppered scrypt digests #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ba371c6
fix(api-keys): HMAC-SHA256 key digests with server pepper and legacy …
PetrefiedThunder 4dc2a94
fix(api-keys)!: HMAC-SHA256 only, drop legacy SHA-256 fallback and re…
PetrefiedThunder 2849d31
fix(security): scrypt+pepper API key hashing with indexed lookup id (…
PetrefiedThunder 1c2fdf1
test(#254): generate test pepper and keys at runtime with secrets.tok…
PetrefiedThunder 64a9f53
Merge remote-tracking branch 'origin/main' into hermes/api-key-hmac-p…
PetrefiedThunder 159954d
fix(auth): keep scrypt work off asynchronous request loops
PetrefiedThunder b66bced
Merge main while preserving API key pepper startup validation
PetrefiedThunder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # API key hashing | ||
|
|
||
| API keys are stored only as digests. New digests are **HMAC-SHA256 keyed by a | ||
| server pepper** (`API_KEY_HASH_PEPPER`), so a leaked `api_keys` table cannot be | ||
| checked offline against candidate keys without also holding the pepper. | ||
|
PetrefiedThunder marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Configuration | ||
|
|
||
| | Variable | Required | Notes | | ||
| |---|---|---| | ||
| | `API_KEY_HASH_PEPPER` | Yes in production-like environments | Long random secret (32+ random bytes). Read from the process environment, then `.env` via Settings (stored as a `SecretStr`). Never logged. | | ||
|
|
||
| ## Lookup and migration | ||
|
|
||
| `APIKeyService.validate_key` computes both candidate digests and looks the row | ||
| up by digest and prefix: | ||
|
|
||
| 1. **HMAC-SHA256(pepper, key)**: the current format. | ||
| 2. **Legacy SHA-256(key)**: rows written before the pepper existed. | ||
|
|
||
| Both digests are compared in constant time. When a key matches on its legacy | ||
| digest and a pepper is configured, the row is rewritten to the HMAC digest after | ||
| the authentication succeeds. The update is guarded on the old digest, so | ||
| concurrent requests upgrade it only once. A failed rehash is logged by key id | ||
| and exception type only, and the legacy row keeps working. | ||
|
|
||
| No schema change is needed, because both digests are 64 hex characters. | ||
|
|
||
| ## When the pepper is unset | ||
|
|
||
| | Environment | Issue new keys | Validate keys | Startup | | ||
| |---|---|---|---| | ||
| | Local-compatible (`local`, `test`, ...) | Legacy SHA-256 digest | Legacy digest only | Boots | | ||
| | Production-like | Refused (`APIKeyHashingUnavailable`) | Every key rejected | **Refuses to boot** | | ||
|
|
||
| Production fails closed so that a missing secret can never quietly put the | ||
| fleet back on unkeyed hashing. | ||
|
|
||
| ## Deploy steps | ||
|
|
||
| 1. Generate the pepper and store it in the secret manager for every | ||
| production-like environment **before** deploying this version. Without it the | ||
| gateway will not start. | ||
| 2. Deploy. Existing keys keep working through the legacy fallback and are | ||
| rehashed on first use. | ||
| 3. Keys that are never used stay in the legacy format. Rotate or revoke them | ||
| if they should be retired. | ||
| 4. Do not change the pepper. Changing it invalidates every rehashed or newly | ||
| issued key. Rotating the pepper needs its own dual-pepper plan. | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.