For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/cloudformation-aws-cloudfront-viewer-protocol-policy-allows-http.md.
A documentation index is available at /llms.txt.
CloudFront distributions must enforce HTTPS for viewer connections to prevent plaintext interception and downgrade attacks that could expose credentials or sensitive content.
In CloudFormation, verify AWS::CloudFront::Distribution resources: DistributionConfig.DefaultCacheBehavior.ViewerProtocolPolicy and each DistributionConfig.CacheBehaviors[].ViewerProtocolPolicy must be set to https-only or redirect-to-https.
Resources with ViewerProtocolPolicy set to allow-all (or missing the property) will be flagged. Ensure both the default cache behavior and all cache behaviors explicitly enforce or redirect to HTTPS.
#this code is a correct code for which the query should not find any resultAWSTemplateFormatVersion:"2010-09-09"Resources:cloudfrontdistribution_1:Type:AWS::CloudFront::DistributionProperties:DistributionConfig:CacheBehaviors:- LambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampDefaultCacheBehavior:ViewerProtocolPolicy:https-onlyLambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampIPV6Enabled:trueOrigins:- CustomOriginConfig:OriginKeepaliveTimeout:60OriginReadTimeout:30Tags:- Key:nameValue:example
#this code is a correct code for which the query should not find any resultAWSTemplateFormatVersion:"2010-09-09"Resources:cloudfrontdistribution_1:Type:AWS::CloudFront::DistributionProperties:DistributionConfig:CacheBehaviors:- LambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampDefaultCacheBehavior:ViewerProtocolPolicy:redirect-to-httpsLambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampIPV6Enabled:trueOrigins:- CustomOriginConfig:OriginKeepaliveTimeout:60OriginReadTimeout:30Tags:- Key:nameValue:example
Non-Compliant Code Examples
#this is a problematic code where the query should report a result(s)AWSTemplateFormatVersion:"2010-09-09"Resources:cloudfrontdistribution_1:Type:AWS::CloudFront::DistributionProperties:DistributionConfig:CacheBehaviors:- LambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampDefaultCacheBehavior:ViewerProtocolPolicy:allow-allLambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampIPV6Enabled:trueOrigins:- CustomOriginConfig:OriginKeepaliveTimeout:60OriginReadTimeout:30Tags:- Key:nameValue:examplecloudfrontdistribution_2:Type:AWS::CloudFront::DistributionProperties:DistributionConfig:CacheBehaviors:- ViewerProtocolPolicy:allow-allLambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampDefaultCacheBehavior:LambdaFunctionAssociations:- EventType:viewer-requestLambdaFunctionARN:exampIPV6Enabled:trueOrigins:- CustomOriginConfig:OriginKeepaliveTimeout:60OriginReadTimeout:30Tags:- Key:nameValue:example