mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigateway): preserve trailing slash in proxy event path (#1863)
* fix(apigateway): preserve trailing slash in proxy event path
The REST proxy event path was built from the JAX-RS {proxy} path param, which strips a trailing slash, so a request to /thing/ produced event.path "/thing" instead of "/thing/". A trailing slash is significant to backends that treat /thing and /thing/ as distinct routes. Recover it from the raw request URI for the event path, requestContext.path and the proxy path parameter, while resource matching and path-parameter extraction keep using the normalized path (AWS routes /thing/ to the /thing resource but preserves the slash in the delivered event).
Fixes #1557
* fix(apigateway): preserve trailing slash in the authorizer event path
buildProxyEvent recovers the trailing slash the JAX-RS {proxy} binding
strips, but toAuthorizerEvent still used the normalized path. A CUSTOM
authorizer therefore gated /admin/special while the Lambda received
/admin/special/, so path-based access control in the authorizer and the
handler operated on different strings. Real API Gateway gives both the
raw path.
Use preserveTrailingSlash for the authorizer event's path, methodArn and
requestContext.path; path-parameter extraction still uses the normalized
path, matching buildProxyEvent. Adds an end-to-end regression asserting
the authorizer event path keeps the slash while pathParameters do not.
* fix(apigateway): keep proxy path param and methodArn normalized
Review feedback: pathParameters come from the matcher, which runs on the
normalized path, so the greedy {proxy+} value carries no trailing slash on
real AWS even when event.path keeps one. Independent reports of the AWS
behaviour agree that event.path preserves the slash while
pathParameters.proxy does not.
methodArn is matched against IAM-style policy resources, where a stray
trailing slash would silently fail wildcards an authorizer returns. No AWS
behaviour was found pinning it either way, so it keeps the normalized path.
event.path and requestContext.path still preserve the slash, which is the
part of the original fix the evidence supports.
This commit is contained in:
1 parent
1150cfc657
commit
42363886d5
3 files changed
+96
-5
No files matched your search
+40
-5
@@ -511,15 +511,23 @@ public class ApiGatewayExecuteController {
|
||||
String httpMethod, String requestPath,
|
||||
String resourcePath, String resourceId, Stage stage, UriInfo uriInfo,
|
||||
String resolvedApiKey) {
|
||||
// Recover the trailing slash the JAX-RS {proxy} binding strips, so the authorizer sees
|
||||
// the same raw path the Lambda later receives from buildProxyEvent (AWS parity). Path
|
||||
// matching and path-parameter extraction keep using the normalized requestPath.
|
||||
String preservedPath = preserveTrailingSlash(requestPath, uriInfo.getRequestUri().getRawPath());
|
||||
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("type", auth.getType());
|
||||
// methodArn keeps the normalized path: it is matched against IAM-style policy resources,
|
||||
// where a stray trailing slash would silently fail wildcards an authorizer already returns.
|
||||
// No AWS behavior was found pinning it either way, so the conservative form wins.
|
||||
node.put("methodArn", buildMethodArn(region, apiId, stageName, httpMethod, requestPath));
|
||||
if ("TOKEN".equals(auth.getType())) {
|
||||
String headerName = auth.getIdentitySource().replace("method.request.header.", "");
|
||||
node.put("authorizationToken", headers.getHeaderString(headerName));
|
||||
} else if ("REQUEST".equals(auth.getType())) {
|
||||
node.put("resource", resourcePath);
|
||||
node.put("path", requestPath);
|
||||
node.put("path", preservedPath);
|
||||
node.put("httpMethod", httpMethod);
|
||||
putSingleValueHeaders(node, headers);
|
||||
putMultiValueHeaders(node, headers);
|
||||
@@ -546,7 +554,7 @@ public class ApiGatewayExecuteController {
|
||||
ctx.put("apiId", apiId);
|
||||
ctx.put("resourceId", resourceId != null ? resourceId : "");
|
||||
ctx.put("resourcePath", resourcePath);
|
||||
ctx.put("path", requestPath);
|
||||
ctx.put("path", preservedPath);
|
||||
ctx.put("httpMethod", httpMethod);
|
||||
ctx.put("stage", stageName);
|
||||
ctx.put("requestId", UUID.randomUUID().toString());
|
||||
@@ -616,9 +624,15 @@ public class ApiGatewayExecuteController {
|
||||
byte[] body, String requestId,
|
||||
String principalId, Map<String, Object> authorizerContext,
|
||||
String resolvedApiKey) {
|
||||
// The JAX-RS {proxy} binding strips a trailing slash, but a trailing slash is
|
||||
// significant in the delivered path (routers treat /x and /x/ as distinct routes).
|
||||
// Recover it from the raw request URI for the event path fields. Resource matching
|
||||
// and path-parameter extraction continue to use the normalized `path`.
|
||||
String requestPath = preserveTrailingSlash(path, uriInfo.getRequestUri().getRawPath());
|
||||
|
||||
ObjectNode event = objectMapper.createObjectNode();
|
||||
event.put("resource", resourcePath);
|
||||
event.put("path", path);
|
||||
event.put("path", requestPath);
|
||||
event.put("httpMethod", httpMethod);
|
||||
|
||||
putSingleValueHeaders(event, headers);
|
||||
@@ -626,8 +640,12 @@ public class ApiGatewayExecuteController {
|
||||
putQueryStringParameters(event, uriInfo);
|
||||
putMultiValueQueryStringParameters(event, uriInfo);
|
||||
|
||||
// pathParameters come from the matcher, which ran on the normalized path, so the greedy
|
||||
// {proxy+} value has no trailing slash on real AWS even when event.path keeps one.
|
||||
ObjectNode pathParams = event.putObject("pathParameters");
|
||||
if (proxy != null && !proxy.isEmpty()) pathParams.put("proxy", proxy);
|
||||
if (proxy != null && !proxy.isEmpty()) {
|
||||
pathParams.put("proxy", proxy);
|
||||
}
|
||||
extractPathParams(resourcePath, path).forEach(pathParams::put);
|
||||
|
||||
// stageVariables: populate from the Stage object (null if no variables configured)
|
||||
@@ -653,7 +671,7 @@ public class ApiGatewayExecuteController {
|
||||
ctx.put("domainPrefix", apiId);
|
||||
ctx.put("extendedRequestId", requestId);
|
||||
ctx.put("httpMethod", httpMethod);
|
||||
ctx.put("path", path);
|
||||
ctx.put("path", requestPath);
|
||||
ctx.put("protocol", "HTTP/1.1");
|
||||
ctx.put("requestId", requestId);
|
||||
ctx.put("requestTime", requestTime);
|
||||
@@ -1938,6 +1956,23 @@ public class ApiGatewayExecuteController {
|
||||
|
||||
// ──────────────────────────── Path matching ────────────────────────────
|
||||
|
||||
/**
|
||||
* Re-appends a trailing slash that the JAX-RS {@code {proxy}} path-param binding strips.
|
||||
* A trailing slash is significant in the proxy event path (many routers treat {@code /x}
|
||||
* and {@code /x/} as distinct routes), so it is recovered from the raw request URI. The
|
||||
* normalized path is still used for resource matching and path-parameter extraction, which
|
||||
* mirrors AWS routing {@code /x/} to the {@code /x} resource while keeping the slash in the
|
||||
* delivered event. Returns {@code normalizedPath} unchanged for the root path or when the
|
||||
* raw request had no trailing slash.
|
||||
*/
|
||||
static String preserveTrailingSlash(String normalizedPath, String rawRequestPath) {
|
||||
if (!"/".equals(normalizedPath) && !normalizedPath.endsWith("/")
|
||||
&& rawRequestPath != null && rawRequestPath.endsWith("/")) {
|
||||
return normalizedPath + "/";
|
||||
}
|
||||
return normalizedPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all matching resources for {@code requestPath}, sorted by specificity.
|
||||
* Priority: exact match > template path match (e.g. /items/{id}) > proxy+ wildcard.
|
||||
|
||||
+27
@@ -154,6 +154,33 @@ class ApiGatewayProxyMatchTest {
|
||||
assertSame(rootProxy, matched.get(2));
|
||||
}
|
||||
|
||||
// ──────────────────────────── trailing-slash preservation (#1557) ────────────────────────────
|
||||
|
||||
@Test
|
||||
void preservesTrailingSlashFromRawRequestPath() {
|
||||
assertEquals("/thing/", ApiGatewayExecuteController.preserveTrailingSlash(
|
||||
"/thing", "/restapis/abc/prod/_user_request_/thing/"));
|
||||
assertEquals("/a/b/", ApiGatewayExecuteController.preserveTrailingSlash(
|
||||
"/a/b", "/restapis/abc/prod/_user_request_/a/b/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesPathUnchangedWhenRawRequestHasNoTrailingSlash() {
|
||||
assertEquals("/thing", ApiGatewayExecuteController.preserveTrailingSlash(
|
||||
"/thing", "/restapis/abc/prod/_user_request_/thing"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotDuplicateSlashOrAlterRootOrHandleNullRaw() {
|
||||
// Root path is never turned into "//".
|
||||
assertEquals("/", ApiGatewayExecuteController.preserveTrailingSlash(
|
||||
"/", "/restapis/abc/prod/_user_request_/"));
|
||||
// An already-normalized path that ends in "/" is not doubled.
|
||||
assertEquals("/thing/", ApiGatewayExecuteController.preserveTrailingSlash("/thing/", "/x/thing/"));
|
||||
// Null raw path is tolerated.
|
||||
assertEquals("/thing", ApiGatewayExecuteController.preserveTrailingSlash("/thing", null));
|
||||
}
|
||||
|
||||
// ──────────────────────────── ELB_LISTENER_ARN ────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
+29
@@ -307,6 +307,35 @@ class ApiGatewayRequestAuthorizerIntegrationTest {
|
||||
"multiValueQueryStringParameters must be null when no query string is present");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void requestAuthorizerReceivesRawPathWithTrailingSlash() throws Exception {
|
||||
// The JAX-RS {proxy} binding strips a trailing slash, but the authorizer event must
|
||||
// still carry the raw path so path-based access control gates the same value the Lambda
|
||||
// later receives from buildProxyEvent (AWS parity). Path parameters keep coming from the
|
||||
// normalized path, exactly as the AWS_PROXY event does.
|
||||
String response = given()
|
||||
.header("Authorization", "Bearer test")
|
||||
.when().get("/execute-api/" + apiId + "/prod/items/42/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().asString();
|
||||
|
||||
JsonNode payload = OBJECT_MAPPER.readTree(response);
|
||||
String receivedEventStr = payload.path("authorizer").path("receivedEvent").asText(null);
|
||||
assertNotNull(receivedEventStr);
|
||||
|
||||
JsonNode event = OBJECT_MAPPER.readTree(receivedEventStr);
|
||||
assertEquals("/items/42/", event.path("path").asText(null),
|
||||
"authorizer event path must preserve the trailing slash");
|
||||
assertEquals("/items/42/", event.path("requestContext").path("path").asText(null),
|
||||
"requestContext.path must preserve the trailing slash");
|
||||
assertTrue(event.path("methodArn").asText("").endsWith("/items/42"),
|
||||
"methodArn keeps the normalized path, since it is matched against policy resources");
|
||||
assertEquals("42", event.path("pathParameters").path("id").asText(null),
|
||||
"path parameters must still be extracted from the normalized path");
|
||||
}
|
||||
|
||||
// ──────────────────────────── TOKEN authorizer preservation ────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user