views:

129

answers:

3

Why did the creators of the HTML DOM and/or Javascript decide to disallow cross-domain requests?

I can see some very small security benefits of disallowing it but in the long run it seems to be an attempt at making Javascript injection attacks have less power. That is all moot anyway with JSONP, it just means that the javascript code is a tiny bit more difficult to make and you have to have server-side cooperation(though it could be your own server)

+8  A: 

When cross-domain scripting is allowed (or hacked by a clever Javascripter), a webpage can access data from another webpage. Example: joeblow.com could access your Gmail while you have mail.google.com open. joeblow.com could read your email, spam your contacts, spoof mail from you, delete your mail, or any number of bad things.

mcandre
+6  A: 

The actual cross-domain issue is huge. Suppose SuperBank.com internally sends a request to http://www.superbank.com/transfer?amount=100&to=123456 to transfer $10,000 to account number 123456. If I can get you to my website, and you are logged in at SuperBank, all I have to do is send an AJAX request to SuperBank.com to move thousands of dollars from your account to mine.

The reason JSON-P is acceptable is that it is pretty darn impossible for it to be abused. A website using JSON-P is pretty much declaring the data to be public information, since that format is too inconvenient to ever be used otherwise. But if it's unclear as to whether or not data is public information, the browser must assume that it is not.

Matchu
Ok I see your point there. Why did they not just make it so that requests to cross-domains just don't get cookies sent in with it though? Or something else a bit more reasonable than a complete ban?
Earlz
@Earlz - I'm not sure I have a full answer, but as someone with a decent amount of experience with white-hat hacking, I can come up with a few ways I could abuse simple cookie-less AJAX requests. For instance, Google tracks your IP on searches, and temporarily bans you if you send too many requests. Even without cookies, I could send Google thousands of search requests on your behalf, and trick it into banning you. I'm sure there are other services out there who do even more in-depth tracking with IP, as well, that might end up spilling sensitive data.
Matchu
@Earlz - besides, JSON-P is a pretty strong opt-in system, so there hasn't been a need for anything else. Adobe came up with [crossdomain.xml](http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html) for Flash, but HTML/JS doesn't really need an equivalent, since we've already been clever enough to come up with one.
Matchu
@Matchu you can do that also by just continually getting (with an img tag or similar) a page with the `?timestamp=..` trick and get banned also.
Earlz
@Earlz - interesting. Just a random example. If I were making the browser, though, I wouldn't take chances on whether or not eliminating cookies is enough.
Matchu
A: 

Here's a distinction for you: Cross-domain AJAX allows a malicious site to make your browser to things on its behalf, while JSON-P allows a malicious server to tamper with a single domain's pages (and to make the browser do things to that domain on your behalf) but (crucial bit) only if the page served went out of its way to load the malicious payload.

So yes, JSON-P has some security implications, but they are strictly opt-in on the part of the website using them. Allowing general cross-domain AJAX opens up a much larger can of worms.

Andrew Aylett