A web font served through CloudFront is blocked by CORS
Font from origin has been blocked from loading by Cross-Origin Resource Sharing policyShort answer
Browsers load web fonts with a CORS request, so the font response must carry Access-Control-Allow-Origin. With CloudFront in front of S3, the usual causes are: the bucket has no CORS rules, CloudFront does not forward the Origin header, or a cached copy has no CORS header. A response headers policy can also add the header at the edge.
Fix it fast
1Find out which cause you have
Request the font from the CloudFront domain with an Origin header, the way a browser does, and look at the response headers. If Access-Control-Allow-Origin is absent, work through the causes below.
curl -s -D - -o /dev/null \ -H "Origin: https://www.example.com" \ https://d111111abcdef8.cloudfront.net/fonts/site.woff22Match what you see, then jump to its fix
- 1
The curl above shows no Access-Control-Allow-Origin, and the same request straight to the bucket shows none eitherThe S3 bucket has no CORS configurationFix - 2
The bucket answers with CORS headers when called directly, but the CloudFront URL does notCloudFront does not forward the Origin header to S3Fix - 3
You cannot or do not want to change the bucket, or the origin is not S3A response headers policy is missing, so CloudFront adds nothing itselfFix - 4
CORS headers appear for some pages of the site but not for the https version, the www host or a portThe allowed origin in the policy does not match the page's originFix
- 1
What does the error look like?
Font from origin has been blocked from loading by Cross-Origin Resource Sharing policyReason: CORS header 'Access-Control-Allow-Origin' missing
Cause 1 of 4
The S3 bucket has no CORS configuration
1Check
Open the bucket in the S3 console, go to Permissions and read the Cross-origin resource sharing (CORS) section. An empty section means S3 never adds CORS headers.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" \
https://my-font-bucket.s3.amazonaws.com/fonts/site.woff2 | grep -i access-control2Fix
Add a CORS rule that allows GET from your site's origin. In the S3 console the configuration must be JSON. List the origin with its scheme, for example https://www.example.com.
[
{
"AllowedHeaders": ["*"],
"AllowedMethods": ["GET", "HEAD"],
"AllowedOrigins": ["https://www.example.com"],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
}
]3Verify
Request the object directly from the bucket endpoint with the Origin header and look for the allow-origin header.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" \
https://my-font-bucket.s3.amazonaws.com/fonts/site.woff2Confirmed by docs.aws.amazon.com
Cause 2 of 4
CloudFront does not forward the Origin header to S3
1Check
S3 only decides what to send based on the Origin header. If the cache behavior's cache policy does not include Origin, CloudFront does not pass it on, and S3 returns the plain object.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" \
https://my-font-bucket.s3.amazonaws.com/fonts/site.woff2 | grep -i access-control
# then compare with the CloudFront URL from the first step2Fix
Use a cache policy for that behavior that forwards the Origin header (and Access-Control-Request-Headers and Access-Control-Request-Method if you want OPTIONS responses cached). Do not forward extra headers you do not need, because it lowers the cache hit ratio.
# In the cache behavior, choose a cache policy whose cache key includes
# these request headers:
# Origin
# Access-Control-Request-Headers
# Access-Control-Request-Method3Verify
Repeat the first-step request. The header should now be present on the CloudFront response.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" \
https://d111111abcdef8.cloudfront.net/fonts/site.woff2 | grep -i access-controlConfirmed by docs.aws.amazon.com
Cause 3 of 4
A response headers policy is missing, so CloudFront adds nothing itself
1Check
Look at the response headers policy attached to the cache behavior. With none attached, CloudFront only returns what the origin sent.
# CloudFront console: open the distribution, Behaviors, Edit, and read the Response headers policy field2Fix
Create a response headers policy with a CORS section that lists your origin, then attach it to the cache behavior. Turn on Origin override only if CloudFront should replace a CORS header the origin already sends.
# Response headers policy, CORS section:
# Access-Control-Allow-Origin = https://www.example.com
# Access-Control-Allow-Methods = GET, HEAD
# Then attach the policy to the cache behavior.3Verify
Request the font again with the Origin header and look for the allow-origin header.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" \
https://d111111abcdef8.cloudfront.net/fonts/site.woff2 | grep -i access-controlConfirmed by docs.aws.amazon.com
Cause 4 of 4
The allowed origin in the policy does not match the page's origin
1Check
Compare the origin shown in the browser's console message with the value in the policy. A value with http:// does not match https://, and a value with no port does not match a custom port.
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://d111111abcdef8.cloudfront.net/fonts/site.woff2 | grep -i access-control2Fix
List each origin that really loads the font. In a response headers policy, a bare host such as example.org matches both http and https on the default port, while http://example.org matches only http. A wildcard is allowed only as the leftmost subdomain.
# Allowed origins in the policy, one per origin:
https://example.com
https://www.example.com3Verify
Load the site in a private window and check the font request in the Network tab for the allow-origin header.
curl -s -D - -o /dev/null -H "Origin: https://www.example.com" https://d111111abcdef8.cloudfront.net/fonts/site.woff2 | grep -i access-controlConfirmed by docs.aws.amazon.com
How do I stop it coming back?
- Put the font bucket's CORS rules in your infrastructure code so a new bucket is never created without them.
- Add a curl check with an Origin header to your deploy checklist for every domain that serves fonts.
- Forward only the headers CORS needs, so the cache keeps a high hit ratio.
- List every real origin (https, www and apex) instead of guessing with wildcards.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Apollo Client is blocked by CORS when the GraphQL server runs on AWS Lambda
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apollo Server does not set CORS for you when it runs behind a framework or integration, and on Lambda that layer is API Gateway.
5 causes, each with a fixWeb apps
A POST to a Google Apps Script web app returns 405 or a CORS error
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.
3 causes, each with a fixWeb apps
Axios sends no cookie to an Express API even with withCredentials set to true
The login response has a Set-Cookie header, but the browser stores nothing
withCredentials only tells the browser to attach cookies.
4 causes, each with a fixWeb apps
A Chrome extension content script cannot read another site's response
Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy
Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.
3 causes, each with a fixWeb apps
The cors-anywhere.herokuapp.com demo proxy stopped working
Missing required request header. Must specify one of: origin,x-requested-with
The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.
2 causes, each with a fixWeb apps
Browser blocks a JSON request: Content-Type is not allowed by Access-Control-Allow-Headers
Reason: missing token 'content-type' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel
Content-Type: application/json is not on the browser's no-preflight list, so the browser sends an OPTIONS request first and asks for content-type.
3 causes, each with a fix