TL;DR
Yes, it is to avoid a very specific type of attack based on Json requests on different domains.
A type of attack
According to an article from You’ve Been Haacked, one of the attacks in this category can be classified as a combination of cross-site request forgery or XSRF and some loopholes in older browsers that allowed malicious scripts to read all or part of the content returned from JSON requests to other domains.
XSRF briefly is when a malicious script requests to a remote server (Facebook in this case) using the user’s credentials to act on the user’s behalf or obtain sensitive data.
How the attack works
The idea is basically the following:
- The user authenticates to Facebook
- User accesses a malicious website
- A script on the malicious site uses a tag
<script>
to make a request GET
to the Facebook web service that returns a JSON
- Malicious script can bypass browser security and get back data from Facebook request
Example of implementation of the attack
Taken from the site cited above, consider the following example:
<script type="text/javascript">
var secrets;
Array = function() {
secrets = this;
};
<script src="http://haacked.com/demos/secret-info.json"
type="text/javascript">
<script type="text/javascript">
var yourData = '';
var i = -1;
while(secrets[++i]) {
yourData += secrets[i] + ' ';
}
alert('I stole your data: ' + yourData);
</script>
In some older browsers, the above code makes the variable secrets
receive the values of any new Array
created after the execution of the initial section. It is a redeclaration of the constructor.
Then the tag <script>
makes the request to the other server and, if there is a Array
in the returned JSON, the data will be captured.
Completion
Even though modern browsers make it very difficult to capture data on such request types, the infinite loop for
makes the malicious script have no chance of doing anything with possible data that could be obtained in web services calls using tags <script>
.
So it’s one more element in security.
Just to link here the same question in Soen; http://stackoverflow.com/q/2669690/91403 (as it may be used as a complement the answer already accepted)
– Renato Gama
Very useful, @renatoargh
– Wallace Maxters