Skip to content
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

chore(cloudfront): prevent WebACL from being created in regions other than us-east-1 #32252

Merged
merged 12 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,14 @@ export class Distribution extends Resource implements IDistribution {
});
}

if (props.webAclId) {
this.validateWebAclId(props.webAclId);
this.webAclId = props.webAclId;
}

this.certificate = props.certificate;
this.errorResponses = props.errorResponses ?? [];
this.publishAdditionalMetrics = props.publishAdditionalMetrics;
this.webAclId = props.webAclId;

// Comments have an undocumented limit of 128 characters
const trimmedComment =
Expand Down Expand Up @@ -606,15 +610,27 @@ export class Distribution extends Resource implements IDistribution {
/**
* Attach WAF WebACL to this CloudFront distribution
*
* WebACL must be in the us-east-1 region
*
* @param webAclId The WAF WebACL to associate with this distribution
*/
public attachWebAclId(webAclId: string) {
if (this.webAclId) {
throw new Error('A WebACL has already been attached to this distribution');
}
this.validateWebAclId(webAclId);
this.webAclId = webAclId;
}

private validateWebAclId(webAclId: string) {
if (webAclId.startsWith('arn:')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this validation also necessary in the place where the webacl props is being passed?

this.webAclId = props.webAclId;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mazyu36

Thank you very much. As you pointed out, validation was needed, so we have modified it!

(I have modified the policy to provide a validation method in the private method.)

4a02e21

const webAclRegion = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME).region;
if (!Token.isUnresolved(webAclRegion) && webAclRegion !== 'us-east-1') {
throw new Error(`WebACL for CloudFront distributions must be created in the us-east-1 region; received ${webAclRegion}`);
}
}
}

private addOrigin(origin: IOrigin, isFailoverOrigin: boolean = false): string {
const ORIGIN_ID_MAX_LENGTH = 128;

Expand Down
26 changes: 26 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,30 @@ describe('attachWebAclId', () => {
distribution.attachWebAclId('473e64fd-f30b-4765-81a0-62ad96dd167b');
}).toThrow(/A WebACL has already been attached to this distribution/);
});

describe('throws if the WebAcl is not in us-east-1 region', () => {
test('when try to attach WebACL using `attachWebAclId` method', () => {
const origin = defaultOrigin();

const distribution = new Distribution(stack, 'MyDist', {
defaultBehavior: { origin },
});

expect(() => {
distribution.attachWebAclId('arn:aws:wafv2:ap-northeast-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a');
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
});

test('when try to attach WebACL by specifying value for props', () => {
const origin = defaultOrigin();

expect(() => {
new Distribution(stack, 'MyDist', {
defaultBehavior: { origin },
webAclId: 'arn:aws:wafv2:ap-northeast-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a',
});
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
});
});

});
Loading