HTML Entity Substitution & XSS Prevention Invariants
When a web browser parses an HTML document, characters like < and > trigger DOM element creation. Escaping translates these characters into inert text entities.
1. HTML Entity Transposition Rule
Character (c)⟶&name;or&#decimal;
2. Core OWASP Sanitization Invariant
Sanitized(DOM)=∀ c ∈ { &, <, >, ", ' } ⟹ Entity(c)
Step-by-Step HTML Tag Escaping Breakdown
Step 1: Parse Raw Input String for Reserved Delimiters
Input:
<script>alert("Test")</script>Step 2: Replace Characters with Corresponding Named Entities
'<' → <
'>' → >
'"' → "
Step 3: Render Safe Markup Representation
Safe Escaped Text=<script>alert("Test")</script>
Standard HTML Entities Reference Table
| Glyph | Named Entity | Decimal Entity | Hex Entity | Standard Purpose |
|---|---|---|---|---|
| & | & | & | & | Ampersand delimiter |
| < | < | < | < | Tag opening delimiter |
| > | > | > | > | Tag closing delimiter |
| " | " | " | " | Attribute value delimiter |
| ' | ' / ' | ' | ' | Single quote attribute delimiter |