fix(ec2): reject subnet CIDRs outside the VPC address space (#5461)

* fix(ec2): reject subnet CIDRs outside the VPC address space

* fix(ec2): serialize CIDR edits with subnet validation

* test(ec2): request native IPv6 subnets in endpoint fixtures
This commit is contained in:
Daniël van Ziel authored and GitHub committed 2026-10-11 18:52:30 -04:00
1 parent 9698c216c5
commit 20bc835b6f
4 files changed
+180 -31

No files matched your search

@@ -4810,13 +4810,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public VpcCidrBlockAssociation associateVpcCidrBlock(String region, String vpcId, String cidrBlock) {
ensureDefaultResources(region);
Vpc vpc = getRequiredVpc(region, vpcId);
VpcCidrBlockAssociation assoc = new VpcCidrBlockAssociation(
"vpc-cidr-assoc-" + randomHex(8), cidrBlock);
vpc.getCidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
synchronized (attachmentTopologyLock(region)) {
synchronized (lockFor(key(region, vpcId))) {
Vpc vpc = getRequiredVpc(region, vpcId);
VpcCidrBlockAssociation assoc = new VpcCidrBlockAssociation(
"vpc-cidr-assoc-" + randomHex(8), cidrBlock);
vpc.getCidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
}
}
}
/**
@@ -4826,11 +4829,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
*/
public VpcIpv6CidrBlockAssociation associateAmazonProvidedIpv6CidrBlock(String region, String vpcId) {
ensureDefaultResources(region);
Vpc vpc = getRequiredVpc(region, vpcId);
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
synchronized (attachmentTopologyLock(region)) {
synchronized (lockFor(key(region, vpcId))) {
Vpc vpc = getRequiredVpc(region, vpcId);
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
}
}
}
/**
@@ -4851,13 +4858,17 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
public void disassociateVpcCidrBlock(String region, String associationId) {
ensureDefaultResources(region);
for (Vpc vpc : vpcs.scan(k -> true)) {
if (vpc.getRegion().equals(region)) {
vpc.getCidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
// The same operation disassociates either family; AWS takes one association id and
// does not ask which set it belongs to.
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
vpcs.put(key(region, vpc.getVpcId()), vpc);
synchronized (attachmentTopologyLock(region)) {
for (Vpc vpc : vpcs.scan(k -> true)) {
if (vpc.getRegion().equals(region)) {
synchronized (lockFor(key(region, vpc.getVpcId()))) {
vpc.getCidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
// The same operation disassociates either family; AWS takes one association id and
// does not ask which set it belongs to.
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
vpcs.put(key(region, vpc.getVpcId()), vpc);
}
}
}
}
}
@@ -5655,8 +5666,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
// overlapping creates in flight together both pass the scan before either is stored.
// The topology lock, outermost, keeps DeleteVpc from removing the VPC in between.
synchronized (attachmentTopologyLock(region)) {
getRequiredVpc(region, vpcId);
Vpc currentVpc = getRequiredVpc(region, vpcId);
synchronized (lockFor(key(region, vpcId))) {
rejectSubnetCidrOutsideVpc(currentVpc, cidrBlock);
rejectConflictingSubnetCidr(region, vpcId, cidrBlock);
subnets.put(key(region, subnetId), subnet);
}
@@ -5677,6 +5689,22 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return subnet;
}
private void rejectSubnetCidrOutsideVpc(Vpc vpc, String cidrBlock) {
if (cidrBlock == null) {
return; // IPv6-only subnets have no IPv4 range to check.
}
boolean contained = Ipv4Cidrs.isIpv4(cidrBlock)
&& ((Ipv4Cidrs.isIpv4(vpc.getCidrBlock()) && Ipv4Cidrs.contains(vpc.getCidrBlock(), cidrBlock))
|| vpc.getCidrBlockAssociationSet().stream()
.filter(association -> "associated".equalsIgnoreCase(association.getCidrBlockState()))
.map(VpcCidrBlockAssociation::getCidrBlock)
.filter(Ipv4Cidrs::isIpv4)
.anyMatch(block -> Ipv4Cidrs.contains(block, cidrBlock)));
if (!contained) {
throw new AwsException("InvalidSubnet.Range", "The CIDR '" + cidrBlock + "' is invalid.", 400);
}
}
/**
* AWS refuses a subnet whose IPv4 CIDR overlaps another subnet's in the same VPC. CreateSubnet
* carries no idempotency token, so this check is also what stops an SDK transport retry after
@@ -47,10 +47,12 @@ import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair;
import io.github.hectorvent.floci.services.ec2.model.Volume;
import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
import io.github.hectorvent.floci.services.ec2.model.VpcCidrBlockAssociation;
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration;
import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
import org.awaitility.Awaitility;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
@@ -72,6 +74,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
@@ -86,6 +89,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
@@ -788,6 +792,84 @@ class Ec2ServiceTest {
.filter(s -> vpc.getVpcId().equals(s.getVpcId())).count());
}
@Test
void createSubnetRejectsCidrOutsideVpcWithoutStoringSubnet() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.0.0.0/16", false).getVpcId();
for (String cidr : List.of("192.168.1.0/24", "10.0.0.0/15", "10.1.0.0/24", "2001:db8::/56")) {
AwsException error = assertThrows(AwsException.class,
() -> service.createSubnet("us-east-1", vpcId, cidr, "us-east-1a"));
assertEquals("InvalidSubnet.Range", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
assertTrue(service.describeSubnets("us-east-1", List.of(), Map.of()).stream()
.noneMatch(subnet -> vpcId.equals(subnet.getVpcId())));
}
@Test
void createSubnetAllowsPrimaryAndSecondaryBlocksButNotDisassociatedBlocks() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.0.0.0/16", false).getVpcId();
String associationId = service.associateVpcCidrBlock("us-east-1", vpcId, "192.168.0.0/16").getAssociationId();
assertEquals("10.0.255.0/24",
service.createSubnet("us-east-1", vpcId, "10.0.255.9/24", "us-east-1a").getCidrBlock());
assertEquals("192.168.1.0/24",
service.createSubnet("us-east-1", vpcId, "192.168.1.9/24", "us-east-1a").getCidrBlock());
service.disassociateVpcCidrBlock("us-east-1", associationId);
assertEquals("InvalidSubnet.Range", assertThrows(AwsException.class,
() -> service.createSubnet("us-east-1", vpcId, "192.168.2.0/24", "us-east-1a")).getErrorCode());
}
@Test
void cidrAssociationEditsWaitForSubnetContainmentValidation() throws Exception {
for (boolean remove : List.of(false, true)) {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
Vpc vpc = service.createVpc("us-east-1", "10.0.0.0/16", false);
String associationId = service.associateVpcCidrBlock("us-east-1", vpc.getVpcId(), "192.168.0.0/16")
.getAssociationId();
CountDownLatch scanning = new CountDownLatch(1);
CountDownLatch resume = new CountDownLatch(1);
VpcCidrBlockAssociation first = spy(vpc.getCidrBlockAssociationSet().getFirst());
doAnswer(call -> {
scanning.countDown();
assertTrue(resume.await(15, TimeUnit.SECONDS));
return call.callRealMethod();
}).when(first).getCidrBlockState();
vpc.getCidrBlockAssociationSet().set(0, first);
AtomicReference<Thread> editor = new AtomicReference<>();
ExecutorService workers = Executors.newFixedThreadPool(2);
try {
Future<Subnet> creating = workers.submit(() ->
service.createSubnet("us-east-1", vpc.getVpcId(), "192.168.1.0/24", "us-east-1a"));
assertTrue(scanning.await(5, TimeUnit.SECONDS));
Future<?> editing = workers.submit(() -> {
editor.set(Thread.currentThread());
if (remove) {
service.disassociateVpcCidrBlock("us-east-1", associationId);
} else {
service.associateVpcCidrBlock("us-east-1", vpc.getVpcId(), "198.18.0.0/16");
}
});
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> editing.isDone()
|| (editor.get() != null && editor.get().getState() == Thread.State.BLOCKED));
assertFalse(editing.isDone(), "CIDR edits must not mutate the list while subnet validation scans it");
resume.countDown();
assertEquals("192.168.1.0/24", creating.get(5, TimeUnit.SECONDS).getCidrBlock());
editing.get(5, TimeUnit.SECONDS);
} finally {
resume.countDown();
workers.shutdownNow();
assertTrue(workers.awaitTermination(5, TimeUnit.SECONDS));
}
}
}
@Test
void createSubnetAllowsConflictingCidrsInDifferentVpcs() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
@@ -0,0 +1,31 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
@QuarkusTest
class Ec2SubnetVpcRangeIntegrationTest {
private static RequestSpecification ec2(String action) {
return given().header("Authorization", "AWS4-HMAC-SHA256 Credential=855555555555/20261011/us-east-1/ec2/aws4_request")
.formParam("Action", action);
}
@Test
void subnetRangeMustFitPrimaryOrAssociatedSecondaryVpcBlock() {
String vpcId = ec2("CreateVpc").formParam("CidrBlock", "10.0.0.0/16").post("/").then()
.statusCode(200).extract().path("CreateVpcResponse.vpc.vpcId");
ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.1.0/24")
.post("/").then().statusCode(400).body("Response.Errors.Error.Code", equalTo("InvalidSubnet.Range"));
ec2("AssociateVpcCidrBlock").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.0.0/16")
.post("/").then().statusCode(200);
String subnetId = ec2("CreateSubnet").formParam("VpcId", vpcId).formParam("CidrBlock", "192.168.1.9/24")
.post("/").then().statusCode(200).body("CreateSubnetResponse.subnet.cidrBlock", equalTo("192.168.1.0/24"))
.extract().path("CreateSubnetResponse.subnet.subnetId");
ec2("DeleteSubnet").formParam("SubnetId", subnetId).post("/").then().statusCode(200);
ec2("DeleteVpc").formParam("VpcId", vpcId).post("/").then().statusCode(200);
}
}
@@ -72,6 +72,18 @@ class Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest {
"VpcId", vpcId, "CidrBlock", cidr, "AvailabilityZone", availabilityZone);
}
private String associateIpv6Block(String vpcId) {
return ec2Value("AssociateVpcCidrBlock", "AssociateVpcCidrBlockResponse.ipv6CidrBlockAssociation.ipv6CidrBlock",
"VpcId", vpcId, "AmazonProvidedIpv6CidrBlock", "true");
}
private String ipv6SubnetCidr(String block, int offset) {
String network = block.substring(0, block.indexOf("::"));
int separator = network.lastIndexOf(':');
int subnet = Integer.parseInt(network.substring(separator + 1), 16) + offset;
return network.substring(0, separator + 1) + Integer.toHexString(subnet) + "::/64";
}
private XmlPath describeEndpoint(String endpointId) {
return given()
.formParam("Action", "DescribeVpcEndpoints")
@@ -157,17 +169,12 @@ class Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest {
@Test
void anEndpointOnASubnetWithoutIpv4StillAnswers() {
// REGRESSION GUARD for the request path this change created. Deriving the
// interfaces now happens while rendering CreateVpcEndpoint and
// DescribeVpcEndpoints, and that derivation computes a private IP by splitting
// the subnet's CidrBlock on dots. CreateSubnet stores whatever CidrBlock it is
// given without validating the family, so the value is not guaranteed to be
// dotted IPv4 -- and before the guard, parts[1] threw, turning an odd subnet into
// a FAILED EC2 response instead of a degraded address. Previously the same
// calculation ran only on the flow-log scheduler, where a throw was contained.
// Endpoint rendering must also work for IPv6-only subnets, whose IPv4 CIDR is absent.
// Use the IPv6 API field rather than placing an IPv6 value in the IPv4 CidrBlock field.
String vpcId = createVpc("10.78.0.0/16");
String ipv6Block = associateIpv6Block(vpcId);
String subnetId = ec2Value("CreateSubnet", "CreateSubnetResponse.subnet.subnetId",
"VpcId", vpcId, "CidrBlock", "2001:db8::/56",
"VpcId", vpcId, "Ipv6CidrBlock", ipv6SubnetCidr(ipv6Block, 0), "Ipv6Native", "true",
"AvailabilityZone", "us-east-1a");
String endpointId = ec2Value("CreateVpcEndpoint",
@@ -193,11 +200,12 @@ class Ec2VpcEndpointNetworkInterfaceIdsIntegrationTest {
// Caught in review, not by the test above, which uses a single subnet and so
// cannot see a collision at all.
String vpcId = createVpc("10.79.0.0/16");
String ipv6Block = associateIpv6Block(vpcId);
String subnetA = ec2Value("CreateSubnet", "CreateSubnetResponse.subnet.subnetId",
"VpcId", vpcId, "CidrBlock", "2001:db8:1::/64",
"VpcId", vpcId, "Ipv6CidrBlock", ipv6SubnetCidr(ipv6Block, 0), "Ipv6Native", "true",
"AvailabilityZone", "us-east-1a");
String subnetB = ec2Value("CreateSubnet", "CreateSubnetResponse.subnet.subnetId",
"VpcId", vpcId, "CidrBlock", "2001:db8:2::/64",
"VpcId", vpcId, "Ipv6CidrBlock", ipv6SubnetCidr(ipv6Block, 1), "Ipv6Native", "true",
"AvailabilityZone", "us-east-1b");
String endpointId = ec2Value("CreateVpcEndpoint",