
Deny plain HTTP on S3 buckets in CDK with enforceSSL and minimumTLSVersion
S3 accepts plain HTTP unless a policy says otherwise. Two CDK props add the deny statements, and cdk synth shows exactly what they generate.
S3 endpoints speak both HTTP and HTTPS. The AWS documentation on encryption in transit says plainly that HTTP traffic is accepted, and that enforcing HTTPS is something you configure, through a bucket policy, an SCP, an RCP or a VPC endpoint policy. Nothing rejects an
http:// request on its own.Most SDK clients use HTTPS by default, so the gap tends to show up elsewhere: a hand-written client, an old script, a tool pointed at an explicit HTTP endpoint. A bucket policy that denies insecure requests closes it, and the CDK
Bucket construct can write that policy for you.The two props
According to the CDK API reference,
enforceSSL is a boolean that defaults to false . minimumTLSVersion is a number, has no default (no minimum is enforced), and requires enforceSSL to be turned on.const cdk = require('aws-cdk-lib');const s3 = require('aws-cdk-lib/aws-s3');
const app = new cdk.App();const stack = new cdk.Stack(app, 'TlsDemo');
new s3.Bucket(stack, 'DataBucket', { enforceSSL: true, minimumTLSVersion: 1.2, blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,});What cdk synth produces
I ran this app with aws-cdk-lib 2.276.0 and looked at the template. Besides the bucket, there is an
AWS::S3::BucketPolicy with two Deny statements, shown here in shortened form:DataBucketPolicy: Type: AWS::S3::BucketPolicy Properties: PolicyDocument: Statement: - Action: s3:* Effect: Deny Principal: { AWS: "*" } Condition: Bool: aws:SecureTransport: "false" Resource: [bucket ARN, bucket ARN/*] - Action: s3:* Effect: Deny Principal: { AWS: "*" } Condition: NumericLessThan: s3:TlsVersion: 1.2 Resource: [bucket ARN, bucket ARN/*]The first statement denies any request where
aws:SecureTransport is false , which means any request that did not use TLS. That key does not look at the TLS version. The second one is what minimumTLSVersion adds: it denies requests where s3:TlsVersion is lower than the number you gave. The S3 documentation shows both condition keys and says they can live in the same policy.Choosing the minimum version
S3 supports TLS 1.2 and TLS 1.3 on its API endpoints, and the AWS SDKs and CLI released since 2014 default to TLS 1.3. The same page notes that TLS 1.3 is not supported on AWS PrivateLink for S3 or on Multi-Region Access Points. A floor of 1.3 would therefore deny clients that reach the bucket that way, so I would start at 1.2 and only raise it when every client path is known.
The S3 server access logs and CloudTrail both record the TLS version and cipher suite of each request. Checking those before raising the floor is cheaper than finding out from an outage.
Things to keep in mind
The deny applies to every principal, administrators included, so a client that still uses HTTP will see
AccessDenied and nothing more helpful. When the bucket is new that is easy to spot. On an existing bucket it is worth reading the access logs first.The CDK reference notes that
enforceSSL relates to control S3.5 of the AWS Foundational Security Best Practices, the S3 control about requiring SSL.What was not tested here: I only ran
cdk synth . I did not deploy the stack or send an HTTP request to a real bucket, so the actual rejection behavior comes from the documentation, not from a test.Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article