Filed under: javascript

Securing JS - "window.location" can we trust in it ?

We need a way to sell JS widgets and license these on a domain basis.

The idea is to provide a key which locks the widget to a given domain.

Basically we will be using window.location.host (and/or others similar like "hostname").

So, for instance  if we do

window.location.host == "mydomain.com"

we can lock the widget to this domain. Or can we ?

window.location.host = "localhost"

doesn't work cause the setter will cause the browser to navigate to "localhost". But what if we did (using Google Chrome):

window.location.__defineGetter__("host", function(){return "localhost";})

now window.location.host returns "localhost"!

The solution would be to use __lookupGetter__

window.location.__lookupGetter__("host")

If you call this before defining you own getter you get "undefined" but after defining the getter it returns a function. This way you can see if they tampered with the getter.

But what if they did

window.location.__lookupGetter__ = function(name){ return undefined;}

Now our previous method doesn't work!

We should do a 

/\[native code\]/.test(window.location.__lookupGetter__.toString())

 to make sure they haven't tampered with the __lookupGetter__ function.

Apparently Firefox doesn't provide __defineGetter__ and __lookupGetter__ for native objects like window.location so you can trust it....

We will be experimenting with this further but it just shows that JS is not to be trusted when it comes to securing your apps or widgets 

RIA Wars

The growing bandwith has allowed developers to provide rich Web based user interfaces and thus bring together Web and Dektop applications. You have Javascript with several new libraries sprouting almost everyday, this allows us to bring live to otherwise static HTML pages through manipulation of the DOM and using AJAX calls to get data from the server. Nonetheless javascript depends on the browser's implementation and your source code is there for anyone to see. AJAX calls are pretty cool but XML is a major overhead with large messages and slow parsing. Adobe has a huge browser penetration, close to 98% if i'm not mistaken. Most people dislike Flash and think it is only used by designers to create sexy and annoying banners. The truth is that Flash uses Mozilla's Tamarin VM for running ActionScript. Both Javascript and ActionScript are ECMAScript but with ActionScript you have the advantage that it get's compiled and your source code is not there for anyone to see (unless they disassemble it). Besides, to reproduce most of the functionality you get with Flash you need a lot of javascript libraries, especially if we're using Flex. Flex provides us a growing number of prebuild components and the UI is specified using MXML with offers a clear separation of controller and view. What's more upcoming versions of Flash will enable browsers to cache commonly used flash modules (like the Flex framework) so, you only download the library the first time you visit a site using Flex. The AMF protocol, used by Flash is now Open and there is a reference implementation freely available. This binary protocol is a lot faster than using XML or JSON and it supports pushing information thus we don't need to have clients constantly pooling our server. There are some more contenders in this RIA war, JavaFX and Silverlight. JavaFX suffers, for now, from the same problems every JAVA applet has. It does not fit well in a page, it takes too long to load and there are still many people without JAVA. When JAVA offers an easier instalation and preloads in the OS's memory then we could have a serious alternative but for now.. well it is just not up to the challenge. Silverlight has some cool ideas, it allows us to program in a myriad of languages since they share a common runtime, has hardware acceleration and, since it is from Microsoft will come preinstalled in Windows machines. Still, there is no real showcase (Microsoft is using it to build a part of their site now in beta), there have been a couple of releases which were incompatible with each other (you had to keep reinstalling the darn thing), and to get a development environment up and ready it took ages just to figure out the right one! I really like the idea of having Desktop like applications on the Web but i'd also like to have them in my desktop. Most of these allows developing both for the Web and for the Desktop but Flash and Java might just have an edge on this one. Adobe AIR allows you to develop some pretty cool desktop apps and JAVA has been improving Swing with LaF. To sum up, I like the fact that Javascript has been evolving at a fast pace but I still feel stranded by browser dependencies, I don't like to have the source code exposed and I just think that Adobe's offer is a better deal. ActionScript is a pretty decent language and the Flex framework offers us a lot of cool components. Add to that the AMF protocol with the Open Source Data Services and you have a very good solution for RIA development. What's more you have a couple of 3D libraries,Thermo's coming up ( pixel shaders ;) ) and we'll get hardware acceleration!