Why does Google prepend while(1); to their JSON responses?












3657















Why does Google prepend while(1); to their (private) JSON responses?



For example, here's a response while turning a calendar on and off in Google Calendar:



while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
['remindOnRespondedEventsOnly','true'],
['hideInvitations_remindOnRespondedEventsOnly','false_true'],
['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]


I would assume this is to prevent people from doing an eval() on it, but all you'd really have to do is replace the while and then you'd be set. I would assume the eval prevention is to make sure people write safe JSON parsing code.



I've seen this used in a couple of other places, too, but a lot more so with Google (Mail, Calendar, Contacts, etc.) Strangely enough, Google Docs starts with &&&START&&& instead, and Google Contacts seems to start with while(1); &&&START&&&.



What's going on here?










share|improve this question




















  • 35





    I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

    – Esteban Küber
    Apr 19 '10 at 18:04






  • 17





    probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

    – Gizmo
    Feb 16 '17 at 18:51








  • 1





    Would prevent eval, but not with an infinite loop.

    – Mardoxx
    May 6 '17 at 20:27








  • 5





    This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

    – Gras Double
    Jul 8 '17 at 20:55






  • 2





    Inorder to prevent disclosure of json i.e. JSON hijacking

    – Ashraf.Shk786
    Aug 8 '17 at 8:05
















3657















Why does Google prepend while(1); to their (private) JSON responses?



For example, here's a response while turning a calendar on and off in Google Calendar:



while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
['remindOnRespondedEventsOnly','true'],
['hideInvitations_remindOnRespondedEventsOnly','false_true'],
['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]


I would assume this is to prevent people from doing an eval() on it, but all you'd really have to do is replace the while and then you'd be set. I would assume the eval prevention is to make sure people write safe JSON parsing code.



I've seen this used in a couple of other places, too, but a lot more so with Google (Mail, Calendar, Contacts, etc.) Strangely enough, Google Docs starts with &&&START&&& instead, and Google Contacts seems to start with while(1); &&&START&&&.



What's going on here?










share|improve this question




















  • 35





    I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

    – Esteban Küber
    Apr 19 '10 at 18:04






  • 17





    probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

    – Gizmo
    Feb 16 '17 at 18:51








  • 1





    Would prevent eval, but not with an infinite loop.

    – Mardoxx
    May 6 '17 at 20:27








  • 5





    This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

    – Gras Double
    Jul 8 '17 at 20:55






  • 2





    Inorder to prevent disclosure of json i.e. JSON hijacking

    – Ashraf.Shk786
    Aug 8 '17 at 8:05














3657












3657








3657


1711






Why does Google prepend while(1); to their (private) JSON responses?



For example, here's a response while turning a calendar on and off in Google Calendar:



while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
['remindOnRespondedEventsOnly','true'],
['hideInvitations_remindOnRespondedEventsOnly','false_true'],
['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]


I would assume this is to prevent people from doing an eval() on it, but all you'd really have to do is replace the while and then you'd be set. I would assume the eval prevention is to make sure people write safe JSON parsing code.



I've seen this used in a couple of other places, too, but a lot more so with Google (Mail, Calendar, Contacts, etc.) Strangely enough, Google Docs starts with &&&START&&& instead, and Google Contacts seems to start with while(1); &&&START&&&.



What's going on here?










share|improve this question
















Why does Google prepend while(1); to their (private) JSON responses?



For example, here's a response while turning a calendar on and off in Google Calendar:



while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
['remindOnRespondedEventsOnly','true'],
['hideInvitations_remindOnRespondedEventsOnly','false_true'],
['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]


I would assume this is to prevent people from doing an eval() on it, but all you'd really have to do is replace the while and then you'd be set. I would assume the eval prevention is to make sure people write safe JSON parsing code.



I've seen this used in a couple of other places, too, but a lot more so with Google (Mail, Calendar, Contacts, etc.) Strangely enough, Google Docs starts with &&&START&&& instead, and Google Contacts seems to start with while(1); &&&START&&&.



What's going on here?







javascript json ajax security






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 30 '17 at 21:20









NH.

89311130




89311130










asked Apr 19 '10 at 18:00









JessJess

20.7k63049




20.7k63049








  • 35





    I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

    – Esteban Küber
    Apr 19 '10 at 18:04






  • 17





    probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

    – Gizmo
    Feb 16 '17 at 18:51








  • 1





    Would prevent eval, but not with an infinite loop.

    – Mardoxx
    May 6 '17 at 20:27








  • 5





    This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

    – Gras Double
    Jul 8 '17 at 20:55






  • 2





    Inorder to prevent disclosure of json i.e. JSON hijacking

    – Ashraf.Shk786
    Aug 8 '17 at 8:05














  • 35





    I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

    – Esteban Küber
    Apr 19 '10 at 18:04






  • 17





    probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

    – Gizmo
    Feb 16 '17 at 18:51








  • 1





    Would prevent eval, but not with an infinite loop.

    – Mardoxx
    May 6 '17 at 20:27








  • 5





    This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

    – Gras Double
    Jul 8 '17 at 20:55






  • 2





    Inorder to prevent disclosure of json i.e. JSON hijacking

    – Ashraf.Shk786
    Aug 8 '17 at 8:05








35




35





I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

– Esteban Küber
Apr 19 '10 at 18:04





I believe that your first impression is correct. If you start looking for code and try to trim the input stream depending on the source, you'd reconsider and do it the safe (and because of Google's actions, easier) way.

– Esteban Küber
Apr 19 '10 at 18:04




17




17





probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

– Gizmo
Feb 16 '17 at 18:51







probably a follow-up question: Why does google prepend )]}' now instead of while(1);? Would the answers be the same?

