fix(money): reject partial dollar amounts passed as numbers#45
Open
zqxuii wants to merge 1 commit into
Open
Conversation
The number branch of dollarsToCents rounded sub-cent values (e.g. 1.234 -> 123)
while the string path rejects the equivalent "$1.234" via MONEY_RE. Because the
auction/bid routes pass req.json() amounts straight through, a numeric JSON body
like {"amount": 5.005} bypassed the partial-amount guard added in moshcoder#40. Reject
sub-cent precision in the number path too; add regression tests.
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.
What
dollarsToCentsrejects sub-cent precision on the string path (MONEY_RE's\.\d{1,2}), so"$1.234"returnsnull. The number path had no equivalent guard and silently rounded:dollarsToCents(1.234)->123,dollarsToCents(5.005)->501.Why it matters
app/api/bidsandapp/api/auctionspassreq.json()values straight intodollarsToCents, and JSON permits numbers. So a numeric body bypasses the partial-amount guard added in #40:POST /api/bids {"amount": 5.005}-> accepted as501cents, while{"amount": "5.005"}-> rejected. Same money, opposite outcome based only on JSON type.{"amount": 0.999}rounds up to100, sneaking past theamountCents < 100"Minimum bid is $1" check with a sub-dollar bid.Fix
Reject sub-cent precision in the number branch too (
Math.abs(cents - Math.round(cents)) > 1e-6), mirroring the string path. Two-decimal and whole-dollar numbers still parse (2.5->250,1.99->199).Tests
Added a regression test asserting
1.234/5.005/0.999->nullwhile1/1.99/1234.56still parse.node --test tests/money.test.mjspasses; no other tests affected.