fix(usage): coerce and validate topup amount_usd as a finite number#40
Merged
ralyodio merged 1 commit intoJul 2, 2026
Merged
Conversation
/api/usage/topup read amount_usd raw from JSON and checked only `!amount_usd` then `amount_usd < 1 || amount_usd > 10000`. A non-numeric value like "abc" is truthy and both "abc" < 1 and "abc" > 10000 are false, so it slipped through and was forwarded to CoinPay and inserted into credit_deposits as a garbage amount (a numeric string like "50" was likewise stored as a string). Coerce with Number() and guard with Number.isFinite, matching the existing pattern in funding/create-invoice/route.ts. Add tests covering non-numeric, out-of-range, valid-number and numeric-string inputs.
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.
Problem
POST /api/usage/topupreadamount_usdraw from JSON and validated it with only!amount_usdandamount_usd < 1 || amount_usd > 10000. A non-numeric value like"abc"is truthy (passes the presence check) and both"abc" < 1and"abc" > 10000arefalse(NaN comparisons), so it slipped through and was forwarded tocreateCoinpayPaymentand inserted intocredit_depositsas a garbage amount. A numeric string like"50"was likewise stored as a string. (See #39.)Fix
Coerce with
Number()and guard withNumber.isFinite, matching the existing pattern infunding/create-invoice/route.ts(L33–36). Downstream now always receives a real number.Tests
Added
usage/topup/__tests__/route.test.ts(mirroring the existing auth route-test style): non-numeric"abc"→ 400 (and CoinPay/insert not called), out-of-range → 400, valid number → 200 with a numeric amount passed to CoinPay and the deposit row, and a numeric string coerced to a number.Fixes #39.