142

There has been a long standing issue with Firefox not loading font from different origin than the current webpage. Usually, the issue arise when the fonts are served on CDNs.

Various solutions has been raised in other questions:

CSS @font-face not working with Firefox, but working with Chrome and IE

With the introduction of Amazon S3 CORS, is there a solution using CORS to address the font loading issue in Firefox?

edit: It would be great to see a sample of the S3 CORS configuration.

edit2: I have found a working solution without actually understanding what it did. If anyone could provide more detailed explanations about the configs and the background magic that happens on Amazon's interpretation of the config, it will be greatly appreciated, as with nzifnab who put up a bounty for it.

Community
  • 1
  • 1
VKen
  • 4,964
  • 4
  • 31
  • 43

13 Answers13

152

Update September 10, 2014:

You shouldn't need to do any of the query string hacks below anymore since Cloudfront properly supports CORS now. See http://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/ and this answer for more info: https://stackoverflow.com/a/25305915/308315


OK, I finally got the fonts working using the config below with a little tweak from examples in the documentation.

My fonts are hosted on S3, but fronted by cloudfront.

I'm not sure why it works, my guess is probably that the <AllowedMethod> GET and <AllowedHeader> Content-* is needed.

If anyone proficient with Amazon S3 CORS config can shed some lights on this, it'll be greatly appreciated.

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>https://mydomain.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Content-*</AllowedHeader>
        <AllowedHeader>Host</AllowedHeader>
    </CORSRule>
    <CORSRule>
        <AllowedOrigin>https://*.mydomain.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Content-*</AllowedHeader>
        <AllowedHeader>Host</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

edit:

