What is the concept behind Access-Control-Allow-Origin and CORS?

倖福魔咒の 提交于 2019-11-29 15:44:35

Given:

  • Alice, a user with a browser
  • Bob, a site owner with a website
  • Mallory, a malicious site owner with a website

Alice has an account on Bob's server. Maybe it is her webmail. Maybe it is her online banking. Maybe it is somewhere she likes to shop.

Alice visits Mallory's website, not knowing that it is evil.

The Same Origin Policy prevents Mallory's website from using JavaScript to tell Alice's browser to make a request to Bob's website and give Alice's personal information (her bank balance for instance) to Mallory's website (and therefore to Mallory).

(Sometimes the request will be blocked because the conditions require a pre-flight request, other times the request will go through but the response will not be provided to Mallory's site. Look up CSRF if you want to defend against attacks where the danger lies in what the server does when it gets the request rather then in information leaking from the response).

CORS allows Bob to say that a resource on his website does not contain any personal information so that it is safe to allow other sites to access it (or that a particular site can be trusted with the personal information).

So as soon as somebody manages to inject a piece of JS code into a page,

XSS is a completely different security problem. You need to prevent people injecting JS.

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