Cross-Origin Resource Sharing (CORS) lets a server tell browsers which other origins may read its responses. A CORS misconfiguration becomes a security issue when it gives an untrusted origin access to sensitive data.
Issue
Same-origin policy and CORS
The same-origin policy (SOP) normally prevents JavaScript on one origin from reading responses from another. Some cross-origin requests can still be sent; sending a request and reading its response are separate permissions.
An origin consists of scheme, host, and port. Paths are not part of it. For example, https://example.com/a and https://example.com/b share an origin, but http://example.com, https://api.example.com, and https://example.com:8443 do not share the origin of https://example.com.
CORS enables legitimate sharing, such as a frontend reading an API on a different origin. The server sends CORS response headers, and the browser enforces them. Same-origin policy reference
Important headers
| Header | Purpose | Example value |
|---|---|---|
Access-Control-Allow-Origin | Specifies one allowed origin, or a wildcard for requests without credentials. | https://app.example.com |
Access-Control-Allow-Credentials | Permits response access when the request's credentials mode is include. The valid value is lowercase true. | true |
Access-Control-Allow-Methods | Lists methods permitted by a preflight response. | GET, POST, PUT |
Access-Control-Allow-Headers | Lists request headers permitted by a preflight response. | Content-Type, Authorization |
Access-Control-Expose-Headers | Lets JavaScript read additional response headers beyond those exposed by default. | X-Request-ID |
Access-Control-Max-Age | Sets the preflight cache lifetime in seconds, subject to browser limits. | 600 |
Content-Length is already exposed by default. Access-Control-Expose-Headers cannot make the forbidden Set-Cookie response header readable. Exposed headers · Preflight caching
A browser sends an OPTIONS preflight before requests such as those using JSON content types, custom headers, or methods such as PUT. It checks permission before sending the actual request. A simple GET may need no preflight, but its response still needs valid CORS headers to be readable. CORS guide
Common configuration mistakes
- Reflecting arbitrary request origins without validating them.
- Using loose substring, prefix, or suffix checks for trusted domains.
- Allowing
nullorigins for sensitive resources. - Trusting origins that can be compromised through XSS or that use insecure HTTP.
- Allowing unrestricted reads of sensitive internal resources.
Examples
Misconfigured response
Illustrative request from a page hosted at https://attacker.example:
HTTP EXAMPLEGET /api/profile HTTP/1.1
Host: api.example.com
Origin: https://attacker.example
Cookie: session=test-session
Illustrative response:
HTTP EXAMPLEHTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: https://attacker.example
Access-Control-Allow-Credentials: true
{"email":"test-user@example.com","customerId":"12345"}
If the browser sends the session cookie and the script can read this private response, the result demonstrates cross-origin disclosure.
Browser proof of concept
Run this from your controlled test origin. Replace the URL with the authorized endpoint. It displays the response locally and does not transmit it elsewhere.
BROWSER EXAMPLE<!doctype html>
<meta charset="utf-8">
<title>CORS test</title>
<button id="test">Test response access</button>
<pre id="output"></pre>
<script>
document.getElementById("test").onclick = async () => {
const output = document.getElementById("output");
try {
const response = await fetch("https://api.example.com/api/profile", {
credentials: "include"
});
output.textContent = await response.text();
} catch (error) {
output.textContent = "Request failed. Check the browser console and network log.";
}
};
</script>
A successful read of a login page or public error message does not establish private-data exposure.
Restricted response
For an approved frontend:
HTTP EXAMPLEAccess-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Vary: Origin
For other origins, omit Allow-Origin. Return one approved origin per response, not a comma-separated list.
Impact
An attacker-controlled page may read private responses using the visitor's session, including profile details or tokens returned in response bodies. This does not directly reveal the target's cookies; the browser sends eligible cookies to the target server. JavaScript cannot read Set-Cookie through CORS. Cookie reference
For a typical cookie-authenticated disclosure, all of these must hold:
- The attacker can run JavaScript on an origin the target allows.
- The browser sends the required credentials.
- The response includes a matching allowed origin and
Access-Control-Allow-Credentials: true. - The response contains sensitive data that the attacker's script can read.
Cookie SameSite settings and third-party cookie restrictions can prevent credentials from being sent. credentials: "include" does not override them. A bearer token stored by the legitimate frontend is not automatically available to an attacker page. Credentialed requests · CORS and cookies
Sensitive resources relying only on network location may also be exposed without cookies if the victim's browser can reach them and browser network protections permit access.
Do not infer account takeover from headers alone. Report the data actually exposed and any demonstrated follow-on action. CORS is not authentication, authorization, or CSRF protection.
Testing
1. Check whether an untrusted origin can read private data
Use a signed-in test account and a private endpoint. Send an untrusted Origin, inspect the allow-origin and allow-credentials headers, and confirm in a browser whether the response is readable.
Example: Use a test profile that contains a known email address. Header reflection alone does not prove that the browser can read it.
HTTP EXAMPLEGET /api/profile HTTP/1.1
Host: api.example.test
Origin: https://outside.example.test
Cookie: session=TEST_SESSION
2. Check origin matching and credential rules
Compare a trusted origin with similar hostnames, a different scheme and port, and the null origin where applicable. Check the browser’s cookie behavior and whether each response is readable.
Example: The intended origin is https://app.example.test. A look-alike hostname is a different origin.
HTTP EXAMPLEGET /api/profile HTTP/1.1
Host: api.example.test
Origin: https://app.example.test.outside.example.test
3. Check wildcard access to sensitive responses
Inspect whether the server returns a wildcard allow-origin value and compare public data with sensitive responses. Confirm whether a browser can read the response under the request’s actual credential conditions.
Example: A wildcard can permit a browser to read an unauthenticated response. Browsers do not permit it for a request whose credentials mode is include.
HTTP EXAMPLEGET /api/profile HTTP/1.1
Host: api.example.test
Origin: https://outside.example.test
4. Review trust in subdomains and partner origins
Identify origins allowed to read sensitive API responses and check who controls their pages. Investigate user-hosted content, insecure HTTP origins, or a confirmed script issue on an allowed site.
Example: A trusted subdomain hosting untrusted scripts may widen access to the API. Trust alone does not establish that such scripts can be placed there.
5. Review preflight, public data, and cached responses
Compare OPTIONS and actual responses for supported methods and headers. Check public versus private routes, errors, redirects, and shared-cache variation by Origin.
Example: Repeat a request through the same cache with two origins. A wrong header is a clue; check the browser and actual data exposure.
Avoid these false positives:
Access-Control-Allow-Credentials: truealone does not grant access.- An allowed origin alone does not prove sensitive data disclosure.
Access-Control-Allow-Origin: *with credentials mode include is blocked by browsers.Origin: *is not a normal browser-generated origin; test wildcard response behavior using a real origin.- Public data intentionally shared across origins is not automatically a vulnerability.
The wildcard and lowercase-true rules are defined in the Fetch Standard.
Mitigation
- Allow exact trusted origins. Compare parsed scheme, host, and effective port against an explicit allowlist. Avoid substring matching and unvalidated reflection.
- Limit the scope. Enable CORS only on endpoints that need it. Allow only required methods, request headers, and exposed response headers.
- Restrict credentialed access. Enable Allow-Credentials only when required and only for approved origins. Use HTTPS origins for sensitive APIs.
- Reject null for sensitive resources. Do not treat opaque origins as trusted identities.
- Protect trusted origins. Fix XSS and review subdomain ownership and third-party hosting dependencies.
- Keep internal data protected. Do not rely on network location alone or apply wildcard sharing to sensitive internal resources.
- Keep server-side controls. Enforce authentication, authorization, session security, and CSRF defenses where needed. Non-browser clients can forge Origin.
- Handle caching correctly. When the allowed origin varies by request, include
Vary: Origin. Use separate cache controls appropriate for private data; Vary is not a privacy control. CORS caching reference - Retest both paths. Confirm the approved frontend works and untrusted origins cannot read sensitive responses. Include these checks in regression testing.
Use Access-Control-Allow-Origin: * only for resources intentionally readable by any origin without credentials.