00:00

#0603

JSON Type Confusion

Medium+100 XPA08:2021 Software and Data Integrity FailuresCWE-502
Type ConfusionInput Validation

Scenario

Your payment endpoint parses a JSON body and immediately uses the `amount` field — trusting that JSON parsing enforces the type.

But JSON allows `amount` to be a string, null, or negative number. Without explicit type validation, a string like `'100; DROP TABLE orders'` can reach downstream SQL builders, and a negative amount can result in credits instead of charges.

Type confusion bugs are among the easiest to exploit — an attacker just changes the JSON value type. They've caused real-world payment bypasses and SQL injections at scale.

Your Tasks

  1. After parsing the JSON string, validate that `amount` is a finite positive number.
  2. Throw `'Invalid amount'` if `amount` is a string, null, negative, zero, or non-finite.
  3. Return `{status: 'ok', charged: amount}` for valid inputs.

Examples

Example 1Exploit — string injection in amount field

processPayment('{"amount":"100; DROP TABLE"}')
// throws: 'Invalid amount'

Example 2Safe — valid decimal amount

processPayment('{"amount":99.99}')
// returns: {status:'ok', charged:99.99}

Constraints

  • Throw exactly `'Invalid amount'`.
  • Zero and negative values must be rejected.
  • Infinity and NaN must be rejected.

Hint

References

solution.js
Ln 1, Col 1UTF-8JavaScript
Sandbox ready
0/0/0not run