HardPro challengeJavaScriptTypeScript

Avoid eval() for Parsing Input

Node.jsSecurity

The function below parses three numeric form fields using eval().

The bug: eval() runs its argument as JavaScript with full access to the current scope. Any user who can control the input string can run arbitrary code on the server — this is the exact bug found in OWASP's NodeGoat (a deliberately-vulnerable Node.js app used for security training), in the endpoint that handles retirement contribution updates:

const preTax = eval(req.body.preTax);
const afterTax = eval(req.body.afterTax);
const roth = eval(req.body.roth);

Your task: fix solve(preTaxInput, afterTaxInput, rothInput) so it returns { preTax, afterTax, roth } with each field safely parsed as a number — using null for any input that isn't a valid, finite number.

solve('500', '1200', '0')  // → { preTax: 500, afterTax: 1200, roth: 0 }
solve('1 + 1', '0', '0')   // → { preTax: null, afterTax: 0, roth: 0 }  (never executed as code)

Sample tests

Test #1Valid numeric strings
Input: ["500","1200","0"]
Output: {"roth":0,"preTax":500,"afterTax":1200}
Test #2Expression string — rejected, not evaluated
Input: ["1 + 1","0","0"]
Output: {"roth":0,"preTax":null,"afterTax":0}
Test #3Code-execution payload — stays inert, never runs
Input: ["require('child_process').execSync('whoami')","0","0"]
Output: {"roth":0,"preTax":null,"afterTax":0}