fix: prevent double-credit vulnerability in reward claim retry path#70
Merged
DeFiVC merged 3 commits intoJul 22, 2026
Merged
Conversation
…cessRewardClaim Wrap processRewardClaim in withLock (distributed lock) and db.transaction with FOR UPDATE row lock, matching the pattern already used by claimReward. This prevents concurrent retry calls from both reading rewardClaimed=false and crediting the user twice. Concurrent calls for the same submissionId are now serialized — only one succeeds. The retry processor continues to function for legitimate retries.
…wrapping Add withLock mock to tests so they run without a Redis instance. Update test helpers to mock db.transaction with tx.select().for(update) chain, reflecting the new lock-based implementation.
DeFiVC
approved these changes
Jul 22, 2026
Contributor
There was a problem hiding this comment.
Review
Overall: Approve. This is a well-written fix for a critical financial vulnerability. The implementation correctly addresses the double-credit issue in processRewardClaim by following the same pattern already established in claimReward.
Checklist
- ✅ All CI checks green (Lint/Typecheck pass, Tests pass)
- ✅
Closes #66in PR description - ✅ PR matches issue requirements — addresses double-credit vulnerability
- ✅ No AI co-authorship markers in commits
- ✅ No scope creep or unrelated changes
- ✅ PR description is detailed with motivation, changes, testing, and risks
- ✅ 2 commits show iterative development (fix + test update)
- ✅ Follows existing codebase patterns (
withLock+db.transaction+.for("update"))
Code Review
The fix is clean and minimal:
src/modules/rewards/reward.service.ts: WrapsprocessRewardClaiminwithLockand moves all DB operations inside a transaction with.for("update")row lock- Cache invalidation correctly moved outside the lock to minimize lock hold time
tests/unit/services/process-reward-claim.test.ts: Updated to mockwithLockand transaction helpers properly
Verdict
Approved — safe to merge. The fix follows established patterns and addresses a critical vulnerability correctly.
6 tasks
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 #66
Type of Change
Summary
Fixed a double-credit vulnerability where concurrent calls to
processRewardClaimcould both readrewardClaimed = falseand credit the user twice. Added distributed locking (withLock) and row-level locking (FOR UPDATEinside a transaction) to serialize concurrent retry claims for the same submission.Motivation / Context
Fixes #66
The
processRewardClaimfunction read quiz submissions without a row-level lock, checkedrewardClaimed, invoked the Stellar contract (slow external call), then setrewardClaimed = true. Two concurrent retry ticks or a retry + direct claim could both readrewardClaimed = falseand both succeed, crediting the user twice. This is a direct financial vulnerability.Detailed Changes
src/modules/rewards/reward.service.ts—processRewardClaimfunction:withLock(\reward:${submissionId}`, ...)` for distributed lockingdb.transaction(async (tx) => { ... }).for("update")row-level lock on the submission queryrewardClaimedcheck inside the lock, after acquisitiontests/unit/services/process-reward-claim.test.ts:withLockmock so tests run without Redisdb.transactionwithtx.select().for("update")chainCurrent Behavior vs. New Behavior
Before: Concurrent
processRewardClaimcalls could both succeed for the same submission, crediting the user multiple times.After: Concurrent calls for the same
submissionIdare serialized via distributed lock + row-level lock — only one succeeds.Testing
npm run typecheck— passes (0 errors)npm run lint— passes (0 errors, 73 pre-existing warnings)npm test— 127 passed, 2 failed (pre-existing credential minting test failures unrelated to this change)process-reward-claim.test.tstests passmain(concurrent-safety.test.tscredential minting tests)Breaking Changes
No.
Risks and Rollback
Risks:
withLockcall requires Redis. If Redis is unavailable,withLockwill fail. However,processRewardClaimis only called by the retry processor which already depends on Redis (for the queue).claimRewardmethod has the same pattern and has been working in production.Rollback: Revert the commit to restore the original
processRewardClaimimplementation.Checklist
Self-Review
console.log,print,debugger, commented-out blocks)Testing
CI / Pipeline
Documentation
Security
Performance
Reviewer Notes
withLock+db.transaction+.for("update")) matches the existingclaimRewardmethod — this is the established pattern in the codebase.Additional References
claimRewardmethod atsrc/modules/rewards/reward.service.ts:155-309src/utils/lock.tssrc/services/retry-queue.ts