How does same origin policy solve issues with XHR, cookies and cross page commuication? [closed]

寵の児 提交于 2019-12-02 10:19:55

XmlHttpRequests

For example: To stop your site using my browser to get my data from my bank's website when my browser is logged into my bank.

cookies

The same origin policy doesn't apply to cookies. Cookies are simply sent to the site for which they are registered.

cross page communication: the only way a malicious page opened in a tab can view information about another page is via cookies and or local storage. So where does same origin policy help here?

You're operating under a misconception. Access to other pages is also available through window.open and frames (including iframes).

Once you have access to the DOM of another page, you can get data from it and you have the same issues that you would if XHR exposed other websites to JavaScript. Thus the same origin policy locks access to remote documents through frames.

It's not about protecting you from accessing untrusted things, it's about preventing your page from accessing things it shouldn't. Without the same origin policy, there's nothing stopping you (with the user's cookies) from doing, say:

$.getJson('http://api.example.com/my/secret/stuff')

or

$('<iframe src="http://gmail.com">').appendTo('body').contents().html()

As for cookies, it's not really the Same Origin Policy in play here:

Cookies use a separate definition of origins. A page can set a cookie for its own domain or any parent domain, as long as the parent domain is not a public suffix.

(from Same-origin policy at MDN)

If I use a XSS flaw to put a remote, evil js on your page, it cannot make requests to your, or any random, server. (actually, it can, but it doesn't have all features on)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!