Reputation: 1362
Can someone help me to better understand the Same Origin Policy. I've seen several websites describing it but I'm looking for an explanation much more simple, how would you describe it to a kid?
This link seems to do the best job that I've found. Can anyone expand? Can someone explain why this policy exists?
Upvotes: 9
Views: 4247
Reputation: 8045
Same-origin policy is needed to prevent CSRF. Imagine this scenario:
var xhr = new XMLHttpRequest(),
data = "from="+victimAccount
+ "&to="+jacksAccount
+ "&amt=a+gazillion+dollars";
xhr.open("POST", "http://tbtfbank.tld/accounts/wiretransfer.aspx", true);
xhr.send(data);
And Jack could have just as easily used the same technique to harvest thousands of account numbers and pins or any other information Joe the bank manager has access to via his account.
Luckily, the same-origin policy protects us from these types of attacks most of the time, since Jack's malicious page is hosted on a different domain from the bank application, it's not allowed to make XHRs to the bank application. Though the malicious page could still contain an image that makes a GET request to the bank application, so it's important that actions with side effects are not initiated via GET requests and that applications check the referrer header of requests they receive and take advantage of anti-CSRF tokens.
Upvotes: 28
Reputation: 499282
Basically it means - only scripts that are served from the same domain can access each others objects and properties without restriction (so if you have a .js
file with named functions defined, you can call it from any other file hosted on the same domain).
So, if you are serving a script from a different domain restriction do apply.
This policy exists because it is too easy to inject a link to a javascript file (say some javascript code that injects a link to such a file) that is on a different domain. This is a security risk - you really only want code that comes from the site you are on to execute and not just any code that is out there.
Upvotes: 6