From 4b6b99c1e58ac98c3acfa6cfdcbcd43e934a2c96 Mon Sep 17 00:00:00 2001 From: been-there-done-that <57761677+been-there-done-that@users.noreply.github.com> Date: Wed, 13 May 2026 06:49:46 +0530 Subject: [PATCH] fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching (#811) * fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching When multiple sibling {proxy+} resources exist (e.g. /auth/{proxy+} and /products/{proxy+}), the first one registered always matched every request regardless of the parent path. Now the proxy+ with the longest parent prefix matching the request path is selected, matching real AWS behaviour. Closes #792 * test(api-gateway): add unit tests for proxy+ path matching Add 7 focused tests for the matchResource method covering root proxy+, sibling proxy+ routing, fallback behaviour, exact/template priority, longest parent prefix, and no-match cases. * fix(api-gateway): reject empty proxy+ segments and use cleaner path extraction Adopt reviewer's suggestion: use substring before {proxy+} for parent prefix extraction instead of lastIndexOf('/'), and require at least one path segment after the prefix for non-root proxy+ resources. Root /{proxy+} still matches / as a special case. Adds tests for empty proxy segment edge case (/auth/ should not match /auth/{proxy+}) and updates longestParentPrefixWins assertion for /api/ (should be null, not match). --- .../ApiGatewayExecuteController.java | 27 +++- .../apigateway/ApiGatewayProxyMatchTest.java | 127 ++++++++++++++++++ 2 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayProxyMatchTest.java 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 resources, String requestPath) { + ApiGatewayResource matchResource(List 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 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 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 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 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 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")); + } +}