Skip to main content

All About CORS Headers

There are already a lot of resources about CORS out there.

I’m going to try and keep this short and sweet — useful tips for web devs.

About CORS

  1. CORS is a security mechanism based on HTTP headers.
  2. CORS must be configured on the web server and consumed by the browser.
  3. It is only relevant for web browsers, not requests from other software such as cURL or a Python script (which will ignore the configuration).

Why Does It Exist?

When Is It Triggered?

Configuring CORS Correctly

Pre-Flight Requests

Access-Control Headers

Access-Control-Allow-Origins

Access-Control-Allow-Credentials

Access-Control-Allow-Methods

Access-Control-Expose-Headers

Other headers

  • Cross-Origin-Resource-Policy (CORP): controls how strict the CORS policy is:
    • same-origin: Only allow requests from the exact same origin.
    • same-site: Allow requests from subdomains (e.g. api.example.com for example.com).
    • cross-origin: Allow all requests from any domain.
  • Cross-Origin-Opener-Policy (COOP) & Cross-Origin-Embedder-Policy (COEP): typically used for security and performance optimizations. A common example is usage with the Origin Private File System (OPFS) and SharedArrayBuffer.
  • Access-Control-Max-Age: defines how long the results of a pre-flight request can be cached before needing to run again.