Access Control
Web Security
CORS
Programming
Origin Domains

Access-Control-Allow-Origin with multiple origin domains

Codemia Knowledge Hub ·

The Access-Control-Allow-Origin (ACAO) header cannot list several domains. To allow more than one origin, the server reads the request's Origin header, checks it against a list of allowed origins, and returns that single origin in ACAO when it matches.

How CORS and ACAO work

CORS is enforced by browsers to stop malicious sites from reading resources and data from other sites without permission. By default, browsers restrict cross-origin HTTP requests made from scripts. A JavaScript app on https://domain-a.com cannot read resources from https://domain-b.com unless domain-b.com permits it with CORS headers such as Access-Control-Allow-Origin.

ACAO says which origin may access the resource. Its value can be:

  • A specific origin, such as https://example.com.
  • *, allowing any origin (not allowed for requests with credentials).
  • null, which matches the opaque null origin (for example sandboxed iframes or file: pages). It does not block everyone, and it should not be used as a "deny" value.

Why a comma-separated list fails

Setting Access-Control-Allow-Origin: https://domain-a.com, https://domain-b.com is not valid. The header holds one origin (or *), so multiple origins need conditional handling.

Echoing the allowed origin from the server

The most flexible approach is to set ACAO dynamically from the incoming request's origin, responding only when that origin is in an allowlist.

Suppose only https://domain-a.com and https://domain-b.com may access https://api.server.com:

python
1from flask import Flask, request
2
3app = Flask(__name__)
4
5ALLOWED_ORIGINS = ["https://domain-a.com", "https://domain-b.com"]
6
7def set_cors_headers(request):
8    origin = request.headers.get('Origin')
9    if origin in ALLOWED_ORIGINS:
10        return {"Access-Control-Allow-Origin": origin}
11    else:
12        return {"Access-Control-Allow-Origin": "null"}  # or omit this header to use same-origin policy
13
14# Assuming Flask framework
15@app.route("/data")
16def data():
17    headers = set_cors_headers(request)
18    return "Data response", headers

For origins that are not allowed, omitting the header is the safer choice than returning "null".

Handling it in a reverse proxy

A reverse proxy can also add or modify CORS headers, using rules more complex than you may want to write in application code.

Security rules for multiple origins

  • Always validate the Origin header against an allowlist.
  • Do not use Access-Control-Allow-Origin: * with credentials (cookies, HTTP authentication, client-side SSL certificates).
  • Consider how CORS fits your overall security posture and use measures such as HTTPS.
FeatureDetails
ACAO Single OriginSet by Access-Control-Allow-Origin: https://example.com
ACAO Multiple OriginsNot directly supported, requires conditional handling
ACAO WildcardSet by Access-Control-Allow-Origin: * (use cautiously)
Server ImplementationDynamic ACAO response based on a whitelist of origins
SecurityValidate every request against a list of allowed origins

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design