-
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): bucket.grantRead to an organization principal grants public read access #32756
Comments
Turns out while trying to come up with a workaround that this is not an issue with |
@ehiggins0 , thanks for reporting this. I see that the implementation of
and this is how
Looks like there should be a check added before granting access to |
I'm not able to reproduce this.
and it's working fine. I'm using 2.178.2. |
Thanks @IkeNefcy for pointing this out. I tried to repro the issue with this sample code - const org = new OrganizationPrincipal('');
const bucket = new s3.Bucket(this, 'bucket', {});
bucket.grantRead(org); and got this generated template - "bucketPolicy638F945D": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "bucket43879C71"
},
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:GetBucket*",
"s3:GetObject*",
"s3:List*"
],
"Condition": {
"StringEquals": {
"aws:PrincipalOrgID": ""
}
},
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Resource": [
{
"Fn::GetAtt": [
"bucket43879C71",
"Arn"
]
}, Though You are right in saying that |
Copy, yeah in the OG example there is no condition key at all
where your current is like what mine does
I can add behavior to check that the organization name is not blank, and that should help with specifically the |
@khushail Pattern; |
Thanks @IkeNefcy for submitting a PR. Appreciate your efforts. |
For sure! |
### Issue # Closes #32756 ### Reason for this change The original issue was related to over permissive s3 permissions. Which originally was being caused by what seems to be something related to an undefined `iam.OrgranizationPrincipal` being allowed. However when using 2.178.2, I'm not seeing this particular issue, but the policy that is generated could still be incorrectly created by leaving a blank string. `iam.OrgranizationPrincipal('')` This can be avoided with a simple check. Although this is not a golden solution since it's not able to check if that organization exists, but for the use case it's better than nothing. ### Description of changes Adding a regex check that matches the Organization ID regex pattern in the docs; https://docs.aws.amazon.com/organizations/latest/APIReference/API_Organization.html ``` if (!organizationId.match(/^o-[a-z0-9]{10,32}$/)) { throw new Error(`Expected Organization ID must match regex pattern ^o-[a-z0-9]{10,32}$, received ${organizationId}`); } ``` ### Description of how you validated changes Added a test for bad names ``` test('throw error when Organization ID does not match regex pattern', () => { // GIVEN const shortOrgId = 'o-shortname'; const noOOrgName = 'no-o-name'; const longOrgName = 'o-thisnameistoooooooooooooooooolong'; // THEN expect(() => new iam.OrganizationPrincipal(shortOrgId)).toThrow(`Expected Organization ID must match regex pattern ^o-[a-z0-9]{10,32}$, received ${shortOrgId}`); expect(() => new iam.OrganizationPrincipal(noOOrgName)).toThrow(`Expected Organization ID must match regex pattern ^o-[a-z0-9]{10,32}$, received ${noOOrgName}`); expect(() => new iam.OrganizationPrincipal(longOrgName)).toThrow(`Expected Organization ID must match regex pattern ^o-[a-z0-9]{10,32}$, received ${longOrgName}`); }); ``` ### Checklist - [X] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Comments on closed issues and PRs are hard for our team to see. |
Describe the bug
When using
bucket.grantRead(org)
, the generated policy allows access to the bucket for all AWS accounts without a condition.Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
The policy should have a condition:
Current Behavior
This policy gets generated:
Reproduction Steps
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
2.150.0
Framework Version
No response
Node.js Version
18.18.2
OS
Ubuntu 24.04
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: