【问题标题】:How to disable access to cloudfront via the *.cloudfront.net url?如何通过 *.cloudfront.net url 禁用对云端的访问?
【发布时间】:2022-01-13 02:09:55
【问题描述】:

我创建了一个 AOI 来将 s3 存储桶的访问权限限制为公开。 因此,您无法通过 s3 端点访问 s3 对象,但 cloudfront 可以访问所有这些对象并为它们提供服务。

我设置了备用域名并为此域添加了 SSL 证书。

我使用 A 规则设置路由 53 来别名 cloudfront 分发

我可以使用 Cloudfront 公共 URL (*.cloudfront.net) 和 mydomain.com 访问该页面

如何删除对我的页面的 *.cloudfront.net 访问权限? 这应该是可能的,因为唯一需要这个 url 的服务是路由 53。

【问题讨论】:

  • 我有同样的问题/问题。你找到解决办法了吗?
  • 我担心的是,当 Google 将同时抓取这两个域时,最终会出现重复的内容。我使用 lambda edge 动态返回一个 robots.txt,它会拒绝 *.cloudfront.net 上的所有内容,但会允许真实域上的 google bot

标签: amazon-s3 amazon-cloudfront amazon-route53


【解决方案1】:

比 Lamda@Edge 容易得多,只需配置一个 ACL 来阻止每个包含 Host 标头的请求以及您的云端分发 url。

Configure AWS WAF / ACL

【讨论】:

  • 我最初正在查看 lambda 选项,但这是一种更简单的方法。我认为可以在多个发行版之间共享一个 ACL 是否正确?
  • “请注意,您可以在同一区域内的多个 AWS 资源之间共享 Web ACL”在定价计算器下的 aws.amazon.com/waf/pricing 中给出。我相信这回答了我的问题!
【解决方案2】:

您可以使用 Lambda@Edge 查看器请求触发器。这允许您在检查缓存之前检查请求,并允许继续处理或返回生成的响应。

因此,您可以检查引用者并确保请求来自您的域。

'use strict';

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

  // extract the request object
  const request = event.Records[0].cf.request;

  // extract the HTTP `Referer` header if present
  // otherwise an empty string to simplify the matching logic
  const referer = (request.headers['referer'] || [ { value: '' } ])[0].value;

  // verify that the referring page is yours
  // replace example.com with your domain
  // add other conditions with logical or ||
  if(referer.startsWith('https://example.com/') ||
     referer.startsWith('http://example.com/'))
  {
    // return control to CloudFront and allow the request to continue normally
    return callback(null,request);
  }

  // if we get here, the referring page is not yours.
  // generate a 403 Forbidden response
  // you can customize the body, but the size is limited to ~40 KB

  return callback(null, {
    status: '403',
    body: 'Access denied.',
    headers: {
      'cache-control': [{ key: 'Cache-Control', value: 'private, no-cache, no-store, max-age=0' }],
      'content-type': [{ key: 'Content-Type', value: 'text/plain' }],
    }
  });
};

更多信息请阅读以下页面:

https://stackoverflow.com/a/51006128/6619626

Generating HTTP Responses in Request Triggers

Updating HTTP Responses in Origin-Response Triggers

最后,这篇文章有很多有价值的信息

How to Prevent Hotlinking by Using AWS WAF, Amazon CloudFront, and Referer Checking

【讨论】:

    【解决方案3】:

    另外,一个非常简单的解决方案是在查看器请求事件中为您的相关行为添加CloudFront function

    function isCloudFrontURL(headers) {
        if(headers && headers["host"]) {
            if(headers["host"].value.includes("cloudfront"))
                return true
            else if(headers["host"].multiValue)
                return headers["host"].multiValue.some(entry => entry.value.includes("cloudfront"))
        }
        return false
    }
    
    function handler(event) {
        if(isCloudFrontURL(event.request.headers))
            return {
                statusCode: 404,
                statusDescription: 'Page not found',
                headers: {
                    "content-type": { 
                        "value": "text/plain; charset=UTF-8" 
                        
                    }
                }
            }
        else
            return event.request;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2017-03-25
      • 2020-06-01
      • 1970-01-01
      • 2014-08-26
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多