Skip to content

CORS config generator

Free, no sign-up Runs in your browser

Generate a correct CORS configuration for nginx, Apache or Express from your allowed origins, methods and headers. Refuses the unsafe wildcard with credentials.

One per line: scheme, host and port, no path. Use a single * only for a public API that needs no cookies or login.

Allowed methods

The headers the browser may send, separated by commas.

How long a browser may cache the preflight answer.

Your CORS config

HTTP headers
Enter at least one allowed origin.
nginx
Enter at least one allowed origin.
Apache
Enter at least one allowed origin.
Express middleware
Enter at least one allowed origin.

These configs compare the Origin header with your list and answer only for a match. Never copy the Origin header into Access-Control-Allow-Origin without that check: it allows every website, and with credentials it lets any site read your users’ data.

Everything you type here stays in your browser. Nothing is sent to HardenKit or to anyone else.

How to use the CORS config generator

  1. List the origins that need access, one per line, with scheme and port.
  2. Tick the methods and list the request headers your API accepts.
  3. Switch on credentials only if the browser must send cookies or HTTP authentication.
  4. Copy the config for your server and test it with the CORS header checker.

About this tool

Browsers stop a script on one origin from reading responses from another. Cross-Origin Resource Sharing (CORS) is how a server lifts that rule for origins it trusts, by answering with Access-Control-Allow-Origin and related headers. It does not protect the server: it relaxes a protection the browser gives your users, so every origin you allow is one you trust with their data.

The header takes exactly one origin, or * for everyone. To allow several, the server has to compare the request’s Origin header with a list and send back the one that matched, with Vary: Origin so caches keep the answers apart. The configs generated here do that with an exact list. Requests that are not simple are preceded by an OPTIONS preflight, which the configs answer with the allowed methods and headers.

The dangerous shortcut is copying whatever Origin arrives into the response. Together with Access-Control-Allow-Credentials: true, that lets any website make requests with your users’ cookies and read the results. Browsers refuse * with credentials for exactly this reason, and so does this tool. Allowing the origin null has the same effect, because any page can produce it from a sandboxed frame.

Sources

Questions

Why can I not use * with credentials?

The Fetch standard forbids it: a browser rejects a credentialed response whose Access-Control-Allow-Origin is *. If it were allowed, any website could read your users’ private data with their cookies. Name the exact origins instead.

How do I allow multiple origins in CORS?

The header cannot hold a list. Keep the allowed origins on the server, compare each request’s Origin with them, and send back that origin only when it matches. Add Vary: Origin so a cache does not serve one origin’s response to another.

What is a preflight request?

Before a request with a method such as PUT or DELETE, or with headers such as Authorization or a JSON content type, the browser sends an OPTIONS request asking for permission. The server answers with the allowed methods and headers. Access-Control-Max-Age lets the browser cache that answer.

Does CORS protect my API from other clients?

No. CORS is enforced by browsers only. curl, scripts and other servers ignore it completely. It decides which websites may read your API from a visitor’s browser; authentication and authorization still have to be done by the API.

Related tools