Review public exposure of an AWS S3 static website

Limit static website content and permissions to the intended public scope

Description

aws_s3_bucket_website_configuration configures a website endpoint. It needs separate public-read permission to serve objects, and it supports HTTP only.

Potential impact

Broad public permissions can expose files outside the intended site. Content served directly over HTTP can be read or changed in transit.

Remediation

To retire a live site, switch traffic to its replacement, then remove the website configuration and public grants. For a site that stays online, isolate its public files. To serve HTTPS from a private bucket, use a CloudFront S3 REST origin with origin access control (OAC).

Examples

Both configurations keep the same bucket. The first names index and error documents without granting read access. The second removes the website configuration and enables all four Block Public Access settings. After applying it, confirm that an anonymous request cannot read an existing object.

Before

hcl
resource "aws_s3_bucket" "website_bucket" {
  bucket = "s3-website-test.hashicorp.com"
}

resource "aws_s3_bucket_website_configuration" "website" {
  bucket = aws_s3_bucket.website_bucket.id

  index_document {
    suffix = "index.html"
  }

  error_document {
    key = "error.html"
  }
}

After

hcl
resource "aws_s3_bucket" "website_bucket" {
  bucket = "s3-website-test.hashicorp.com"
}

resource "aws_s3_bucket_public_access_block" "website_bucket" {
  bucket = aws_s3_bucket.website_bucket.id

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

References