Some developers are facing issues of Cloudfront caching the Access-Control-Allow-Origin header. This issue has been addressed by the AWS staff in the link (https://forums.aws.amazon.com/thread.jspa?threadID=114646) below, commented by @Jeff-Atwood.

From the linked thread, it is advised, as a workaround, to use a Query String for differentiating between calls from different domains. I'll reproduce the shortened example here.

Using curl to check response headers:

Domain A: a.domain.com

curl -i -H "Origin: https://a.domain.com" http://hashhashhash.cloudfront.net/font.woff?https_a.domain.com

Response headers from Domain A:

Access-Control-Allow-Origin: https://a.domain.com
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
X-Cache: Miss from Cloudfront

Domain B: b.domain.com

curl -i -H "Origin: http://b.domain.com" http://hashhashhash.cloudfront.net/font.woff?http_b.domain.com

Response headers from Domain B:

Access-Control-Allow-Origin: http://b.domain.com
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
X-Cache: Miss from Cloudfront

You will notice the Access-Control-Allow-Origin has returned different values, which got past the Cloudfront caching.

Community
  • 1
  • 1
VKen
  • 4,964
  • 4
  • 31
  • 43
  • 2
    have you experienced issues similar to what's described [here](https://forums.aws.amazon.com/thread.jspa?threadID=103402) - the `Access-Control-Allow-Origin` header gets cached and invalidate CORS when a subsequent request is made through a different subdomain? – Oleg Sep 29 '12 at 01:18
  • 1
    @o.v. I do not experience the issue as I explicitly set the domains that uses the resources. I've read the link you posted before. I vaguely remembered some replies on another thread saying that domains have to be explicitly stated, so * is not actually allowed, due to some restrictions. I can't find those reply posts now, it could be blog post I read elsewhere. Hope that helps. – VKen Oct 01 '12 at 17:26
  • 3
    You can have multiple AllowedOrigin elements inside a single CORSRule elements, so you could combine those CORSRules into a single element, since the other elements in them are identical. – Ben Hull Dec 06 '12 at 22:17
  • 1
    The OP states that Cloudfront is being used in front of S3, did you not have a problem with Cloudfront caching the Access-Control-Allow-Origin header and thus a response from one domain breaks the other? – Dan Jan 24 '13 at 23:37
  • 4
    @dan if the S3 bucket is served by CloudFront, it looks like the answer is to **vary the font querystring by domain** as documented in this official Amazon answer: https://forums.aws.amazon.com/thread.jspa?threadID=114646 – Jeff Atwood Jan 26 '13 at 07:54
  • 2
    This has been an extremely frustrating issue. The good news is that S3 now appears to be doing the right thing, so at least it is possible to serve everything other than webfonts through CloudFront and serve the font files directly from S3. Sadly, the querystring hack isn't really practical in our application without more significant refactoring, as the assets are all served through the Rails asset pipeline, and there's no convenient way to tweak the asset URLs at request time (they are all generated during deployment when the assets are precompiled). The font's URL in css is already up on S3. – Zach Lipton Apr 30 '13 at 00:13
  • @ZachLipton I share your pain. I have problems with IE9 caching CSS file to use with both the `WWW` subdomain, and the base domain without the the `www`. This causes the frustrating font loading problem all over again. – VKen May 02 '13 at 21:05
  • 1
    @VKen Thanks for this! In case anyone else can't figure out why this doesn't appear to be working, I had to invalidate objects in CloudFront otherwise the cached versions were used: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html#invalidating-objects-console – Simon Jul 01 '13 at 09:02
  • 1
    The querystring solution may help, but it isn't a real solution. The only real solution will be if Cloudfront supports the `Vary: Origin` header. (Currently it only respects `Vary: Accept-Encoding`) – philfreo Aug 02 '13 at 00:17
  • 1
    I was able to get this working by explicitly setting the headers. When we used a wildcard, the headers were not included even after invalidating the files on cloudfront. I did not need to use the querystring option. – Jim Wrubel Oct 10 '13 at 23:29
  • Hi @VKen , I am having some what same issue http://stackoverflow.com/q/22375472/1877909 but it is not working for me. I too added aws cors configuration – Hitesh Mar 13 '14 at 10:50
  • 1
    I believe you meant to use `curl -I` ? – Noz Jun 18 '14 at 21:15
  • 1
    Important note to the above: You shouldn't need to do any of the query string hacks anymore since Cloudfront properly supports CORS now. See http://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/ and this answer for more info: http://stackoverflow.com/a/25305915/308315 – iwasrobbed Sep 10 '14 at 12:34
104

After some tweaking I seem to have got this to work without the query string hack. More info here: http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorS3Origin.html#RequestS3-cors

I'm going to go through my entire setup so that it's easy to see what I've done, hopefully this helps others.

Background Information: I'm using a Rails app that has the asset_sync gem to put assets onto S3. This includes fonts.

Within S3 console, I clicked on my bucket, properties and 'edit cors configuration', here: CORS config button

Inside the textarea I have something like:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>https://*.example.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Then within Cloudfront panel (https://console.aws.amazon.com/cloudfront/home) I created a distribution, added an Origin that pointed to my S3 bucket adding an origin

Then added a behavior for a default path to point to the S3 based origin I setup. What I also did was click on Whitelist headers and added Origin: adding a behavior and whitelist headers

What happens now is the following, which I believe is right:

1) Check that S3 headers are being set correctly

curl -i -H "Origin: https://example.com" https://s3.amazonaws.com/xxxxxxxxx/assets/fonts/my-cool-font.ttf
HTTP/1.1 200 OK
x-amz-id-2: Ay63Qb5uR98ag47SRJ91+YALtc4onRu1JUJgMTU98Es/pzQ3ckmuWhzzbTgDTCt+
x-amz-request-id: F1FFE275C0FBE500
Date: Thu, 14 Aug 2014 09:39:40 GMT
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Cache-Control: public, must-revalidate, proxy-revalidate, max-age=180
Last-Modified: Mon, 09 Dec 2013 14:29:04 GMT
ETag: "98918ee7f339c7534c34b9f5a448c3e2"
Accept-Ranges: bytes
Content-Type: application/x-font-ttf
Content-Length: 12156
Server: AmazonS3

2) Check Cloudfront works with the headers

