Skip to content

fix(ec2): validate VPC CIDR alignment behind a feature flag - #34851

Open
DaltJM wants to merge 10 commits into
aws:mainfrom
DaltJM:fix/ec2-cidr-validation
Open

DaltJM wants to merge 10 commits into
aws:mainfrom
DaltJM:fix/ec2-cidr-validation

Conversation

@DaltJM

@DaltJM DaltJM commented Jun 29, 2025 •

Copy link
Copy Markdown

Issue # (if applicable)

Closes #34784.

Reason for this change

When a Vpc is given a CIDR block through IpAddresses.cidr() (or the deprecated cidr property) whose base address is not aligned to its prefix length, such as 10.0.40.0/19, the CDK silently rounds the block up to the next aligned block (10.0.64.0/19). The deployed VPC then has a different address range than the one requested, which can go unnoticed and cause routing or peering issues later.

Rejecting these CIDRs unconditionally would break existing apps that currently synthesize and deploy with a misaligned CIDR, so the validation is introduced behind a feature flag.

Description of changes

  • Add the @aws-cdk/aws-ec2:validateVpcCidrAlignment bug-fix feature flag (unconfiguredBehavesLike: false, recommended value true).
  • When the flag is enabled, Vpc throws a ValidationError if the CIDR block provided through IpAddresses.cidr() or the deprecated cidr property is not aligned to its prefix length. The error suggests the aligned blocks below and above the given one, e.g.:
    VPC CIDR block '10.0.40.0/19' is not aligned to its /19 prefix length. Use '10.0.32.0/19' or '10.0.64.0/19' instead.
    
  • When the flag is disabled, the existing rounding behavior is preserved.
  • The check lives in Vpc (where the construct scope needed to read the flag is available) and only applies to the local IpAddresses.cidr() provider. CidrBlock and NetworkBuilder are unchanged, so subnet allocation behaves exactly as before.
  • Document the behavior in the aws-ec2 README and fix two doc examples that used a misaligned CIDR (10.0.1.0/20).
  • Regenerate FEATURE_FLAGS.md and recommended-feature-flags.json.

Describe any new or updated permissions being added

N/A

Description of how you validated changes

Added unit tests in aws-ec2/test/ip-addresses.test.ts covering:

  • a misaligned CIDR is rounded up when the flag is disabled (existing behavior),
  • a misaligned CIDR throws when the flag is enabled, via both IpAddresses.cidr() and the deprecated cidr property,
  • aligned and default CIDRs are accepted when the flag is enabled.

Added the flag to the defaults check in cx-api/test/features.test.ts. The ip-addresses, network-utils and features test suites pass, and ESLint passes on the changed files.

No integration test was added, as the change only affects synthesis-time validation and is fully covered by unit tests (per review feedback).

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions Bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Jun 29, 2025
@aws-cdk-automation
aws-cdk-automation requested a review from a team June 29, 2025 07:20
@github-actions github-actions Bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Jun 29, 2025

@aws-cdk-automation aws-cdk-automation left a comment •

Copy link
Copy Markdown
Collaborator

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.

@DaltJM DaltJM changed the title feat(ec2): strict alignment for CidrBlock (no silent rounding) fix(ec2): strict alignment for CidrBlock (no silent rounding) Jun 29, 2025
@DaltJM
DaltJM marked this pull request as draft June 29, 2025 07:25
@DaltJM
DaltJM marked this pull request as ready for review June 29, 2025 07:27
@aws-cdk-automation

Copy link
Copy Markdown
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 7b462fa
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@DaltJM

DaltJM commented Jul 5, 2025

Copy link
Copy Markdown
Author

@aws-cdk-automation Exemption Request - Requested changes have been committed.

@aws-cdk-automation aws-cdk-automation added pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Jul 5, 2025
@aws-cdk-automation aws-cdk-automation added pr/community-review-timeout pr/needs-maintainer-review This PR needs a review from a Core Team Member and removed pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels May 12, 2026

