Intermittent 403 CORS Errors (Access-Control-Allow-Origin) With Cloudfront Using Signed URLs To GET S3 Objects

前端 未结 1 656
囚心锁ツ
囚心锁ツ 2021-01-05 18:30

In Brief

In order to keep the uploaded media (S3 objects) private for all the clients on my multi-tenant system I implemented a Cloudfront CDN deployment and confi

相关标签:
1条回答
  • 2021-01-05 19:05

    Found the solution for the same on serverfault.

    https://serverfault.com/questions/856904/chrome-s3-cloudfront-no-access-control-allow-origin-header-on-initial-xhr-req

    You apparently cannot successfully fetch an object from HTML and then successfully fetch it again with as a CORS request with Chrome and S3 (with or without CloudFront), due to peculiarities in the implementations.

    Adding the answer from original post so that it does not get lost.

    Workaround:

    This behavior can be worked-around with CloudFront and Lambda@Edge, using the following code as an Origin Response trigger.

    This adds Vary: Access-Control-Request-Headers, Access-Control-Request-Method, Origin to any response from S3 that has no Vary header. Otherwise, the Vary header in the response is not modified.

    'use strict';
    
    // If the response lacks a Vary: header, fix it in a CloudFront Origin Response trigger.
    
    exports.handler = (event, context, callback) => {
        const response = event.Records[0].cf.response;
        const headers = response.headers;
    
        if (!headers['vary'])
        {
            headers['vary'] = [
                { key: 'Vary', value: 'Access-Control-Request-Headers' },
                { key: 'Vary', value: 'Access-Control-Request-Method' },
                { key: 'Vary', value: 'Origin' },
            ];
        }
        callback(null, response);
    };
    
    0 讨论(0)
提交回复
热议问题