curl -i -H "Origin: https://example.com" https://xxxxx.cloudfront.net/assets/fonts/my-cool-font.ttf
HTTP/1.1 200 OK
Content-Type: application/x-font-ttf
Content-Length: 12156
Connection: keep-alive
Date: Thu, 14 Aug 2014 09:35:26 GMT
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
Cache-Control: public, must-revalidate, proxy-revalidate, max-age=180
Last-Modified: Mon, 09 Dec 2013 14:29:04 GMT
ETag: "98918ee7f339c7534c34b9f5a448c3e2"
Accept-Ranges: bytes
Server: AmazonS3
Vary: Origin
X-Cache: Miss from cloudfront
Via: 1.1 77bdacfea247b6cbe84dffa61da5a554.cloudfront.net (CloudFront)
X-Amz-Cf-Id: cmCxaUcFf3bT48zpPw0Q-vDDza0nZoWm9-_3qY5pJBhj64iTpkgMlg==

(Note the above was a miss from cloudfront because these files are cached for 180 seconds, but the same was working on hits)

3) Hit cloudfront with a different origin (but one that is allowed on CORS for the S3 bucket) - the Access-Control-Allow-Origin is not cached! yay!

curl -i -H "Origin: https://www2.example.com" https://xxxxx.cloudfront.net/assets/fonts/my-cool-font.ttf
HTTP/1.1 200 OK
Content-Type: application/x-font-ttf
Content-Length: 12156
Connection: keep-alive
Date: Thu, 14 Aug 2014 10:02:33 GMT
Access-Control-Allow-Origin: https://www2.example.com
Access-Control-Allow-Methods: GET
Access-Control-Max-Age: 3000
Access-Control-Allow-Credentials: true
Cache-Control: public, must-revalidate, proxy-revalidate, max-age=180
Last-Modified: Mon, 09 Dec 2013 14:29:04 GMT
ETag: "98918ee7f339c7534c34b9f5a448c3e2"
Accept-Ranges: bytes
Server: AmazonS3
Vary: Origin
X-Cache: Miss from cloudfront
Via: 1.1 ba7014bad8e9bf2ed075d09443dcc4f1.cloudfront.net (CloudFront)
X-Amz-Cf-Id: vy-UccJ094cjdbdT0tcKuil22XYwWdIECdBZ_5hqoTjr0tNH80NQPg==

Note above that the domain has successfully changed without a query string hack.

When I change the Origin header, there seems to always be a X-Cache: Miss from cloudfront on the first request then afterwards I get the expected X-Cache: Hit from cloudfront

P.S. It is worth noting that when doing curl -I (capital I) will NOT show the Access-Control-Allow-Origin headers as it only a HEAD, I do -i to make it a GET and scroll up.

Samuel Bolduc
  • 18,163
  • 7
  • 34
  • 55
Eamonn Gahan
  • 1,938
  • 1
  • 15
  • 12
  • 1
    Worked when all the others didn't. Thanks for taking the time to post in such detail! – iwasrobbed Sep 10 '14 at 12:39
  • It works!! FYI - I had a huge http response text when testing this... gonna edit the answer to use this curl solution ... http://stackoverflow.com/questions/10060098/getting-only-response-header-from-http-post-using-curl – Michael Gorham Sep 10 '14 at 14:13
  • Cool thanks guys - glad to see it's working for others. – Eamonn Gahan Sep 10 '14 at 16:25
  • I can't tell you how much you have helped us! +1 – nothing-special-here Jan 23 '15 at 20:46
  • 1
    +1 for adding the customer header `Origin` from the viewers so that Cloudfront caches the object based on that header (and forward the server CORS headers back to the user) – Sébastien Saunier Feb 17 '15 at 14:39
  • thanks it worked for me. @EamonnGahan I have a doubt why is it working fine for images and css files i.e other static content. – kishore Mar 02 '15 at 08:39
  • I recommend using Advanced Rest Client Chrome Extension ( https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US ) in order to see what is going on with the headers, it does a great job of separating content from headers in the output so you dont have to scroll as much as you do in terminal – Tamik Soziev May 08 '15 at 21:41
  • As simple as that! Just white list the 'Origin' header. Thanks Eamonn. – webStuff Sep 07 '15 at 08:38
  • Thanks so much for the comment about using -i vs -I when testing with curl. I couldn't figure out what was wrong with my CORS config, and the answer was nothing, my curl request just wasn't showing the header. – kielni Oct 28 '15 at 16:36
  • AWS just adds a similar CORS configuration in the popup by default. I lost few minutes before realizing that it's kind of a **placeholder**, you should save the configuration before it's taken in account for real! – keepthepeach Sep 02 '16 at 11:06
  • This doesn't work. I added that CORS configuration to my existing bucket, and Chrome still blocks all requests to Cloudfront. – Cerin Jun 29 '17 at 21:24
