Resource-based policies granting access to all users can lead to information leakage.
Ask Yourself Whether
- The AWS resource stores or processes sensitive data.
- The AWS resource is designed to be private.
There is a risk if you answered yes to any of those questions.
Recommended Secure Coding Practices
It’s recommended to implement the least privilege principle, i.e. to grant necessary permissions only to users for their required tasks. In the
context of resource-based policies, list the principals that need the access and grant to them only the required privileges.
Sensitive Code Example
This policy allows all users, including anonymous ones, to access an S3 bucket:
import { aws_iam as iam } from 'aws-cdk-lib'
import { aws_s3 as s3 } from 'aws-cdk-lib'
const bucket = new s3.Bucket(this, "ExampleBucket")
bucket.addToResourcePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["s3:*"],
resources: [bucket.arnForObjects("*")],
principals: [new iam.AnyPrincipal()] // Sensitive
}))
Compliant Solution
This policy allows only the authorized users:
import { aws_iam as iam } from 'aws-cdk-lib'
import { aws_s3 as s3 } from 'aws-cdk-lib'
const bucket = new s3.Bucket(this, "ExampleBucket")
bucket.addToResourcePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["s3:*"],
resources: [bucket.arnForObjects("*")],
principals: [new iam.AccountRootPrincipal()]
}))
See