来自 S3 源的 Amazon CloudFront 上的 HSTS

chr*_*vdb 13 ssl amazon-cloudfront hsts

是否可以在来自 S3 源的 Amazon CloudFront 分配上设置 HSTS 标头?

小智 13

关于这个的更新...

现在可以通过 Lambda@edge 函数自定义 HTTP 响应标头。有关文档,请参阅http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html。要尝试此操作,请在 AWS 控制台中创建一个新的 lambda 函数。为语言选择“Edge Nodge.js 4.3”并查找 cloudfront-modify-response-header 模板。如果您这样做,Lambda 将询问您要将函数应用到哪个 CloudFront 分配和事件。请注意,您可以随时通过转到 Cloudfront 行为选项卡来编辑或更改此设置。

这是一个示例 lambda 函数...

'use strict';
exports.handler = (event, context, callback) => {

    const response = event.Records[0].cf.response;
    response.headers['Strict-Transport-Security'] = 'max-age=2592000; includeSubDomains';

    callback(null, response);
};
Run Code Online (Sandbox Code Playgroud)


Jas*_*tin 8

目前不可能,请参阅https://forums.aws.amazon.com/thread.jspa?threadID=162252以了解有关它的讨论。

编辑:Lambda@Edge 使它成为可能,见下文。

  • AWS 最近在 CloudFront 上引入了标头策略,这是完成此任务的一种不太繁琐的方法。请参阅:https://aws.amazon.com/blogs/networking-and-content-delivery/amazon-cloudfront-introduces-response-headers-policies/ (2认同)

小智 5

添加到安德鲁的答案:

我刚刚尝试了这个和一些注意事项:不再有特定的边缘 nodejs 运行时,但需要在弗吉尼亚北部地区创建 lambda 并由 cloudfront origin-responseviewer-response触发。

开箱即用的代码似乎不再起作用。它给出了 ERR_CONTENT_DECODING_FAILED。

解决方法是使用json语法如下:

response.headers['Strict-Transport-Security'] = [ { key: 'Strict-Transport-Security', value: "max-age=31536000; includeSubdomains; preload" } ];
response.headers['X-Content-Type-Options']    = [ { key: 'X-Content-Type-Options', value: "nosniff" } ];
Run Code Online (Sandbox Code Playgroud)