首页 文章

如何通过* .cloudfront.net网址禁用对cloudfront的访问?

提问于
浏览
6

我创建了一个AOI来限制s3存储桶对公众的访问 . 因此,您无法通过s3 endpoints 访问s3对象,但cloudfront可以访问所有这些对象并为其提供服务 .

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

我使用A规则设置路由53以对 Cloud 端分布进行别名

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

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

1 回答

  • 0

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

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

    '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

    最后,本文有很多有 Value 的信息

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

相关问题