Add the Access-Control-Allow-Origin header to the browser response
CORS (Cross-Origin Resource Sharing) is a browser security mechanism. When a browser makes a cross-origin request, it checks Access-Control-Allow-Origin to decide whether the calling page can read the response, and blocks access without a permitting header even if the server returned 200 OK with the full content.
Consider a user visiting http://site-a.com who requests an image hosted at http://cdn.site-b.com/image.jpg. The browser sends a request to http://cdn.site-b.com/:
GET /image.jpg HTTP/1.1
Host: cdn.site-b.com
Origin: http://site-a.com
The Origin header tells the server which site sent the request. The http://cdn.site-b.com server uses this value to decide whether to include Access-Control-Allow-Origin in the response.
If the header is present and permits http://site-a.com, the browser allows the page to read the response and the image is displayed.
If the header is absent or does not permit http://site-a.com, the browser blocks access to the response — even though the server returned a 200 OK with the image.
Orange's CDN adds the Access-Control-Allow-Origin header to browser responses, which controls which origins can read the response. Without the header, the browser reports XMLHttpRequest cannot load http://domain.com and No Access-Control-Allow-Origin header is present on the requested resource.
Configure CORS header support
Add the header in one of three places:
Resource settings apply it to all files delivered over the CDN.
A rule applies it only to particular files.
The origin server sends Access-Control-Allow-Origin on its own response.
Configure CORS in the resource settings
Resource settings add the header to every file on the CDN resource.
1
Open the CDN resource
In the CDB Technical Web Portal, navigate to CDN > CDN resources and click the CDN resource to configure.
2
Open CORS header support
In the resource settings panel, click HTTP headers to expand the section, then click CORS header support.
3
Enable the toggle
Enable the CORS header support toggle.
4
Select an origin mode
Select one of three options:
5
Set the status-code behavior
(Optional) Enable Always add the header to response from CDN regardless of response code to add Access-Control-Allow-Origin on every status code, including a response for unavailable content. With the toggle off, the header is added only on status 200, 201, 204, 206, 301, 302, 303, 304, 307, and 308.
6
Save the resource
Click Save changes.
Configure CORS via rule creation
A rule limits the header to particular files.
1
Open the CDN resource
In the Customer Portal, navigate to CDN > CDN resources and click the CDN resource to configure.
2
Create a blank rule
Open the RULES tab, click Create rule, and select Create blank rule.
3
Set the rule path
Set the rule name and the file path in create a rule.
4
Add CORS header support
Click Add option, select CORS header support, and click Close.
5
Set the header behavior
Configure the option. An enabled option adds Access-Control-Allow-Origin. An option that is added and left disabled does not add the header.
6
Save the rule
Click Create rule.
Configure CORS on the origin server
Apache sets * on images only when the request includes Origin, and sets * on fonts, CSS, and JavaScript for every request.
Clear the CDN resource or file cache in clear CDN cache, then confirm the header with cURL or browser DevTools.
Check CORS through cURL
Send the file URL with an Origin header. That header is required for the $http_origin modes, because without it the CDN omits Access-Control-Allow-Origin even when the option is set, while the * mode returns the header whether or not Origin is present.
1
Request the file
Run this command with the file URL and the site origin:
In the output, confirm that Access-Control-Allow-Origin is present and the value is * or the origin of the request.
HTTP/1.1 200 OK
Server: nginx/1.13.1
Date: Fri, 24 Feb 2023 12:54:24 GMT
Content-Type: image/jpeg
Content-Length: 124024
Connection: keep-alive
X-Image-Generated: 29
X-Image-Meta: 1024x768
X-Image-Read: 71
Expires: Fr, 10 March 2023 12:51:43 GMT
Cache-Control: max-age=15552000
Access-Control-Allow-Origin: *
Last-Modified: Sun, 29 Jan 2023 12:00:00 GMT
Cache-Control: max-age=315360000, public
Cache: HIT
X-Cached-Since: 2022-12-09T12:51:43+00:00
X-ID: m9-up-e245
Check CORS with DevTools in a browser
Browser DevTools shows the same header on a CDN file in the Network panel.
1
Open the Network panel
Open the site in Google Chrome.
Right-click the page and select Inspect.
Select the Network tab.
Refresh the page.
2
Confirm the header value
Select a CDN-delivered JPEG, PNG, or CSS file. In the Headers tab, confirm that Access-Control-Allow-Origin is present and the value is * or the origin of the request.
For a page to read a file from another site, the response must include an Access-Control-Allow-Origin header that permits the requesting origin. The cors option stores those origins on the CDN resource, and Orange's CDN adds the header to the response the browser receives.
An origin web server can send the same header on its own response. A resource rule stores cors for one path, while the resource option applies it to every file.
Set value to ["*"]. The response header is Access-Control-Allow-Origin: *, so a browser on any site can read it.
enabled and value are both required, and a request that omits either field returns HTTP 400. With always set to false, the header is added only on status 200, 201, 204, 206, 301, 302, 303, 304, 307, and 308.
Set value to hostnames, with no scheme and no path. A resource accepts up to 20 names.
A request whose Origin matches one name receives that origin in Access-Control-Allow-Origin, and any other origin receives no header. A value that includes https://, or that mixes * or $http_origin with a hostname, returns HTTP 400.
Set value to ["$http_origin"]. Orange's CDN copies the request Origin into Access-Control-Allow-Origin.
Use $http_origin when * is unsuitable, including a request that sends credentials. A browser that sends credentials accepts the response only when it also includes Access-Control-Allow-Credentials: true.
Add Access-Control-Allow-Origin to the response the browser receives with the Terraform provider v2. When cors is enabled and value is ["*"], Orange's CDN adds Access-Control-Allow-Origin: *, so a browser on any site can read the response. Run terraform apply after editing options.
CORS header
Set enabled to true and value to ["*"]. With always set to false, the header is added only on status 200, 201, 204, 206, 301, 302, 303, 304, 307, and 308. Set always to true to add the header on every status code, including a response for a missing file.
Set value to ["site-a.com", "site-b.com"] to name the permitted hostnames, with no scheme and no path. A request whose Origin matches one name receives that origin in Access-Control-Allow-Origin, and any other origin receives no header. Set value to ["$http_origin"] to copy the request Origin into the header. Set enabled to false and value to an empty list to stop adding the header. The empty list stores no origins.