☼
⌃
☰

CORS

Table of Contents

Cross-Origin Resource Sharing is a browser security mechanism that controls how web pages can request resources from a different origin. An origin is defined as the combination of protocol, domain, and port.

Related: CORS Misconfigurations CSRF XSS

Same-Origin Policy

Browsers enforce the Same-Origin Policy by default — a page at https://target.com cannot read responses from https://api.target.com or https://other.com. CORS is the mechanism by which servers explicitly relax this restriction for trusted origins.

How It Works

Simple Requests

For simple requests (GET, POST with certain content types), the browser automatically includes an Origin header. The server responds with Access-Control-Allow-Origin indicating which origins are permitted to read the response.

Request:
Origin: https://app.target.com

Response:
Access-Control-Allow-Origin: https://app.target.com
Access-Control-Allow-Credentials: true

Preflight Requests

For complex requests (PUT, DELETE, custom headers, JSON body), the browser first sends an OPTIONS preflight request to check whether the actual request is permitted.

OPTIONS /api/data HTTP/1.1
Origin: https://app.target.com
Access-Control-Request-Method: DELETE
Access-Control-Request-Headers: Authorization

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.target.com
Access-Control-Allow-Methods: GET, POST, DELETE
Access-Control-Allow-Headers: Authorization

Key Headers

Header Direction Description
Origin Request The origin making the request
Access-Control-Allow-Origin Response Which origins are permitted
Access-Control-Allow-Credentials Response Whether cookies are included
Access-Control-Allow-Methods Response Permitted HTTP methods
Access-Control-Allow-Headers Response Permitted request headers
Access-Control-Expose-Headers Response Headers the browser can read
Access-Control-Max-Age Response How long to cache preflight results

Credentials

By default, cross-origin requests do not include cookies or auth headers. To include them, both the request and the server response must opt in:

  • Client: fetch(url, { credentials: 'include' })
  • Server: Access-Control-Allow-Credentials: true

When credentials are enabled, Access-Control-Allow-Origin cannot be a wildcard (*) — it must specify an explicit origin.

Tips

  • CORS is enforced by the browser — it does not protect against direct server requests with curl or Burp Suite
  • The restriction is on reading responses, not making requests — a CSRF attack can still send requests even without CORS
  • Misconfigured CORS is a server-side issue — the fix is always on the server, not the client