– Gizmo
Feb 16 '17 at 18:51






1




1





Would prevent eval, but not with an infinite loop.

– Mardoxx
May 6 '17 at 20:27







Would prevent eval, but not with an infinite loop.

– Mardoxx
May 6 '17 at 20:27






5




5





This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

– Gras Double
Jul 8 '17 at 20:55





This )]}' may also be to save bytes, like facebook used for(;;); which saves one byte :)

– Gras Double
Jul 8 '17 at 20:55




2




2





Inorder to prevent disclosure of json i.e. JSON hijacking

– Ashraf.Shk786
Aug 8 '17 at 8:05





Inorder to prevent disclosure of json i.e. JSON hijacking

– Ashraf.Shk786
Aug 8 '17 at 8:05












6 Answers
6






active

oldest

votes


















3887














It prevents JSON hijacking, a major JSON security issue that is formally fixed in all major browsers since 2011 with EMCA5.



Contrived example: say Google has a URL like mail.google.com/json?action=inbox which returns the first 50 messages of your inbox in JSON format. Evil websites on other domains can't make AJAX requests to get this data due to the same-origin policy, but they can include the URL via a <script> tag. The URL is visited with your cookies, and by overriding the global array constructor or accessor methods they can have a method called whenever an object (array or hash) attribute is set, allowing them to read the JSON content.



The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error.



This does not address the issue of cross-site request forgery.






share|improve this answer





















  • 213





    Why doesn't the request to obtain this data require a CSRF-token instead?

    – Jakub P.
    Feb 3 '13 at 1:43






  • 214





    Does for(;;); do the same job? I've seen this in facebook's ajax responses.

    – King Julien
    Feb 4 '13 at 8:27






  • 163





    @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

    – abraham
    Feb 5 '13 at 5:12






  • 116





    @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

    – bluesmoon
    Feb 5 '13 at 6:10






  • 87





    It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

    – mcv
    Dec 30 '13 at 15:06



















481














It prevents disclosure of the response through JSON hijacking.



In theory, the content of HTTP responses are protected by the Same Origin Policy: pages from one domain cannot get any pieces of information from pages on the other domain (unless explicitly allowed).



An attacker can request pages on other domains on your behalf, e.g. by using a <script src=...> or <img> tag, but it can't get any information about the result (headers, contents).



Thus, if you visit an attacker's page, it couldn't read your email from gmail.com.



Except that when using a script tag to request JSON content, the JSON is executed as Javascript in an attacker's controlled environment. If the attacker can replace the Array or Object constructor or some other method used during object construction, anything in the JSON would pass through the attacker's code, and be disclosed.



Note that this happens at the time the JSON is executed as Javascript, not at the time it's parsed.



There are multiple countermeasures:



Making sure the JSON never executes



By placing a while(1); statement before the JSON data, Google makes sure that the JSON data is never executed as Javascript.



Only a legitimate page could actually get the whole content, strip the while(1);, and parse the remainder as JSON.



Things like for(;;); have been seen at Facebook for instance, with the same results.



Making sure the JSON is not valid Javascript



Similarly, adding invalid tokens before the JSON, like &&&START&&&, makes sure that it is never executed.



Always return JSON with an Object on the outside



This is OWASP recommended way to protect from JSON hijacking and is the less intrusive one.



Similarly to the previous counter-measures, it makes sure that the JSON is never executed as Javascript.



A valid JSON object, when not enclosed by anything, is not valid in Javascript:



eval('{"foo":"bar"}')
// SyntaxError: Unexpected token :


This is however valid JSON:



JSON.parse('{"foo":"bar"}')
// Object {foo: "bar"}


So, making sure you always return an Object at the top level of the response makes sure that the JSON is not valid Javascript, while still being valid JSON.



As noted by @hvd in the comments, the empty object {} is valid Javascript, and knowing the object is empty may itself be valuable information.



Comparison of above methods



The OWASP way is less intrusive, as it needs no client library changes, and transfers valid JSON. It is unsure whether past or future browser bugs could defeat this, however. As noted by @oriadam, it is unclear whether data could be leaked in a parse error through an error handling or not (e.g. window.onerror).



Google's way requires client library in order for it to support automatic de-serialization and can be considered to be safer with regard to browser bugs.



Both methods require server side changes in order to avoid developers from accidentally sending vulnerable JSON.






share|improve this answer





















  • 19





    OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

    – funroll
    Mar 15 '14 at 1:47






  • 14





    I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

    – vaxquis
    Apr 12 '14 at 15:54






  • 19





    It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

    – Manngo
    Nov 8 '15 at 10:59






  • 10





    Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

    – oriadam
    Dec 6 '15 at 23:06






  • 7





    "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

    – user743382
    May 6 '17 at 20:50



















349














This is to ensure some other site can't do nasty tricks to try to steal your data. For example, by replacing the array constructor, then including this JSON URL via a <script> tag, a malicious third-party site could steal the data from the JSON response. By putting a while(1); at the start, the script will hang instead.



A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.






