Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(apigateway): capture HTTP proxy greedy suffixes (#4980)

Abhigya12e9cbd78b53
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md
index 2f8ad31bca..33201414bc 100644
--- a/docs/services/api-gateway.md
+++ b/docs/services/api-gateway.md
@@ -303,6 +303,12 @@ A `MOCK` integration renders its request template and uses the `statusCode` it p
`HTTP_PROXY` forwards the request to the integration's `uri` — with `{param}` placeholders resolved from the matched resource's path parameters — and relays the backend's status, headers and body unchanged. Per AWS, no request templates and no integration-response selection apply to `HTTP_PROXY`, so a backend `4xx`/`5xx` reaches the caller verbatim rather than being remapped. `integration.request.{header,querystring,path}.*` → `method.request.*` mappings are applied. Hop-by-hop headers (including `Host`) are stripped. An unreachable or failing backend yields `502`.
+A greedy resource captures only the path segments after its parent. For example,
+`/api/v1/auth/signup/{proxy+}` captures `checkpoint/status` from
+`/api/v1/auth/signup/checkpoint/status`, so an integration URI ending in
+`/api/v1/auth/signup/{proxy}` forwards the prefix once. Greedy parameter names other
+than `proxy` are supported, including in `integration.request.path.*` mappings.
+
A backend response body larger than the 10 MB API Gateway payload quota yields `413` with `{"message":"Request Entity Too Large"}`. The same limit applies to HTTP API `HTTP_PROXY` integrations.
Passthrough keeps repeated values repeated, in both directions: `?tag=a&tag=b` reaches the backend as two `tag` parameters rather than one `tag=a,b`, a header sent twice arrives twice, and a backend that returns two `Set-Cookie` headers relays two to the caller. Comma-joining them would not be reversible, since a cookie's `Expires` attribute contains a comma of its own. An explicit `integration.request.header.X` or `integration.request.querystring.X` mapping overwrites, so it replaces any repeated inbound values with the single mapped one.
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
index 035c5b99b9..08d7504729 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
@@ -467,7 +467,7 @@ public class ApiGatewayExecuteController {
iamIdentity);
case "AWS" -> invokeAwsIntegration(scope, region, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body, authorizerResult);
- case "HTTP_PROXY" -> invokeHttpProxy(scope, apiId, httpMethod, path, proxy, stageName,
+ case "HTTP_PROXY" -> invokeHttpProxy(scope, apiId, httpMethod, path, stageName,
matched, integration, headers, uriInfo, body, iamIdentity);
case "HTTP" -> invokeHttpIntegration(scope, region, apiId, httpMethod, path, proxy, stageName,
matched, integration, headers, uriInfo, body, authorizerResult);
@@ -542,7 +542,7 @@ public class ApiGatewayExecuteController {
* {@code integration.request.*} parameter mapping applies on the way out.
*/
private Response invokeHttpProxy(GatewayResponseScope scope, String apiId, String httpMethod, String path,
- String proxy, String stageName, ApiGatewayResource resource,
+ String stageName, ApiGatewayResource resource,
Integration integration, HttpHeaders headers,
UriInfo uriInfo, byte[] body,
ExecuteApiSigV4Authorizer.CallerIdentity iamIdentity) {
@@ -570,8 +570,8 @@ public class ApiGatewayExecuteController {
queryMap.put(e.getKey(), String.join(",", e.getValue()));
}
Map<String, String> pathMap = new LinkedHashMap<>();
- if (proxy != null && !proxy.isEmpty()) pathMap.put("proxy", proxy);
pathMap.putAll(extractPathParams(resource.getPath(), path));
+ pathMap.putAll(greedyPathParam(resource.getPath(), path));
// integration.request.{header,querystring,path}.X ← method.request.*, applied here rather
// than by the v2 RequestParameterMapper: that mapper reads the unrelated v2 syntax
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java
index 5512e16bb5..3a3158e6fd 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpProxyIntegrationTest.java
@@ -10,6 +10,8 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import java.io.IOException;
import java.net.InetSocketAddress;
@@ -129,6 +131,11 @@ class ApiGatewayHttpProxyIntegrationTest {
private String createProxyApi(String name, String targetUri, String requestParametersJson,
String authorizationType) {
+ return createProxyApi(name, targetUri, requestParametersJson, authorizationType, "/{proxy+}", "ANY");
+ }
+
+ private String createProxyApi(String name, String targetUri, String requestParametersJson,
+ String authorizationType, String resourcePath, String httpMethod) {
String apiId = given()
.contentType(ContentType.JSON)
.body("{\"name\":\"" + name + "\"}")
@@ -140,15 +147,18 @@ class ApiGatewayHttpProxyIntegrationTest {
String rootId = given().when().get("/restapis/" + apiId + "/resources")
.then().statusCode(200).extract().path("item[0].id");
- String resourceId = given()
- .contentType(ContentType.JSON)
- .body("{\"pathPart\":\"{proxy+}\"}")
- .when().post("/restapis/" + apiId + "/resources/" + rootId)
- .then().statusCode(201).extract().path("id");
+ String resourceId = rootId;
+ for (String pathPart : resourcePath.substring(1).split("/")) {
+ resourceId = given()
+ .contentType(ContentType.JSON)
+ .body(Map.of("pathPart", pathPart))
+ .when().post("/restapis/" + apiId + "/resources/" + resourceId)
+ .then().statusCode(201).extract().path("id");
+ }
given().contentType(ContentType.JSON)
.body("{\"authorizationType\":\"" + authorizationType + "\"}")
- .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/ANY")
+ .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/" + httpMethod)
.then().statusCode(201);
String integrationBody = "{\"type\":\"HTTP_PROXY\",\"httpMethod\":\"ANY\",\"uri\":\"" + targetUri + "\""
@@ -156,17 +166,10 @@ class ApiGatewayHttpProxyIntegrationTest {
+ "}";
given().contentType(ContentType.JSON)
.body(integrationBody)
- .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/ANY/integration")
+ .when().put("/restapis/" + apiId + "/resources/" + resourceId + "/methods/" + httpMethod + "/integration")
.then().statusCode(201);
- String deploymentId = given().contentType(ContentType.JSON)
- .body("{\"description\":\"v1\"}")
- .when().post("/restapis/" + apiId + "/deployments")
- .then().statusCode(201).extract().path("id");
- given().contentType(ContentType.JSON)
- .body("{\"stageName\":\"test\",\"deploymentId\":\"" + deploymentId + "\"}")
- .when().post("/restapis/" + apiId + "/stages")
- .then().statusCode(201);
+ createStage(apiId, deploy(apiId));
return apiId;
}
@@ -228,6 +231,88 @@ class ApiGatewayHttpProxyIntegrationTest {
assertEquals("limit=5", lastQuery.get());
}
+ @ParameterizedTest
+ @CsvSource({
+ "/{proxy+}, /checkpoint/status, /checkpoint/status",
+ "/api/v1/auth/signup/{proxy+}, /api/v1/auth/signup/status, /status",
+ "/api/v1/auth/signup/{proxy+}, /api/v1/auth/signup/checkpoint/status, /checkpoint/status"
+ })
+ void mapsOnlyTheGreedySuffixIntoTheIntegrationPath(String resourcePath, String requestPath,
+ String expectedSuffix) {
+ resetRecordings();
+ String apiId = createProxyApi("http-proxy-greedy-path-api",
+ "http://127.0.0.1:" + backendPort + "/api/v1/auth/signup/{proxy}",
+ "{\"integration.request.path.proxy\":\"method.request.path.proxy\"}",
+ "NONE", resourcePath, "ANY");
+
+ given().when().get("/execute-api/" + apiId + "/test" + requestPath)
+ .then().statusCode(200);
+
+ assertEquals("GET", lastMethod.get());
+ assertEquals("/api/v1/auth/signup" + expectedSuffix, lastPath.get());
+ }
+
+ @Test
+ void substitutesNestedGreedySuffixWithoutExplicitParameterMapping() {
+ resetRecordings();
+ String apiId = createProxyApi("http-proxy-implicit-greedy-api",
+ "http://127.0.0.1:" + backendPort + "/files/{proxy}", null,
+ "NONE", "/files/{proxy+}", "ANY");
+
+ given().when().get("/execute-api/" + apiId + "/test/files/images/logo.png")
+ .then().statusCode(200);
+
+ assertEquals("/files/images/logo.png", lastPath.get());
+ }
+
+ @Test
+ void mapsGreedyParameterWithADifferentName() {
+ resetRecordings();
+ String apiId = createProxyApi("http-proxy-named-greedy-api",
+ "http://127.0.0.1:" + backendPort + "/static/{remainder}",
+ "{\"integration.request.path.remainder\":\"method.request.path.rest\"}",
+ "NONE", "/assets/{rest+}", "ANY");
+
+ given().when().get("/execute-api/" + apiId + "/test/assets/images/logo.png")
+ .then().statusCode(200);
+
+ assertEquals("/static/images/logo.png", lastPath.get());
+ }
+
+ @Test
+ void mapsOrdinaryNamedParameters() {
+ resetRecordings();
+ String apiId = createProxyApi("http-proxy-named-path-api",
+ "http://127.0.0.1:" + backendPort + "/tenants/{tenant}/files/{file}",
+ "{\"integration.request.path.tenant\":\"method.request.path.tenantId\","
+ + "\"integration.request.path.file\":\"method.request.path.fileId\"}",
+ "NONE", "/accounts/{tenantId}/assets/{fileId}", "ANY");
+
+ given().when().get("/execute-api/" + apiId + "/test/accounts/acme/assets/logo.png")
+ .then().statusCode(200);
+
+ assertEquals("/tenants/acme/files/logo.png", lastPath.get());
+ }
+
+ @Test
+ void forwardsUnauthenticatedOptionsWithOnlyTheGreedySuffix() {
+ resetRecordings();
+ String apiId = createProxyApi("http-proxy-options-greedy-api",
+ "http://127.0.0.1:" + backendPort + "/api/v1/auth/signup/{proxy}",
+ "{\"integration.request.path.proxy\":\"method.request.path.proxy\"}",
+ "NONE", "/api/v1/auth/signup/{proxy+}", "OPTIONS");
+
+ given()
+ .header("Origin", "https://example.com")
+ .header("Access-Control-Request-Method", "GET")
+ .when().options("/execute-api/" + apiId + "/test/api/v1/auth/signup/checkpoint/status")
+ .then().statusCode(200);
+
+ assertEquals("OPTIONS", lastMethod.get());
+ assertEquals("/api/v1/auth/signup/checkpoint/status", lastPath.get());
+ assertNull(lastAuthorization.get());
+ }
+
@Test
void importsAndInvokesHttpProxyFromOpenApi() {
resetRecordings();