Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(apigateway): mark custom-domain routed requests so S3 virtual-host does not claim them (#4966)

Allen Sanborn832bd9796689
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java
index ed2304ab01..5fc8797e43 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilter.java
@@ -33,6 +33,9 @@ import java.net.URI;
@ApplicationScoped
public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
+ /** Set on requests this filter rewrote, so later Host filters do not claim them again. */
+ public static final String ROUTED_PROPERTY = ApiGatewayCustomDomainFilter.class.getName() + ".routed";
+
private static final Logger LOG = Logger.getLogger(ApiGatewayCustomDomainFilter.class);
private static final String REGIONAL_SUFFIX = ".regional.local";
@@ -120,6 +123,7 @@ public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
routeContext.routeToRestApi();
}
routeContext.recordSignedRequestPath(path);
+ requestContext.setProperty(ROUTED_PROPERTY, Boolean.TRUE);
requestContext.setRequestUri(newUri);
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
index 59bbf59a62..ca1e0293ef 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
+import io.github.hectorvent.floci.services.apigateway.ApiGatewayCustomDomainFilter;
import io.github.hectorvent.floci.services.cloudfront.CloudFrontDistributionFilter;
import io.github.hectorvent.floci.services.cognito.CognitoCustomDomainFilter;
import jakarta.enterprise.context.ApplicationScoped;
@@ -160,10 +161,11 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
return;
}
- // A higher-priority Host filter (CloudFront, Cognito custom domain) may have already
- // routed this request. Use its server-side marker rather than trusting a
- // user-controlled path prefix.
- if (Boolean.TRUE.equals(requestContext.getProperty(CloudFrontDistributionFilter.ROUTED_PROPERTY))
+ // A higher-priority Host filter (API Gateway custom domain, CloudFront, Cognito custom
+ // domain) may have already routed this request. Use its server-side marker rather than
+ // trusting a user-controlled path prefix.
+ if (Boolean.TRUE.equals(requestContext.getProperty(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY))
+ || Boolean.TRUE.equals(requestContext.getProperty(CloudFrontDistributionFilter.ROUTED_PROPERTY))
|| requestContext.getProperty(CognitoCustomDomainFilter.POOL_PROPERTY) != null) {
return;
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilterTest.java
index bb6593cfa0..3174cc221f 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainFilterTest.java
@@ -9,6 +9,8 @@ import org.junit.jupiter.api.Test;
import java.lang.reflect.Proxy;
import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -36,6 +38,7 @@ class ApiGatewayCustomDomainFilterTest {
assertEquals("/execute-api/" + API_ID + "/prod/iam", request.routedUri().getRawPath());
assertEquals("tenant=alpha", request.routedUri().getRawQuery());
assertEquals("/v1/iam", routeContext.signedRequestPath());
+ assertEquals(Boolean.TRUE, request.property(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY));
}
@Test
@@ -50,6 +53,7 @@ class ApiGatewayCustomDomainFilterTest {
assertEquals("/execute-api/" + API_ID + "/prod/orders/42", request.routedUri().getRawPath());
assertEquals("/orders/42", routeContext.signedRequestPath());
+ assertEquals(Boolean.TRUE, request.property(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY));
}
@Test
@@ -64,6 +68,7 @@ class ApiGatewayCustomDomainFilterTest {
assertNull(request.routedUri());
assertNull(routeContext.signedRequestPath());
+ assertNull(request.property(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY));
}
@Test
@@ -81,6 +86,7 @@ class ApiGatewayCustomDomainFilterTest {
assertNull(request.routedUri());
assertNull(routeContext.signedRequestPath());
+ assertNull(request.property(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY));
}
private static ApiGatewayService serviceWithMapping(String basePath, String stage) {
@@ -105,6 +111,7 @@ class ApiGatewayCustomDomainFilterTest {
private final String host;
private final URI requestUri;
private URI routedUri;
+ private final Map<String, Object> properties = new HashMap<>();
private RecordingRequest(String host, URI requestUri) {
this.host = host;
@@ -122,6 +129,10 @@ class ApiGatewayCustomDomainFilterTest {
routedUri = (URI) args[0];
yield null;
}
+ case "setProperty" -> {
+ properties.put((String) args[0], args[1]);
+ yield null;
+ }
default -> throw new UnsupportedOperationException(method.getName());
});
}
@@ -130,6 +141,10 @@ class ApiGatewayCustomDomainFilterTest {
return routedUri;
}
+ private Object property(String name) {
+ return properties.get(name);
+ }
+
private UriInfo uriInfo() {
return (UriInfo) Proxy.newProxyInstance(
UriInfo.class.getClassLoader(),
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainIntegrationTest.java
index 54aafae5fa..6adb756dcd 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCustomDomainIntegrationTest.java
@@ -246,6 +246,37 @@ class ApiGatewayCustomDomainIntegrationTest {
}
@Test @Order(16)
+ void invokeViaDottedDomainUnderLocalhost_isNotClaimedByS3() {
+ // api.mycorp.localhost also reads as S3 virtual-hosted bucket "api.mycorp"; the
+ // custom-domain route must win.
+ given()
+ .contentType(ContentType.JSON)
+ .body("{\"domainName\":\"api.mycorp.localhost\",\"certificateArn\":\"arn:aws:acm:us-east-1:123456789012:certificate/loc\"}")
+ .when().post("/domainnames")
+ .then()
+ .statusCode(201);
+
+ given()
+ .contentType(ContentType.JSON)
+ .body("{\"basePath\":\"(none)\",\"restApiId\":\"" + apiId + "\",\"stage\":\"prod\"}")
+ .when().post("/domainnames/api.mycorp.localhost/basepathmappings")
+ .then()
+ .statusCode(201);
+
+ try {
+ given()
+ .header("Host", "api.mycorp.localhost:4566")
+ .when().get("/items")
+ .then()
+ .statusCode(200)
+ .body("message", equalTo("custom-domain-works"));
+ } finally {
+ given().when().delete("/domainnames/api.mycorp.localhost/basepathmappings/(none)");
+ given().when().delete("/domainnames/api.mycorp.localhost");
+ }
+ }
+
+ @Test @Order(17)
void cleanup() {
given().when().delete("/domainnames/api.example.com/basepathmappings/v1").then().statusCode(anyOf(is(200), is(202), is(204)));
given().when().delete("/domainnames/api.example.com/basepathmappings/(none)").then().statusCode(anyOf(is(200), is(202), is(204)));
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
index caac9fedc2..1ab96b3eca 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.s3;
+import io.github.hectorvent.floci.services.apigateway.ApiGatewayCustomDomainFilter;
import jakarta.enterprise.inject.Instance;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MultivaluedHashMap;
@@ -434,6 +435,24 @@ class S3VirtualHostFilterTest {
assertEquals("/www.example.com/index.html", rewritten.getValue().getRawPath());
}
+ @Test
+ void filterLeavesApiGatewayCustomDomainRoutedRequestAlone() {
+ // ApiGatewayCustomDomainFilter already rewrote this dotted .localhost custom domain to
+ // /execute-api/...; the host must not also be read as the dotted bucket "api.mycorp".
+ URI requestUri = URI.create("http://api.mycorp.localhost:4566/execute-api/abc123/prod/items");
+ UriInfo uriInfo = mock(UriInfo.class);
+ when(uriInfo.getRequestUri()).thenReturn(requestUri);
+ when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
+ ContainerRequestContext ctx = mock(ContainerRequestContext.class);
+ when(ctx.getUriInfo()).thenReturn(uriInfo);
+ when(ctx.getHeaderString("Host")).thenReturn("api.mycorp.localhost:4566");
+ when(ctx.getProperty(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY)).thenReturn(Boolean.TRUE);
+
+ new S3VirtualHostFilter().filter(ctx);
+
+ verify(ctx, never()).setRequestUri(any(URI.class));
+ }
+
@Test
void filterRewritesFormRequestExplicitlySignedForS3() {
URI requestUri = URI.create("http://vhost-bucket.localhost:4566/archive.zip?uploads=");