Struggling to tell your APIs from your CDNs? Read our comprehensive cloud computing glossary covering the most common terms.
< Back to glossary
Cross-Origin Resource Sharing (CORS) is a web security mechanism that allows a web page to access resources from a different origin (domain, protocol, or port) than its own. It is implemented through HTTP headers and enables controlled cross-origin requests, bypassing the same-origin policy that browsers enforce to protect against malicious activity123.
Same-Origin Policy: By default, browsers block cross-origin requests to prevent unauthorized access to sensitive data. This restriction applies to scripts, APIs, and other resources.
CORS Mechanism: When a browser makes a cross-origin request, it includes an Origin header in the request.
The server responds with specific CORS headers like Access-Control-Allow-Origin to indicate whether the request is allowed.
For certain types of requests (e.g., non-safelisted methods), browsers perform a “preflight” request using the HTTP OPTIONS method to verify permissions before making the actual request.
Access-Control-Allow-Origin: Specifies which origins are allowed to access the resource.
Access-Control-Allow-Methods: Lists HTTP methods permitted for the resource.
Access-Control-Allow-Headers: Indicates which custom headers can be used.
Access-Control-Allow-Credentials: Determines whether cookies or credentials can be included in the request.
Enhanced Functionality: Allows web applications to fetch resources like APIs, fonts, and images from third-party domains, enabling richer user experiences.
Controlled Access: Provides fine-grained control over which origins and methods are allowed, reducing security risks.
Interoperability: Facilitates integration between client-side applications and external APIs or services.
Complex Configuration: Misconfigured CORS policies can either block legitimate requests or expose sensitive data.
Security Concerns: While CORS enables cross-origin communication, it is not inherently secure and must be implemented carefully to avoid increasing the attack surface.
Browser Dependencies: Different browsers may handle CORS slightly differently, requiring thorough testing.
Real-World Example: A frontend application hosted on frontend.example.com fetches data from an API hosted on api.example.com. The API server uses CORS headers to allow requests from frontend.example.com, enabling seamless data exchange without violating browser security policies.