share|improve this answer





















  • 5





    Technically, a "normal" JSON parser should give an error if you have a prefix.

    – Matthew Crumley
    May 16 '09 at 3:31






  • 12





    Attackers would just use a plain old <script> element, not an XHR.

    – Laurence Gonsalves
    May 16 '09 at 4:22






  • 9





    @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

    – bdonlan
    Feb 24 '11 at 12:54






  • 7





    Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

    – Dennis G
    Feb 5 '13 at 13:37








  • 7





    @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

    – user69173
    Jun 3 '13 at 19:44



















101














That would be to make it difficult for a third-party to insert the JSON response into an HTML document with the <script> tag. Remember that the <script> tag is exempt from the Same Origin Policy.






share|improve this answer





















  • 19





    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:37













  • but how it will prevent iframes

    – Ravinder Payal
    Aug 3 '16 at 16:24






  • 1





    @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

    – YoYoYonnY
    Sep 6 '17 at 16:28











  • Script tag is never exempt from same origin policy. Could you clear that up for me?

    – Suraj Jain
    Jun 3 '18 at 14:59



















68














It prevents it from being used as the target of a simple <script> tag. (Well, it doesn't prevent it, but it makes it unpleasant.) That way bad guys can't just put that script tag in their own site and rely on an active session to make it possible to fetch your content.



edit — note the comment (and other answers). The issue has to do with subverted built-in facilities, specifically the Object and Array constructors. Those can be altered such that otherwise innocuous JSON, when parsed, could trigger attacker code.






share|improve this answer





















  • 14





    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:39



















7














Since the <script> tag is exempted from the Same Origin Policy which is a security necessity in the web world, while(1) when added to the JSON response prevents misuse of it in the <script> tag.






share|improve this answer
























    protected by Abdulla Nilam Oct 25 '17 at 16:09



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3887














    It prevents JSON hijacking, a major JSON security issue that is formally fixed in all major browsers since 2011 with EMCA5.



    Contrived example: say Google has a URL like mail.google.com/json?action=inbox which returns the first 50 messages of your inbox in JSON format. Evil websites on other domains can't make AJAX requests to get this data due to the same-origin policy, but they can include the URL via a <script> tag. The URL is visited with your cookies, and by overriding the global array constructor or accessor methods they can have a method called whenever an object (array or hash) attribute is set, allowing them to read the JSON content.



    The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error.



    This does not address the issue of cross-site request forgery.






    share|improve this answer





















    • 213





      Why doesn't the request to obtain this data require a CSRF-token instead?

      – Jakub P.
      Feb 3 '13 at 1:43






    • 214





      Does for(;;); do the same job? I've seen this in facebook's ajax responses.

      – King Julien
      Feb 4 '13 at 8:27






    • 163





      @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

      – abraham
      Feb 5 '13 at 5:12






    • 116





      @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

      – bluesmoon
      Feb 5 '13 at 6:10






    • 87





      It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

      – mcv
      Dec 30 '13 at 15:06
















    3887














    It prevents JSON hijacking, a major JSON security issue that is formally fixed in all major browsers since 2011 with EMCA5.



    Contrived example: say Google has a URL like mail.google.com/json?action=inbox which returns the first 50 messages of your inbox in JSON format. Evil websites on other domains can't make AJAX requests to get this data due to the same-origin policy, but they can include the URL via a <script> tag. The URL is visited with your cookies, and by overriding the global array constructor or accessor methods they can have a method called whenever an object (array or hash) attribute is set, allowing them to read the JSON content.



    The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error.



    This does not address the issue of cross-site request forgery.






    share|improve this answer





















    • 213





      Why doesn't the request to obtain this data require a CSRF-token instead?

      – Jakub P.
      Feb 3 '13 at 1:43






    • 214





      Does for(;;); do the same job? I've seen this in facebook's ajax responses.

      – King Julien
      Feb 4 '13 at 8:27






    • 163





      @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

      – abraham
      Feb 5 '13 at 5:12






    • 116





      @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

      – bluesmoon
      Feb 5 '13 at 6:10






    • 87





      It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

      – mcv
      Dec 30 '13 at 15:06














    3887












    3887








    3887







    It prevents JSON hijacking, a major JSON security issue that is formally fixed in all major browsers since 2011 with EMCA5.



    Contrived example: say Google has a URL like mail.google.com/json?action=inbox which returns the first 50 messages of your inbox in JSON format. Evil websites on other domains can't make AJAX requests to get this data due to the same-origin policy, but they can include the URL via a <script> tag. The URL is visited with your cookies, and by overriding the global array constructor or accessor methods they can have a method called whenever an object (array or hash) attribute is set, allowing them to read the JSON content.



    The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error.



    This does not address the issue of cross-site request forgery.






    share|improve this answer















    It prevents JSON hijacking, a major JSON security issue that is formally fixed in all major browsers since 2011 with EMCA5.



    Contrived example: say Google has a URL like mail.google.com/json?action=inbox which returns the first 50 messages of your inbox in JSON format. Evil websites on other domains can't make AJAX requests to get this data due to the same-origin policy, but they can include the URL via a <script> tag. The URL is visited with your cookies, and by overriding the global array constructor or accessor methods they can have a method called whenever an object (array or hash) attribute is set, allowing them to read the JSON content.



    The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error.



    This does not address the issue of cross-site request forgery.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 3 '18 at 19:29









    user1034533

    49048




    49048










    answered Apr 19 '10 at 18:11









    rjhrjh

    40k34359




    40k34359








    • 213





      Why doesn't the request to obtain this data require a CSRF-token instead?

      – Jakub P.
      Feb 3 '13 at 1:43






    • 214





      Does for(;;); do the same job? I've seen this in facebook's ajax responses.

      – King Julien
      Feb 4 '13 at 8:27






    • 163





      @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

      – abraham
      Feb 5 '13 at 5:12






    • 116





      @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

      – bluesmoon
      Feb 5 '13 at 6:10






    • 87





      It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

      – mcv
      Dec 30 '13 at 15:06














    • 213





      Why doesn't the request to obtain this data require a CSRF-token instead?

      – Jakub P.
      Feb 3 '13 at 1:43






    • 214





      Does for(;;); do the same job? I've seen this in facebook's ajax responses.

      – King Julien
      Feb 4 '13 at 8:27






    • 163





      @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

      – abraham
      Feb 5 '13 at 5:12






    • 116





      @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

      – bluesmoon
      Feb 5 '13 at 6:10






    • 87





      It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

      – mcv
      Dec 30 '13 at 15:06








    213




    213





    Why doesn't the request to obtain this data require a CSRF-token instead?

    – Jakub P.
    Feb 3 '13 at 1:43





    Why doesn't the request to obtain this data require a CSRF-token instead?

    – Jakub P.
    Feb 3 '13 at 1:43




    214




    214





    Does for(;;); do the same job? I've seen this in facebook's ajax responses.

    – King Julien
    Feb 4 '13 at 8:27





    Does for(;;); do the same job? I've seen this in facebook's ajax responses.

    – King Julien
    Feb 4 '13 at 8:27




    163




    163





    @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

    – abraham
    Feb 5 '13 at 5:12





    @JakubP. Storing and maintaining CSRF-tokens at Google's scale requires a large amount of infrastructure and cost.

    – abraham
    Feb 5 '13 at 5:12




    116




    116





    @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

    – bluesmoon
    Feb 5 '13 at 6:10





    @JakubP. anti-CSRF tokens mess with caching, and require some amount of cryptographic evaluation server-side. At Google scale, that would require a lot of CPU. This sort of offloads it to the client.

    – bluesmoon
    Feb 5 '13 at 6:10




    87




    87





    It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

    – mcv
    Dec 30 '13 at 15:06





    It seems to me a better way would be to let the server only send the JSON if the correct header has been set. You can do that in an AJAX call, but not with script tags. That way, the sensitive information never even gets sent, and you don't have to rely on browser-side security.

    – mcv
    Dec 30 '13 at 15:06













    481














    It prevents disclosure of the response through JSON hijacking.



    In theory, the content of HTTP responses are protected by the Same Origin Policy: pages from one domain cannot get any pieces of information from pages on the other domain (unless explicitly allowed).



    An attacker can request pages on other domains on your behalf, e.g. by using a <script src=...> or <img> tag, but it can't get any information about the result (headers, contents).



    Thus, if you visit an attacker's page, it couldn't read your email from gmail.com.



    Except that when using a script tag to request JSON content, the JSON is executed as Javascript in an attacker's controlled environment. If the attacker can replace the Array or Object constructor or some other method used during object construction, anything in the JSON would pass through the attacker's code, and be disclosed.



    Note that this happens at the time the JSON is executed as Javascript, not at the time it's parsed.



    There are multiple countermeasures:



    Making sure the JSON never executes



    By placing a while(1); statement before the JSON data, Google makes sure that the JSON data is never executed as Javascript.



    Only a legitimate page could actually get the whole content, strip the while(1);, and parse the remainder as JSON.



    Things like for(;;); have been seen at Facebook for instance, with the same results.



    Making sure the JSON is not valid Javascript



    Similarly, adding invalid tokens before the JSON, like &&&START&&&, makes sure that it is never executed.



    Always return JSON with an Object on the outside



    This is OWASP recommended way to protect from JSON hijacking and is the less intrusive one.



    Similarly to the previous counter-measures, it makes sure that the JSON is never executed as Javascript.



    A valid JSON object, when not enclosed by anything, is not valid in Javascript:



    eval('{"foo":"bar"}')
    // SyntaxError: Unexpected token :


    This is however valid JSON:



    JSON.parse('{"foo":"bar"}')
    // Object {foo: "bar"}


    So, making sure you always return an Object at the top level of the response makes sure that the JSON is not valid Javascript, while still being valid JSON.



    As noted by @hvd in the comments, the empty object {} is valid Javascript, and knowing the object is empty may itself be valuable information.



    Comparison of above methods



    The OWASP way is less intrusive, as it needs no client library changes, and transfers valid JSON. It is unsure whether past or future browser bugs could defeat this, however. As noted by @oriadam, it is unclear whether data could be leaked in a parse error through an error handling or not (e.g. window.onerror).



    Google's way requires client library in order for it to support automatic de-serialization and can be considered to be safer with regard to browser bugs.



    Both methods require server side changes in order to avoid developers from accidentally sending vulnerable JSON.






    share|improve this answer





















    • 19





      OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

      – funroll
      Mar 15 '14 at 1:47






    • 14





      I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

      – vaxquis
      Apr 12 '14 at 15:54






    • 19





      It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

      – Manngo
      Nov 8 '15 at 10:59






    • 10





      Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

      – oriadam
      Dec 6 '15 at 23:06






    • 7





      "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

      – user743382
      May 6 '17 at 20:50
















    481














    It prevents disclosure of the response through JSON hijacking.



    In theory, the content of HTTP responses are protected by the Same Origin Policy: pages from one domain cannot get any pieces of information from pages on the other domain (unless explicitly allowed).



    An attacker can request pages on other domains on your behalf, e.g. by using a <script src=...> or <img> tag, but it can't get any information about the result (headers, contents).



    Thus, if you visit an attacker's page, it couldn't read your email from gmail.com.



    Except that when using a script tag to request JSON content, the JSON is executed as Javascript in an attacker's controlled environment. If the attacker can replace the Array or Object constructor or some other method used during object construction, anything in the JSON would pass through the attacker's code, and be disclosed.



    Note that this happens at the time the JSON is executed as Javascript, not at the time it's parsed.



    There are multiple countermeasures:



    Making sure the JSON never executes



    By placing a while(1); statement before the JSON data, Google makes sure that the JSON data is never executed as Javascript.



    Only a legitimate page could actually get the whole content, strip the while(1);, and parse the remainder as JSON.



    Things like for(;;); have been seen at Facebook for instance, with the same results.



    Making sure the JSON is not valid Javascript



    Similarly, adding invalid tokens before the JSON, like &&&START&&&, makes sure that it is never executed.



    Always return JSON with an Object on the outside



    This is OWASP recommended way to protect from JSON hijacking and is the less intrusive one.



    Similarly to the previous counter-measures, it makes sure that the JSON is never executed as Javascript.



    A valid JSON object, when not enclosed by anything, is not valid in Javascript:



    eval('{"foo":"bar"}')
    // SyntaxError: Unexpected token :


    This is however valid JSON:



    JSON.parse('{"foo":"bar"}')
    // Object {foo: "bar"}


    So, making sure you always return an Object at the top level of the response makes sure that the JSON is not valid Javascript, while still being valid JSON.



    As noted by @hvd in the comments, the empty object {} is valid Javascript, and knowing the object is empty may itself be valuable information.



    Comparison of above methods



    The OWASP way is less intrusive, as it needs no client library changes, and transfers valid JSON. It is unsure whether past or future browser bugs could defeat this, however. As noted by @oriadam, it is unclear whether data could be leaked in a parse error through an error handling or not (e.g. window.onerror).



    Google's way requires client library in order for it to support automatic de-serialization and can be considered to be safer with regard to browser bugs.



    Both methods require server side changes in order to avoid developers from accidentally sending vulnerable JSON.






    share|improve this answer





















    • 19





      OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

      – funroll
      Mar 15 '14 at 1:47






    • 14





      I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

      – vaxquis
      Apr 12 '14 at 15:54






    • 19





      It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

      – Manngo
      Nov 8 '15 at 10:59






    • 10





      Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

      – oriadam
      Dec 6 '15 at 23:06






    • 7





      "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

      – user743382
      May 6 '17 at 20:50














    481












    481








    481







    It prevents disclosure of the response through JSON hijacking.



    In theory, the content of HTTP responses are protected by the Same Origin Policy: pages from one domain cannot get any pieces of information from pages on the other domain (unless explicitly allowed).



    An attacker can request pages on other domains on your behalf, e.g. by using a <script src=...> or <img> tag, but it can't get any information about the result (headers, contents).



    Thus, if you visit an attacker's page, it couldn't read your email from gmail.com.



    Except that when using a script tag to request JSON content, the JSON is executed as Javascript in an attacker's controlled environment. If the attacker can replace the Array or Object constructor or some other method used during object construction, anything in the JSON would pass through the attacker's code, and be disclosed.



    Note that this happens at the time the JSON is executed as Javascript, not at the time it's parsed.



    There are multiple countermeasures:



    Making sure the JSON never executes



    By placing a while(1); statement before the JSON data, Google makes sure that the JSON data is never executed as Javascript.



    Only a legitimate page could actually get the whole content, strip the while(1);, and parse the remainder as JSON.



    Things like for(;;); have been seen at Facebook for instance, with the same results.



    Making sure the JSON is not valid Javascript



    Similarly, adding invalid tokens before the JSON, like &&&START&&&, makes sure that it is never executed.



    Always return JSON with an Object on the outside



    This is OWASP recommended way to protect from JSON hijacking and is the less intrusive one.



    Similarly to the previous counter-measures, it makes sure that the JSON is never executed as Javascript.



    A valid JSON object, when not enclosed by anything, is not valid in Javascript:



    eval('{"foo":"bar"}')
    // SyntaxError: Unexpected token :


    This is however valid JSON:



    JSON.parse('{"foo":"bar"}')
    // Object {foo: "bar"}


    So, making sure you always return an Object at the top level of the response makes sure that the JSON is not valid Javascript, while still being valid JSON.



    As noted by @hvd in the comments, the empty object {} is valid Javascript, and knowing the object is empty may itself be valuable information.



    Comparison of above methods



    The OWASP way is less intrusive, as it needs no client library changes, and transfers valid JSON. It is unsure whether past or future browser bugs could defeat this, however. As noted by @oriadam, it is unclear whether data could be leaked in a parse error through an error handling or not (e.g. window.onerror).



    Google's way requires client library in order for it to support automatic de-serialization and can be considered to be safer with regard to browser bugs.



    Both methods require server side changes in order to avoid developers from accidentally sending vulnerable JSON.






    share|improve this answer















    It prevents disclosure of the response through JSON hijacking.



    In theory, the content of HTTP responses are protected by the Same Origin Policy: pages from one domain cannot get any pieces of information from pages on the other domain (unless explicitly allowed).



    An attacker can request pages on other domains on your behalf, e.g. by using a <script src=...> or <img> tag, but it can't get any information about the result (headers, contents).



    Thus, if you visit an attacker's page, it couldn't read your email from gmail.com.



    Except that when using a script tag to request JSON content, the JSON is executed as Javascript in an attacker's controlled environment. If the attacker can replace the Array or Object constructor or some other method used during object construction, anything in the JSON would pass through the attacker's code, and be disclosed.



    Note that this happens at the time the JSON is executed as Javascript, not at the time it's parsed.



    There are multiple countermeasures:



    Making sure the JSON never executes



    By placing a while(1); statement before the JSON data, Google makes sure that the JSON data is never executed as Javascript.



    Only a legitimate page could actually get the whole content, strip the while(1);, and parse the remainder as JSON.



    Things like for(;;); have been seen at Facebook for instance, with the same results.



    Making sure the JSON is not valid Javascript



    Similarly, adding invalid tokens before the JSON, like &&&START&&&, makes sure that it is never executed.



    Always return JSON with an Object on the outside



    This is OWASP recommended way to protect from JSON hijacking and is the less intrusive one.



    Similarly to the previous counter-measures, it makes sure that the JSON is never executed as Javascript.



    A valid JSON object, when not enclosed by anything, is not valid in Javascript:



    eval('{"foo":"bar"}')
    // SyntaxError: Unexpected token :


    This is however valid JSON:



    JSON.parse('{"foo":"bar"}')
    // Object {foo: "bar"}


    So, making sure you always return an Object at the top level of the response makes sure that the JSON is not valid Javascript, while still being valid JSON.



    As noted by @hvd in the comments, the empty object {} is valid Javascript, and knowing the object is empty may itself be valuable information.



    Comparison of above methods



    The OWASP way is less intrusive, as it needs no client library changes, and transfers valid JSON. It is unsure whether past or future browser bugs could defeat this, however. As noted by @oriadam, it is unclear whether data could be leaked in a parse error through an error handling or not (e.g. window.onerror).



    Google's way requires client library in order for it to support automatic de-serialization and can be considered to be safer with regard to browser bugs.



    Both methods require server side changes in order to avoid developers from accidentally sending vulnerable JSON.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 15:44









    Sayed Mohd Ali

    1,1051319




    1,1051319










    answered Feb 2 '14 at 12:09









    arnaud576875arnaud576875

    69.8k16171173




    69.8k16171173








    • 19





      OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

      – funroll
      Mar 15 '14 at 1:47






    • 14





      I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

      – vaxquis
      Apr 12 '14 at 15:54






    • 19





      It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

      – Manngo
      Nov 8 '15 at 10:59






    • 10





      Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

      – oriadam
      Dec 6 '15 at 23:06






    • 7





      "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

      – user743382
      May 6 '17 at 20:50














    • 19





      OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

      – funroll
      Mar 15 '14 at 1:47






    • 14





      I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

      – vaxquis
      Apr 12 '14 at 15:54






    • 19





      It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

      – Manngo
      Nov 8 '15 at 10:59






    • 10





      Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

      – oriadam
      Dec 6 '15 at 23:06






    • 7





      "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

      – user743382
      May 6 '17 at 20:50








    19




    19





    OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

    – funroll
    Mar 15 '14 at 1:47





    OWASP recommendation is interesting because of its simplicity. Anyone know a reason Google's way is more secure?

    – funroll
    Mar 15 '14 at 1:47




    14




    14





    I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

    – vaxquis
    Apr 12 '14 at 15:54





    I believe it isn't more secure in any way. Providing OWASP here seems a good enough reason for +1.

    – vaxquis
    Apr 12 '14 at 15:54




    19




    19





    It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

    – Manngo
    Nov 8 '15 at 10:59





    It may be worth noting why returning an object literal fails the script tag or eval function. The braces {} can be interpreted as either a block of code or an object literal, and by itself, JavaScript prefers the former. As a block of code it is, of course, invalid. By this logic, I can’t see any foreseeable changes in future browser behaviour.

    – Manngo
    Nov 8 '15 at 10:59




    10




    10





    Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

    – oriadam
    Dec 6 '15 at 23:06





    Bad code is not enough becaues an attacker can also hijack the script-error hander of the browser (window.onerror) I'm not sure what is the behavior of onerror with syntax errors. I guess Google were unsure as well.

    – oriadam
    Dec 6 '15 at 23:06




    7




    7





    "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

    – user743382
    May 6 '17 at 20:50





    "A valid JSON object, when not enclosed by anything, is not valid in Javascript:" -- Except for the trivial case of an empty object ({}), which is a valid empty block as well. If knowing the object is empty may itself be valuable information, this might be exploitable.

    – user743382
    May 6 '17 at 20:50











    349














    This is to ensure some other site can't do nasty tricks to try to steal your data. For example, by replacing the array constructor, then including this JSON URL via a <script> tag, a malicious third-party site could steal the data from the JSON response. By putting a while(1); at the start, the script will hang instead.



    A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.






    share|improve this answer





















    • 5





      Technically, a "normal" JSON parser should give an error if you have a prefix.

      – Matthew Crumley
      May 16 '09 at 3:31






    • 12





      Attackers would just use a plain old <script> element, not an XHR.

      – Laurence Gonsalves
      May 16 '09 at 4:22






    • 9





      @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

      – bdonlan
      Feb 24 '11 at 12:54






    • 7





      Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

      – Dennis G
      Feb 5 '13 at 13:37








    • 7





      @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

      – user69173
      Jun 3 '13 at 19:44
















    349














    This is to ensure some other site can't do nasty tricks to try to steal your data. For example, by replacing the array constructor, then including this JSON URL via a <script> tag, a malicious third-party site could steal the data from the JSON response. By putting a while(1); at the start, the script will hang instead.



    A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.






    share|improve this answer





















    • 5





      Technically, a "normal" JSON parser should give an error if you have a prefix.

      – Matthew Crumley
      May 16 '09 at 3:31






    • 12





      Attackers would just use a plain old <script> element, not an XHR.

      – Laurence Gonsalves
      May 16 '09 at 4:22






    • 9





      @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

      – bdonlan
      Feb 24 '11 at 12:54






    • 7





      Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

      – Dennis G
      Feb 5 '13 at 13:37








    • 7





      @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

      – user69173
      Jun 3 '13 at 19:44














    349












    349








    349







    This is to ensure some other site can't do nasty tricks to try to steal your data. For example, by replacing the array constructor, then including this JSON URL via a <script> tag, a malicious third-party site could steal the data from the JSON response. By putting a while(1); at the start, the script will hang instead.



    A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.






    share|improve this answer















    This is to ensure some other site can't do nasty tricks to try to steal your data. For example, by replacing the array constructor, then including this JSON URL via a <script> tag, a malicious third-party site could steal the data from the JSON response. By putting a while(1); at the start, the script will hang instead.



    A same-site request using XHR and a separate JSON parser, on the other hand, can easily ignore the while(1); prefix.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 31 '14 at 23:43









    hippietrail

    7,5741072109




    7,5741072109










    answered May 16 '09 at 2:08









    bdonlanbdonlan

    175k23223301




    175k23223301








    • 5





      Technically, a "normal" JSON parser should give an error if you have a prefix.

      – Matthew Crumley
      May 16 '09 at 3:31






    • 12





      Attackers would just use a plain old <script> element, not an XHR.

      – Laurence Gonsalves
      May 16 '09 at 4:22






    • 9





      @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

      – bdonlan
      Feb 24 '11 at 12:54






    • 7





      Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

      – Dennis G
      Feb 5 '13 at 13:37








    • 7





      @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

      – user69173
      Jun 3 '13 at 19:44














    • 5





      Technically, a "normal" JSON parser should give an error if you have a prefix.

      – Matthew Crumley
      May 16 '09 at 3:31






    • 12





      Attackers would just use a plain old <script> element, not an XHR.

      – Laurence Gonsalves
      May 16 '09 at 4:22






    • 9





      @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

      – bdonlan
      Feb 24 '11 at 12:54






    • 7





      Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

      – Dennis G
      Feb 5 '13 at 13:37








    • 7





      @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

      – user69173
      Jun 3 '13 at 19:44








    5




    5





    Technically, a "normal" JSON parser should give an error if you have a prefix.

    – Matthew Crumley
    May 16 '09 at 3:31





    Technically, a "normal" JSON parser should give an error if you have a prefix.

    – Matthew Crumley
    May 16 '09 at 3:31




    12




    12





    Attackers would just use a plain old <script> element, not an XHR.

    – Laurence Gonsalves
    May 16 '09 at 4:22





    Attackers would just use a plain old <script> element, not an XHR.

    – Laurence Gonsalves
    May 16 '09 at 4:22




    9




    9





    @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

    – bdonlan
    Feb 24 '11 at 12:54





    @Matthew, sure, but you can remove it before passing the data to the JSON parser. You can't do that with a <script> tag

    – bdonlan
    Feb 24 '11 at 12:54




    7




    7





    Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

    – Dennis G
    Feb 5 '13 at 13:37







    Are there any examples of this? Replacing the array constructor is referenced again, but that's a bug long fixed. I don't understand how one would have access to the data received via the script tag. I'd love to see a dummy implementation which works in recent browser.

    – Dennis G
    Feb 5 '13 at 13:37






    7




    7





    @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

    – user69173
    Jun 3 '13 at 19:44





    @joeltine, no, it's not. See stackoverflow.com/questions/16289894/… .

    – user69173
    Jun 3 '13 at 19:44











    101














    That would be to make it difficult for a third-party to insert the JSON response into an HTML document with the <script> tag. Remember that the <script> tag is exempt from the Same Origin Policy.






    share|improve this answer





















    • 19





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:37













    • but how it will prevent iframes

      – Ravinder Payal
      Aug 3 '16 at 16:24






    • 1





      @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

      – YoYoYonnY
      Sep 6 '17 at 16:28











    • Script tag is never exempt from same origin policy. Could you clear that up for me?

      – Suraj Jain
      Jun 3 '18 at 14:59
















    101














    That would be to make it difficult for a third-party to insert the JSON response into an HTML document with the <script> tag. Remember that the <script> tag is exempt from the Same Origin Policy.






    share|improve this answer





















    • 19





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:37













    • but how it will prevent iframes

      – Ravinder Payal
      Aug 3 '16 at 16:24






    • 1





      @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

      – YoYoYonnY
      Sep 6 '17 at 16:28











    • Script tag is never exempt from same origin policy. Could you clear that up for me?

      – Suraj Jain
      Jun 3 '18 at 14:59














    101












    101








    101







    That would be to make it difficult for a third-party to insert the JSON response into an HTML document with the <script> tag. Remember that the <script> tag is exempt from the Same Origin Policy.






    share|improve this answer















    That would be to make it difficult for a third-party to insert the JSON response into an HTML document with the <script> tag. Remember that the <script> tag is exempt from the Same Origin Policy.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 30 '13 at 3:10









    George Stocker

    45.7k28155217




    45.7k28155217










    answered Apr 19 '10 at 18:04









    Daniel VassalloDaniel Vassallo

    268k59445405




    268k59445405








    • 19





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:37













    • but how it will prevent iframes

      – Ravinder Payal
      Aug 3 '16 at 16:24






    • 1





      @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

      – YoYoYonnY
      Sep 6 '17 at 16:28











    • Script tag is never exempt from same origin policy. Could you clear that up for me?

      – Suraj Jain
      Jun 3 '18 at 14:59














    • 19





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:37













    • but how it will prevent iframes

      – Ravinder Payal
      Aug 3 '16 at 16:24






    • 1





      @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

      – YoYoYonnY
      Sep 6 '17 at 16:28











    • Script tag is never exempt from same origin policy. Could you clear that up for me?

      – Suraj Jain
      Jun 3 '18 at 14:59








    19




    19





    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:37







    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:37















    but how it will prevent iframes

    – Ravinder Payal
    Aug 3 '16 at 16:24





    but how it will prevent iframes

    – Ravinder Payal
    Aug 3 '16 at 16:24




    1




    1





    @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

    – YoYoYonnY
    Sep 6 '17 at 16:28





    @RavinderPayal <iframe> tags are not exempt from the Same Origin Policy...

    – YoYoYonnY
    Sep 6 '17 at 16:28













    Script tag is never exempt from same origin policy. Could you clear that up for me?

    – Suraj Jain
    Jun 3 '18 at 14:59





    Script tag is never exempt from same origin policy. Could you clear that up for me?

    – Suraj Jain
    Jun 3 '18 at 14:59











    68














    It prevents it from being used as the target of a simple <script> tag. (Well, it doesn't prevent it, but it makes it unpleasant.) That way bad guys can't just put that script tag in their own site and rely on an active session to make it possible to fetch your content.



    edit — note the comment (and other answers). The issue has to do with subverted built-in facilities, specifically the Object and Array constructors. Those can be altered such that otherwise innocuous JSON, when parsed, could trigger attacker code.






    share|improve this answer





















    • 14





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:39
















    68














    It prevents it from being used as the target of a simple <script> tag. (Well, it doesn't prevent it, but it makes it unpleasant.) That way bad guys can't just put that script tag in their own site and rely on an active session to make it possible to fetch your content.



    edit — note the comment (and other answers). The issue has to do with subverted built-in facilities, specifically the Object and Array constructors. Those can be altered such that otherwise innocuous JSON, when parsed, could trigger attacker code.






    share|improve this answer





















    • 14





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:39














    68












    68








    68







    It prevents it from being used as the target of a simple <script> tag. (Well, it doesn't prevent it, but it makes it unpleasant.) That way bad guys can't just put that script tag in their own site and rely on an active session to make it possible to fetch your content.



    edit — note the comment (and other answers). The issue has to do with subverted built-in facilities, specifically the Object and Array constructors. Those can be altered such that otherwise innocuous JSON, when parsed, could trigger attacker code.






    share|improve this answer















    It prevents it from being used as the target of a simple <script> tag. (Well, it doesn't prevent it, but it makes it unpleasant.) That way bad guys can't just put that script tag in their own site and rely on an active session to make it possible to fetch your content.



    edit — note the comment (and other answers). The issue has to do with subverted built-in facilities, specifically the Object and Array constructors. Those can be altered such that otherwise innocuous JSON, when parsed, could trigger attacker code.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 8 '15 at 13:49

























    answered Apr 19 '10 at 18:02









    PointyPointy

    315k44455515




    315k44455515








    • 14





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:39














    • 14





      This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

      – Mark Amery
      Jan 17 '14 at 9:39








    14




    14





    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:39





    This is only half an answer. If it weren't for the trick of overriding the Object and Array constructors, executing a valid JSON response as though it were JavaScript would be totally innocuous in all circumstances. Yes, the while(1); prevents the response from being executed as JavaScript if targeted by a <script> tag, but your answer doesn't explain why that's necessary.

    – Mark Amery
    Jan 17 '14 at 9:39











    7














    Since the <script> tag is exempted from the Same Origin Policy which is a security necessity in the web world, while(1) when added to the JSON response prevents misuse of it in the <script> tag.






    share|improve this answer






























      7














      Since the <script> tag is exempted from the Same Origin Policy which is a security necessity in the web world, while(1) when added to the JSON response prevents misuse of it in the <script> tag.






      share|improve this answer




























        7












        7








        7







        Since the <script> tag is exempted from the Same Origin Policy which is a security necessity in the web world, while(1) when added to the JSON response prevents misuse of it in the <script> tag.






        share|improve this answer















        Since the <script> tag is exempted from the Same Origin Policy which is a security necessity in the web world, while(1) when added to the JSON response prevents misuse of it in the <script> tag.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 28 '18 at 9:45









        Pang

        6,9011664101




        6,9011664101










        answered Aug 18 '17 at 4:14









        kg11kg11

        81979




        81979

















            protected by Abdulla Nilam Oct 25 '17 at 16:09



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?