aws / aws/aws-cdk

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

Open
#34,784 2 comments 3 reactions 0 assignees View on GitHub
@aws-cdk/aws-ec2 bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

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.

### Last Known Working CDK Library Version

_No response_

### Expected Behavior

The CDK should validate the base address against the specified prefix length and throw an error if the base address is not aligned correctly for that prefix. At minimum, it should issue a warning or explicitly document that rounding will occur.

### Current Behavior

CDK accepts misaligned CIDR specifications without validation. It internally adjusts the base address to the next aligned block for the given prefix length and proceeds with deployment. This adjustment is not surfaced to the user, leading to potential confusion or misconfiguration of network boundaries.

### Reproduction Steps

1. Create a new CDK stack using TypeScript.

2. Define a VPC or subnet using an invalid CIDR block such as 10.0.40.0/19:

``` ts
ec2.Vpc.fromVpcAttributes(this, 'MyVpc', {
vpcId: 'vpc-abc123',
ipAddresses: ec2.IpAddresses.cidr('10.0.40.0/19')
});
```

3. Synthesize or deploy the stack.

4. Observe that the resulting CIDR block is 10.0.64.0/19 rather than the originally specified 10.0.40.0/19.

### Possible Solution

Implement validation in IpAddresses.cidr() to check whether the base IP address is aligned for the given prefix length.

If misaligned, throw an error to prevent silent correction.

Alternatively, provide a clear warning or log message during synthesis to inform the user that the input CIDR block has been modified.

Document this behavior explicitly in the method's API documentation to prevent confusion for users relying on deterministic network design.

### Additional Information/Context

_No response_

### AWS CDK Library version (aws-cdk-lib)

2.1019.1

### AWS CDK CLI version

2.27.40

### Node.js Version

22.16.0lts

### OS

Windows 10

### Language

TypeScript

### Language Version

5.8.3

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the aws_ec2.IpAddresses.cidr() entry point and reproduce the 10.0.40.0/19 case by synthesizing the TypeScript example. Trace how the base address becomes 10.0.64.0/19 and check the surrounding network-utils behavior. Done means a misaligned base is rejected or its adjustment is clearly surfaced, with coverage for the reported input.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
infrastructure, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.