What is CORS?
Also known as: Cross-Origin Resource Sharing
CORS (Cross-Origin Resource Sharing) is a browser security mechanism that lets servers explicitly allow web pages from one origin to request resources from a different origin via HTTP response headers.
CORS stands for Cross-Origin Resource Sharing. It is a browser-based security feature that controls how web pages from one origin (scheme, host, and port) can request resources from a different origin. Without CORS, browsers enforce the same-origin policy, which blocks cross-origin requests to prevent malicious sites from reading sensitive data from another site. CORS provides a safe way to relax this restriction when the target server explicitly opts in.
When a browser makes a cross-origin request (for example, a script on https://example.com fetching data from https://api.example.org), the browser adds an Origin header to the request. The server responds with one or more CORS headers, most notably Access-Control-Allow-Origin. If the server's response includes the requesting origin (or a wildcard *), the browser allows the page to read the response. For certain requests that are not simple (like those with custom headers or methods other than GET/POST), the browser first sends a preflight OPTIONS request to check permissions before sending the actual request. The preflight uses headers like Access-Control-Request-Method and Access-Control-Request-Headers, and the server must respond with matching Allow headers.
CORS is a server-driven policy, not a client-side fix. It is widely used for public APIs, CDN-hosted fonts, and web applications that consume third-party services. Misconfiguration can lead to security vulnerabilities, such as using a wildcard origin with credentials (which browsers block) or reflecting arbitrary origins without validation. Proper CORS setup requires careful consideration of which origins, methods, and headers are allowed, often documented in the server's configuration or middleware.
Key facts
- CORS is defined in the Fetch specification (WHATWG) and originally in W3C Working Draft.
- The browser adds an Origin header to every cross-origin request; the server responds with Access-Control-Allow-Origin.
- Non-simple requests trigger a preflight OPTIONS request before the actual request is sent.
- Credentials (cookies, HTTP auth) require Access-Control-Allow-Credentials: true and a specific origin, not a wildcard.
- CORS headers are ignored by non-browser clients like curl or server-to-server calls.
How it works in practice
Related terms
References
More in Web Protocols
GraphQL
GraphQL is a query language and runtime for APIs that lets clients request exactly the data they need in a single round trip, reducing over-fetching and under-fetching.
gRPC
gRPC is a high-performance, open-source remote procedure call framework initially developed by Google. It uses HTTP/2 for transport, Protocol Buffers as its interface definition language and message serialization format, and supports bidirectional streaming.
HTTP
HTTP is a text-based request-response protocol that defines how web clients and servers exchange resources, forming the foundation of data communication on the World Wide Web.
HTTP Pipelining
HTTP Pipelining is a technique in HTTP/1.1 that sends multiple requests on a single connection without waiting for each response, now largely replaced by HTTP/2 multiplexing.
HTTP Status Code
A three-digit integer in an HTTP response that indicates the result of the server's attempt to process the request, grouped into five classes (1xx through 5xx).
HTTP/1.1
HTTP/1.1 is the persistent-connection version of the Hypertext Transfer Protocol, defined in RFC 2616 and updated by RFCs 7230-7235, enabling multiple requests and responses over a single TCP connection.
HTTP/2
HTTP/2 is a binary, multiplexed version of HTTP that reduces latency through header compression, stream prioritization, and server push, as defined in RFC 7540.
HTTP/3
HTTP/3 is the third major version of the Hypertext Transfer Protocol, which runs over QUIC instead of TCP to reduce latency, eliminate head-of-line blocking, and improve connection setup time.
HTTPS
HTTPS (HTTP Secure) is HTTP traffic encrypted inside a TLS session, protecting data confidentiality, integrity, and server authenticity between a client and a web server.
Keep-Alive
Keep-Alive, also called persistent connection, allows multiple HTTP requests and responses over a single TCP connection, reducing latency and overhead compared to opening a new connection per request.