Skip to main content

Why do people put code like "throw 1; <dont be evil>' and "for(;;);' in front of json responses?


Google returns json like this:




throw 1; <dont be evil> { foo: bar}



and Facebooks ajax has json like this:




for(;;); {"error":0,"errorSummary": ""}



  • Why do they put code that would stop execution and makes invalid json?

  • How do they parse it if it's invalid and would crash if you tried to eval it?

  • Do they just remove it from the string (seems expensive)?

  • Are there any security advantages to this?



In response to it being for security purposes:



If the scraper is on another domain they would have to use a script tag to get the data because XHR won't work cross-domain. Even without the for(;;); how would the attacker get the data? It's not assigned to a variable so wouldn't it just be garbage collected because there's no references to it?



Basically to get the data cross domain they would have to do




<script src="http://target.com/json.js"></script>



But even without the crash script prepended the attacker can't use any of the Json data without it being assigned to a variable that you can access globally (it isn't in these cases). The crash code effectivly does nothing because even without it they have to use server sided scripting to use the data on their site.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Even without the for(;;); how would the attacker get the data?


    Attacks are based on altering the behaviour of the built-in types, in particular Object and Array, by altering their constructor function or its prototype. Then when the targeted JSON uses a {...} or [...] construct, they'll be the attacker's own versions of those objects, with potentially-unexpected behaviour.

    For example, you can hack a setter-property into Object, that would betray the values written in object literals:

    Object.prototype.__defineSetter__('x', function(x) {
    alert('Ha! I steal '+x);
    });


    Then when a <script> was pointed at some JSON that used that property name:

    {"x": "hello"}


    the value "hello" would be leaked.

    The way that array and object literals cause setters to be called is controversial. Firefox removed the behaviour in version 3.5, in response to publicised attacks on high-profile web sites. However at the time of writing Safari (4) and Chrome (5) are still vulnerable to this.

    Another attack that all browsers now disallow was to redefine constructor functions:

    Array= function() {
    alert('I steal '+this);
    };

    [1, 2, 3]


    And for now, IE8's implementation of properties (based on the ECMAScript Fifth Edition standard and Object.defineProperty) currently does not work on Object.prototype or Array.prototype.

    But as well as protecting past browsers, it may be that extensions to JavaScript cause more potential leaks of a similar kind in future, and in that case chaff should protect against those too.

    ReplyDelete
  2. EDIT

    These strings are commonly referred to as an "unparseable curft" and they are used to patch an information leakage vulnerability that affects the JSON specification. This attack is real world and a vulnerability in gmail was discovered by Jeremiah Grossman. Mozilla also believes this to be a vulnerability in the JSON specification and it has been patched in Firefox 3. However because this issue still affects other browsers this "unparseable curft" is required because it is a compatible patch.

    Bobice's answer has a technical explanation of this attack and it is correct.

    ReplyDelete
  3. How do they parse it if it's invalid and would crash if you tried to eval
    it?


    It's a feature that it would crash if you tried to eval it. eval allows arbitary JavaScript code, which could be used for a cross-site scripting attack.


    Do they just remove it from the string (seems expensive)?


    I imagine so. Probably something like:

    function parseJson(json) {
    json = json.replace("throw 1; <dont be evil>", "");
    if (/* regex to validate the JSON */) {
    return eval(json);
    } else {
    throw "XSS";
    }
    }


    The "don't be evil" cruft prevents developers from using eval directly instead of a more secure alternative.

    ReplyDelete
  4. Just another case of superstition. I am sure someone will find a logically sounding rationalization for it.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex