-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aws-s3: blockPublicAccess
has a counterintuitive behaviour
#32811
Comments
Hi @garysassano , thanks for reporting this. I tried to repro with 3 different scenario and here is my observation - export class BucketAccessv2Stack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucketWithDefaultAccess = new s3.Bucket(this, 'bucketdefaulted', {}); //means by default, it should have blockallPublicaccess :true which is true when seen in console
const bucketwithsetaccess = new s3.Bucket(this, 'bucketwithsetaccess', {
blockPublicAccess: new s3.BlockPublicAccess({
blockPublicAcls: true,
ignorePublicAcls: true,
blockPublicPolicy: true,
restrictPublicBuckets: true, // means it should have blockallPublicaccess :true
}),
});
const bukcetwithfalseaccess = new s3.Bucket(this, "bucketwithfalseaccess", {
blockPublicAccess: new s3.BlockPublicAccess({
blockPublicPolicy: false,
restrictPublicBuckets: false, //means it should have blockPublicaccess: false for defined policy and rest 2 which are undefined should be defaulted to true
}),
});
}
} In console , the setting for resppective buckets are -
![]()
![]()
![]() As its seen in the code, default value for these variables is set to Code commit - 299fb6a#diff-97a6344bb43117c16441333d96eede412c2c7f7df034f88bbebb90b151eca42d export class BlockPublicAccess {
public static readonly BLOCK_ALL = new BlockPublicAccess({
blockPublicAcls: true,
blockPublicPolicy: true,
ignorePublicAcls: true,
restrictPublicBuckets: true,
});
public static readonly BLOCK_ACLS = new BlockPublicAccess({
blockPublicAcls: true,
ignorePublicAcls: true,
});
public blockPublicAcls: boolean | undefined;
public blockPublicPolicy: boolean | undefined;
public ignorePublicAcls: boolean | undefined;
public restrictPublicBuckets: boolean | undefined;
constructor(options: BlockPublicAccessOptions) {
this.blockPublicAcls = options.blockPublicAcls;
this.blockPublicPolicy = options.blockPublicPolicy;
this.ignorePublicAcls = options.ignorePublicAcls;
this.restrictPublicBuckets = options.restrictPublicBuckets;
}
} One can edit the settings by going through the console or setting the policies explicitly to true in the code But this should be addressed so I am marking this as P2 which means it won't be immediately addressed by the team but would be on their radar. |
hmm I'm questioning if this is a bug or not. -- When a bucket is created without specifying the blockPublicAccess property: const myBucket = new Bucket(this, "MyBucket"); It is equivalent to explicitly setting all BlockPublicAccess options to true: const myBucket = new Bucket(this, "MyBucket", { This might lead you to assume that all BlockPublicAccess options default to true -- But in reality, if we leave out BlockPublicAccess from the CDK / it's not in the template, all 4 are being turned on yes, but NOT because this is a CFN default, but because this is the S3 default state when creating buckets.
is
But if we force true to appear when the user has not explicitly set it, then we would be doing something that is different from how both CFN and console behave and could just blur the line totally on this function. |
As I mentioned in the original post, AWS updated the default security settings for S3 buckets a couple of years ago. We aren't planning to override those defaults. When a new S3 bucket is created without any additional configuration, all
CDK has always taken a different approach from CloudFormation by applying sensible defaults to improve the developer experience. And no, this would't be inconsistent with the AWS Console. As you pointed out, in the AWS Console, all options are automatically enabled by default, and users only specify which ones they want to disable. This aligns with the behavior CDK would enforce—assuming everything is enabled by default and only specifying exceptions. That said, I'm okay with requiring users to explicitly set all four options. While this could introduce some friction in the developer experience, it would ensure users make deliberate choices and avoid unexpected configurations. |
hmm color me convinced, will work on a PR later today |
Describe the bug
When a bucket is created without specifying the
blockPublicAccess
property:It is equivalent to explicitly setting all
BlockPublicAccess
options totrue
:This might lead you to assume that all
BlockPublicAccess
options default totrue
. However, that's not the case. For example, if you deploy a bucket like this:You would get this configuration:
This happens because all options within
BlockPublicAccess
areundefined
by default, which is equivalent tofalse
.This behavior is counterintuitive. If you do not define
blockPublicAccess
, all options default totrue
. However, if you define aBlockPublicAccess
, any unspecified options default tofalse
.This seemingly paradoxical situation stems from a change introduced a couple of years ago.
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
see above.
Current Behavior
see above.
Reproduction Steps
see above.
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.174.1
Framework Version
No response
Node.js Version
22.12.0
OS
Ubuntu 24.04.1
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: