Skip to content

Commit 64a0e2c

Browse files
chore(cloudfront): prevent WebACL from being created in regions other than us-east-1 (#32252)
### Reason for this change When attaching a WebACL to CloudFront Distribution, the region must be `us-east-1`, but no validation was done. see: https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-creating.html > For Region, if you've chosen a Regional resource type, choose the Region where you want AWS WAF to store the web ACL. > > You only need to choose this option for Regional resource types. For CloudFront distributions, the Region is hard-coded to the US East (N. Virginia) Region, us-east-1, for Global (CloudFront) applications. ### Description of changes Add validation to the `attachWebAclId` method of CloudFront Distribution ### Description of how you validated changes Unit and integ testing ### 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*
1 parent f091714 commit 64a0e2c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/distribution.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,14 @@ export class Distribution extends Resource implements IDistribution {
336336
});
337337
}
338338

339+
if (props.webAclId) {
340+
this.validateWebAclId(props.webAclId);
341+
this.webAclId = props.webAclId;
342+
}
343+
339344
this.certificate = props.certificate;
340345
this.errorResponses = props.errorResponses ?? [];
341346
this.publishAdditionalMetrics = props.publishAdditionalMetrics;
342-
this.webAclId = props.webAclId;
343347

344348
// Comments have an undocumented limit of 128 characters
345349
const trimmedComment =
@@ -606,15 +610,27 @@ export class Distribution extends Resource implements IDistribution {
606610
/**
607611
* Attach WAF WebACL to this CloudFront distribution
608612
*
613+
* WebACL must be in the us-east-1 region
614+
*
609615
* @param webAclId The WAF WebACL to associate with this distribution
610616
*/
611617
public attachWebAclId(webAclId: string) {
612618
if (this.webAclId) {
613619
throw new Error('A WebACL has already been attached to this distribution');
614620
}
621+
this.validateWebAclId(webAclId);
615622
this.webAclId = webAclId;
616623
}
617624

625+
private validateWebAclId(webAclId: string) {
626+
if (webAclId.startsWith('arn:')) {
627+
const webAclRegion = Stack.of(this).splitArn(webAclId, ArnFormat.SLASH_RESOURCE_NAME).region;
628+
if (!Token.isUnresolved(webAclRegion) && webAclRegion !== 'us-east-1') {
629+
throw new Error(`WebACL for CloudFront distributions must be created in the us-east-1 region; received ${webAclRegion}`);
630+
}
631+
}
632+
}
633+
618634
private addOrigin(origin: IOrigin, isFailoverOrigin: boolean = false): string {
619635
const ORIGIN_ID_MAX_LENGTH = 128;
620636

packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1394,4 +1394,30 @@ describe('attachWebAclId', () => {
13941394
distribution.attachWebAclId('473e64fd-f30b-4765-81a0-62ad96dd167b');
13951395
}).toThrow(/A WebACL has already been attached to this distribution/);
13961396
});
1397+
1398+
describe('throws if the WebAcl is not in us-east-1 region', () => {
1399+
test('when try to attach WebACL using `attachWebAclId` method', () => {
1400+
const origin = defaultOrigin();
1401+
1402+
const distribution = new Distribution(stack, 'MyDist', {
1403+
defaultBehavior: { origin },
1404+
});
1405+
1406+
expect(() => {
1407+
distribution.attachWebAclId('arn:aws:wafv2:ap-northeast-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a');
1408+
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
1409+
});
1410+
1411+
test('when try to attach WebACL by specifying value for props', () => {
1412+
const origin = defaultOrigin();
1413+
1414+
expect(() => {
1415+
new Distribution(stack, 'MyDist', {
1416+
defaultBehavior: { origin },
1417+
webAclId: 'arn:aws:wafv2:ap-northeast-1:123456789012:global/web-acl/MyWebAcl/473e64fd-f30b-4765-81a0-62ad96dd167a',
1418+
});
1419+
}).toThrow(/WebACL for CloudFront distributions must be created in the us-east-1 region; received ap-northeast-1/);
1420+
});
1421+
});
1422+
13971423
});

0 commit comments

Comments
 (0)