S3 bucket policy combines wildcard principals with put actions

Granting S3 Put actions to wildcard principals can allow unnecessary uploads or configuration changes. Limit each operation to the roles and resources that need it and check effective access controls.

Description

A bucket policy can grant s3:PutObject to callers outside the intended upload roles. Other Put actions can change settings such as ACLs. Check the action, resource ARN, conditions, explicit denies and Block Public Access.

Potential impact

Unwanted uploads can increase costs or change the content an application reads at an existing key. With versioning enabled, an upload to the same key becomes the current version. Administrative grants can also change access controls.

Remediation

  • Give upload roles only the required object paths and actions. Reserve ACL and bucket-policy changes for administrators. Grant public read access only to the objects meant for public use.
  • Review ACLs and the effective policy. If you use S3 module 3.7.0 to attach a custom policy, set attach_policy = true. Test approved and unapproved uploads, and verify versioning and recovery.

Examples

These are alternative policies for a separately defined aws_s3_bucket.example. Use one policy for the bucket and replace the documentation address 203.0.113.10/32 with the actual source range.

Allow object writes with an IP condition

hcl
resource "aws_s3_bucket_policy" "put_open_policy" {
  bucket = aws_s3_bucket.example.id

  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Id": "MYBUCKETPOLICY",
  "Statement": [
    {
      "Sid": "IPAllow",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:PutObject",
      "Resource": "${aws_s3_bucket.example.arn}/*",
      "Condition": {
         "IpAddress": {"aws:SourceIp": "203.0.113.10/32"}
      }
    }
  ]
}
POLICY
}

The condition limits uploads by source IP. Any principal using that address can match the grant.

Explicit deny with an IP condition

hcl
resource "aws_s3_bucket_policy" "put_restricted_policy" {
  bucket = aws_s3_bucket.example.id

  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Id": "MYBUCKETPOLICY",
  "Statement": [
    {
      "Sid": "IPDeny",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": "${aws_s3_bucket.example.arn}/*",
      "Condition": {
         "IpAddress": {"aws:SourceIp": "203.0.113.10/32"}
      }
    }
  ]
}
POLICY
}

The deny covers object operations from that same address, including legitimate uploads. Requests from other addresses fall outside this statement. Restrict the upload grant to the intended roles.

References