offsecnotes

SOP, CORS, Pre-flight

by frankheat

Same-origin policy (SOP)


Cross-origin resource sharing

The cross-origin resource sharing specification provides controlled relaxation of the same-origin policy. The CORS specification identifies a collection of protocol headers

This means that the browser will allow code running on normal-website.com to access the response because the origins match.

Note: Access-Control-Allow-Origin is returned only if the whitelisted values or * or null matched the Origin.

Access-Control-Allow-Origin: *

The use of the wildcard * is restricted in the specification as you cannot combine the wildcard with the cross-origin transfer of credentials (authentication, cookies or client-side certificates). This following response is not permitted

Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

Access-Control-Allow-Origin: null

Specifies that only origins with a null origin are allowed to access the resource. Browsers might send the value null in the Origin header in various unusual situations:


Pre-flight checks

Under certain circumstances, when a cross-domain request includes a non-standard HTTP method or headers, the cross-origin request is preceded by a request using the OPTIONS method.

For example, this is a pre-flight request that is seeking to use the PUT method together with a custom request header called Special-Request-Header

OPTIONS /data HTTP/1.1
Host: <some website>
[...]
Origin: https://normal-website.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: Special-Request-Header
HTTP/1.1 204 No Content
    [...]
    Access-Control-Allow-Origin: https://normal-website.com
    Access-Control-Allow-Methods: PUT, POST, OPTIONS
    Access-Control-Allow-Headers: Special-Request-Header
    Access-Control-Allow-Credentials: true
    Access-Control-Max-Age: 240