fix(ec2): reject a DescribeVpcs filter name the service does not serve (#4366)

* fix(ec2): reject a DescribeVpcs filter name the service does not serve

matchesFilters answers true for a name it does not recognise, so an
unsupported filter matched every VPC in the account rather than narrowing
anything. A caller reading that result gets a wrong answer with nothing saying
it is wrong, which is worse than an error. Real EC2 refuses the name.

VPC_FILTERS is the set DescribeVpcs documents, plus the undocumented
cidr-block alias matchesFilters already honours and real EC2 accepts.
describeVpcs now runs requireSupportedFilters, the same helper the
egress-only gateway and VPN gateway describes already use.

* fix(ec2): match the three VPC filters the check newly accepted

VPC_FILTERS admitted dhcp-options-id, owner-id and
ipv6-cidr-block-association.ipv6-pool, and matchesFilters had no arm for any of
them, so each fell to the catch-all and matched every VPC. Accepting a name the
matcher does not implement is worse than refusing it, because the filter is
then taken and silently matches everything. All three have backing data on the
model, so they are matched rather than dropped from the set.

The test asserted only a 200, which is why it did not catch this. It now
creates a VPC and checks each accepted name narrows away from it on a value
nothing can carry, and still finds it on a value it does.

The javadoc claimed one undocumented alias. There are three: cidr-block,
isDefault and tag-value.

Also imports ValidatableResponse rather than qualifying it inline.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-25 10:43:38 -04:00
1 parent a777879518
commit fde0688ef3
2 files changed
+113

No files matched your search

@@ -172,6 +172,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
private static final Set<String> VPN_GATEWAY_FILTERS = Set.of(
"amazon-side-asn", "attachment.state", "attachment.vpc-id", "availability-zone",
"state", "tag-key", "tag-value", "type", "vpn-gateway-id");
/**
* The filter names DescribeVpcs documents, plus three undocumented aliases matchesFilters
* already honours and real EC2 accepts: "cidr-block", "isDefault" and "tag-value". Being more
* permissive than the documentation is the safe direction, since the cost of refusing a name
* callers really use is higher than the cost of serving one the docs omit.
*
* <p>Every name here has a matching arm in matchesFilters. Accepting a name the matcher does
* not implement would be worse than refusing it, because the filter would then be accepted and
* silently match everything.
*/
private static final Set<String> VPC_FILTERS = Set.of(
"cidr", "cidr-block",
"cidr-block-association.association-id", "cidr-block-association.cidr-block",
"cidr-block-association.state", "dhcp-options-id",
"ipv6-cidr-block-association.association-id", "ipv6-cidr-block-association.ipv6-cidr-block",
"ipv6-cidr-block-association.ipv6-pool", "ipv6-cidr-block-association.state",
"is-default", "isDefault", "owner-id", "state", "tag-key", "tag-value", "vpc-id");
private static final Set<String> EGRESS_ONLY_INTERNET_GATEWAY_FILTERS = Set.of(
"attachment.state", "attachment.vpc-id",
"egress-only-internet-gateway-id", "tag-key", "tag-value");
@@ -4022,6 +4039,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
public List<Vpc> describeVpcs(String region, List<String> vpcIds, Map<String, List<String>> filters) {
ensureDefaultResources(region);
// matchesFilters answers true for a name it does not recognise, so without this an
// unsupported filter matches every VPC instead of narrowing anything. Real EC2 rejects the
// name outright, which is the difference between a wrong result and an error.
requireSupportedFilters(filters, VPC_FILTERS);
if (!vpcIds.isEmpty()) {
for (String id : vpcIds) {
getRequiredVpc(region, id);
@@ -8162,6 +8183,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
.anyMatch(a -> matchesValue(values, a.getCidrBlock()));
case "cidr-block-association.state" -> vpc.getCidrBlockAssociationSet().stream()
.anyMatch(a -> matchesValue(values, a.getCidrBlockState()));
case "dhcp-options-id" -> matchesValue(values, vpc.getDhcpOptionsId());
case "owner-id" -> matchesValue(values, vpc.getOwnerId());
case "ipv6-cidr-block-association.ipv6-pool" -> vpc.getIpv6CidrBlockAssociationSet()
.stream().anyMatch(a -> matchesValue(values, a.getIpv6Pool()));
case "ipv6-cidr-block-association.association-id" -> vpc.getIpv6CidrBlockAssociationSet().stream()
.anyMatch(a -> matchesValue(values, a.getAssociationId()));
case "ipv6-cidr-block-association.ipv6-cidr-block" -> vpc.getIpv6CidrBlockAssociationSet().stream()
@@ -0,0 +1,88 @@
package io.github.hectorvent.floci.services.ec2;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* DescribeVpcs rejects a filter name it does not serve.
*
* <p>{@code matchesFilters} answers true for an unrecognised name, so an unsupported filter used to
* match every VPC in the account rather than narrowing anything. A caller reading that result sees
* a wrong answer with no indication it was wrong, which is worse than an error. Real EC2 refuses
* the name.
*/
@QuarkusTest
class Ec2DescribeVpcsUnknownFilterIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260908/us-east-1/ec2/aws4_request";
private static ValidatableResponse describeVpcs(String filterName, String value) {
return given()
.formParam("Action", "DescribeVpcs")
.formParam("Filter.1.Name", filterName)
.formParam("Filter.1.Value.1", value)
.header("Authorization", AUTH_HEADER)
.when().post("/").then();
}
@Test
void anUnknownFilterNameIsRefused() {
describeVpcs("not-a-filter", "anything")
.statusCode(400)
.body(containsString("InvalidParameterValue"))
.body(containsString("not-a-filter"));
}
@Test
void theDocumentedFilterNamesAreAccepted() {
for (String name : new String[]{"cidr", "cidr-block-association.state", "dhcp-options-id",
"ipv6-cidr-block-association.ipv6-pool", "is-default", "owner-id", "state",
"tag-key", "vpc-id"}) {
describeVpcs(name, "whatever").statusCode(200);
}
}
/** The undocumented alias matchesFilters already honours, which real EC2 accepts too. */
@Test
void theCidrBlockAliasStaysAccepted() {
describeVpcs("cidr-block", "10.0.0.0/16").statusCode(200);
}
/** A tag filter names the tag in the filter name, so the prefix has to pass the check. */
@Test
void aTagFilterIsAccepted() {
describeVpcs("tag:Owner", "TeamA").statusCode(200);
}
/**
* Accepting a name the matcher does not implement is worse than refusing it, because the filter
* then matches every VPC rather than narrowing. A 200 alone does not catch that, so these check
* the result actually narrows.
*/
@Test
void everyAcceptedNameNarrowsRatherThanMatchingEverything() {
String vpcId = given()
.formParam("Action", "CreateVpc").formParam("CidrBlock", "10.91.0.0/16")
.header("Authorization", AUTH_HEADER)
.when().post("/").then().statusCode(200)
.extract().path("CreateVpcResponse.vpc.vpcId");
// Values that cannot belong to any VPC, so a working matcher returns none.
describeVpcs("dhcp-options-id", "dopt-no-such-option")
.statusCode(200).body(not(containsString(vpcId)));
describeVpcs("owner-id", "999999999999")
.statusCode(200).body(not(containsString(vpcId)));
describeVpcs("ipv6-cidr-block-association.ipv6-pool", "no-such-pool")
.statusCode(200).body(not(containsString(vpcId)));
// And the matching value still finds it.
describeVpcs("dhcp-options-id", "dopt-default")
.statusCode(200).body(containsString(vpcId));
}
}