Validate, Don't Sanitize
Sanitization is almost always a mistake, and it’s important to understand why.
Advice to sanitize input is maddeningly common in security advice. It’s always a mistake, and it’s a mistake that collects compounding technical debt interest.
Let’s start with what you should do. Check that input is what you expect. If it’s not, issue a specific error like “malformed input at byte X. See our spec at URL.” (This advice doesn’t apply to web browsers, and we’ll come to that.)
The most important reason is that when you sanitize, you’re parsing input that doesn’t meet your spec. Let me repeat that: you’re parsing input that doesn’t meet your spec. And so you’re helpfully trying to make it right. The trouble is, it’s out of spec. And getting it in spec carries both a security hazard (you might bypass your own checks) and a reliability hazard (you might not fix it in alignment with the caller’s intent even if they’re not attacking).
A security example would be: if you filter out slashes, and then do a URL decoding, your %2F will become a slash, and that slash might get passed to somewhere that your code treats as a path. Of course, it’s possible to get this right every time. The work to get that right and to build unit tests to ensure it’s right, grows with each such element. The portion of your codebase and build time dedicated to security special cases grows without bounds.
The debt isn’t just yours. Once you accept bad input, your sanitizer is subject to a perversely ugly version of Hyrum’s Law: you’re committing to parse input that’s now included in the unwritten part of your spec.
The best you can get from all the investment is the chance to invest more, which is, admittedly, sometimes worth it.
That brings me back to web browsers. In the first browser war, they did their
damnedest to turn bad HTML into properly displayed web pages. They
did that because if the browser parsed carefully, it was “broken.”
The user
would open their other browser and not switch back until the other
browser broke didn’t display a page. This contributed
to it being so expensive to create and maintain a browser that
only a few companies make the investment.
It might be tempting to try to parse more of the input, so you can provide “more useful” errors. And again, you’re parsing input your code doesn’t understand. Enjoy! Now, that’s not to say you can’t put a copy of your parser on the far side of a boundary to provide a fast, local test. But you cannot trust that that parser ran, or that its output is unmodified.
Sometimes people argue that the robustness principle of “Be conservative in what you send and liberal in what you accept” implies sanitization. It does not. You can be liberal about what you accept and robustly send “malformed input” errors. The input is accepted and handled robustly.
The distinction between validation and sanitization is an example of how to code denseively, which is a layer of the Swiss cheese model in the new book.
As I say in Threats: What Every Engineer Should Learn from Star Wars, you can never quite get the stink of Tauntaun out of your clothes. Stop trying.
Image by Gemini Nano Banana 2: "A watercolor painting of the front panel of a pale cream vending machine, with a round coin slot and a small coin return tray. A single bent lime green coin sits in the return tray, rejected. Deep indigo purple only as thin outlines around the slot and tray and in small shadows, bright orange trim along the panel edge, red-orange highlights on the coin return button. Mostly warm cream and white, light and airy."