Skip to content

fix(money): reject partial dollar amounts passed as numbers#45

Open
zqxuii wants to merge 1 commit into
moshcoder:masterfrom
zqxuii:fix/money-partial-number
Open

fix(money): reject partial dollar amounts passed as numbers#45
zqxuii wants to merge 1 commit into
moshcoder:masterfrom
zqxuii:fix/money-partial-number

Conversation

@zqxuii

@zqxuii zqxuii commented Jul 21, 2026

Copy link
Copy Markdown

What

dollarsToCents rejects sub-cent precision on the string path (MONEY_RE's \.\d{1,2}), so "$1.234" returns null. The number path had no equivalent guard and silently rounded: dollarsToCents(1.234) -> 123, dollarsToCents(5.005) -> 501.

Why it matters

app/api/bids and app/api/auctions pass req.json() values straight into dollarsToCents, and JSON permits numbers. So a numeric body bypasses the partial-amount guard added in #40:

  • POST /api/bids {"amount": 5.005} -> accepted as 501 cents, while {"amount": "5.005"} -> rejected. Same money, opposite outcome based only on JSON type.
  • {"amount": 0.999} rounds up to 100, sneaking past the amountCents < 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 -> null while 1 / 1.99 / 1234.56 still parse. node --test tests/money.test.mjs passes; no other tests affected.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant