111. What is CORS?
Define Cross-Origin Resource Sharing as an HTTP-header mechanism through which a server can permit selected cross-origin browser reads that the same-origin policy would otherwise restrict. Explain simple requests, preflight requests, allowed origins, methods, headers, credentials, caching, and why CORS is neither authentication nor a way to stop non-browser clients from sending requests.
CORS is an HTTP-header mechanism that lets a server permit selected cross-origin browser reads that the same-origin policy would otherwise restrict. It controls allowed origins, methods, headers, and credentials. It is not authentication or authorization, and non-browser clients are not stopped by CORS.
CORS is a browser rule that controls when a website opened from one place can read information returned by a different website or service. Browsers normally keep different places separated so one website cannot freely read another website's replies. The service receiving the request can choose which websites are allowed to read its reply. Some requests can be sent immediately, while others need an extra permission check first. The service can also decide whether requests using cookies or similar account information are allowed. This browser rule does not prove who a user is or decide what that user may access.
- Should I explain both simple requests and preflighted requests?
- Should I include credentialed requests such as requests using cookies or HTTP authentication?
- Should I also explain what security checks must still happen on the server?
CORS stands for Cross-Origin Resource Sharing. It is an HTTP-header mechanism through which a server tells a browser which cross-origin responses browser JavaScript is allowed to read.
An origin is the combination of scheme, host, and port. For example, https://app.example.com and https://api.example.com are different origins because their hosts differ. Browsers normally enforce the same-origin policy, which restricts JavaScript from reading responses from another origin. CORS gives the server a controlled way to relax that restriction for selected origins.
For a cross-origin request that qualifies as a CORS simple request, the browser can send the request without a preflight. It includes an Origin header, for example Origin: https://app.example.com. If the server wants that origin to be able to read the response, it can return Access-Control-Allow-Origin: https://app.example.com. The browser checks the CORS response headers before exposing the response to JavaScript.
A request qualifies as simple only when it satisfies specific CORS safelist rules. Its method must be GET, HEAD, or POST, and any manually set request headers and, for POST, the Content-Type must meet the CORS safelist requirements. For example, a POST using application/json is not a simple request and normally requires preflight.
When a request requires preflight, the browser first sends an OPTIONS request. That preflight asks whether the server permits the intended cross-origin operation. It contains Origin and Access-Control-Request-Method, and it can also contain Access-Control-Request-Headers when the real request plans to use non-safelisted headers.
The server can answer with headers including Access-Control-Allow-Origin, Access-Control-Allow-Methods, and Access-Control-Allow-Headers. The browser evaluates that response. If the preflight does not grant the required permission, the browser does not send the actual preflighted request.
Allowed origins should be configured deliberately. Access-Control-Allow-Origin: * allows any origin to access a CORS response when credentials mode is not include. For sensitive browser APIs, the server should normally allow only the origins that genuinely need access. If the server dynamically chooses an allowed origin from the request's Origin header, it must validate that value against a trusted allowlist. Blindly reflecting every supplied Origin defeats the purpose of restricting origins.
CORS can also govern methods and request headers. During preflight, Access-Control-Allow-Methods tells the browser which methods the server permits for the cross-origin request, and Access-Control-Allow-Headers tells it which requested non-safelisted headers are permitted. These are browser CORS permissions. They do not replace server-side authentication, authorization, input validation, or business rules.
Credentials need special care. For Fetch, credentials include cookies, TLS client certificates, and HTTP authentication credentials. A cross-origin Fetch request that needs credentials such as cookies commonly uses credentials: "include". For the browser to expose a credentialed cross-origin response, the server must return Access-Control-Allow-Credentials: true and must return a specific permitted origin in Access-Control-Allow-Origin; it cannot use * for that credentialed response.
CORS does not override cookie policy. Cookies are separately controlled by attributes such as Secure, HttpOnly, SameSite, Domain, and Path. Whether a cookie is actually sent therefore depends on cookie rules as well as the Fetch credentials mode. For cross-site cookie use, the cookie's SameSite policy is especially important.
Preflight permissions can be cached. A server may send Access-Control-Max-Age to tell the browser how long a successful preflight result may be reused, subject to browser-specific limits. This can reduce repeated OPTIONS requests and their extra latency. A long cache period can also delay the effect of a changed CORS policy until the cached permission expires.
When a server returns different Access-Control-Allow-Origin values depending on the request's Origin, it should send Vary: Origin. This tells HTTP caches that the response can vary according to the Origin request header and helps prevent cached CORS metadata for one origin from being incorrectly reused for another.
The most important security point is that CORS is not authentication. It does not prove who the user is. It is also not authorization. The trusted server must authenticate requests where required and enforce authorization for every protected resource and operation. A request coming from an allowed origin must not automatically be trusted.
CORS is also not a firewall. It does not generally prevent HTTP requests from reaching a server. CORS enforcement is mainly performed by browsers when scripts make cross-origin requests and try to access the results. Tools such as curl, backend services, native applications, and other non-browser clients do not become unable to call an API simply because its CORS policy would reject a browser origin. The server must therefore protect sensitive endpoints with real server-side security controls.
CORS should also not be confused with CSRF. CORS primarily governs cross-origin browser access to responses and introduces preflight checks for certain cross-origin requests. CSRF is an attack in which a victim's browser is induced to perform an unwanted authenticated action. Appropriate CSRF defenses can include SameSite cookies, anti-CSRF tokens, and server-side Origin or Referer validation where suitable. CORS alone is not a complete CSRF defense.
The practical rule is: allow only the browser origins that need cross-origin access, allow only necessary methods and headers, enable credentials only when needed, cache preflight results carefully, and keep authentication and authorization on the trusted server. Then verify the policy from both permitted and unpermitted origins.
- Identify the frontend origin and API origin. If their scheme, host, or port differs, the browser request is cross-origin.
- Decide exactly which browser origins need access and maintain a strict server-side allowlist.
- Determine whether each request qualifies as a simple request or requires preflight because of its method, headers, or Content-Type.
- Return only the required CORS response headers for approved origins, methods, and headers.
- Enable credentialed cross-origin access only when necessary, use a specific allowed origin rather than *, and configure credentials consistently on the browser and server.
- Keep authentication, authorization, validation, and CSRF protections independent of CORS.
- Configure preflight caching carefully and send Vary: Origin when the response's allowed origin varies by request Origin.
- Test permitted and unpermitted origins, simple requests, preflighted requests, credentialed requests, and authorization failures.
CORS itself usually adds little application CPU or memory work. A simple cross-origin request does not require an extra preflight network round trip. A preflighted request can add an OPTIONS request before the real request, increasing latency and server traffic. Successful preflight results can be cached to reduce that cost. The main operational cost is maintaining correct origin, method, header, credential, and cache policies across environments. CORS configuration also needs testing because a policy that is too strict can break legitimate browser clients, while a policy that is too broad can expose responses to unintended browser origins.
Interviewers want to know whether you understand the browser same-origin policy, how a server selectively relaxes it with CORS response headers, when preflight requests occur, how credentials and preflight caching affect the policy, and where the real security boundary exists. A strong answer also makes clear that CORS does not authenticate users, does not replace server-side authorization, and does not prevent non-browser HTTP clients from contacting an API.
Common mistakes include saying CORS blocks all cross-origin network requests; confusing the same-origin policy with a rule that prevents all requests from being sent; treating Access-Control-Allow-Origin: * as appropriate for every API; blindly reflecting any Origin value; attempting to use wildcard Access-Control-Allow-Origin with credentialed requests whose credentials mode is include; forgetting that cookies have separate SameSite, Secure, HttpOnly, Domain, and Path rules; forgetting Vary: Origin when the allowed origin is selected dynamically; assuming a successful preflight authenticates or authorizes a user; treating CORS as a complete CSRF defense; and assuming CORS prevents curl, backend services, native applications, or other non-browser clients from contacting the API.
Start with the same-origin policy, then define CORS as the server-controlled HTTP-header mechanism that selectively permits cross-origin browser reads. Explain simple requests and preflight, then cover origins, methods, headers, credentials, and preflight caching. Finish by stating clearly that CORS is neither authentication nor authorization and that the trusted server must enforce access control.










