XSS without HTML: Client-Side Template Injection with AngularJS
AngularJS treats pages marked with ng-app as templates, so user input containing double curly braces is evaluated as an Angular expression even when it is HTML-encoded. The author found a way to overwrite native JavaScript functions from inside the Angular expression sandbox, which let him corrupt the HTML sanitiser and break out of the sandbox to run arbitrary JavaScript, giving XSS on otherwise secure sites.
Published on portswigger.net
Naive use of the extremely popular JavaScript framework AngularJS is exposing numerous websites to Angular Template Injection. This relatively low profile sibling of server-side template injection can be combined with an Angular sandbox escape to launch cross-site scripting (XSS) attacks on otherwise secure sites.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the AngularJS expression sandbox relied on native JavaScript functions such as charAt and fromCharCode behaving normally, so an attacker who could overwrite those functions from within an expression could make the parser and sanitiser produce attacker-controlled output.
What the attacker needed
Page uses AngularJS with ng-app; user input reflected into the template; attacker can inject curly braces
The fix
The sandbox escape was privately reported and patched in Angular 1.5.0, but the author argues updating Angular is not a robust fix and that curly braces in user input should be treated as dangerous or server-side reflection of user input avoided entirely.
How it was found
- Place a breakpoint in angular.js in the browser's sources tab and add a watch on fnString to see how an expression is rewritten.
- Try to reach the Function constructor from an expression and observe the ensureSafeObject checks that block it.
- Overwrite a native function such as String.fromCharCode or charCodeAt using an existing function like Array join, and confirm the backdoor works inside the sandbox.
- Use the overwritten charCodeAt to break the Angular sanitiser so it emits unfiltered attributes.
- Overwrite charAt and pass an expression to $eval so the rewritten code contains injected JavaScript.
- Adjust the injected string so the escape works in both Chrome and Firefox, then confirm the alert fires.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Some websites use a JavaScript library called AngularJS to build pages. AngularJS reads special markers in the page, such as double curly braces, and treats the text inside them as a small program to run. If a site puts text a visitor typed into such a page, the visitor can sneak in their own little program. The library tries to block dangerous programs with a set of checks called a sandbox, but the author found a trick to get around those checks and run real JavaScript, which is cross-site scripting.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.