OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

Escape for the context you are in: an HTML output-encoding test

Escape for the HTML context you are in. Python fixtures run five payloads through three templates and three encoders, and show where escaping stops.

OOmnistu
Reviewed 2026-11-18 · 3 min read
Injected payload counts out of five: no encoding 2,1,4; angle-brackets-only 0,1,4; full quote escaping 0,0,4 for text, quoted and unquoted templates respectively.
Original Omnistu diagram of the article fixture. Scroll horizontally on a small screen. These synthetic test results do not certify a live system.

A comment shows up inside a <div>, and the same user's name is placed in an <input value="...">. Both are "HTML", yet the characters that break out of each one differ. A fix that works in one place can leave the other open.

The OWASP Cross Site Scripting Prevention Cheat Sheet says that the right protection depends on how the data is used, and that browsers parse HTML, JS, URLs and CSS differently, so the wrong encoding method can introduce weaknesses. For an HTML context it recommends HTML entity encoding. For an ordinary text attribute it says to use attribute encoding, always surround the value with quotation marks, and keep element and attribute names fixed. It also says attribute encoding alone does not protect JavaScript in an event-handler attribute, because the HTML parser decodes character references before the JavaScript is interpreted. This guide tests those statements with Python's html module. It was prepared with AI assistance and reviewed on 18 November 2026.

The method

Three templates: text (<div>{}</div>), a quoted attribute (<input value="{}">), and an unquoted attribute (<input value={}>). Five payloads: a script tag, a double-quote breakout with onfocus, a single-quote variant, a space followed by onfocus for the unquoted case, and an img tag with onerror. Three encoders: none, replacing only < and >, and html.escape(value, quote=True).

Python's html.parser reads each result. A payload counts as structurally injected if the ordered start-tag list differs from the template or the set of attribute names differs from the intended set. That is a narrow structural predicate: it does not execute JavaScript, inspect end tags, or generally validate duplicate attributes or attribute values. Separate round-trip assertions require the exact original text or single attribute value for the fully escaped, quoted cases. The parser is not a browser, so this shows how one HTML tokenizer reads the markup, and nothing about any browser.

QATTR = '<input value="{}">'
safe = QATTR.format(html.escape(value, quote=True))   # quoted, and quotes escaped

What the table shows

The result comes from the template, not the encoder alone. Quoting the attribute is part of the defense, and the test fails if it is dropped.

The limit that encoding does not fix

The last check puts ');alert(1);// through html.escape and into onclick="show('...')". The parser decodes the entities, and the decoded handler attribute text is show('');alert(1);//'). The input has broken the intended JavaScript string in the decoded handler text even though the HTML was escaped. The fixture does not invoke a JavaScript engine or demonstrate execution. This matches the sheet's second warning, and it is why the sheet prefers addEventListener() with trusted handlers and user values passed as data.

All checks pass on Python 3.10.12.

What this does not cover

It does not test a browser, script or style contexts, URL attributes such as href, CSS, or sanitizing user-authored HTML. It does not test a template engine's auto-escaping, which the sheet recommends starting with, and it does not cover Content Security Policy or DOM-based XSS. Five payloads are a sample, not a proof of safety. Treat the fixture as a teaching test of why context and quoting matter.

Sources