13

My fonts were served correctly until the last push to Heroku... I don't know why, but the wildcard in the CORS allowed origin stopped working. I added all of my prepro and pro domains to the CORS policy in the bucket setting so now it looks like this:

<CORSConfiguration>
    <CORSRule>
        <AllowedOrigin>http://prepro.examle.com</AllowedOrigin>
        <AllowedOrigin>https://prepro.examle.com</AllowedOrigin>
        <AllowedOrigin>http://examle.com</AllowedOrigin>
        <AllowedOrigin>https://examle.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>

</CORSConfiguration>

UPDATE: add your http://localhost:PORT too

luigi7up
  • 5,779
  • 2
  • 48
  • 58
9

In Amazon S3 CORS configuration (S3 Bucket / Permissions / CORS) if you use this:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>

CORS works well for Javascript and CSS files, but It does not work for Font files.

You have to specify the domain to allow CORS using the pattern expressed in the @VKen answer: https://stackoverflow.com/a/25305915/618464

So, use this:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
    <AllowedOrigin>https://*.mydomain.com</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Remember to replace "mydomain.com" for your domain.

After this, invalidate the CloudFront cache (CloudFront / Invalidations / Create Invalidation) and It will work.

educoutinho
  • 869
  • 9
  • 16
8

Well, the documentation states that you can stick the configuration as "the cors subresource in your bucket." I took this to mean I would create a file called "cors" at the root of my bucket with the configuration, but this would not work. In the end I had to login to the Amazon S3 administration area and add the configuration within the properties dialog of my bucket.

S3 could use some better documentation...

nzifnab
  • 15,876
  • 3
  • 50
  • 65
  • 1
    Yep, but I was lucky to spot some new interface changes at the properties panel. I've been editing bucket policies, so naturally I hunt for CORS configuration in the same panel. – VKen Oct 01 '12 at 17:14
  • worked for me, i was looking to set this in my application, who knew it would be so simple – Richlewis May 31 '13 at 11:47
6

In my case, I hadn't defined XML namespace and version in CORS configuration. Defining those worked.

Changed

<CORSConfiguration>

to

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
Gaurav Toshniwal
  • 3,552
  • 2
  • 24
  • 23
5

There is a better and easier way!

I personally prefer using my DNS subdomains to solve this problem. If my CDN is behind cdn.myawesomeapp.com instead of sdf73n7ssa.cloudfront.net then browsers are not going to freakout and block them as cross domain security problems.

To point your subdomain to your AWS Cloudfront domain go to AWS Cloudfront control panel, select your Cloudfront distribution and enter your CDN subdomain into the Alternate Domain Names (CNAMEs) field. Something like cdn.myawesomeapp.com will do.

Now you can go to your DNS provider (like AWS Route 53) and create a CNAME for cdn.myawesomeapp.com pointing to sdf73n7ssa.cloudfront.net.

http://blog.cloud66.com/cross-origin-resource-sharing-cors-blocked-for-cloudfront-in-rails/

msroot
  • 817
  • 11
  • 13
  • This breaks SSL or rather it costs a lot of money to do with SSL therefore a lot of people don't do this. – maletor Feb 24 '16 at 22:28
4

This configuration worked for me. I can list object, retrieve, update and delete.

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
    <ExposeHeader>ETag</ExposeHeader>
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader>
  </CORSRule>
