Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason behind using OPTION request before POST on CORS requests? [duplicate]

What is the reason behind sending an OPTION request before the actual POST, UPDATE, PUT or DELETE request when a different domain is called? (So on CORS requests) I know it supposed to check whether the server can process the real request but why not send just the real request immediately?

Some of the reasons I have thought about:

  1. See if the method is supported
    • Sending the real request will return the same status code, so no need to send OPTION request first.
  2. Check if the user allowed to send the request
    • Make no sense as no auth headers are sent with the OPTION requests
  3. Prevent heavy load on the server
    • Make no sense, as checking the auth rules is before the processing of the data.
  4. To check if requested headers and origin are allowed
    • This is how it works now, but again why not just send the request, and we can read the error from the real request.
  5. Prevent sending the post data, if it wont be processed
    • This is the only reason what is valid. Using options request will prevent sending the post data to the server unnecessarily. However I think this is not a problem in the 99% of the time, as only a small chunk of data is sent.

Can someone shed some light on the reasons why browser vendors implemented OPTION requests when calling a different domain?

like image 282
NoNameProvided Avatar asked Jul 14 '16 13:07

NoNameProvided


People also ask

What is the purpose of options request?

The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk ( * ) to refer to the entire server.

Do you need options for CORS?

OPTIONS requests are what we call pre-flight requests in Cross-origin resource sharing (CORS) . They are necessary when you're making requests across different origins in specific situations.

What is the purpose of options method in restful web services?

The OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI.

What is the OPTIONS request in Cors?

The OPTIONS request mentioned in the introduction is a preflight request, which is part of the CORS (Cross-Origin Resource Sharing). CORS is a mechanism that provides configuration to configure access to shared resources.

What is a CORS preflight request?

The CORS specification also states that setting origins to "*" (all origins) is invalid if the Access-Control-Allow-Credentials header is present. For some CORS requests, the browser sends an additional OPTIONS request before making the actual request. This request is called a preflight request.

What is cors and how does it work?

CORS is a mechanism that provides configuration to configure access to shared resources. CORS applies when a webpage makes a request to another server other than its origin server, this could mean that either the domain, protocol, or port differs. Using the request the browser checks with the server whether the request is allowed.

What is Cors (enable cross-origin requests)?

Enable Cross-Origin Requests (CORS) in ASP.NET Core. Browser security prevents a web page from making requests to a different domain than the one that served the web page. This restriction is called the same-origin policy. The same-origin policy prevents a malicious site from reading sensitive data from another site.


1 Answers

CORS is a basically a browser security feature not server side.

By default the browser will NOT allow certain cross origin requests. The server you're talking to can publish whether or not it's safe to use cross origin requests but it's the client that understands and uses that information and therefore provides the protection not the server.

So for a GET request you can get the resource, check the CORS header and then decide whether to process it or not based on the header. Nice and simple.

For a POST (or other changing) event it's not so simple. You make the POST request, the server process it (remember the server doesn't care about CORS, only the browser) and sends back the response. The browser sees CORS is not enabled and ignores the response but by that point it's too late - the POST request has been processed at the server side and all that's prevented is the display of the results that it's been processed. So for an online banking application, for example, a malicious request to transfer funds means the funds will be transferred but your browser will ignore the "funds transferred successfully" response - big deal the damage is done as the money is gone and the malicious request would likely have ignored the response anyway!

So you can't send the request until you know what the CORS header will be on the response - which requires sending the request! Chicken and egg situation.

So the browser sends an OPTIONS request to the same address which doesn't change anything like a POST request might, but does return the CORS header. After that the browser knows whether it's safe to send the real request.

And btw the reason that a server doesn't implement CORS security is that it's incredibly easy to alter the Referrer header so it wouldn't offer any protection anyway. The server will have other security features (e.g. checking session is valid and authorised to make the request) but an attack that CORS is designed to prevent is one where these don't help (e.g. user is logged in to their online banking on another tab).

like image 196
Barry Pollard Avatar answered Nov 08 '22 11:11

Barry Pollard