Use a wildcard CORS rule only for public responses

  • Access-Control-Allow-Origin: * permits cross-origin reads without credentials and can be appropriate for public resources. It can't authorize a credentialed browser read, even alongside Access-Control-Allow-Credentials: true.

  • For credentialed reads, return an explicitly allowed origin. MDN's CORS guide explains how browsers handle credentials and wildcards.

Check allowed origins to avoid a CORS misconfiguration

  • A server that copies any incoming Origin into Access-Control-Allow-Origin can let an untrusted site read the response. Keep a list of allowed origins and check it before returning a matching origin.

  • If that response header changes with the request's origin, include Vary: Origin so caches can tell the responses apart.

Keep server permission checks separate from CORS

A browser's cross-origin read restriction isn't a login check. Enforce authentication and authorization on the API itself, and handle cross-site request forgery separately.

Frequently asked questions

Why does my request work with curl but fail CORS in the browser?

Browsers enforce CORS. A command-line client such as curl doesn't, so getting a response there doesn't tell you whether a page on another origin may read it.

  • Website security

    Catch leaked API keys and exposed files before someone uses them against you.

  • Cookie security

    Choose cookie attributes that fit your sign-in flow and limit unnecessary exposure.

  • Open redirects

    Keep a return-address parameter from sending visitors to an unapproved destination.

  • API rate limiting

    Put limits on sign-in, password reset, forms and other public endpoints.