What is CORS in IT? Browser security explained
Short answer
Cross-Origin Resource Sharing (CORS) is a browser security mechanism that lets a server declare which other origins may read its responses. It extends the same-origin policy so trusted sites can use APIs and resources hosted elsewhere. Without the right headers, a script's cross-origin request fails in the browser.
Key points
- CORS is enforced by browsers, not by the remote server alone.
- A server grants access through HTTP response headers such as Access-Control-Allow-Origin.
- Preflight OPTIONS requests check permission before non-simple requests.
- Credentials require explicit server permission and cannot be combined with a wildcard origin.
- CORS errors are visible in browser developer tools, not in JavaScript error details.
What is CORS in IT?
When people ask what is CORS in IT, they are usually asking how a browser decides whether a web page may read data from another origin. Cross-Origin Resource Sharing is a browser security mechanism that uses HTTP headers to let a server declare which origins, meaning combinations of scheme, host, and port, may access its responses. It does not remove the same-origin policy; it creates a controlled exception to it. The server that owns the resource makes the decision, and the browser enforces it before JavaScript can read the response.
An origin is more specific than a domain name. https://app.example.com and http://app.example.com are different origins because the schemes differ. https://app.example.com and https://api.example.com are different because the hosts differ. https://app.example.com:443 and https://app.example.com:8443 are different because the ports differ. This precision matters because a page loaded from one origin cannot freely read responses from another unless the second origin sends permission headers.
Why browsers restrict cross-origin requests
By default, a script loaded from one origin cannot read responses from another, because pages may execute untrusted code and users may be signed in elsewhere. The same-origin policy prevents a script on one site from reading responses from another site by default. Without that rule, a malicious page could make requests with the user's cookies and inspect private data returned by a bank, mail service, or internal API.
CORS exists so legitimate applications can still share resources across origins without weakening that default. A public API, a separate frontend and backend, or a third-party widget can work when the resource owner explicitly allows the caller's origin. The browser checks the response headers and blocks JavaScript access when permission is missing or too broad.
How a server grants permission
The resource server grants permission by sending CORS headers in its HTTP response. The most important header is Access-Control-Allow-Origin, which names an allowed origin or, for public resources, a wildcard. Other headers describe allowed methods, allowed request headers, exposed response headers, and whether credentials may be included.
| Header | Purpose |
|---|---|
| Access-Control-Allow-Origin | Names the origin that may read the response, or uses * for public resources. |
| Access-Control-Allow-Methods | Lists HTTP methods permitted for the actual request. |
| Access-Control-Allow-Headers | Lists request headers the client may send. |
| Access-Control-Allow-Credentials | Allows cookies or authorization headers when set to true. |
| Access-Control-Max-Age | Tells the browser how long to cache a preflight result. |
| Access-Control-Expose-Headers | Makes selected response headers readable by JavaScript. |
The header is set by the external service that owns the resource, not by the site that loaded the page. For example, if a page on www.example.com calls an API on service.example.com, service.example.com decides whether www.example.com is allowed. This separation keeps the resource owner in control.
Simple requests and preflight checks
Some cross-origin requests are sent directly, while others trigger an extra OPTIONS request called a preflight. The browser sends the preflight to ask whether the server will permit the method and headers planned for the actual request. If the server rejects the preflight, the browser does not send the actual request.
- Methods other than GET, HEAD, or POST, such as PUT or DELETE.
- Custom request headers, such as X-Requested-With or X-Auth-Token.
- A Content-Type of application/json, which is not treated as a simple content type.
The term simple request comes from an older CORS specification, and the current Fetch specification does not use that term. The protocol also changed over time: behavior that was once required is no longer required, but not every browser has implemented the change. That is why testing in the browsers your users actually run remains useful.
A typical preflighted request looks like this in network logs: first an OPTIONS request with Access-Control-Request-Method and Access-Control-Request-Headers, then the actual POST or PUT only if the response allows it. Developers often discover CORS problems by watching that sequence in browser developer tools.
Credentials, cookies, and wildcard origins
Credentialed requests need extra care. When a request includes cookies, HTTP authentication, or client certificates, the server must respond with Access-Control-Allow-Credentials set to true. It must also name the calling origin explicitly in Access-Control-Allow-Origin rather than using a wildcard. Combining a wildcard origin with credentials is not allowed because it would let any site read authenticated responses.
A wildcard origin suits public resources meant to be readable by any code on any site, for example a publicly hosted web font. For private or user-specific data, use an allowlist of exact origins. Reflecting the request's Origin header without validation is dangerous because it can let untrusted sites read sensitive responses.
CORS is a browser rule, not server authorization
CORS controls what a browser lets a script read; it is not a substitute for authentication or authorization on the server. A request can still reach the server and perform an action even when the browser blocks the response from JavaScript. Therefore, APIs must validate identity, permissions, and input on the server regardless of CORS settings.
This distinction matters during security reviews. A misconfigured Access-Control-Allow-Credentials header can expose sensitive information to third-party sites, but removing CORS headers does not protect an endpoint from direct requests made by tools, scripts, or other clients. Treat CORS as part of the browser security model, not as the whole security model.
Common CORS failures and how to debug them
CORS failures produce errors, but JavaScript receives only a generic network error for security reasons. The browser console and network panel contain the specific cause, such as a missing header, a disallowed origin, or a failed preflight. Checking those tools is the fastest way to move from a vague failure to a concrete fix.
- Open the browser console and confirm that the error is CORS-related rather than a DNS, TLS, or server error.
- Inspect the failed request in the network panel and look for an OPTIONS preflight.
- Compare the request origin with the value returned in Access-Control-Allow-Origin.
- Check whether the method and headers are listed in Access-Control-Allow-Methods and Access-Control-Allow-Headers.
- Verify credential settings on both the client request and the server response.
- Test with a known allowed origin before changing production configuration.
Related topics include a practical guide to choosing a programming language, computer vision: what it is and how it works, and Organize Email Inbox with Filters and Labels.
When CORS may not be required
Not every cross-origin interaction needs CORS. If a page merely embeds a resource, such as an image, stylesheet, script, iframe, or video, and has no way to read its data, CORS may not be required. The same is true when a page only writes to an external resource. In those cases, data from the external origin is less likely to be exposed.
Because browsers now treat cross-origin requests more strictly, new features that start requests usually require CORS no matter what they do. Relying on an absence of CORS for a new feature is risky. When in doubt, design the endpoint to return explicit permission headers and test the exact browser behavior.
Practical configuration guidance
Start with the smallest useful permission. Allow specific origins instead of a wildcard whenever the data is not public. Keep allowed methods and headers to the minimum needed by the client. Set Access-Control-Max-Age to reduce repeated preflight traffic, but not so high that policy changes take too long to reach users.
Expose only the response headers that client code needs. Log rejected origins and failed preflights on the server so configuration mistakes are visible. Review credential settings carefully, especially Access-Control-Allow-Credentials, because they affect whether third-party sites can perform privileged actions or retrieve sensitive information.
For public APIs, a wildcard origin can be appropriate. For authenticated APIs, use exact origins, validate them against an allowlist, and avoid reflecting arbitrary Origin values. This approach keeps cross-origin sharing intentional rather than accidental.
Questions people also ask
What does CORS stand for?
Cross-Origin Resource Sharing. It is a browser mechanism that lets a server declare which other origins may read its responses.
Why do I see a CORS error in the console?
The browser blocked JavaScript from reading a cross-origin response because the server did not return permission headers for your origin. The console and network panel show the specific missing header or failed preflight.
Does CORS protect my API from all requests?
No. CORS is enforced by browsers and controls script access to responses. Direct requests from tools or other clients can still reach the server, so authentication and authorization remain necessary.
When is a preflight request sent?
A preflight is sent for requests that use methods other than GET, HEAD, or POST, custom headers, or a Content-Type of application/json. The browser sends OPTIONS first and makes the actual request only if the server allows it.
Related pages
- Client vs Server Rendering: Architecture and Performance
Client-side rendering generates HTML directly inside the user browser via downloaded JavaScript, whereas server-side rendering compiles the full HTML document on the origin or edge server prior to transmission. This core difference shifts computational burdens between client hardware and cloud infrastructure, altering First Contentful Paint times, search indexing reliability, and hosting costs. Teams select between them based on content volatility, interaction requirements, and performance objectives.
- How Does Browser Cache Work? A Practical Guide
A browser cache stores local copies of web resources so repeat visits skip downloads. Servers set freshness with Cache-Control, and browsers validate or evict entries when space runs low.
- How Do Web Cookies Work? A Practical Guide
A web cookie is a small piece of data a server sends to a browser, which the browser stores and returns with later requests to the same site. Cookies let stateless HTTP remember logins, preferences, and activity, but they also raise privacy and security concerns.
- What is WebSocket and how it works?
WebSocket is a protocol for bidirectional, low-latency communication over a persistent connection between a client and a server. It begins with an HTTP handshake and then stays open so either side can send messages without creating a new request each time. Use it when an application needs frequent real-time updates; use ordinary HTTP when request-response traffic is enough.
- What is a REST API vs API? A Practical Guide
An API is a contract that lets software systems exchange requests and responses. A REST API is one kind of API that uses HTTP methods and resource-based URLs to manage data, usually with JSON. All REST APIs are APIs, but many APIs use other protocols or styles.
- What is GraphQL API? A Practical Guide
A GraphQL API is a query language and runtime for APIs that lets a client request only the fields it needs, including related data, in one operation. It is backed by a strongly typed schema that defines types, fields, and allowed operations. Clients control the shape of the response, while the server resolves data from its sources.
How this page was prepared
Before writing, the topic was researched in the sources below. The page then passed automatic quality checks.