Coinbase AngularJS DOM XSS via Kiteworks

8:45 AM

In this post, I will explore a dom based angular XSS I found in Coinbase and explain why the AngularJS bypass actually executes client side code.
AngularJS is a popular JavaScript framework that allows embedding expressions within double curly braces. For example, the expression {{2+2}} will render as 4, In angular, you can also use $eval('1+1') to evaluate anything, its same as {{1+1}}
We could try providing user-input of {{alert('hi')}}, but that would be blocked by Expression Sandboxing. that might seem like we are safe. However, despite appearing as a security feature, it was not really supposed to be for sandboxing anything for security.
The subdomain of coinbase, was running a service called Kiteworks that is developed by Accellion. by using angular.version on the site, we can see it's running AngularJS 1.4.7.
So lets submit {{alert('hi')}} and see why Angular doesn't execute the code. In earlier versions of AngularJS (<=1.0.8) , the bypasses were simpler.
Assume an Object a with properties b and c. (a = { b: 1, c: 2 }) -- we can check a has the property b by using the function hasOwnProperty('b') which shall return true. and a has automatic magic functions like toString() so functions are just properties of an object. which can be accessed using a.toString() or a["toString"](), in angular, the getterFn function creates a new function which tries to check the property alert from object before execute it.
As you may know getterFn creates a function that tries to get the property alert of the object. and Angluar got a scope that predefines everything that should be valid or not.  so accessing the scope of Angular with alert scope.hasOwnProperty('alert') shall return false or in angulars case becomes noop; which is just an empty function.  so basically, get property, understand and parse the evaluation and check scope and call it as a function.
This javascript sandbox will link {{document.window}} to scope.document which shall return false, {{document.confirm}} as scope.confirm, which is also false. something like {{name}} would be evaluated if it's known by the scope. which basically means we can't access global functions.
In earlier Angular versions,  breaking out of the scope object was not very complicated because the scope object is like any other javascript object. it automatically got magic functions like constructors. and the constructor of a scope object is object constructor.
As you probably have figured out, the constructor of the object constructor is a Function, which is the highest constructor in javscript. and we can create an arbitrary javascript function using the constructor, which will call a new javascript function. and we will get an alert by calling the highest constructor with an alert. we are simply doing:
scope.hasOwnProperty('constructor') -- evaluating true, which means by using constructor of the constructor of any object, we should be able to create a function with alert and execute that.
Angualr < =1.0.8: scope.constructor.constructor("alert(0)")() equivalent to {{constructor.constructor("alert(0)")()}} as each object gets attached to the scope anyways.
This was a clever and simple way found by Mario Heiderich to bypass the sandbox.
Now notice this payload is fixed and doesn't work on Angular 1.4.7, sending the payload and debugging and we can see that angular refuses to work with a function constructor.
As a fix and feature, Angular implemented lexical analysis, which is the process of converting a sequence of characters into a sequence of tokens, used in the process of compiling.
AngularJS implements a compiler that takes an angularjs expression and complies it to real javascript. A compiler in angular, compiling expressions to javascript. It uses the lexer function parser as a string and extract tokens (identifiers) from it using the while loop below.
lex: function(text) {
this.text = text;
this.index = 0;
this.tokens = [];
while (this.index < this.text.length) {
  var ch = this.text.charAt(this.index);
  if (ch === '"' || ch === "'") {
The while loop iterates the full length of the text, and in each iteration it uses text.CharAt() to get the next character of the string. And checks for single or double quotes because those are usually the start & end of a string. And after that the lex is just a load of helper functions trying to compile and help the lex and return an array of tokens.
If we try accessing the tokens after compiling, (this.tokens) they are just objects. the first one (this.tokens[0]) is the first character string because it’s a fixed string (text) if it starts with a quote or double quote. we can then use an identifier (another token) of the string.
Now notice while running the earlier sandbox bypass it doesn’t work and if you debug it you can see the error is coming from a new function called ensureSafeObject:
function ensureSafeObject(obj, fullExpression) {
 if (obj) {
if (obj.constructor === obj) {
This is a clever way to check if the param coming as obj is the function constructor which means we will keep ending up in the function of what we are accessing instead of the highest object despite how many constructors we add.
Note that every string we use is a decedent from the String() object. As prototype in javascript can be used to refer to the actual method or function that is inherited to all string objects.  If we access a string's constructor prototype, it will give us access to the String constructor.

By accessing a string's constrcutor, ('a'.constructor) we will call ensureSafeObject to check if the value is safe, and since it’s a string constructor, it is considered safe which will bypass ensureSafeObject.
We can then access the string constructor's prototype to access the main constructor. Defeating the whole purpose of angular's safechecks. We can then access the charAt the String constructor with a value that doesn't exist and assign it a built in conjuction function (join, concat...) which will also call ensureSafeObject as a text(string) consturctor, it is safe yet again. Then angular assigns that to an object using ensureSafeAssignContext. Since the checks are done using charAt, we would need to break it and redefine it to our own prefered unity function to chain our required payload.
We can reference the charAt function to all the other javascript functions inherit and assign a variable or function. We then can try to crash or redefine the charAt function because Angular uses charAt multiple times while calling checks and assigning expressions before compiling.
Final bypass: {{'a'.constructor.prototype.charAt=[].join;$eval('x=1} } };alert(1)//');}} by @garethheyes

Kiteworks, implemented by Coinbase was evaluating user-names sent with post using Angular. Which caused this AngularJS DOMXSS in

P.S: in the video, you can see a different sandbox bypass payload, this is because after initial report, they just updated the AngularJS version from 1.2.7 (when I recorded) to 1.4.7 as a fix but that one is also bypassed.

Angular starting from 1.6 have stopped the sandbox and one can easily break out of it using multiple ways.

You Might Also Like


  1. Yes, AngularJS is a popular JavaScript framework. Nice Post, thanks for sharing...

    App development company Dubai
    iPhone apps development Dubai

  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai


Note: Only a member of this blog may post a comment.