OWASP Output Encoding

Input validation guards the entrance. Output encoding guards the exit. Output encoding converts risky characters into safe text right before the application places data into a web page, so the browser shows the data instead of running it as code.

The Problem It Solves

Browsers read special characters such as the less-than sign as the start of an HTML tag. If an application prints user text without care, a visitor can type a tag that the browser treats as part of the page. Cross-site scripting grows from this confusion between data and code.

A movie subtitle offers a useful comparison. The subtitle shows spoken words on screen but never directs the film. Encoding turns user text into subtitles: visible, harmless, and unable to give orders to the browser.

How Encoding Works

 User text:        Tom & <b>Jerry</b>
        |
        ↓
+-------------------------+
|     Output encoder      |
+-------------------------+
        |
        ↓
 Safe HTML sent to browser:   Tom &amp; &lt;b&gt;Jerry&lt;/b&gt;
        |
        ↓
 Browser displays:            Tom & <b>Jerry</b>   (plain text, no bold)

Common Character Replacements for HTML

CharacterSafe Replacement
<&lt;
>&gt;
&&amp;
"&quot;
'&#x27;

Context Matters

The correct encoding depends on where the data lands. A method that protects an HTML paragraph can fail inside a script block or a web address.

Output LocationEncoding to Use
HTML body textHTML entity encoding
HTML attribute valueAttribute encoding with quotes around the value
JavaScript dataJavaScript string encoding, or pass data as JSON
URL parameterPercent encoding
CSS valueCSS hex encoding

Use Framework Features

Modern template engines encode output by default. Developers lose that protection when they switch it off for convenience. Search your code for unsafe helpers and raw-output switches, and review each one carefully.

Safe Pattern

<p>Hello, {{ user_name }}</p>

The template engine encodes user_name automatically.

Risky Pattern

<p>Hello, {{ user_name | raw }}</p>

The raw filter turns off encoding. Use it only for content that your own code created and trusts.

When Users Need Rich Text

Some applications let users format comments with bold or links. Plain encoding would show the tags as text. Use a well-maintained HTML sanitizer library with a strict allowlist of tags and attributes. Never write your own sanitizer.

Encoding Versus Validation

Beginners often mix the two ideas. Validation decides whether the application accepts a value. Encoding decides how the application displays a value. A comment such as "5 &lt; 10" passes validation because it is ordinary text, and encoding keeps it safe on the page. The two controls answer different questions, and a secure application uses both.

Best Practices

  1. Encode at the moment of output, not at the moment of storage.
  2. Pick the encoder that matches the output context.
  3. Rely on framework defaults and trusted libraries.
  4. Add a Content Security Policy header as a second layer of defense.
  5. Test pages with harmless special characters to confirm they display as text.

Key Takeaways

  • Encoding turns user data into inert text for the browser.
  • Each output context needs its own encoding rules.
  • Framework defaults provide strong protection when developers keep them on.
  • Sanitizer libraries handle rich text safely.