Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 CORS headers only show during OPTIONS (preflight) and not during GET request

I have an S3 bucket with the following CORS config.

<?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>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

The preflight check works as expected.

★ ~$ curl -i -X OPTIONS -H "Origin: http://stackoverflow.com" -H "Access-Control-Request-Method: GET" https://s3.amazonaws.com/random-stuff-ohyea/coderot.gif
HTTP/1.1 200 OK
x-amz-id-2: H6tzMUCJtYgiCRrhj5DucMhjjYtj1kKWqL7u2yaRGEorOeKhu/sTKlgGqY7uHxQC
x-amz-request-id: E784C4373565CBE6
Date: Mon, 21 Oct 2013 22:14:18 GMT
Access-Control-Allow-Origin: http://stackoverflow.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
Content-Length: 0
Server: AmazonS3

However the origin header on a GET request doesn't.

★ ~$ curl -iI -H "Origin: http://stackoverflow.com" https://s3.amazonaws.com/random-stuff-ohyea/coderot.gif
HTTP/1.1 200 OK
x-amz-id-2: KlrSviRSwq/40zPwOGp2/lJZk0J2Fyu7kOg966osOvQ2mpbpiv5BLkihGSOfoLd8
x-amz-request-id: 9D051B0001F48AB7
Date: Mon, 21 Oct 2013 22:11:57 GMT
Last-Modified: Mon, 21 Oct 2013 22:10:53 GMT
ETag: "4fa16333380378e116479646b40dd1ee"
Accept-Ranges: bytes
Content-Type: image/gif
Content-Length: 1774246
Server: AmazonS3

This matters because firefox doesn't seem to do preflight checks when loading remote fonts that I have in my s3 bucket. It only seems to send the origin header.

like image 421
reconbot Avatar asked Oct 21 '13 22:10

reconbot


People also ask

How do I fix a CORS issue in AWS?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do you fix CORS missing Allow origin?

If the server is under your control, add the origin of the requesting site to the set of domains permitted access by adding it to the Access-Control-Allow-Origin header's value. You can also configure a site to allow any site to access it by using the * wildcard. You should only use this for public APIs.

How do I enable cross-origin requests in S3 bucket?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a bucket policy for. Choose Permissions. In the Cross-origin resource sharing (CORS) section, choose Edit.


1 Answers

With your curl -iI option you will be doing a HEAD request and not a GET request. So you will not get the CORS headers. To simplify testing add <AllowedMethod>HEAD</AllowedMethod> to your CORS configuration and you'll get the expected results.

like image 83
Albert Bertilsson Avatar answered Sep 20 '22 06:09

Albert Bertilsson