Cross-domain queries – is very interesting topic to talk about, besides, it requires a lot of developer’s attention and knowledge of the specification.
Any cross-domain request is under security control. Its goal is to protect the server with old standards from hacking attack. “Old” mean those servers that do not even suspect that pages can send queries with the methods “PUT”, “DELETE”, etc.
For a clear classification, the CORS specification divides all requests into two types.
“Simple” requests are all those that satisfy the conditions below:
In other words, all those queries that a page could generate without using “XMLHttpRequest” are considered simple ones. However, they also have their own security requirements.
If the request does not satisfy at least one of these items above, it is immediately transferred to the list of “incomplete” one and needs an additional pre-request.
In the case of a simple cross-domain request, the browser automatically adds another header to the query: “Origin” where it writes down the domain from which the request was made. The serve transmits the “Access-Control-Allow-Origin” parameter, which should contain the domain from which the request was sent.
The last one means that the access to specific headers used to authorize a user the server must open by passing their list in the parameter: “Access-Control-Expose-Headers”.
In a cross-domain query, you can specify not only GET / POST methods but also any others, for example: DELETE, UPDATE.
However, the services of the past years were not ready to the fact that web pages can send such requests. In some of these old services, requests that use incomplete methods are mistakenly treated as requests from applications. There are errors connected with it, including confidentiality, because in this case server can give a client a lot of privileges.
There are many variants of such problem, it is only one of them. To avoid such problems a protective mechanism of pre-inquiries was developed.
Pre-request already from its meaning gives us an idea that the browser will send extra request before sending a basic one. Let’s look at it to understand all details more clearly.
As a method, this query uses OPTIONS. If you get an error with the word “preflight” and you see a request with this method in devtools on the Network tab it means there was an error during the security procedures and the web service is not ready to accept requests from your domain.
This request does not have a body, but there are two header parameters: Access-Control-Request-Method and Access-Control-Request-Headers which contain the list of methods and parameters of the header to which access is requested.
Properly speaking, task of this request is to ask the server to be ready to accept from us certain methods and additional parameters. If the server is ready, it will give the code “200”.
Additionally, the server can cache its response for a specific time by passing the caching period in seconds in the Access-Control-Max-Age header parameter. If this parameter is specified -allowed pre-request will not be sent from the list during requests.