Trust boundaries and input validation
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Allowlist the inventory input
function parseItem(value) { if (!value || typeof value !== 'object' || Array.isArray(value)) return null; const keys = Object.keys(value); if (keys.length !== 2 || !keys.includes('sku') || !keys.includes('quantity')) return null; if (typeof value.sku !== 'string' || !/^[A-Z0-9]{1,12}$/.test(value.sku)) return null; if (!Number.isInteger(value.quantity) || value.quantity < 1 || value.quantity > 100) return null; return {sku:value.sku,quantity:value.quantity};}for (const v of [{sku:'B1',quantity:2},{sku:'B1',quantity:'2'}, {sku:'B1',quantity:2,owner:'u2'},{sku:'B1',quantity:101}]) { console.log(JSON.stringify(parseItem(v)));}Expected output:
{"sku":"B1","quantity":2}nullnullnullThe server-side contract rejects incorrect types, unexpected fields and out-of-range values.
How it works
For JSON bodies, impose a byte limit before parsing; handle parse failures separately from schema failures. Require the expected object shape, reject unexpected fields for write operations, and construct a fresh allowlisted object. A quantity may be a number yet still invalid if fractional, negative or too large. Prefer a maintained schema validator for larger contracts. Keep SQL values parameterized and encode output for its actual destination. For outbound URLs use approved destinations and network policy; path normalization alone does not establish filesystem containment. Avoid unsafe recursive merging of untrusted data into configuration. The fixtures validate already-parsed toy objects or short strings; they are not HTTP body collectors or comprehensive security filters.
Distinguish byte limit and malformed JSON
function decode(raw) { if (Buffer.byteLength(raw,'utf8') > 16) return 413; try { JSON.parse(raw); return 200; } catch { return 400; }}console.log(decode('{"n":1}'));console.log(decode('{'));console.log(decode(JSON.stringify('é'.repeat(8))));Expected output:
200400413UTF-8 byte length, not JavaScript string length, determines this fixture limit. Parsing success does not imply schema validity.
Common mistakes
Never spread an entire request body into a persistence model. TypeScript types do not validate incoming JSON at runtime. Reject oversized bodies while reading the stream in a real server, not only after collecting them.
Practice and next steps
Add a maximum title length of 80 Unicode code points to a new title contract. Specify whitespace policy and test empty, valid and oversized strings.
Further reading: Primary documentation. Related staged reference: Request validation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Trust boundaries and input validation — knowledge check
Apply the security boundary and distinguish the example from a production control.
What should happen before parsing a streamed JSON body?
A quantity is the string "2" but the contract requires an integer. What should this validator do?
Which object should reach the business operation?