The Access-Control-Allow-Origin
header is part of the Cross-Origin Resource Sharing (CORS) mechanism that allows or restricts resources (e.g., data, APIs) on a web server to be requested from a different domain than the one from which the resource originated. It is used by web servers to define which external websites (or domains) are allowed to access their resources.
How It Works
When a browser makes a request to a server from a different origin (domain, protocol, or port), this is known as a cross-origin request. By default, web browsers implement the same-origin policy, which restricts web pages from making requests to a different domain for security reasons.
However, if a web server allows cross-origin requests, it sends the Access-Control-Allow-Origin
header in the response to specify which domains are permitted to access the resource.
Key Points:
- The
Access-Control-Allow-Origin
Header:- This header tells the browser which domains are allowed to access the server’s resources.
- If the requesting domain is allowed, the browser will proceed with the request; otherwise, it will block the request due to the same-origin policy.
- Possible Values for
Access-Control-Allow-Origin
:- A specific domain (e.g.,
https://example.com
): Allows only that specific domain to access the resource. - A wildcard (
*
): Allows any domain to access the resource. This is useful for public APIs or resources that don’t need strict security but can be accessed by any website. - Null: In some cases, a value of
null
might be returned, indicating that the server only allows requests from specific origins, such as file-based requests (file://
).
- A specific domain (e.g.,
Example Scenarios
1. Allowing a Specific Origin
If a server wants to allow access only from https://example.com
, the response might look like:
Access-Control-Allow-Origin: https://example.com
This means that only https://example.com
is allowed to access the resource.
2. Allowing Any Origin (Wildcard)
If the server allows access from any domain, the header would look like:
Access-Control-Allow-Origin: *
This allows any origin to access the resource. This is commonly used for public resources or APIs that don’t need restrictions.
3. Handling Preflight Requests (OPTIONS Method)
For certain types of requests (e.g., non-GET requests, or requests with custom headers), the browser sends a preflight request using the OPTIONS
method. The server must respond with the appropriate headers to inform the browser if the request is allowed.
For example, the server’s response to a preflight request might look like this:
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: X-Custom-Header
Access-Control-Allow-Methods
: Specifies the allowed HTTP methods (e.g.,GET
,POST
, etc.).Access-Control-Allow-Headers
: Lists the allowed headers that can be sent in the actual request.
4. Allowing Multiple Origins (Dynamic)
While the Access-Control-Allow-Origin
header can only have one value (either a specific origin or a wildcard), some servers can dynamically adjust the value based on the request’s origin. For example, a server might check the Origin
header in the request and respond with the appropriate allowed origin.
Example (in a server-side script like Node.js or Python Flask):
if (request.origin === "https://allowed-site.com") {
response.setHeader('Access-Control-Allow-Origin', 'https://allowed-site.com');
} else {
response.setHeader('Access-Control-Allow-Origin', 'null'); // or block
}
Security Considerations
- Wildcard (
*
) Limitation: If you allow all origins (*
), the browser won’t allow credentials (cookies, HTTP authentication) to be included in the request. If you want to allow credentials, you must specify a specific origin rather than using the wildcard.Example:
Access-Control-Allow-Origin: https://example.com Access-Control-Allow-Credentials: true
- Allowing Credentials: If you want to allow the browser to send cookies or authentication headers with cross-origin requests, you need to set
Access-Control-Allow-Credentials
totrue
, and you cannot use the wildcard*
forAccess-Control-Allow-Origin
. You must specify the exact origin.
How the Browser Handles It
- Request from a different origin: The browser sends a request with the
Origin
header indicating the domain of the requesting site. - Server response: The server responds with the
Access-Control-Allow-Origin
header.- If the header matches the origin of the request, the browser allows the request to proceed.
- If the header is missing or doesn’t match the origin, the browser blocks the request and reports a CORS error.