Resources

Secure Your App With Input Validation And Output Escape

Secure Your App With Input Validation And Output Escape

Input validation cannot stop cross-site scripting by itself, and neither can output escaping. A comment box or a bio field has to accept characters like < and an apostrophe, because real people type them, so a validator cannot simply reject them. XSS ranks first on MITRE’s 2024 Common Weakness Enumeration list. Output escaping is the control that actually holds; validation narrows what reaches it.

Why does input validation fail on its own?

Because free text exists.

A classic attack script such as <script>alert(‘malicious code’)</script> is just a string until some page renders it as HTML, and once it runs in a visitor’s browser it can plant malware, read personal data or take over the account. Fake login pages built through XSS are a common route into phishing, where the loss is the user’s money and your reputation.

Exploding Topics’ roundup of phishing statistics puts the median amount lost at about $50,000 when phishing leads to a transaction. Staff awareness helps too (Huntress has a short guide on how to spot phishing attempts), but it is a separate defense from the code.

Validation is the weaker control here, or rather, it is weaker only for free text. On fields with a fixed shape it earns its place:

  • email addresses, which need an @ and a real domain
  • IDs and enum values, which should match a known list exactly
  • dates and numbers
  • usernames, where a length limit and a character set are easy to enforce

Passwords are the exception to the old habit of forcing a mix of letters, digits and symbols. NIST’s current guidance says verifiers should not impose composition rules like that, so a six-character mixed-case rule is more annoying than protective.

Escape output for the context it lands in

Escaping keeps the data exactly as the user typed it and tells the browser to treat it as text. The same character is harmless in one place and dangerous in another: an apostrophe in a message is fine in HTML body text and can break out of a JavaScript string or an attribute. Which means the escape has to match the destination (HTML body, attribute, JavaScript, URL), not just run once on the way in.

Flow from user input through validation and storage to context-aware escaping, so a script tag is displayed as text

In PHP, htmlspecialchars() does this for HTML output, and its defaults convert both single and double quotes. Obviously, you want escaping on by default, and most template engines already do that, so the real bug is almost always the one place someone switched it off.

YouTube player

Add a Content Security Policy as backup

A Content-Security-Policy header limits where scripts may load from and blocks inline scripts unless you allow them. It will not fix an unescaped output, but it can turn an exploitable one into a harmless console error. The catch is rollout. A policy written for a site full of inline scripts and third-party widgets breaks pages on day one, so teams usually start in report-only mode, read the violations, and tighten the policy over a few releases. Skipping that step is one way to end up with a policy so loose it blocks nothing.

Allowlist or blocklist

An allowlist (the old “whitelist”) accepts only the format you define and rejects the rest. A blocklist tries to catch bad characters such as < and >. Attackers keep finding encodings, tags and event handlers the list missed, and every new browser feature adds a few more, so a blocklist should never be your only layer. Is there any field where a blocklist is the better design?

Treating every input as untrusted is the same instinct behind zero trust, which a Forbes Technology Council piece covers in the context of zero-day attacks.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.