SECURITY / A CONCEPT NOTE

CORS

the browser's same-origin policiy enforcer

~70 sec read

Overview · mechanism
pitfall · examples

01 / THE SHORT VERSION

The idea in a few sentences.

Cross-Origin Resource Sharing (CORS) is a browser mechanism that controls which domains can access resources from your server. When a web app at app.example.com fetches data from api.example.com, the browser sends a preflight OPTIONS request first. The server responds with Access-Control-Allow-Origin headers to permit or deny the cross-origin request.

02 / FOLLOW THE MECHANISM

How a preflight request works

  1. Browser

    detects a cross-origin fetch — different domain, protocol, or port than the page's origin.

  2. Browser

    sends an OPTIONS preflight request with Origin: https://app.example.com and Access-Control-Request-Method: POST.

  3. Server

    responds with Access-Control-Allow-Origin: https://app.example.com (or *) and the allowed methods/headers.

  4. Browser

    checks the response. If the origin is not allowed, the browser blocks the actual request with a CORS error.

  5. Actual request

    if preflight passes, the browser sends the real GET or POST and exposes the response to JavaScript.

04 / COMMAND NOTES

Read the command, then the result.

Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.

EXAMPLE 01 · REFERENCE

simulate a cross-origin request

curl -H "Origin: https://evil.com" -v https://api.example.com/data

EXAMPLE 02 · REFERENCE

trigger a preflight manually

curl -X OPTIONS -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: GET" -v https://api.example.com/data

Explore command anatomy in the CLI lab