Welcome to another Vulnerability Explain breakdown. Today we explore Client-Side Template Injection (CSTI), a vulnerability that occurs when web applications embed untrusted user input directly into HTML containers parsed by client-side JavaScript templating frameworks such as AngularJS, Vue.js, or client-rendered template libraries. Unlike traditional Cross-Site Scripting (XSS), CSTI bypasses standard HTML entity sanitizers because payloads use benign template syntax that the client framework compiles into executable JavaScript. Let's break down how CSTI works, how to discover it, and how to eliminate it.
What is CSTI?
Client-Side Template Injection occurs when a server-side application (built with Django, Rails, PHP, Express, etc.) renders user-supplied data into an HTML page that is subsequently parsed by a frontend JavaScript framework.
Frontend frameworks like AngularJS and Vue.js scan the DOM for interpolation directives (such as {{ … }} or v-html). While a standard server-side HTML sanitizer safely encodes characters like < and > into < and >, it leaves curly braces {{ }} intact. When the browser executes the client-side framework, the framework compiles the DOM, encounters the user's template syntax, and evaluates it as an active JavaScript expression.
Attackers leverage sandbox escapes and prototype constructor chaining to break out of the framework scope and execute arbitrary JavaScript in the victim's browser, achieving full client-side compromise.
How does CSTI work?
CSTI unfolds across the boundary between server-side rendering and client-side framework execution:
- User Input Reflected by Backend
- The user submits input (e.g., in a search query, username, or comment) containing template interpolation syntax like {{ 7*7 }}.
- Server-Side Sanitizer Passes Braces
- The backend sanitizes HTML tags (), but a cloud WAF blocked the requests. The attacker realized that the user biography was rendered inside atag containing an ng-app directive.
The attacker updated their biography to: {{$on.constructor('alert(document.cookie)')()}}. The WAF allowed the update because curly braces and quotes did not match any traditional HTML script injection signatures. When administrative agents reviewed the user profile, AngularJS compiled the DOM, executed the payload, and exfiltrated admin session tokens.
Vulnerable and secure code of CSTI
The following example shows the contrast between vulnerable and secure code for CSTI. It helps you see how the flaw creeps into real code and the changes that shut it down.
🥺 Vulnerable Code:
<!-- Vulnerable: Server-side template renders user input directly inside an ng-app DOM container --> <!DOCTYPE html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> </head> <body> <div class="user-profile"> <!-- Django/PHP server-side reflection without escaping curly braces --> <h2>Welcome, {{ username }}!</h2> <div class="bio"> <!-- If user submits '{{$on.constructor("alert(1)")()}}', AngularJS evaluates it as code --> <p>Bio: {{ user_bio }}</p> </div> </div> </body> </html>- The backend server safely encodes HTML characters, but leaves curly braces {{ }} unescaped.
- Because the container has an active ng-app directive, AngularJS compiles all DOM children.
- Allows full sandbox breakout and arbitrary JavaScript execution in the victim's browser.
😎 Secure Code:
<!-- Secure: Uses ng-non-bindable and passes data safely via JSON attributes --> <!DOCTYPE html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script> </head> <body> <div class="user-profile"> <!-- Secure: ng-non-bindable tells AngularJS to skip template compilation for this subtree --> <div class="bio" ng-non-bindable> <p>Bio: {{ user_bio }}</p> </div> <!-- Alternative Secure Pattern: bind data via scope variables, never inline interpolation --> <div ng-controller="ProfileController"> <p ng-bind="safeBioText"></p> </div> </div> </body> </html>- The ng-non-bindable directive prevents the framework compiler from interpreting curly braces inside user text.
- Using ng-bind binds data strictly as text, ensuring that expressions are never compiled.
- A strict CSP forbidding 'unsafe-eval' prevents constructor-based sandbox breakouts.
Conclusion
Client-Side Template Injection demonstrates that XSS sanitizers focused solely on HTML markup are insufficient when modern JavaScript frameworks compile DOM content. Eliminating CSTI requires separating template markup from dynamic user data, leveraging framework non-bindable directives like ng-non-bindable and v-pre, and enforcing ahead-of-time (AOT) template compilation across production applications.
- The backend sanitizes HTML tags (), but a cloud WAF blocked the requests. The attacker realized that the user biography was rendered inside a