CORS - What is the motivation behind introducing preflight requests?

前端 未结 9 2304
轻奢々
轻奢々 2020-11-22 11:30

Cross-origin resource sharing is a mechanism that allows a web page to make XMLHttpRequests to another domain (from wikipedia).

I\'ve been fiddling with COR

9条回答
  •  情话喂你
    2020-11-22 12:23

    I feel that the other answers aren't focusing on the reason pre-fight enhances security.

    Scenarios:

    1) With pre-flight. An attacker forges a request from site dummy-forums.com while the user is authenticated to safe-bank.com
    If the Server does not check for the origin, and somehow has a flaw, the browser will issue a pre-flight request, OPTION method. The server knows none of that CORS that the browser is expecting as a response so the browser will not proceed (no harm whatsoever)

    2) Without pre-flight. An attacker forges the request under the same scenario as above, the browser will issue the POST or PUT request right away, the server accepts it and might process it, this will potentially cause some harm.

    If the attacker sends a request directly, cross origin, from some random host it's most likely one is thinking about a request with no authentication. That's a forged request, but not a xsrf one. so the server has will check credentials and fail. CORS doesn't attempt to prevent an attacker who has the credentials to issue requests, although a whitelist could help reduce this vector of attack.

    The pre-flight mechanism adds safety and consistency between clients and servers. I don't know if this is worth the extra handshake for every request since caching is hardy use-able there, but that's how it works.

提交回复
热议问题