Fix/auth jti suspended apikey lookup - #282
Merged
martinzhames merged 5 commits intoSep 1, 2026
Merged
Conversation
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().
|
@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! 🚀 |
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.
Closes #143
Closes #142
Closes #141
Closes #140