
Cross-Origin Resource Sharing (CORS) is a browser security mechanism that allows controlled access to resources located on a different origin. It extends the browser’s same-origin policy by enabling secure, rule-based cross-domain communication. CORS is widely used in modern distributed web systems where frontends, APIs, microservices, and CDNs often operate across separate domains, protocols, or ports.
Web browsers enforce the Same-Origin Policy (SOP) to prevent unauthorized access between different web origins. Without SOP, malicious sites could read sensitive data from authenticated sessions on other websites. However, SOP restricts legitimate scenarios—such as a hosted frontend calling an external API—creating the need for CORS.
SOP determines origin based on:
If any component differs, the request is considered cross-origin.
SOP primarily protects against:
CORS provides a controlled exception layer to allow secure cross-origin communication.
CORS works through browser-server negotiation using HTTP headers. Depending on the request complexity, the browser classifies requests as simple or non-simple (preflighted).
Key CORS headers include:
If the server response lacks valid headers, the browser blocks access—even if the request technically reaches the server.
Proper configuration ensures both functionality and security. Common approaches include:
Misconfiguration risks include:
CORS is essential in modern application ecosystems where front-end clients and back-end APIs are separated.
Common implementations include:
Proper CORS setup enables:
Without CORS, many modern app structures—including React/Vue frontends and cloud API backends—would be impractical.