Resource-based policies granting access to all users can lead to information leakage.
There is a risk if you answered yes to any of those questions.
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.
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
}))
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()]
}))