如何使用 CORS 限制 AWS S3 访问?

Bob*_*lof 3 javascript amazon-s3 amazon-web-services cors

如何在 Amazon S3 上设置 CORS 以仅允许经批准的域访问我的 S3 存储桶中的 JS 脚本?目前,我的 S3 存储桶上的 CORS 设置如下:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>www.someapproveddomain.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>
Run Code Online (Sandbox Code Playgroud)

然而,任何域都可以访问和运行脚本hello.js(驻留在 S3 存储桶中),如JSFiddle 所示。有没有人我做错了什么?或者,也许我只是误解了 CORS 应该做什么?

Mar*_*k B 5

与其尝试使用 CORS 解决此问题,您还需要使用限制对特定 HTTP 引用程序的访问的 S3 存储桶策略来解决此问题。这在此处记录

这是 AWS 提供的示例存储桶策略:

{
  "Version":"2012-10-17",
  "Id":"http referer policy example",
  "Statement":[
    {
      "Sid":"Allow get requests originating from www.example.com and example.com.",
      "Effect":"Allow",
      "Principal":"*",
      "Action":"s3:GetObject",
      "Resource":"arn:aws:s3:::examplebucket/*",
      "Condition":{
        "StringLike":{"aws:Referer":["http://www.example.com/*","http://example.com/*"]}
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

请注意,攻击者很容易欺骗 HTTP 引荐来源网址。