title: JSON security best practices?
body: <p>While researching the issue of <a href="http://www.subbu.org/blog/2006/08/json-vs-xml" rel="noreferrer">JSON vs XML</a>, I came across <a href="https://stackoverflow.com/questions/325085/when-to-prefer-json-over-xml">this question</a>.  Now one of the reasons to prefer JSON was listed as the ease of conversion in Javascript, namely with the <code>eval()</code>.  Now this immediately struck me as potentially problematic from a security perspective.</p>
  
  <p>So I started doing some research into the security aspects of JSON and across this blog post about how <a href="http://incompleteness.me/blog/2007/03/05/json-is-not-as-safe-as-people-think-it-is/" rel="noreferrer">JSON is not as safe as people think it is</a>.  This part stuck out:</p>
  
  <blockquote>
    <p><strong>Update:</strong> 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's looking at
    a block rather than an object. This
    goes a long way to protecting against
    these attacks, however it's still best
    to protect your secure data with
    un-predictable URLs.</p>
  </blockquote>
  
  <p>Ok, so that's a good rule to start with: JSON objects at the top level should always be objects and never arrays, numbers or strings.  Sounds like a good rule to me.</p>
  
  <p>Is there anything else to do or avoid when it comes to JSON and AJAX related security?</p>
  
  <p>The last part of the above quote mentions unpredictable URLs.  Does anyone have more information on this, especially how you do it in PHP?  I'm far more experienced in Java than PHP and in Java it's easy (in that you can map a whole range of URLs to a single servlet) whereas all the PHP I've done have mapped a single URL to the PHP script.</p>
  
  <p>Also, how exactly do you use unpredictable URLs to increase security?</p>