fix(apigateway): accept a region label before the built-in execute-api suffixes (#4705)

* fix(apigateway): accept a region label before the built-in execute-api suffixes

extractApiId accepted a region label only before localhost, the configured hostname, or an AWS DNS suffix, so <apiId>.execute-api.<region>.localhost.floci.io (the shape a client derives from a stage's invoke URL) was never claimed and fell through to S3's path routes, answering NoSuchBucket for the stage segment. Accept the built-in localhost.floci.io and localhost.localstack.cloud suffixes after a region label too, for REST and HTTP APIs alike.

Closes #4701

* fix(apigateway): accept every published region label on execute-api hosts

extractApiId validated the region label with a two-letter-geo pattern, so eusc-de-east-1 was rejected before the suffix was checked and the request fell through to S3, on every tail including amazonaws.eu. Check the label against the published region ids with AwsRegions.isRegionId, the same test RegionResolver.resolveRegionFromHost applies, so the hosts the filter claims match the regions it can resolve.

* fix(apigateway): accept execute-api region labels not yet in the partition catalog

isRegionId accepts only regions in the vendored published list, so a region launched after that data was generated was rejected on its execute-api host even though signed requests accept it. Use RegionResolver.isKnownRegion, which falls back to each partition's botocore region regex; an unrecognized label such as zz-nowhere-9 is still rejected.
This commit is contained in:
danielxoi authored and GitHub committed 2026-10-01 12:31:35 -04:00
1 parent b2dd5665db
commit 5000bcd041
3 files changed
+122 -7

No files matched your search

@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
@@ -34,8 +35,6 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
private static final Logger LOG = Logger.getLogger(ApiGatewayExecuteApiHostFilter.class);
private static final Pattern EXECUTE_API_PREFIX = Pattern.compile("^([a-z0-9-]+)\\.execute-api\\.(.+)$",
Pattern.CASE_INSENSITIVE);
private static final Pattern AWS_REGION = Pattern.compile("^[a-z]{2}-[a-z-]+-\\d+$",
Pattern.CASE_INSENSITIVE);
private final ApiGatewayLookup apiGatewayLookup;
private final RegionResolver regionResolver;
@@ -241,8 +240,9 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
/**
* Extracts the {@code apiId} from an execute-api virtual host, or {@code null} when the host
* is not an execute-api host. Region-bearing hosts accept the configured Floci hostname, the
* local {@code localhost} form, and AWS's {@code amazonaws.com} form. The public built-in
* suffixes remain regionless convenience forms.
* local {@code localhost} form, the public built-in suffixes, and AWS's
* {@code amazonaws.com} form. The built-in suffixes are also accepted regionless, as a
* convenience form.
*/
public static String extractApiId(String host, String baseHostname) {
if (host == null) {
@@ -255,18 +255,18 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
}
String tail = matcher.group(2);
if ("localhost.floci.io".equalsIgnoreCase(tail)
|| "localhost.localstack.cloud".equalsIgnoreCase(tail)) {
if (isBuiltInSuffix(tail)) {
return matcher.group(1).toLowerCase(Locale.ROOT);
}
int firstDot = tail.indexOf('.');
if (firstDot <= 0 || !AWS_REGION.matcher(tail.substring(0, firstDot)).matches()) {
if (firstDot <= 0 || !RegionResolver.isKnownRegion(tail.substring(0, firstDot))) {
return null;
}
String endpointHost = tail.substring(firstDot + 1);
if ("localhost".equalsIgnoreCase(endpointHost)
|| isBuiltInSuffix(endpointHost)
|| AwsPartitions.isDnsSuffix(endpointHost)
|| (baseHostname != null && baseHostname.equalsIgnoreCase(endpointHost))) {
return matcher.group(1).toLowerCase(Locale.ROOT);
@@ -274,6 +274,15 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
return null;
}
private static boolean isBuiltInSuffix(String host) {
for (String suffix : EmbeddedDnsServer.BUILTIN_SUFFIXES) {
if (suffix.equalsIgnoreCase(host)) {
return true;
}
}
return false;
}
/**
* True when the path is a WebSocket {@code @connections} management route,
* {@code /{stage}/@connections[/{connectionId}]} (raw or percent-encoded {@code @}).