Use this when browsers must allow cross-origin reads.
CORS
CORS (Cross-Origin Resource Sharing) uses response headers like Access-Control-Allow-Origin to let browsers allow specific other origins to read responses. Never reflect arbitrary origins with credentials allowed.
1. CORS relaxes same-origin policy for specific origins.
2. Never reflect arbitrary Origin with credentials.
3. Allowlist exact origins.
4. Minimal methods/headers.
5. CORS is not authentication.
cross-origin request → preflight → allowed origin check
Context & applicabilityCommon mistakes · platform/version boundaries
- Reflecting any
Originback inAccess-Control-Allow-Origintogether withAllow-Credentials: true. - Allowing
nullorigin (sandboxed iframes, file URLs can send it). - Regex checks like
endsWith("example.com")that also matchevilexample.com. - Thinking CORS protects the server — it only controls what browsers let scripts read.
Operational reference
Fast context before you go deeper.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Allow only specific trusted origins.
- Never reflect any origin when credentials are allowed.
- Preflight requests check methods and headers.
curl -sI -H 'Origin: https://evil.example' https://<api>— CORS response- Access-Control-Allow-Origin values — no wildcard with credentials
- Allowed methods/headers — minimal
ReferenceWhy it matters1 section · depth on demand
Safe configuration
- Return
Access-Control-Allow-Originonly for an allowlist of known origins; do not reflect the request's Origin blindly. - Never combine
*with credentials; browsers block it, and reflecting origins withAccess-Control-Allow-Credentials: trueexposes user data. - Limit methods and headers in preflight responses.
CORS controls which origins can read responses in a browser; it does not protect the API from direct requests.
ReferenceImportant terms & boundaries1 section · depth on demand
In depth
CORS (Cross-Origin Resource Sharing) lets a server allow web pages from other origins to read its responses in the browser. By default, the same-origin policy blocks such reads; CORS headers relax it selectively. Misconfigured CORS can expose authenticated data to malicious sites.
Access-Control-Allow-Origin: https://portal.example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
Vary: Origin
Documented example, not captured output.
Dangerous patterns:
| Pattern | Risk |
|---|---|
Reflecting any Origin with credentials allowed | Any site can read user data |
null origin allowed | Exploitable from sandboxed pages |
Weak origin matching (endsWith("example.com")) | evilexample.com allowed |
Use an explicit allow-list of origins, and remember CORS is a browser control — it does not protect APIs from direct requests.
ReferenceConfiguration & state1 section · depth on demand
Example (preflight)
OPTIONS /api/orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: PUT
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, PUT
Access-Control-Allow-Credentials: true
Vary: Origin
Documented example, not captured output. Vary: Origin stops caches from serving one origin's response to another.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → CORS. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%