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 Calen
Note: as of 2019, many of the old vulnerabilities that lead to the preventative measures discussed in this question are no longer an issue in modern browsers. I'll leave the answer below as a historical curiosity, but really the whole topic has changed radically since 2010 (!!) when this was asked.
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.