编辑:这有一个similar answer here:
对于现在出现的那些,您可以使用 Lambda@Edge 添加 HSTS 标头以及其他“frame-buster”标头,例如 x-frame-options 和 referrer-policy。
这很便宜,每百万个请求大约需要 30 美分。
这个link from the AWS networking and content delivery blog 详细描述了如何做到这一点。
这里重复整个内容太长了,但本质上它描述了以下流程:
这是该过程的工作原理:
- 查看者导航到网站。
- 在 CloudFront 从缓存中提供内容之前,它将触发与该行为的查看器请求触发器关联的任何 Lambda 函数。
- CloudFront 提供缓存中的内容(如果可用),否则转到第 4 步。
- 仅在 CloudFront 缓存“未命中”之后,才会针对该行为触发源请求触发器。
- S3 Origin 返回内容。
- 在从 S3 返回内容后但在缓存到 CloudFront 之前,会触发源响应触发器。
- 在 CloudFront 中缓存内容后,会触发查看器响应触发器,这是查看器接收内容之前的最后一步。
- 查看者接收内容。
再一次,如果链接到的博客消失了,下面的代码是通过 Lambda 添加安全标头的示例(请记住,这是由 CloudFront 使用 Lambda@Edge 集成运行的):
'use strict';
exports.handler = (event, context, callback) => {
//Get contents of response
const response = event.Records[0].cf.response;
const headers = response.headers;
//Set new headers
headers['strict-transport-security'] = [{key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubdomains; preload'}];
headers['content-security-policy'] = [{key: 'Content-Security-Policy', value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'"}];
headers['x-content-type-options'] = [{key: 'X-Content-Type-Options', value: 'nosniff'}];
headers['x-frame-options'] = [{key: 'X-Frame-Options', value: 'DENY'}];
headers['x-xss-protection'] = [{key: 'X-XSS-Protection', value: '1; mode=block'}];
headers['referrer-policy'] = [{key: 'Referrer-Policy', value: 'same-origin'}];
//Return modified response
callback(null, response);
};