Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

amazon S3 bucket policy - restricting access by referer BUT not restricting if urls are generated via query string authentication

I have the following bucket policy set on my bucket:

{

"Version": "2008-10-17",

"Id": "My access policy",

"Statement": [

     {

 "Sid": "Allow only requests from our site",

 "Effect": "Allow",

 "Principal": { "AWS": "*"},

 "Action": "s3:GetObject",

 "Resource": "arn:aws:s3:::my_bucket/*",

 "Condition": {

   "StringLike": {

      "aws:Referer": [" http://mydomain.com/*"," http://www.mydomain.com/*"]

        }

              }

 },

{

   "Sid": "Dont allow direct acces to files  when no referer is present",

   "Effect": "Deny",

   "Principal": {"AWS": "*" },

  "Action": "s3:GetObject",

  "Resource": "arn:aws:s3:::my_bucket/*",

  "Condition": {

  "Null": {"aws:Referer": true }

         }

         }

]

  }

I also configured query string authentication, but it looks like I can't have both. If I have my bucket policies set to deny any request that doesn't originate from mydomain, my temporary url using query string authentication will also not get served. So my question is, how can i have both ? Is there a way to check for url parameters and see if it has a parameter called "Signature" and in that case not apply the referer policy?

like image 688
Simon Polak Avatar asked May 04 '11 15:05

Simon Polak


People also ask

What is the recommended approach to restrict access to S3 buckets?

Restrict access to your S3 buckets or objects by doing the following: Writing IAM user policies that specify the users that can access specific buckets and objects. IAM policies provide a programmatic way to manage Amazon S3 permissions for multiple users.

Which of the below allows you to restrict access to individual objects in an S3 bucket?

Amazon S3 is the only object storage service that allows you to block public access to all of your objects at the bucket or the account level, now and in the future by using S3 Block Public Access. To ensure that public access to all your S3 buckets and objects is blocked, turn on block all public access.

How do I fix an AWS S3 bucket policy and public permissions access denied error?

If you're denied permissions, then use another IAM identity that has bucket access, and edit the bucket policy. Or, delete and recreate the bucket policy if no one has access to it. If you're trying to add a public read policy, then disable the bucket's S3 Block Public Access.

How do I restrict Amazon S3 bucket access to a specific IAM user?

You can use the NotPrincipal element of an IAM or S3 bucket policy to limit resource access to a specific set of users. This element allows you to block all users who are not defined in its value array, even if they have an Allow in their own IAM user policies.


1 Answers

Remove the space in the referrers string " http://mydomain.com/*" that's wrong... the Amazon examples made that mistake too.

For the second statement the easier way to solve it is to remove that entire statement and have your files permissions (ACLs) set to private (Owner-Read/Write and World-NoRead/NoWrite)

I am not sure, but in appears that even if you have a Deny Statement a file can still be read if it has a public permission (World Read).

Also, if you are distributing the files on CloudFront remember to allow it to read the bucket too. So a complete bucket policy will look like:

{
"Version": "2008-10-17",
"Id": "YourNetwork",
"Statement": [
    {
        "Sid": "Allow get requests to specific referrers",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::yourbucket/*",
        "Condition": {
            "StringLike": {
                "aws:Referer": [
                    "http://www.yourwebsite.com/*",
                    "http://yourwebsite.com/*"
                ]
            }
        }
    },
    {
        "Sid": "Allow CloudFront get requests",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::12345678:root"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::yourbucket/*"
    }
]
}

(change the 12345678 to your AWS account ID number without the dashes)

like image 126
André Felipe Avatar answered Sep 22 '22 00:09

André Felipe