fix: atomic capped increment for promo code redemption - #106
Closed
0xzino wants to merge 2 commits into
Closed
Conversation
SESSION_SECRET fell back to a hardcoded dev value even in production, making session token hashes trivially forgeable (issue profullstack#88, HIGH). Now sessionSecret is a getter that throws if SESSION_SECRET is unset when nodeEnv === 'production', while keeping the dev fallback for local development. Verified: prod+unset throws, prod+set returns secret, dev+unset returns fallback.
quotePromo() checks uses < max_uses, then recordPromoRedemption() did a blind 'uses = uses + 1'. Two concurrent requests both pass the quote check and over-redeem the code (profullstack#92). Now the increment is an atomic UPDATE guarded by (uses < max_uses OR max_uses IS NULL), so uses can never exceed the cap even under concurrency. Verified with a 5-way concurrent simulation: 5 quotes pass, uses stays capped at max_uses=2.
Contributor
|
Closing as superseded by #110, now merged. #110 covers the same capped increment ( The |
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.
Bug
quotePromo()validatesuses < max_uses, thenrecordPromoRedemption()performs a blindUPDATE promo_codes SET uses = uses + 1. Under concurrency, two requests can both pass the quote check and both increment, over-redeeming the code (issue #92, race condition).Fix
Make the increment atomic and capped:
The
WHEREguard makes over-redemption impossible: SQLite serializes writes, so onceusesreachesmax_uses, the UPDATE matches zero rows.Verification
Simulated 5 concurrent users all passing the quote check against a
max_uses = 2code: finaluses= 2 (capped), no over-redemption.Closes #92