task: enforce unique active api_key prefixes#371
Open
elijah4196 wants to merge 1 commit into
Open
Conversation
|
@elijah4196 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
task: enforce unique active api_key prefixes
Summary
The gateway auth flow in
src/middleware/gatewayApiKeyAuth.tsperforms aprefix-based lookup before a timing-safe full-key hash comparison. Without a
database-level guarantee, two active keys could share the same prefix, making
the lookup ambiguous and potentially allowing one key to shadow another.
This PR adds the missing constraint and its regression tests.
Changes
migrations/0006_api_key_prefix_unique.sql(new)Adds a partial unique index on
api_keys (prefix) WHERE revoked = FALSE.migrations/0006_api_key_prefix_unique.down.sql(new)Rollback:
DROP INDEX IF EXISTS uq_api_keys_prefix_active;src/repositories/apiKeyRepository.prefix.test.ts(new)Constraint regression tests using pg-mem (no external DB required):
api_idtests/helpers/db.ts(updated)Added
prefix VARCHAR(16)column and the partial unique index to the sharedpg-mem schema used by integration tests, keeping it in sync with the real
PostgreSQL schema.
docs/gateway-api-key-auth.md(updated)Added a Prefix uniqueness guarantee section documenting the new index and
pointing to the regression tests.
migrations/README.md(updated)Added migration 0006 to the table.
Acceptance criteria
Testing
Tests run with Jest + pg-mem (no external PostgreSQL required):
All 10 constraint regression tests pass.
closes #309