S3 bucket policy uses wildcard principals

Limit the actions, resources, and conditions of wildcard principals in S3 bucket policies, and review Block Public Access to prevent unnecessary access.

Description

A wildcard Principal includes all principals, but a source IP or organization condition can narrow the grant. Allow only the actions and bucket or object ARNs the workload needs.

Potential impact

Relaxing conditions or Block Public Access can expose object data or listings. Grants that also allow writes or deletion can put data at risk.

Remediation

Review the policy, ACLs, explicit denies and Block Public Access. Test intended and denied requests and inspect terraform plan. If you use version 3.7.0 of terraform-aws-modules/s3-bucket/aws instead of the policy resource shown, set attach_policy = true for a custom policy.

Examples

The policies are alternatives for the same bucket. Define aws_s3_bucket.example separately and substitute the actual source IP for the documentation address.

Allow with an IP condition

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

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

The first statement allows a broad set of S3 object actions when aws:SourceIp matches 203.0.113.10/32.

Explicit deny with an IP condition

hcl
resource "aws_s3_bucket_policy" "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 second explicitly denies matching requests from that same source against this bucket’s objects, including legitimate calls. It does not deny bucket-level operations or requests from other sources.

References