Template Injection in Client-Side Frameworks
What you'll learn: How attackers exploit unsafe template interpolation in modern JavaScript frameworks to execute arbitrary code in the browser.
The Core Idea
Modern front-end frameworks like Angular, Vue, and React use special template syntax to dynamically insert data into HTML. When user-controlled input flows directly into these template expressions without proper sanitization, attackers can inject malicious code that the framework will execute.
Think of it like a mad-libs game gone wrong: the framework expects you to fill in the blanks with safe words, but an attacker inserts entire sentences that change the story's meaning.
How It Works
Frameworks use special delimiters to mark dynamic content:
- Angular:
{{ expression }} - Vue:
{{ expression }} - React JSX:
{expression}(though React has better default protections)
When user input appears inside these contexts without escaping, attackers can inject JavaScript expressions. For example, in Angular:
{{constructor.constructor('alert(1)')()}}
This leverages JavaScript's prototype chain to access the Function constructor and execute arbitrary code.
Why It's Different from Regular XSS
Template injection exploits the framework's own evaluation engine. Unlike traditional XSS that relies on HTML parsing, template injection abuses the framework's client-side rendering logic. The framework itself becomes the attack vector because it's designed to evaluate expressions—attackers just provide malicious ones.
Common Vulnerable Patterns
Directly binding user input to template expressions without sanitization, using unsafe HTML directives (like Angular's innerHTML binding or Vue's v-html), or passing unsanitized data through URL parameters that feed into templates.
Key Takeaway: Template injection exploits framework-specific expression evaluation by inserting malicious code into unsafe interpolation contexts—always sanitize user input before it reaches template expressions and avoid direct HTML binding with untrusted data.