Skip to content

Add a unique partial index and constraint test for api_keys.prefix to guarantee prefix-lookup correctness #309

@greatest0fallt1me

Description

@greatest0fallt1me

Description

The gateway auth flow in src/middleware/gatewayApiKeyAuth.ts performs prefix-based lookup before a timing-safe full-key hash compare, but there is no database guarantee that key prefixes are unique among active keys. This backend task adds the constraint/index and tests so prefix collisions cannot cause ambiguous lookups.

Requirements and Context

  • Add a migration introducing a unique index on active (non-revoked) api_keys prefixes.
  • Verify src/repositories/apiKeyRepository.ts lookups rely on the indexed column.
  • Add constraint tests using pg-mem.
  • Must be secure, tested, and documented
  • Should be efficient and easy to review

Suggested Execution

  1. Fork the repo and create a branch
    git checkout -b task/api-key-prefix-unique-index
  2. Implement changes
    • migrations/0006_api_key_prefix_unique.sql — partial unique index
    • src/repositories/apiKeyRepository.ts — confirm lookup path
    • Add a constraint regression test
  3. Test and commit
    • npm test -- src/repositories/apiKeyRepository.test.ts
    • Cover edge cases
    • Include test output and notes in the PR

Example commit message

task: enforce unique active api_key prefixes

Acceptance Criteria

  • Inserting a duplicate active prefix fails at the database level
  • Revoked keys do not block reuse of a prefix
  • Tests cover collision and revocation cases

Guidelines

  • Minimum 90% test coverage with Jest
  • Clear documentation and inline comments
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbackendBackend service workdatabaseDatabase schema/migrationssecuritySecurity hardening

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions