-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix(ec2): strict alignment for CidrBlock (no silent rounding) #34851
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request linter fails with the following errors:
❌ Fixes must contain a change to an integration test file and the resulting snapshot.
If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request
. Additionally, if clarification is needed, add Clarification Request
to a comment.
✅ A exemption request has been requested. Please wait for a maintainer's review.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
@aws-cdk-automation Exemption Request - Requested changes have been committed. |
Issue #34784
Closes #34784 .
Reason for this change
When using aws_ec2.IpAddresses.cidr() in the AWS CDK to define a VPC or subnet CIDR block, if the provided base IP address is not properly aligned for the specified prefix length, the CDK silently "rounds up" the address to the next valid CIDR block without issuing a warning or error. This can result in unexpected address space being allocated, which may go unnoticed during deployment and lead to future routing or peering issues.
Description of changes
Removed hidden rounding from the CidrBlock constructor. Now, both string and numeric branches strictly validate alignment and throw on misaligned input. An explicit pre‐alignment step was also added in NetworkBuilder.addSubnets (and the EC2 allocator) before calling new CidrBlock(base, mask). These changes address the issue by ensuring that any user-provided CIDR fails fast with a clear alignment error while preserving the original auto-allocation behaviour through explicit builder-side rounding. EnforceAlignment flag to the constructor (too leaky and confusing), mixed rounding logic in the constructor (inconsistent API), keeping global silent rounding (reintroduces the original bug), and exporting a separate helper function (only relocates logic without simplifying carve sites) were all considered and rejected. Key design decisions include centralising validation in the constructor, moving rounding into the builder for separation of concerns, avoiding public-API bloat by not introducing new flags or overloads, and preserving the existing test suite unchanged.
Describe any new or updated permissions being added
N/A
Description of how you validated changes
The CidrBlock constructor in network-utils.ts validates alignment only for CIDRs within the VPC-allowed range of /16 to /28, enabling broader blocks such as /12 to be used in contexts like EKS without triggering alignment errors. This ensures strict alignment enforcement for VPC and subnet definitions while supporting more flexible usage elsewhere. In network-utils.test.ts, the import for UnscopedValidationError was updated to reference the core module. A misaligned numeric offset test was added to verify error handling, and the maxIp() test was adjusted to reflect correct behaviour for aligned blocks. A new block of tests, CidrBlock alignment validation, was introduced to confirm that both string- and numeric-form CIDRs behave correctly with respect to alignment rules.
All tests were verified as completing successfully after implementing these changes. A local package was created and imported into a new TS CDK project. A VPC was created like so:
props.cidr
was given the value of10.0.32.0/19
and acdk synth
was performed. This resulted in a successfully synthed stack with a VPC with a CIDR value of 10.0.32.0/19. Additionally, several subnets were automatically created with CIDR of10.0.40.0/21
,10.0.48.0/21
,10.0.32.0/24
and10.0.33.0/24
.When
props.cidr
was given a value of10.0.40.0/19
andcdk synth
was performed, the following error was returned in the CLI.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license