Skip to content

fix(airdrops): strict amount parsing in parseCSV - #137

Open
devkryssie wants to merge 1 commit into
SmartDropLabs:mainfrom
devkryssie:fix/134-parseCsv-strict-amount-parsing
Open

fix(airdrops): strict amount parsing in parseCSV#137
devkryssie wants to merge 1 commit into
SmartDropLabs:mainfrom
devkryssie:fix/134-parseCsv-strict-amount-parsing

Conversation

@devkryssie

Copy link
Copy Markdown

Summary

Fixes #134.

parseCSV() used parseFloat to parse recipient amounts, which silently truncates malformed strings and causes bad amounts to be silently accepted or bad rows to be silently dropped:

Input Before (parseFloat) After (strictParseAmount)
"1,000" (comma-formatted) Parsed as 1 — silently mangled Rejected with row-numbered error
"100USD" (trailing garbage) Parsed as 100 — silently mangled Rejected with row-numbered error
"" / missing Row silently dropped, no error Rejected with row-numbered error

Changes

src/routes/airdrops.js

  • Added strictParseAmount(raw) helper that validates against /^-?\d+(\.\d+)?$/ before accepting a value.
  • parseCSV() now throws AppError('VALIDATION_ERROR', recipient ${rowCount}: amount is missing or invalid..., 400) for any invalid amount, matching the row-numbered error style already used for invalid/duplicate addresses in the same handler.
  • Removed the silent if (address && !Number.isNaN(amount)) { results.push(...) } fallthrough.

test/airdrops.test.js

  • Added 3 tests covering all three scenarios from the issue: missing amount, comma-formatted amount, and trailing-garbage amount.

What was tested

  • Test suite run locally (CI will confirm — node_modules not available in the dev environment due to no network access).
  • Fork is in sync with upstream main (fast-forward, no divergence).

…#134

Replace parseFloat with strictParseAmount() helper that validates
amount strings against /^-?\d+(\.\d+)?$/ before accepting them.

Previously:
- parseFloat('1,000')  → 1  (silent truncation)
- parseFloat('100USD') → 100 (silent truncation)
- missing/empty amount → row silently dropped, no error

Now all three cases throw an AppError(VALIDATION_ERROR) with the
1-indexed row number and the raw value that failed, matching the
row-numbered error style used elsewhere in the same handler.

Tests added for all three malformed-amount scenarios.
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.

parseCSV() silently drops rows with unparseable amounts and lets parseFloat silently truncate malformed numeric strings

1 participant