The Quiet Responsibility
S2 · Nº 16

4 MINMisha Tryndiuk

Never trust input – not even your own

Three years in a database do not make a value fit a new purpose. Follow an illustrative quantity from an old import, through an internal queue and into code with different rules. The question is what the number means where it ends up and what the rest of the code can rely on.

The half everyone learned

The row comes from our own database. Surely it has been checked already?

It is easy to be careful at the form and relax once the data is inside. The queue message comes from our own producer. The neighboring service is run by people who sit beside us.

But “our own” says little about what the receiver can actually rely on.

“Our own data” has a past

Take a value imported three years ago, before the current rules existed, and follow its history:

The old import accepted inventory adjustments, where the quantity could be negative. A later job reads the row from the database and sends it through an internal queue. At the other end, a service needs to use the quantity in a new order, where only positive values are valid. The source has sent exactly what is in the database. What changed is the use.

“Internal” describes where the data is now. The import got it into the database; it did not promise that the value would suit every piece of code you would later write.

Validate at the boundaries – your boundaries

The principle that replaces the half-rule: validate where the data contract or trust assumptions change, regardless of who sent it.

That may be at the entrance to a service, an import or a module receiving data under different assumptions. A value already checked and preserved in a precise type does not need the same check at every internal function call.

The boundary check rejects data that breaks the contract before it reaches logic relying on it. It does not cover everything. A compromised neighbor can send perfectly valid data and misuse the access it actually has; authentication and authorization decide how far that access reaches.

A valid value also has to be used safely. OWASP separates validation from safe use of values: SQL needs parameterized queries, and text displayed in HTML needs encoding for the right context.

For the old imported row, the boundary check means the error can be given a clear location at the entrance before it becomes a mysterious crash deep in the logic. The contract lives in executable code beside the service, where it can also be tested when expectations change.

Try it with one field from an internal source you trust. An order quantity of -3 is easy to read as an integer. If the order requires a positive quantity, you still do not have a valid value. Have the parser check both the shape and that business rule, then return a type that preserves what you checked. Limit size before parsing and nesting depth as you parse, so a huge or deeply nested message is refused before it exhausts memory or the stack.

Reject data that breaks the contract with an error that helps the sender correct it without revealing internal information; on a queue, set the message aside instead of retrying it. Log source, field and reason as controlled metadata, without the raw value. Read the rejections after a week: which come from a sender’s bug, changed assumptions or a contract you have made too strict?

That is the point of Alexis King’s parse, don’t validate: let the type retain what the check learned. Once the rest of the code receives a valid order quantity, it should be able to work without guessing whether a number from the import is valid for this order.

Three years after the import

“Never trust input” was never meant to stop at the form. The row from the import three years ago is still there, and none of the assumptions it was written under travel with it. Check what the contract requires where the value enters, including when the sender sits two desks away.

Your own data is just input with a better reputation.