a detailed xmlhttprequest of cross-domain resource sharing 0x00 Background
In the browser security-homology policy, pseudo URL domain This article refers to the browser's homology strategy, which refers to the XMLHttpRequest strict adherence to the homologous policy, not the same origin is not requested. However, in practice, there is often a need for cross-domain requests for resources, more typical such as a subdomain to the user is responsible for authentication of the subdomain request user information applications.
In the past, one solution was to use JSONP for Cross-domain resource requests, but there are several drawbacks to this scenario:
1 only GET request
2) Lack of effective error trapping mechanisms. Therefore, a model of cors (Cross Origin resourse-sharing) is proposed under the XMLHttpRequest V2 standard to ensure the security of Cross-domain requests.
Now, the major mainstream browsers support the Cors model, where IE 8, ie 9 need to use Xdomainrequest for Cross-domain requests. 0x01 Basic Model
Although XMLHttpRequest requests are permitted, this permission is not unconditional.
When the browser makes the request, it also determines the legality of the request, the validation is done through the server's return header, and the specific request type is involved, so a simple cross-domain request is defined in the standard.
A simple Cross-domain request is a request that satisfies the following criteria:
The request method
does not set the request header in the Get or POST request method (Accept, Accept-language, Content-language, content-type excepted)
if the Content-type header is set, The value is application/x-www-form-urlencoded, Multipart/form-data or Text/plain
The difference between a simple cross-domain request and a simple cross-domain request is that, before validating a simple cross-domain request, the browser sends a pre-check request to the server for a options method, and if the preflight fails, the actual request is discarded. The simple Cross-domain request browser sends the request normally and then judges the return header to check the legality of the request. When the check fails, the browser will prevent the script from accessing the returned content.
In http://zone.wooyun.org/content/4917, LZ because set the x-forward-for head, so the browser first sent a preflight request, check failed to discard the actual head, so caused the LZ misunderstanding. HTTP headers sent when 0x02 request
Simple Cross-domain requests do not include the following HTTP headers. The following HTTP headers will be sent by the pre-test request
Origin: Ordinary HTTP requests are also provided in Cors as Origin information for backend
Access-control-request-method: The next requested method, such as put, delete, etc.
Access-control-request-headers: Custom head, all headers set with the setRequestHeader method will be enclosed in this header in a comma-separated form
Other headers, such as the actual requested head, will not be included in the pre-test request for the cookie first class. HTTP headers returned by 0x03
The browser primarily determines whether the request is legitimate by returning these HTTP headers. It is worth noting that a pre-test request does not necessarily mean that the request succeeds, and if the HTTP header returned by the server makes the actual request valid when the check request is made, but the actual request contains an access control header that appears to be illegal, the browser will still determine that the request is illegal. To hide the details returned to the script.
Access-control-allow-origin: A domain that allows cross-domain access, either a list of domains or a wildcard "*". Note here that the origin rule is valid only for domain names and not for subdirectories. That is, the http://foo.example/subdir/is invalid. But different subdomains need to be set separately, where the rules can refer to the same homology policy
access-control-allow-credentials: whether to allow the request with authentication information, this part will be explained
in detail below Access-control-expose-headers: Allow script access to the return header, after the successful request, the script can access the header information in XMLHttpRequest (seemingly webkit did not implement this)
Access-control-max-age: The number of seconds to cache this request. In this time range, all of the same type of request will no longer send a pre-inspection request, but directly using the returned header as a basis for judging, very useful, a large number of optimization requests
Access-control-allow-methods: allowed to use the request method, separated by commas
access-control-allow-headers: Allows custom headers, separated by commas, insensitive to capitalization
Whether it is a pre-inspection request or a practical request, if in Access-control-allow-origin, Access-control-allow-credentials, Access-control-allow-methods, A access-control-allow-headers check fails and is considered to be a failed request. 0x04, talk alone credentials .
In Cross-domain requests, by default, HTTP authentication information, the cookie header, and the user's SSL certificate are not sent either in the prefetch request or in the actual request.
However, by setting the XMLHttpRequest credentials to true, the authentication information mechanism is enabled.
Although a simple request does not require a prefetch request to be sent, it is now necessary to determine whether the request is successful or not, and if Access-control-allow-credentials is false, the Access-control-allow-credentials Request failed.
It is important to note that at this point Access-control-allow-origin cannot be a wildcard "*" (really cheap a bunch of lazy programmers), if the Access-control-allow-origin is a wildcard "*", The request will still be considered failed
Even if it is a failed request, if you return a header with a Set-cookie header, the browser will still set the security risks in the cookie 0x05 cors
The biggest danger is that some lazy programmers will set the Access-control-allow-origin to "*" so that the Cors model is basically invalidated, but because of the protection of credentials model, Many online articles believe that the problem of information disclosure does not actually exist. The risk here is actually to construct DDoS attacks.
In addition, we will find that although the return value cannot be obtained, the simple request is actually issued, so the POST request can be issued. At this time, in fact, and the usual csrf the same, so it is not to ensure that Cross-domain request qualified domain can not do csrf prevention.