我在Amazon S3储存桶上的静态文件(js、css、jpg、gif等)设置标头"X-Content-Type-Options:nosniff"时遇到了问题。
当我尝试添加它时,它告诉我:"用户自定义元数据键必须以x-amz-meta-开头。"
应该怎么做?我需要这样做"x-amz-meta-X-Content-Type-Options"吗?
提前致谢!
我在Amazon S3储存桶上的静态文件(js、css、jpg、gif等)设置标头"X-Content-Type-Options:nosniff"时遇到了问题。
当我尝试添加它时,它告诉我:"用户自定义元数据键必须以x-amz-meta-开头。"
应该怎么做?我需要这样做"x-amz-meta-X-Content-Type-Options"吗?
提前致谢!
x-amz-meta-*
开头,但这并不能帮助你 - 当获取对象时,它们也会作为x-amz-meta-*
头部返回,并且浏览器不会识别x-amz-meta-X-Content-Type-Options
。x-amz-meta-*
开头的标头支持非常有限。 Content-Type
、Content-Disposition
和Content-Encoding
是有效的,但大多数其他标头都无效。X-Robots-Tag
,S3确实接受并将其与响应一起返回,尽管AWS控制台如果您使用API添加它,则无法编辑它。'use strict';
exports.handler = (event, context, callback) => {
const response = event.Records[0].cf.response;
const headers = response.headers;
headers['X-Content-Type-Options'] = ['nosniff'];
callback(null, response);
};
...给出以下响应...
$ curl -v http://dxxxexample.cloudfront.net/robots.txt
* Hostname was NOT found in DNS cache
* Trying x.x.x.x...
* Connected to dxxxexample.cloudfront.net (x.x.x.x) port 80 (#0)
> GET /robots.txt HTTP/1.1
> User-Agent: curl/7.35.0
> Host: dxxxexample.cloudfront.net
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Content-Length: 324
< Connection: keep-alive
< Date: Tue, 10 Jan 2017 20:38:33 GMT
< Last-Modified: Tue, 10 Jan 2017 17:13:36 GMT
< ETag: "dbe2f9a267e8ef192f0fdf0c888da01c"
< Cache-Control: no-cache
< Accept-Ranges: bytes
* Server AmazonS3 is not blacklisted
< Server: AmazonS3
< Via: 1.1 xxxxxxxxxx.cloudfront.net (CloudFront)
< X-Content-Type-Options: nosniff
< X-Cache: Miss from cloudfront
< X-Amz-Cf-Id: xxxxx
<
User-agent: *
Disallow: /
...所以这似乎是一种可行的解决方法。
我将此函数配置为在“Viewer Response”上触发(触发器在响应从CloudFront返回到浏览器之前触发,允许修改响应),但实际上它可能可以触发“Origin Response”而不是“Viewer Response”,这样需要运行的频率更少(假设您没有像上面的示例那样使用Cache-Control: no-cache
,就像我在测试中所做的那样。 我只是使用了/robots.txt
,因为我已经将其与CloudFront和Lambda一起设置在一个存储桶中 - 显然,这个文件不是X-Content-Type-Options
的特别有趣的应用程序,但正如您所看到的,这确实有效)。
我不知道Lambda@Edge何时会从预览版发布。
如果您想将其作为S3本身的功能请求提交,请联系您的AWS账户代表(如果您有一个),或在AWS支持论坛中发布。 (我与AWS无关)。