@leonmk-aws leonmk-aws left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This adds validation that rejects input which previously synthesized and deployed successfully — a misaligned base like 10.0.1.0/20 used to be rounded to 10.0.0.0/20 and deployed fine. Existing apps that pass a misaligned CIDR (deliberately or by typo that "worked") will now fail at synth the moment they upgrade, with no opt-out.
Adding this validation should be done under a feature flag to preserve backwards compatibility.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Integ tests for EC2 reside in: packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test, though I don't think we need an integration test for this.

Comment thread CHANGELOG.v2.md Outdated

### Bug Fixes

* **ec2:** validate that mis-aligned CIDR blocks now throw an error instead of silently rounding up (#34784)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The changelog is auto generated, no need to edit this file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is this file ?

export class InvalidCidrRangeError extends Error {
constructor(cidr: string) {
super(cidr + ' is not a valid VPC CIDR range. VPCs must be between /16 and /28 and the minimum subnet size is /28.');
super(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's not do these linting changes (applies to the other changes as well).

…tion

Resolve conflicts by taking upstream's version of the CIDR code and drop
the previous approach (unconditional validation in CidrBlock, formatting
changes, manual CHANGELOG entry and integ test files) in favor of a
feature-flagged check in the follow-up commit.
A misaligned CIDR block passed to `IpAddresses.cidr()` (or the deprecated
`cidr` property), such as `10.0.40.0/19`, is silently rounded up to the
next aligned block (`10.0.64.0/19`).

Add the `@aws-cdk/aws-ec2:validateVpcCidrAlignment` feature flag. When
enabled, `Vpc` throws a validation error for a misaligned CIDR block and
suggests the aligned blocks below and above it. When disabled, the existing
rounding behavior is preserved.

Closes aws#34784.
@DaltJM
DaltJM deployed to automation October 17, 2026 16:16 — with GitHub Actions Active
@DaltJM

DaltJM commented Oct 17, 2026

Copy link
Copy Markdown
Author

@leonmk-aws thanks for the review. I've reworked the PR to address all of your feedback:

  • Feature flag: the validation is now behind a new @aws-cdk/aws-ec2:validateVpcCidrAlignment flag (unconfiguredBehavesLike: false, recommended true). With the flag disabled, existing apps keep the current behavior and a misaligned CIDR is still rounded up to the next aligned block. With the flag enabled, Vpc throws for a misaligned CIDR passed via IpAddresses.cidr() or the deprecated cidr property, and the error suggests the aligned blocks on either side (e.g. 10.0.40.0/19 → use 10.0.32.0/19 or 10.0.64.0/19). CidrBlock and NetworkBuilder are unchanged.
  • Integ test: removed, along with the .fail.json file (it was meant to assert the synth error, but it isn't needed). The change is covered by unit tests for both flag states.
  • Linting changes: reverted; the diff now only contains the functional change.
  • CHANGELOG: reverted.

I also fixed the README example, which used a misaligned CIDR (10.0.1.0/20), and regenerated FEATURE_FLAGS.md and recommended-feature-flags.json. The branch is up to date with main.

@DaltJM
DaltJM deployed to automation October 17, 2026 16:16 — with GitHub Actions Active
@DaltJM DaltJM changed the title fix(ec2): strict alignment for CidrBlock (no silent rounding) fix(ec2): validate VPC CIDR alignment behind a feature flag Oct 17, 2026
@DaltJM

DaltJM commented Oct 17, 2026

Copy link
Copy Markdown
Author

Exemption Request - this change only adds a synthesis-time validation behind a feature flag and is fully covered by unit tests for both flag states. As suggested in review, an integration test isn't needed for this change.

This branch is waiting to be deployed

1 active and 1 waiting deployments
test-pipeline — 048691d5 Waiting Oct 21, 2026 by aws-cdk-automation via approve-test-pipeline #50157
automation — 048691d5 Deployed Oct 21, 2026 by aws-cdk-automation via validate-pr #373609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr/community-review-timeout pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Network-Utils: IpAddresses.cidr() silently adjusts misaligned base addresses to the next valid CIDR block

4 participants