Skip to content

Fix/auth jti suspended apikey lookup - #282

Merged
martinzhames merged 5 commits into
dupdab:mainfrom
dami-005:fix/auth-jti-suspended-apikey-lookup
Sep 1, 2026
Merged

Fix/auth jti suspended apikey lookup#282
martinzhames merged 5 commits into
dupdab:mainfrom
dami-005:fix/auth-jti-suspended-apikey-lookup

Conversation

@dami-005

Copy link
Copy Markdown
Contributor

Closes #143
Closes #142
Closes #141
Closes #140

signToken() previously signed { sub, email, role } with no jti.
JwtStrategy.validate() falls back to payload.sub for the
blacklist/session cache key when jti is absent, so every token ever
issued to the same merchant resolved to the same blacklist key. If
logout() were wired up, revoking one session would blacklist every
active session/device for that merchant.

Generate a unique jti (crypto.randomUUID()) per signed token so
logout/session-cache keys are per-token instead of per-merchant.
login() checked credentials but never checked merchant.status,
so a suspended merchant (e.g. flagged for fraud/AML) with correct
credentials could still obtain a valid access token.

Throw UnauthorizedException when status is SUSPENDED before issuing
a token.
findMerchantByApiKey() loaded every merchant with a non-null
apiKeyHash and ran bcrypt.compare in a loop until a match was found.
bcrypt.compare is deliberately CPU-expensive, so this made
API-key-authenticated request latency scale linearly with merchant
count and gave any client an easy CPU-exhaustion lever (N bcrypt
comparisons per request via JwtAuthGuard).

Add an indexed apiKeyLookupHash column (SHA-256 hex digest of the raw
key) to find the single candidate merchant in O(1), then bcrypt.compare
only against that candidate's hash. generateApiKey() now populates the
new column alongside the existing bcrypt hash; migration
1772300000002 adds the column, unique constraint, and index.

Note: merchants with an API key issued before this migration will
need to regenerate it, since the raw key (needed to compute the new
lookup hash) isn't recoverable from the existing bcrypt hash.
login() now rejects suspended merchants, but a merchant suspended
after obtaining a token could keep using it for the token's full
lifetime since JwtStrategy.validate() never re-checked status.

Reject already-issued tokens for suspended merchants in the DB
fallback path of validate().
@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@dami-005 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! 🚀

Learn more about application limits

@martinzhames
martinzhames merged commit 71db918 into dupdab:main Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants