Access-Control-Allow-Origin Multiple Origin Domains?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The Access-Control-Allow-Origin (ACAO) header is a crucial component of the Cross-Origin Resource Sharing (CORS) policy that enables web applications running at one origin (domain) to request resources from another origin securely. Web developers often encounter scenarios where they need to allow multiple origin domains to access resources, which requires a nuanced understanding of how to manipulate the ACAO header effectively.
Understanding CORS and ACAO
CORS is a security feature enforced by web browsers to prevent malicious websites from accessing resources and data from other sites without permission. By default, web browsers restrict cross-origin HTTP requests initiated from scripts. For example, a JavaScript application running under https://domain-a.com cannot request resources from https://domain-b.com unless domain-b.com explicitly permits it using CORS headers, like Access-Control-Allow-Origin.
The ACAO header specifies which origins can access the resources on the server. Its value can be:
- A specific URI, such as
https://example.com. *, allowing any origin (not secure for credentials).null, which prohibits all domains from accessing the resource.
Challenges with Multiple Origins
The challenge arises when a resource needs to be accessed by multiple origins, not just one. The ACAO header does not naturally support listing multiple domains. For example, setting the header to multiple origins like Access-Control-Allow-Origin: https://domain-a.com, https://domain-b.com is not valid.
Solutions for Handling Multiple Origins
Server-Side Dynamic Setting
The most flexible approach involves dynamically setting the ACAO header on the server based on the incoming request’s origin. This method allows the server to respond selectively based on whether the request’s origin is in a list of allowed origins.
Example:
Consider a scenario where only https://domain-a.com and https://domain-b.com are allowed to access resources from https://api.server.com. The server code might look like:
Using a Proxy
Another method to handle multiple origins is to use a reverse proxy that can add or modify CORS headers based on more complex rules than what might be supportable directly in server code.
Best Practices and Security Considerations
- Always validate the
Originheader against a whitelist of allowed origins. - Avoid using
Access-Control-Allow-Origin: *with credentials (cookies, HTTP authentication, and client-side SSL certificates). - Consider the impact of CORS on your overall security posture and use appropriate security measures, such as HTTPS.
Summary of Key Points
| Feature | Details |
| ACAO Single Origin | Set by Access-Control-Allow-Origin: https://example.com |
| ACAO Multiple Origins | Not directly supported, requires conditional handling |
| ACAO Wildcard | Set by Access-Control-Allow-Origin: * (use cautiously) |
| Server Implementation | Dynamic ACAO response based on a whitelist of origins |
| Security | Validate every request against a list of allowed origins |
Conclusion
Handling multiple origins in CORS is a common requirement that can be addressed effectively with server-side logic to dynamically set the ACAO header based on the origin of incoming requests. When implementing such features, it's crucial to consider the security implications, ensuring that only trusted origins are allowed access, thereby safeguarding the resources against potential cross-origin attacks.

