fix(core): work around double decoding on routed hosts (#7559)

This commit is contained in:
Abhigya authored and GitHub committed 2026-11-05 04:55:16 -04:00
1 parent b235792722
commit a15dfb621e
15 files changed
+236 -23

No files matched your search

@@ -0,0 +1,24 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.UriBuilder;
import java.net.URI;
/**
* Shares S3's workaround for RESTEasy Reactive's second path decode (Floci #7556).
* Raw-path consumers must retain the intended path before applying the routing escape.
*/
public final class RequestUriRewrite {
private RequestUriRewrite() {
}
/** Escapes percent signs once more so RESTEasy stores the intended raw routing path. */
public static void apply(ContainerRequestContext requestContext, URI requestUri) {
URI routingUri = UriBuilder.fromUri(requestUri)
.replacePath(requestUri.getRawPath().replace("%", "%25"))
.build();
requestContext.setRequestUri(routingUri);
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.RequestScopes;
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import jakarta.annotation.Priority;
@@ -120,8 +121,9 @@ public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
|| !apiGatewayV2Service.hasApi(mappedRegion, restApiId)));
routeContext.routeToMappedApi(domain, restApi);
routeContext.recordSignedRequestPath(path);
routeContext.recordRewrittenRequestPath(newUri.getRawPath());
requestContext.setProperty(ROUTED_PROPERTY, Boolean.TRUE);
requestContext.setRequestUri(newUri);
RequestUriRewrite.apply(requestContext, newUri);
}
private static String stripPort(String host) {
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.RequestContext;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
import jakarta.annotation.Priority;
@@ -261,7 +262,8 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
// AWS_IAM dispatch rebuilds the caller's canonical request, which covers the path they
// signed: the virtual-host path, not the /execute-api/... form this rewrite produces.
routeContext.recordSignedRequestPath(originalPath);
requestContext.setRequestUri(newUri);
routeContext.recordRewrittenRequestPath(newUri.getRawPath());
RequestUriRewrite.apply(requestContext, newUri);
}
private void routeToRestApi(ContainerRequestContext requestContext, URI originalUri,
@@ -273,7 +275,8 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
LOG.debugv("REST execute-api host routing: {0} -> {1}", originalPath, newUri.getRawPath());
routeContext.routeToRestApi();
routeContext.recordSignedRequestPath(originalPath);
requestContext.setRequestUri(newUri);
routeContext.recordRewrittenRequestPath(newUri.getRawPath());
RequestUriRewrite.apply(requestContext, newUri);
}
private boolean stageExists(String region, String apiId, String stageName) {
@@ -660,7 +660,7 @@ public class ApiGatewayExecuteController {
pathMap.putAll(extractPathParams(resource.getPath(), path));
pathMap.putAll(greedyPathParam(resource.getPath(), path));
String rawPath = rawStagelessPath(path, uriInfo.getRequestUri().getRawPath());
String rawPath = rawStagelessPath(path, rawRequestPath(uriInfo));
Map<String, String> rawPathMap = new LinkedHashMap<>();
rawPathMap.putAll(extractPathParams(resource.getPath(), rawPath));
rawPathMap.putAll(greedyPathParam(resource.getPath(), rawPath));
@@ -969,7 +969,7 @@ public class ApiGatewayExecuteController {
if (proxy != null && !proxy.isEmpty()) pathMap.put("proxy", proxy);
pathMap.putAll(extractPathParams(resource.getPath(), path));
String rawPath = rawStagelessPath(path, uriInfo.getRequestUri().getRawPath());
String rawPath = rawStagelessPath(path, rawRequestPath(uriInfo));
Map<String, String> rawPathMap = new HashMap<>();
if (rawPath != null && pathMap.containsKey("proxy")) {
rawPathMap.put("proxy", rawPath.substring(1));
@@ -1337,7 +1337,7 @@ public class ApiGatewayExecuteController {
// 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());
String preservedPath = preserveTrailingSlash(requestPath, rawRequestPath(uriInfo));
ObjectNode node = objectMapper.createObjectNode();
node.put("type", auth.getType());
@@ -1470,7 +1470,7 @@ public class ApiGatewayExecuteController {
// 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());
String requestPath = preserveTrailingSlash(path, rawRequestPath(uriInfo));
ObjectNode event = objectMapper.createObjectNode();
event.put("resource", resourcePath);
@@ -3025,7 +3025,7 @@ public class ApiGatewayExecuteController {
queryParams.put(e.getKey(), String.join(",", e.getValue()));
}
Map<String, String> pathParams = extractV2PathParams(route.getRouteKey(), path);
String rawPath = rawStagelessPath(path, uriInfo.getRequestUri().getRawPath());
String rawPath = rawStagelessPath(path, rawRequestPath(uriInfo));
Map<String, String> rawPathParams = rawPath == null ? Map.of() : extractV2PathParams(route.getRouteKey(), rawPath);
// Reuses the claims dispatchV2 already verified via enforceJwtAuthorizer, rather than
@@ -3528,7 +3528,7 @@ public class ApiGatewayExecuteController {
// do NOT: an HTTP API has no REST-style resource template, so both are copies of the
// request path here rather than of a matched resource, and AWS's own 1.0 payload example
// shows `resource` and `path` carrying the same value.
String preservedPath = preserveTrailingSlash(path, uriInfo.getRequestUri().getRawPath());
String preservedPath = preserveTrailingSlash(path, rawRequestPath(uriInfo));
ObjectNode event = objectMapper.createObjectNode();
event.put("version", "1.0");
@@ -3580,7 +3580,7 @@ public class ApiGatewayExecuteController {
// rawPath is by contract the raw, unmodified path, so recover the trailing slash the
// JAX-RS {proxy} binding stripped. routeArn keeps the normalized path for the same reason
// methodArn does in the 1.0 shape above.
String preservedPath = preserveTrailingSlash(path, uriInfo.getRequestUri().getRawPath());
String preservedPath = preserveTrailingSlash(path, rawRequestPath(uriInfo));
ObjectNode event = objectMapper.createObjectNode();
event.put("version", "2.0");
@@ -3908,7 +3908,7 @@ public class ApiGatewayExecuteController {
// request URI for the event path fields. Route matching in dispatchV2 and the
// pathParameters extraction below continue to use the normalized `path`, mirroring what
// buildProxyEvent already does for REST (V1).
String preservedPath = preserveTrailingSlash(path, uriInfo.getRequestUri().getRawPath());
String preservedPath = preserveTrailingSlash(path, rawRequestPath(uriInfo));
ObjectNode event = objectMapper.createObjectNode();
@@ -4405,8 +4405,13 @@ public class ApiGatewayExecuteController {
return defaultStage ? path : "/" + stageName + path;
}
private static String proxyTargetPath(String normalizedPath, UriInfo uriInfo, String stageName) {
return rawTargetPath(normalizedPath, uriInfo.getRequestUri().getRawPath(), stageName);
String rawRequestPath(UriInfo uriInfo) {
String rewrittenPath = routeContext.rewrittenRequestPath();
return rewrittenPath != null ? rewrittenPath : uriInfo.getRequestUri().getRawPath();
}
private String proxyTargetPath(String normalizedPath, UriInfo uriInfo, String stageName) {
return rawTargetPath(normalizedPath, rawRequestPath(uriInfo), stageName);
}
/**
@@ -78,6 +78,7 @@ public class ApiGatewayExecuteRouteContext {
private String httpApiRegion;
private boolean restApiRoute;
private String signedRequestPath;
private String rewrittenRequestPath;
private ApiGatewayService.CustomDomainRoute mappedDomain;
void routeToMappedApi(ApiGatewayService.CustomDomainRoute domain, boolean restApi) {
@@ -122,4 +123,13 @@ public class ApiGatewayExecuteRouteContext {
String signedRequestPath() {
return signedRequestPath;
}
/** Retains the intended execute-api path before the routing workaround adds an escape layer. */
void recordRewrittenRequestPath(String rawPath) {
this.rewrittenRequestPath = rawPath;
}
String rewrittenRequestPath() {
return rewrittenRequestPath;
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.cloudfront;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
import jakarta.annotation.Priority;
import jakarta.enterprise.inject.Instance;
@@ -91,6 +92,6 @@ public class CloudFrontDistributionFilter implements ContainerRequestFilter {
LOG.debugv("CloudFront distribution routing: {0}{1} -> {2}", host, rawPath, newUri.getPath());
requestContext.setProperty(ROUTED_PROPERTY, Boolean.TRUE);
requestContext.setRequestUri(newUri);
RequestUriRewrite.apply(requestContext, newUri);
}
}
@@ -5,6 +5,7 @@ import io.github.hectorvent.floci.config.LocalStackParity;
import io.github.hectorvent.floci.core.common.AwsPartitions;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.RequestHost;
import io.github.hectorvent.floci.core.common.RequestUriRewrite;
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;
@@ -175,14 +176,12 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
// Rewrite path from /key to /bucket/key
String newPath = "/" + bucket + (path.startsWith("/") ? "" : "/") + path;
// RESTEasy keeps the decoded path of the URI it is given and decodes it again to route,
// so escape it once more to leave the raw path in place, as a path-style request has it.
URI newUri = UriBuilder.fromUri(uri)
.replacePath(newPath.replace("%", "%25"))
.replacePath(newPath)
.build();
requestContext.setProperty(ORIGINAL_REQUEST_URI_PROPERTY, uri);
requestContext.setRequestUri(newUri);
RequestUriRewrite.apply(requestContext, newUri);
}
/**