amazon-web-services - 基于引用者限制对 AWS S3 存储桶的访问

标签 amazon-web-services amazon-s3 amazon-cloudfront policy

我正在尝试限制对 S3 存储桶的访问,并且仅允许基于引用者的列表中的某些域。

桶策略基本上是:

{
"Version": "2012-10-17",
"Id": "http referer domain lock",
"Statement": [
    {
        "Sid": "Allow get requests originating from specific domains",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::example.com/*",
        "Condition": {
            "StringLike": {
                "aws:Referer":  [ 
                    "*othersite1.com/*",
                    "*othersite2.com/*",
                    "*othersite3.com/*"
                ]
            }
        }
    }
 ]
}

这个 othersite1、2 和 3 调用我存储在域 example.com 下的 s3 存储桶中的对象。
我还有一个附加到存储桶的云端分发。我在字符串条件之前和之后使用 * 通配符。引用者可以是 othersite1.com/folder/another-folder/page.html。引用者也可以使用 http 或 https。

我不知道为什么会收到 403 Forbidden 错误。

我这样做基本上是因为我不希望其他站点调用该对象。

任何帮助将不胜感激。

最佳答案

对于正确的缓存行为而言,CloudFront 会在将请求转发到源服务器之前从请求中剥离几乎所有的请求 header 。

Referer | CloudFront removes the header.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#request-custom-headers-behavior



因此,如果您的存储桶试图阻止基于引用页面的请求,有时会这样做以防止盗链,S3 将不会 - 默认情况下 - 能够看到 Referer header ,因为 CloudFront 不转发它。

而且,这是 的一个很好的说明。为什么 CloudFront 不会转发它。如果 CloudFront 转发 header 然后盲目缓存结果,则存储桶策略是否具有预期效果将取决于第一个请求是来自预期站点之一,还是来自其他地方 - 其他请求者将获得缓存响应,这可能是错误的 react 。

(tl;dr) 将 Referer 列入白名单转发到源的 header (在 CloudFront 缓存行为设置中)解决了这个问题。

但是,有一点问题。

现在您正在转发 Referer header 到 S3,您已经扩展了缓存键 - CloudFront 缓存响应的事物列表 - 以包括 Referer标题。

因此,现在,对于每个对象,CloudFront 将不会提供来自缓存的响应,除非传入请求的 Referer header 与已缓存请求中的一个完全匹配……否则请求必须转到 S3。而且,关于referer header 的事情,它是引用页面,而不是引用站点,因此来自授权站点的每个页面都将在 CloudFront 中拥有自己的这些 Assets 的缓存副本。

这本身不是问题。这些额外的对象副本是免费的,这就是 CloudFront 的设计工作方式……问题是,它降低了给定对象在给定边缘缓存中的可能性,因为每个对象的引用次数必然更少.如果您有大量流量,这将变得不那么重要 - 微不足道 - 如果您的流量较小,则更重要。更少的缓存命中意味着更慢的页面加载和更多的请求到 S3。

对于这是否适合您没有正确答案,因为它非常具体地针对您如何使用 CloudFront 和 S3。

但是,这是另一种选择:

您可以删除 Referer通过将 CloudFront 配置为触发 Lambda@Edge Viewer Request trigger,将 header 白名单中的 header 转发到 S3 并撤消对缓存命中产生负面影响的可能性它将检查前门中的每个请求,并阻止那些不是来自您希望允许的引用页面的请求。

查看器请求触发器在特定缓存行为匹配后触发,但在检查实际缓存之前,并且大多数传入 header 仍然完好无损。您可以允许请求继续进行,可选择进行修改,或者您可以生成响应并取消 CloudFront 处理的其余部分。这就是我在下面说明的内容——如果 Referer 的主机部分 header 不在可接受值的数组中,我们生成 403 响应;否则,请求继续,缓存被检查,并且只在需要时查询源。

触发此触发器会为每个请求增加少量开销,但与降低缓存命中率相比,该开销可能会分摊到更理想的情况下。因此,以下不是“更好”的解决方案——只是一个替代解决方案。

这是一个用 Node.js 6.10 编写的 Lambda 函数。

'use strict';

const allow_empty_referer = true;

const allowed_referers = ['example.com', 'example.net'];

exports.handler = (event, context, callback) => {

    // extract the original request, and the headers from the request
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    // find the first referer header if present, and extract its value;
    // then take http[s]://<--this-part-->/only/not/the/path.
    // the || [])[0]) || {'value' : ''} construct is optimizing away some if(){ if(){ if(){ } } } validation

    const referer_host = (((headers.referer || [])[0]) || {'value' : ''})['value'].split('/')[2];

    // compare to the list, and immediately allow the request to proceed through CloudFront 
    // if we find a match

    for(var i = allowed_referers.length; i--;)
    {
        if(referer_host == allowed_referers[i])
        {
            return callback(null,request);
        }
    }

    // also test for no referer header value if we allowed that, above
    // usually, you do want to allow this

    if(allow_empty_referer && referer_host === "")
    {
        return callback(null,request);
    }

    // we did not find a reason to allow the request, so we deny it.

    const response = {
        status: '403',
        statusDescription: 'Forbidden',
        headers: {
            'vary':          [{ key: 'Vary',          value: '*' }], // hint, but not too obvious
            'cache-control': [{ key: 'Cache-Control', value: 'max-age=60' }], // browser-caching timer
            'content-type':  [{ key: 'Content-Type',  value: 'text/plain' }], // can't return binary (yet?)
        },
        body: 'Access Denied\n',
    };

    callback(null, response);
};

关于amazon-web-services - 基于引用者限制对 AWS S3 存储桶的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46010789/

相关文章:

java - 连接 kinesis 生产者 AWS kinesis 443 时出错

ruby-on-rails - 配置 ActiveStorage 以将 S3 与 IAM 角色一起使用

php - 使用 Amazon s3 作为 PHP 应用程序的(某种)数据库是不是一个错误的决定?

amazon-web-services - AWS Cloudfront 失效未按预期工作

ios - 在 AWS 中获取请求时出错

linux - mongodb远程连接问题

java - 您如何测试针对 AWS API 编写的代码

amazon-s3 - 如何删除现有的 S3 事件通知?

amazon-s3 - 使用 CloudFront 从自定义路径解析 S3 静态页面

nginx - 修改 HTML 响应(不是 header )