mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): mark custom-domain routed requests so S3 virtual-host does not claim them (#4966)
A custom domain under the endpoint suffix (e.g. api.mycorp.localhost) was rewritten to /execute-api/... by ApiGatewayCustomDomainFilter, then S3VirtualHostFilter read the host as the dotted bucket "api.mycorp" and prepended it to the path, so S3 answered NoSuchBucket. ApiGatewayCustomDomainFilter now sets a server-side ROUTED_PROPERTY marker when it rewrites, and S3VirtualHostFilter skips marked requests, the same idiom already used for CloudFront distributions and Cognito custom domains.
This commit is contained in:
1 parent
b679fe2312
commit
832bd97966
5 files changed
+75
-4
No files matched your search
+4
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+15
@@ -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(),
|
||||
|
||||
+31
@@ -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)));
|
||||
|
||||
@@ -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=");
|
||||
|
||||
Reference in new issue
Block a user