My AS3 Bucket Policy only applies to some Objects

流过昼夜 提交于 2021-02-17 06:22:07

问题


I'm having a really hard time setting up my bucket policy, it looks like my bucket policy only applies to some objects in my bucket.

What I want is pretty simple: I store video files in the bucket and I want them to be exclusively downloadable from my webiste.

My approach is to block everything by default, and then add allow rules:

  • Give full rights to root and Alice user.
  • Give public access to files in my bucket from only specific referers (my websites).

Note: I manually made all the objects 'public' and my settings for Block Public Access are all set to Off.

Can anyone see any obvious errors in my bucket policy? I don't understand why my policy seems to only work for some files. Thank you so much

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::MY_BUCKET/*",
            "Condition": {
                "StringNotLike": {
                    "aws:Referer": [
                        "https://mywebsite1.com/*",
                        "https://mywebsite2.com/*"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::MY_BUCKET/*",
            "Condition": {
                "StringLike": {
                    "aws:Referer": [
                        "https://mywebsite1.com/*",
                        "https://mywebsite2.com/*"
                    ]
                }
            }
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::426873019732:root",
                    "arn:aws:iam::426873019732:user/alice"
                ]
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::MY_BUCKET",
                "arn:aws:s3:::MY_BUCKET/*"
            ]
        }
    ]
}

回答1:


Controlling access via aws:Referer is not secure. It can be overcome quite easily. A simple web search will provide many tools that can accomplish this.

The more secure method would be:

  • Keep all objects in your Amazon S3 bucket private (do not "Make Public")
  • Do not use a Bucket Policy
  • Users should authenticate to your application
  • When a user wishes to access one of the videos, or when your application creates an HTML page that refers/embeds a video, the application should determine whether the user is entitled to access the object.
  • If the user is entitled to access the object, the application creates an Amazon S3 pre-signed URL, which provides time-limited access to a private object.
  • When the user's browser requests to retrieve the object via the pre-signed URL, Amazon S3 will verify the contents of the URL. If the URL is valid and the time limit has not expired, Amazon S3 will return the object (eg the video). If the time has expired, the contents will not be provided.

The pre-signed URL can be created in a couple of lines of code and does not require and API call back to Amazon S3.

The benefit of using pre-signed URLs is that your application determines who is entitled to view objects. For example, a user could choose to share a video with another user. Your application would permit the other user to view this shared video. It would not require any changes to IAM or bucket policies.

See: Amazon S3 pre-signed URLs

Also, if you wish to grant access to an Amazon S3 bucket to specific IAM Users (that is, users within your organization, rather than application users), it is better to grant access on the IAM User rather than via an Amazon S3 bucket. If there are many users, you can create an IAM Group that contains multiple IAM Users, and then put the policy on the IAM Group. Bucket Policies should generally be used for granting access to "everyone" rather than specific IAM Users.

In general, it is advisable to avoid using Deny policies since they can be difficult to write correctly and might inadvertently deny access to your Admin staff. It is better to limit what is being Allowed, rather than having to combine Allow and Deny.



来源:https://stackoverflow.com/questions/61344892/my-as3-bucket-policy-only-applies-to-some-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!