The web CORS 2 min read
What is CORS?
A dashboard calls an API on a different host. The API returns data, but the dashboard script can’t read it. What permission is the browser looking for?
-
Start with the Origin header
The dashboard script runs at
https://dashboard.example. In this cross-origin fetch, the browser sends that origin in theOriginheader. The API can use that information to decide what browser access to allow.In short Origin tells the API which website initiated the browser request.
-
Read the API’s permission
The API can return
Access-Control-Allow-Origin: https://dashboard.example. When the browser’s CORS checks pass, it makes the response available to the dashboard script. Some requests need a preliminary permission check called a preflight.In short The API states which origin can read the response, and the browser enforces it.
Where this applies
CORS is a browser rule. The API still has to authenticate callers and check what they may access.
Keep this
CORS grants browser read access across origins.
Keep the list of allowed origins as narrow as the application needs.