Skip to content
BENIBIX Learn

Output Encoding

Development🔐 SecurityDefences

Also known as HTML Escaping, Output Escaping, HTML Encoding

Turning signs like < into safe codes. Then user text can go on a page.

Think of it like

Think of it like putting a toy sword in a glass case. People can see it, but nobody can swing it.

Example

Emma types <b>hi</b> as her name. The page encodes it, so other users see the tags as plain text, not bold words.

Why it matters

It turns an attacker's script into harmless words. Most modern frameworks do it for you by default.

Definition

Output encoding changes special characters before user text is put on a page. In HTML, < becomes &lt; and > becomes &gt;. The browser then shows the text instead of running it as code. Each place needs its own rules, so HTML, links and scripts are each encoded their own way. It is the main defence against cross-site scripting.

Example code

html
<!-- What the user typed -->
<script>alert(1)</script>

<!-- What the page writes -->
&lt;script&gt;alert(1)&lt;/script&gt;
Once encoded, the script tag shows up as text and never runs.
The right panel: the typed script tag is written as codes, so it shows as text.

Common mistake

Turning the safe default off to show rich text. That puts raw user text back on the page.

Key takeaways

  1. 1
    Encode on the way out. Just before text reaches the page.
  2. 2
    Match the place. HTML, links and scripts differ.
  3. 3
    Trust the framework. Keep its safe default on.

Compare