Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A web font served through CloudFront is blocked by CORS

ErrorFont from origin has been blocked from loading by Cross-Origin Resource Sharing policy

Short 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.

  • Applies to Amazon CloudFront, Amazon S3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.woff2
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The bucket answers with CORS headers when called directly, but the CloudFront URL does notCloudFront does not forward the Origin header to S3Fix
    3. 3You 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. 4CORS 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

What does the error look like?

  • Font from origin has been blocked from loading by Cross-Origin Resource Sharing policy
  • Reason: 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-control

2Fix

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.woff2

Confirmed by docs.aws.amazon.com

Did this fix it?

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 step

2Fix

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-Method

3Verify

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-control

Confirmed by docs.aws.amazon.com

Did this fix it?

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 field

2Fix

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-control

Confirmed by docs.aws.amazon.com

Did this fix it?

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-control

2Fix

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.com

3Verify

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-control

Confirmed by docs.aws.amazon.com

Did this fix it?

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

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 fix

Web 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 fix

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.