</CORSConfiguration>
Shahid
  • 87
  • 2
  • 4
  • you need to change domain, as i was testing from localhost, Just look at this page for CORS: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html – Shahid Aug 02 '15 at 15:37
3

2021 Solution, without risking security by allowing "*" in AllowedDomains.

Step 1) Allow S3 to take in CORS

In S3 bucket > Permissions > Cross-origin resource sharing (CORS), add the list of your domain/domains in AllowedOrigins. See the official doc for examples. You only need GET for AllowedMethods.

Step 2) Tell CloudFront to send over the CORS headers

In your CloudFront Behavior < Origin Request Policy, make sure you select a policy that sends over origin and access-control-request-headers headers, e.g. Managed-CORS-S3Origin.

enter image description here

Step 3) [Optional, only if you have more than one domain]

See this answer of mine on how to handle multiple domains in CORS for S3+CloudFront.

Step 4) Invalidate Your CloudFront Distribution

Good luck!

Aidin
  • 25,146
  • 8
  • 76
  • 67
1
<ifModule mod_headers.c>

   Header set Access-Control-Allow-Origin: http://domainurl.com

</ifModule>

Simple Solution

bicycle
  • 8,315
  • 9
  • 52
  • 72
O-mkar
  • 5,430
  • 8
  • 37
  • 61
  • Thanks for sharing! Gave me the idea to just add this header as **'meta-data'** while **uploading** static assets to cloud storage. _(Though that way it will work with only 1 `particular domain` or `all domains`)_ – Vinay Vissh Apr 10 '18 at 09:39
1

This is not related to fonts but to images, it might be an edge case, but as it happened to me, it might happen to another one. I'll leave this here hoping it will help someone:

If you are in the scenario "I have done everything they told, but it still won't work" probabily it's a cache related problem in Chrome and Safari. Let's suppose your server has a proper CORS configuration set:

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
    </CORSRule>
</CORSConfiguration>

and in Firefox everything works fine, but in Chrome and Safari it doesn't. If you are accessing to your remote image path from both a simple <img src="http://my.remote.server.com/images/cat.png"> tag and from a js Image element src, like in the following way:

var myImg = new Image()
myImg.crossOrigin = 'Anonymous'
myImg.onload = () => {
  // do stuff (maybe draw the downloaded img on a canvas)
}
myImg.src = 'http://my.remote.server.com/images/cat.png'

You might obtain the No 'Access-Control-Allow-Origin' error in Chrome and Safari. This happen because the first <img> somehow messes up the browser cache, and when you are trying to access the same image later (on the in-code Image element), it simply break. To avoid this, you can add a fictitious GET param to one .src path, in order to force the browser to re-request the image and avoid to use cache, like this:

<img src="http://my.remote.server.com/images/cat.png?nocache=true"></img>
Nicola Elia
  • 190
  • 9
0

Restarting my spring boot application (server) solved the problem for me.

I had configured CORS correctly on S3. The curl was giving the correct response with origin header. Safari was fetching the font correctly. It was only the chrome who was not willing to accept the CORS.

Not sure what exactly caused the behaviour. Must be something to do with If-modified-since

Sujit Kamthe
  • 811
  • 11
  • 14
-1

Yes, of course. Firefox supports CORS for fonts, just like the spec requires at http://dev.w3.org/csswg/css3-fonts/#allowing-cross-origin-font-loading

Boris Zbarsky
  • 34,758
  • 5
  • 52
  • 55
  • Thanks for your prompt response, Boris Zbarsky. Would you be able to show some example configurations for the S3 CORS settings? – VKen Sep 03 '12 at 18:18
  • I've never looked into configuring S3... As far as what to send on the HTTP level, if you're OK with it just sending "Access-Control-Allow-Origin: *" in the HTTP response for the font files should work. – Boris Zbarsky Sep 03 '12 at 20:46
  • Thanks, I'm trying to find out exactly how to do that setting with the S3 CORS configurations. – VKen Sep 06 '12 at 15:41