S3 bucket policy uses wildcard actions and principals

Wildcard actions and principals in an S3 policy can grant more access than needed. Check the actual actions, resources, and conditions, then apply least privilege.

Description

Check wildcards in both Action and Principal. s3:* matches every S3 action and s3:Get* matches a subset. An object ARN covers object-level operations.

Potential impact

Unneeded reads can disclose contents. Writes and deletions can change published content or cause data loss and outages.

Remediation

Limit the principals, actions and ARNs. Check account and bucket Block Public Access, conditions and explicit denies. Even where reads are public, restrict and test writes and administration separately. S3 module 3.7.0 requires attach_policy = true for a custom policy.

Legacy policy examples

For a new bucket, use an available unique name in both the bucket configuration and the policy’s object ARN. For an existing bucket, keep its name and Terraform resource address when editing the policy. The excerpts give the same bucket different resource addresses. Check Object Ownership and Block Public Access. AWS provider 4.2.0 treats policy and acl as read-only on this resource, so define the policy as a separate resource with that version.

Allow all object actions

hcl
resource "aws_s3_bucket" "open_bucket" {
  bucket = "example-s3b-181355"
  acl    = "private"

  policy = <<EOF
  {
    "Id": "id113",
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": [
          "s3:*"
        ],
        "Effect": "Allow",
        "Resource": "arn:aws:s3:::example-s3b-181355/*",
        "Principal": "*"
      }
    ]
  }
  EOF
}

The first statement allows every S3 action applicable to the named objects for any principal. A private ACL does not cancel that grant.

Narrow only the action to object writes

hcl
resource "aws_s3_bucket" "restricted_bucket" {
  bucket = "example-s3b-181355"
  acl    = "private"

  policy = <<EOF
  {
    "Id": "id113",
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": [
          "s3:putObject"
        ],
        "Effect": "Allow",
        "Resource": "arn:aws:s3:::example-s3b-181355/*",
        "Principal": "*"
      }
    ]
  }
  EOF
}

The second still allows s3:putObject for every principal. Action names are case-insensitive. Limit uploads to approved callers and object ARNs.

References