Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching (#811)

been-there-done-that4b6b99c1e58a
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 16ce8b08d6..4d74521948 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
@@ -1180,7 +1180,7 @@ public class ApiGatewayExecuteController {
* Finds the best-matching resource for {@code requestPath}.
* Priority: exact match > template path match (e.g. /items/{id}) > proxy+ wildcard.
*/
- private ApiGatewayResource matchResource(List<ApiGatewayResource> resources, String requestPath) {
+ ApiGatewayResource matchResource(List<ApiGatewayResource> resources, String requestPath) {
// 1. Exact match
for (ApiGatewayResource r : resources) {
if (requestPath.equals(r.getPath())) {
@@ -1195,13 +1195,30 @@ public class ApiGatewayExecuteController {
}
}
}
- // 3. Proxy+ wildcard — {proxy+} matches any remaining path
+ // 3. Proxy+ wildcard — {proxy+} matches longest parent prefix
+ // Requires at least one path segment after the parent prefix (except root /{proxy+})
+ ApiGatewayResource best = null;
+ int bestLen = -1;
for (ApiGatewayResource r : resources) {
- if (r.getPathPart() != null && r.getPathPart().contains("{")) {
- return r;
+ if (r.getPath() == null || !r.getPath().contains("{proxy+}")) continue;
+ String parentPrefix = r.getPath().substring(0, r.getPath().indexOf("{proxy+}"));
+ // Root /{proxy+} matches everything including /
+ if ("/".equals(parentPrefix)) {
+ if (best == null) {
+ best = r;
+ bestLen = 0;
+ }
+ continue;
+ }
+ // Non-root proxy+ requires at least one char after the prefix
+ if (requestPath.startsWith(parentPrefix)
+ && requestPath.length() > parentPrefix.length()
+ && parentPrefix.length() > bestLen) {
+ best = r;
+ bestLen = parentPrefix.length();
}
}
- return null;
+ return best;
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayProxyMatchTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayProxyMatchTest.java
new file mode 100644
index 0000000000..4420c9a35e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayProxyMatchTest.java
@@ -0,0 +1,127 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import io.github.hectorvent.floci.services.apigateway.model.ApiGatewayResource;
+import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
+import io.github.hectorvent.floci.services.apigatewayv2.websocket.WebSocketConnectionManager;
+import io.github.hectorvent.floci.services.lambda.LambdaService;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@ExtendWith(MockitoExtension.class)
+class ApiGatewayProxyMatchTest {
+
+ @Mock ApiGatewayService apiGatewayService;
+ @Mock ApiGatewayV2Service apiGatewayV2Service;
+ @Mock LambdaService lambdaService;
+ @Mock VtlTemplateEngine vtlEngine;
+ @Mock AwsServiceRouter serviceRouter;
+ @Mock WebSocketConnectionManager webSocketConnectionManager;
+
+ private ApiGatewayExecuteController ctrl;
+
+ @BeforeEach
+ void setUp() {
+ ctrl = new ApiGatewayExecuteController(apiGatewayService, apiGatewayV2Service, lambdaService,
+ new RegionResolver("us-east-1", "000000000000"),
+ new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager);
+ }
+
+ private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
+ ApiGatewayResource r = new ApiGatewayResource();
+ r.setId(id);
+ r.setParentId(parentId);
+ r.setPathPart(pathPart);
+ r.setPath(path);
+ return r;
+ }
+
+ @Test
+ void rootProxyMatchesEverything() {
+ ApiGatewayResource rootProxy = resource("r1", "root", "{proxy+}", "/{proxy+}");
+
+ assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/anything"));
+ assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/"));
+ assertSame(rootProxy, ctrl.matchResource(List.of(rootProxy), "/a/b/c"));
+ }
+
+ @Test
+ void siblingProxyRoutesToCorrectParent() {
+ ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
+ ApiGatewayResource productsProxy = resource("r2", "root", "{proxy+}", "/products/{proxy+}");
+ List<ApiGatewayResource> resources = List.of(authProxy, productsProxy);
+
+ assertSame(authProxy, ctrl.matchResource(resources, "/auth/login"));
+ assertSame(productsProxy, ctrl.matchResource(resources, "/products/123"));
+ assertSame(productsProxy, ctrl.matchResource(resources, "/products/abc/def"));
+ assertNull(ctrl.matchResource(resources, "/other"));
+ }
+
+ @Test
+ void emptyProxySegmentDoesNotMatchNonRootProxy() {
+ ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
+ ApiGatewayResource productsProxy = resource("r2", "root", "{proxy+}", "/products/{proxy+}");
+
+ assertNull(ctrl.matchResource(List.of(authProxy), "/auth/"));
+ assertNull(ctrl.matchResource(List.of(authProxy, productsProxy), "/auth/"));
+ assertNull(ctrl.matchResource(List.of(authProxy, productsProxy), "/products/"));
+ }
+
+ @Test
+ void rootProxyFallbackWhenNoSpecificMatch() {
+ ApiGatewayResource rootProxy = resource("r0", "root", "{proxy+}", "/{proxy+}");
+ ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
+ List<ApiGatewayResource> resources = List.of(rootProxy, authProxy);
+
+ assertSame(authProxy, ctrl.matchResource(resources, "/auth/login"));
+ assertSame(rootProxy, ctrl.matchResource(resources, "/other"));
+ assertSame(rootProxy, ctrl.matchResource(resources, "/"));
+ }
+
+ @Test
+ void exactMatchTakesPriorityOverProxy() {
+ ApiGatewayResource exact = resource("r1", "root", "login", "/auth/login");
+ ApiGatewayResource authProxy = resource("r2", "root", "{proxy+}", "/auth/{proxy+}");
+ List<ApiGatewayResource> resources = List.of(authProxy, exact);
+
+ assertSame(exact, ctrl.matchResource(resources, "/auth/login"));
+ assertSame(authProxy, ctrl.matchResource(resources, "/auth/register"));
+ }
+
+ @Test
+ void templatePathTakesPriorityOverProxy() {
+ ApiGatewayResource itemDetail = resource("r1", "root", "{id}", "/items/{id}");
+ ApiGatewayResource itemsProxy = resource("r2", "root", "{proxy+}", "/items/{proxy+}");
+ List<ApiGatewayResource> resources = List.of(itemsProxy, itemDetail);
+
+ assertSame(itemDetail, ctrl.matchResource(resources, "/items/123"));
+ assertSame(itemsProxy, ctrl.matchResource(resources, "/items/123/sub"));
+ }
+
+ @Test
+ void longestParentPrefixWins() {
+ ApiGatewayResource apiProxy = resource("r1", "root", "{proxy+}", "/api/{proxy+}");
+ ApiGatewayResource apiV1Proxy = resource("r2", "root", "{proxy+}", "/api/v1/{proxy+}");
+ List<ApiGatewayResource> resources = List.of(apiV1Proxy, apiProxy);
+
+ assertSame(apiV1Proxy, ctrl.matchResource(resources, "/api/v1/users"));
+ assertSame(apiProxy, ctrl.matchResource(resources, "/api/v2"));
+ assertNull(ctrl.matchResource(resources, "/api/"));
+ }
+
+ @Test
+ void noMatchReturnsNull() {
+ ApiGatewayResource authProxy = resource("r1", "root", "{proxy+}", "/auth/{proxy+}");
+
+ assertNull(ctrl.matchResource(List.of(authProxy), "/"));
+ assertNull(ctrl.matchResource(List.of(authProxy), "/other/path"));
+ }
+}