Cross-Origin Resource Sharing (CORS)

Security Glossary - Security Headers

Definition: CORS is a browser security mechanism that controls which external domains can make requests to your server. By default, browsers block cross-origin requests (requests from a different domain). CORS headers like Access-Control-Allow-Origin tell the browser which origins are permitted to access your API or resources.

Configuration Reference

HeaderPurposeExample
Access-Control-Allow-OriginAllowed originshttps://app.example.com
Access-Control-Allow-MethodsAllowed HTTP methodsGET, POST, PUT
Access-Control-Allow-HeadersAllowed request headersContent-Type, Authorization
Access-Control-Allow-CredentialsAllow cookies/authtrue
Access-Control-Max-AgePreflight cache duration86400

Why CORS Is Important

CORS exists to prevent malicious websites from making requests to your API on behalf of a user without their knowledge. Without the Same-Origin Policy and CORS, a malicious page could use JavaScript to read data from your banking API using the user's session cookies.

Misconfigured CORS is a common security vulnerability. Setting Access-Control-Allow-Origin: * allows any website to make requests to your API. If your API uses cookies or authentication, this is dangerous because any malicious site could make authenticated requests. Only allow specific, trusted origins.

The most dangerous misconfiguration is reflecting the request's Origin header back as Access-Control-Allow-Origin combined with Access-Control-Allow-Credentials: true. This allows any site to make authenticated cross-origin requests to your API. Always validate the Origin header against a whitelist of trusted domains.

Checking Your Setup

A security audit checks your server's CORS headers. Verify that Access-Control-Allow-Origin is not set to * if your API uses authentication. Check that only trusted origins are allowed and that Access-Control-Allow-Credentials is not combined with a wildcard origin.

See how your site handles CORS

Run a Security Audit

Common Questions About CORS

When should I use CORS?
CORS is needed when your frontend and API are on different domains (like app.example.com calling api.example.com). Configure specific allowed origins rather than using a wildcard. If your frontend and API share a domain, CORS is not needed.
Is Access-Control-Allow-Origin: * dangerous?
It depends. For truly public APIs that do not use authentication (like public weather data), a wildcard is fine. For APIs that use cookies, sessions, or authentication headers, never use a wildcard. Specify exact allowed origins instead.
Disclaimer: DomainOptic provides automated informational scans only. Results do not constitute professional security advice, compliance certification, or a guarantee of security. Always verify findings independently.