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

+8
View File
@@ -286,6 +286,7 @@ These management-plane operations have no handler in v1. Calls will return `404`
The execute plane (actual proxied HTTP traffic via `/restapis/{id}/{stage}/_user_request_/…`)
is implemented separately and is not counted as management-plane operations. A deployed REST
API is also available at `http://{apiId}.execute-api.localhost.floci.io:4566/{stage}/{path}`
(or with the region label, `{apiId}.execute-api.{region}.localhost.floci.io`)
and `/execute-api/{apiId}/{stage}/{path}`. All three forms use the same method authorization
and mappings. It supports these integration types; others return an error:
@@ -482,6 +483,13 @@ When an API has a `$default` stage, callers may omit the stage segment:
curl http://{apiId}.execute-api.localhost.floci.io:4566/{path}
```
The region-bearing form a client derives from a stage's invoke URL is accepted on the
same domain, as it is on `localhost` and the configured `FLOCI_HOSTNAME`:
```bash
curl http://{apiId}.execute-api.{region}.localhost.floci.io:4566/{stageName}/{path}
```
APIs created or updated with `disableExecuteApiEndpoint` reject requests to
this default hostname with `404 Not Found`, matching AWS HTTP API behavior.
@@ -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 @}).
@@ -199,6 +199,104 @@ class ApiGatewayExecuteApiHostFilterTest {
assertEquals("ap-northeast-2", routeContext.httpApiRegion());
}
/**
* The regionless built-in suffix is a convenience form; the region-bearing one is what a
* client derives from a stage's invoke URL. Unclaimed, the request fell through to S3's path
* routes and answered NoSuchBucket for the stage segment.
*/
@Test
void routesRegionBearingBuiltInSuffixHost() {
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
lookup.addApi("ap-northeast-2", API_ID, "HTTP");
lookup.addStage("ap-northeast-2", API_ID, "$default");
RecordingRequest request = new RecordingRequest(
"abc123.execute-api.ap-northeast-2.localhost.floci.io:4566",
URI.create("http://abc123.execute-api.ap-northeast-2.localhost.floci.io:4566/accounts"));
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
new ApiGatewayExecuteApiHostFilter(
lookup, new RegionResolver(REGION, "000000000000"), routeContext)
.filter(request.context());
assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath());
assertEquals("ap-northeast-2", routeContext.httpApiRegion());
}
@Test
void routesRestApiOnRegionBearingBuiltInSuffixHost() {
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
RecordingRequest request = new RecordingRequest(
"abc123.execute-api.us-east-1.localhost.floci.io:4566",
URI.create("http://abc123.execute-api.us-east-1.localhost.floci.io:4566/prod/users"));
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
new ApiGatewayExecuteApiHostFilter(
lookup, new RegionResolver(REGION, "000000000000"), routeContext)
.filter(request.context());
assertEquals("/execute-api/abc123/prod/users", request.routedUri().getRawPath());
assertNull(routeContext.httpApiRegion());
}
@Test
void extractsApiIdFromRegionBearingBuiltInSuffixHosts() {
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.us-east-1.localhost.floci.io:4566", null));
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.us-east-1.localhost.localstack.cloud", null));
assertNull(ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.us-east-1.example.test", null));
}
/**
* A region label is any region a partition recognizes, not a two-letter geo prefix: the EUSC
* partition's {@code eusc-de-east-1} was rejected before the suffix was even looked at, on
* every tail.
*/
@Test
void extractsApiIdFromEuscRegionHosts() {
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566", null));
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.eusc-de-east-1.localhost:4566", null));
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.eusc-de-east-1.amazonaws.eu", null));
}
/**
* A region launched after the vendored partition data was generated matches its partition's
* region regex without being in the published list; signed requests already accept it.
*/
@Test
void extractsApiIdFromRegionNotYetInTheCatalog() {
assertEquals("abc123", ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.eu-south-9.localhost.floci.io", null));
}
@Test
void ignoresRegionShapedLabelThatIsNotAKnownRegion() {
assertNull(ApiGatewayExecuteApiHostFilter.extractApiId(
"abc123.execute-api.zz-nowhere-9.localhost.floci.io", null));
}
@Test
void routesHttpApiOnEuscRegionBuiltInSuffixHost() {
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();
lookup.addApi("eusc-de-east-1", API_ID, "HTTP");
lookup.addStage("eusc-de-east-1", API_ID, "$default");
RecordingRequest request = new RecordingRequest(
"abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566",
URI.create("http://abc123.execute-api.eusc-de-east-1.localhost.floci.io:4566/accounts"));
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
new ApiGatewayExecuteApiHostFilter(
lookup, new RegionResolver(REGION, "000000000000"), routeContext)
.filter(request.context());
assertEquals("/execute-api/abc123/$default/accounts", request.routedUri().getRawPath());
assertEquals("eusc-de-east-1", routeContext.httpApiRegion());
}
@Test
void routesRegionBearingConfiguredHost() {
FakeApiGatewayLookup lookup = new FakeApiGatewayLookup();