CloudFront viewer policy allows HTTP

Use HTTPS between clients and CloudFront.

Description

With viewer_protocol_policy = "allow-all", clients can use HTTP or HTTPS to request content.

Potential impact

HTTP exposes requests and responses to interception or changes in transit.

Remediation

For each cache behavior, use https-only to reject HTTP or redirect-to-https to redirect supported requests. Send sensitive data over HTTPS from the start because the first HTTP request is unencrypted.

Examples

The example rejects HTTP on the default behavior. Set var.bucket_regional_domain_name to your S3 bucket’s regional domain. This S3 origin uses the viewer’s protocol, so new origin requests also use HTTPS.

Before

hcl
resource "aws_cloudfront_distribution" "example" {
  origin {
    domain_name = var.bucket_regional_domain_name
    origin_id   = "myS3Origin"

    s3_origin_config {
      origin_access_identity = "origin-access-identity/cloudfront/ABCDEFG1234567"
    }
  }

  enabled = true

  default_cache_behavior {
    allowed_methods        = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = "myS3Origin"
    viewer_protocol_policy = "allow-all"

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }
}

After

hcl
resource "aws_cloudfront_distribution" "example" {
  origin {
    domain_name = var.bucket_regional_domain_name
    origin_id   = "myS3Origin"

    s3_origin_config {
      origin_access_identity = "origin-access-identity/cloudfront/ABCDEFG1234567"
    }
  }

  enabled = true

  default_cache_behavior {
    allowed_methods        = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = "myS3Origin"
    viewer_protocol_policy = "https-only"

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }
}

References