XSS issues with server-rendered Angular templates
Dear Angular community,
we would greatly appreciate any comments on the issue below.
Overview of the issue:
When rendering Angular templates with a server-side templating engine like ERB or Haml it is easy to introduce XSS vulnerabilities. These vulnerabilities are enabled by AngularJS evaluating user-provided strings containing interpolation symbols (default symbols are {{ and }}).
The standard mitigation strategy by templating engines is to always escape meaningful symbols in strings (unless they are explicitely marked as "safe"). Unfortunately the inability to escape AngularJS interpolation symbols makes it hard to apply this strategy here.
Reproducing the issue:
We are using AngularJS for the frontend and Rails in the backend. While we have written full Single Page Applications with AngularJS, we are often embedding small snippets of AngularJS in server-rendered views. We love that AngularJS lets us replace jQuery spaghetti with simple code like this:
<div ng-show="detailsShown">
<%= user.profile %>
</div>
<span ng-click="detailsShown = true">Show details</span>
The problem is that the code snippet above enables Cross Site Scripting (XSS). If the server-parsed expression user.profile contains AngularJS markup like "Foo {{signOut()}} Bar", it will trigger functions on the current scope, allowing an attacker to trigger side effects on another computer.
Proposed fix:
The standard mitigation strategy by templating engines is to always escape symbols that have meaning for downsteam parsers, unless they are explicitely marked as "safe". E. g. if the server-parsed expression user.profile in the code example above would yield the string "Foo <script>alert('owned')</script> Bar", Rails, Haml, etc. would automatically replace the angle brackets with HTML entities.
To allow HTML tags to be rendered without escaping, the developer needs to explicitely mark a string as "safe" like so:
<%= user.profile.html_safe %>
(AngularJS actually uses this approach itself, when inserting the results of an evaluated Angular expression into the DOM.)
A good default for people rendering Angular templates using server-side templating engines would be to make templating engines automatically escape Angular interpolation symbols unless an evaluated string is explictely flagged as "safe". A patch for engines like ERB or Haml would be easily created, but unfortunately Angular's $interpolate service does not recognize any kind of escape symbol.
We propose that the $interpolate service should recognize escaped interpolation symbols like this:
Foo \{\{signOut()\}\} Bar
... or maybe a second pair of start/end symbols.
When encountering escaped interpolation symbols, the service would not evaluate the enclosed expression. Instead it would strip the escape symbols from the string.
(Note that we first tried to replace the curly braces with their HTML entities, which would have the added bonus of being output-neutral outside of an ng-app element. However, entitized braces are still recognized and their contents evaluated by Angular. We're not sure if the entities are expanded by Angular or by the browser.)
Plea to not dismiss this issue
We understand that this issue is not an actual bug in AngularJS, but (like all XSS issues) is caused by combining multiple technologies that are unaware of each other. Still we believe that AngularJS' ability to co-exist with non-Angular code in the same DOM is one of AngularJS greatest strengths. It sets Angular apart from frameworks like Ember, which need to own the entire page.
By offering a way to escape interpolation symbols (or maybe some other solution we didn't think of), AngularJS could be used safely in combination with upstream templating engines.
What do you think?
Regarding the proposal for a simpler escape/unescape grammar, I can try my hand at a pull request next week, building on @caitp's work.
@henning-koch I think that the most tricky part about conversion like what angular_xss does is rather a conceptual issue that the correct thing to do depends on what formats the input and the output of the converter are in. If the input is plain text and not HTML, then { literally means a string of six characters, and the converter must not treat it as an open brace. As a plaintext-to-plaintext conversion, or more precisely, conversion from plain text to the content of text node in the DOM for processing by Angular, the correct conversion (assuming your simpler rules on the Angular side) is replace {{ to {{{{ and }} to }}}}, and nothing else. But often the server stores some (restricted) HTML in the database, in which case { means an open brace and therefore the situation is different. The point is that the programmer has to be aware of what the input really is. I doubt that there is a catch-all solution.
@yuasakusa This means we might eventually have to change angular_xss to let developers express: "This string may contain HTML, but it may not contain Angular interpolations". Right now we only let developers choose between "this is a string safe for all purposes" and "this is a string unsafe for every interpretation whatsoever". So right now angular_xss might be a little inconvenient for developers who store actual HTML in the database, e.g. makers of CMS software.
But this change is entirely doable, and until then workarounds are available (like calling angular_xss before marking the string as safe). I believe the most important step right now is to make sure that the new default is to not allow accidental XSS. Err on the side of caution, and require explicit statements by the developer if the default should not be applied.
I was working with @henning-koch on the promised pull request. There is none yet, this is just an update:
We've run into some issues with the server-side part of our solution. Those are most likely solvable (at least for our own needs). Nevertheless, we don't want to make any changes to Angular before we've solved the full problem. Otherwise we might end up with something that doesn't even help ourselves.
There is also a strong possiblity we won't even need any Angular patch at all, by using a different escape mechanism:
- escape
{{with{{ DOUBLE_OPEN_BRACE }} - define
$rootScope.DOUBLE_OPEN_BRACE = '{{'
This would allow us to automatically escape {{ to something that renders correctly, it does not require a patch, and it has no problems with double escaping and just looks a bit more robust.
We'll keep you posted, but it's gonna take a bit longer.
@kratob That is a clever hack. I think that {{'{{'}} works equally with an advantage of not relying on any variables in scope. (Note that {{'}}'}} does not work, but I do not think that it is needed. Even if it is needed, you can write {{'}'+'}'}}.) I am not sure if people are entirely happy with these hacks as a long-term solution, but for now it is definitely useful to know some ways to correctly mix server-side and client-side processing which do not require any modification to Angular.
@yuasakusa The {{ DOUBLE_OPEN_BRACE }} seemed a bit safer than {{'{{'}}. I was mainly afraid of accidentally escaping
<el attr='{{'>
to
<el attr='{{'{{'}}'>
which would be invalid (and might allow XSS again). Maybe there is an alternative that is safe and doesn't require the constant.
@kratob Good point! I did not think of the case of attributes at all. My apologies. I think that {{'{{'}} or {{"{{"}} works as long as it is sent to the browser (without further server-side processing) and processed by Angular. However, this is probably more fragile than your method if the server performs some additional processing, so your method seems like a much safer bet.
(I commented on the change - https://github.com/angular/angular.js/pull/7517#issuecomment-47135479 - comment copied here. I appreciate how tricky escaping is and I want us to get it right! I'd be happy to submit pull requests for docs and/or code, if you approve.)
While this change thankfully makes it possible to write text that expands to {{, it makes it impossible to write text that expands to \{\{, as best I can tell. You could require all backslashes in Angular-processed text to be escaped (i.e. Angular converting \\ to \), and then it would be possible, albeit ugly, to write any literal string.
Alternately, the suggested workarounds in https://github.com/angular/angular.js/issues/5601#issuecomment-32104581 could be documented as the official way to escape Angular's curly braces. They appear to me to be a sufficient escaping system that is no more fragile than this commit's way of backslash-escaping Angular's interpolation markers. It's true that {{'{{'}} riskily adds single-quotes, and {{ DOUBLE_OPEN_BRACE }} requires DOUBLE_OPEN_BRACE to be defined. So you could add, say, NG_DOUBLE_OPEN_BRACE to the default scope environment to make it a solution anyone can easily use. Or make something else within curly brackets that's not syntactical JavaScript magically expand to {{; say, {{\}}.
I am new to this. Please help in below code
Use the ng-bind directive to bind the innerHTML of an element to a property in the data model.
here {{lastname}} is not giving any alert.
Please read https://angularjs.blogspot.com/2016/09/angular-16-expression-sandbox-removal.html for the most recent update.