fix: generate coupon codes with a CSPRNG instead of Math.random - #12
Open
BrianWillows wants to merge 1 commit into
Open
fix: generate coupon codes with a CSPRNG instead of Math.random#12BrianWillows wants to merge 1 commit into
BrianWillows wants to merge 1 commit into
Conversation
randomSymbol() picked each character with Math.random(), which is not cryptographically secure - V8's xorshift128+ state can be recovered from a handful of observed outputs, so someone who receives a few issued coupons can predict or enumerate other valid codes (CWE-338). Since the fourth character of each part is a deterministic checksum, the whole code is a pure function of Math.random() output. Coupon codes are redeemable value, so they should not be guessable. Use crypto.randomBytes() with rejection sampling, which keeps the symbol distribution uniform (a plain modulo would bias it, as 2^32 is not a multiple of 32). Output format, checksums and the parts option are unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
randomSymbol()picks each code character withMath.random():Math.random()is not cryptographically secure — V8 implements it withxorshift128+, whose internal state can be recovered from a handful of observedoutputs. Because the fourth character of each part is a deterministic checksum,
the entire coupon is a pure function of
Math.random()output, so someone whoreceives a few issued coupons can predict or enumerate other valid codes
(CWE-338, use of a cryptographically weak PRNG).
Coupon codes gate redeemable value, so guessability matters here.
Fix
Draw the symbols from
crypto.randomBytes()with rejection sampling, whichkeeps the distribution uniform — a plain
% 32over a 32-bit value would biasthe symbol set.
cryptois a core module, so no new dependency is introduced.Verification
npm test), including allthe checksum-validation cases.
XXXX-XXXX-XXXX(Y03B-EH76-W2XM)validate()round-trip{parts: 4}still works (021G-G7HM-X3VU-8L0E, validates)Notes
Found and fixed with AI assistance (Claude). If you consider coupon codes
non-security by design (the
Algorithm::CouponCodeheritage is about typoresistance rather than unpredictability), feel free to close — but since the
codes are redeemable, a CSPRNG seemed the safer default. Happy to add a
regression test.