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,35 @@
package io.github.hectorvent.floci.core.common;
import jakarta.ws.rs.container.ContainerRequestContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
class RequestUriRewriteTest {
@ParameterizedTest
@ValueSource(strings = {
"plain", "x%25y", "trailing%25", "a%252Fb", "a%2Fb", "a%20b", "a%3Fb%23c", "caf%C3%A9",
"a//b", "a/./b", "a/../b", "a//x%25y"
})
void escapesTheRoutingPathOnceAndPreservesOtherUriComponents(String suffix) {
ContainerRequestContext requestContext = mock(ContainerRequestContext.class);
URI intendedUri = URI.create("http://example.test:4566/files/" + suffix + "?q=a%2525%26b");
RequestUriRewrite.apply(requestContext, intendedUri);
ArgumentCaptor<URI> capturedUri = ArgumentCaptor.forClass(URI.class);
verify(requestContext).setRequestUri(capturedUri.capture());
URI routingUri = capturedUri.getValue();
assertEquals(intendedUri.getRawPath(), routingUri.getPath());
assertEquals(intendedUri.getRawQuery(), routingUri.getRawQuery());
assertEquals(intendedUri.getScheme(), routingUri.getScheme());
assertEquals(intendedUri.getRawAuthority(), routingUri.getRawAuthority());
}
}
@@ -77,6 +77,21 @@ class ApiGatewayCustomDomainFilterTest {
assertEquals(Boolean.TRUE, request.property(ApiGatewayCustomDomainFilter.ROUTED_PROPERTY));
}
@ParameterizedTest
@ValueSource(strings = {"a%25b", "a%2Fb", "a%252Fb"})
void recordsTheIntendedPathBeforeAddingTheRoutingEscape(String suffix) {
ApiGatewayService service = serviceWithMapping("v1", "prod");
RecordingRequest request = new RecordingRequest(DOMAIN, URI.create("http://" + DOMAIN + "/v1/" + suffix));
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
new ApiGatewayCustomDomainFilter(service, mock(ApiGatewayV2Service.class), routeContext)
.filter(request.context());
assertEquals("/execute-api/" + API_ID + "/prod/" + suffix, routeContext.rewrittenRequestPath());
assertEquals(routeContext.rewrittenRequestPath(), request.routedUri().getPath());
assertEquals("/v1/" + suffix, routeContext.signedRequestPath());
}
@Test
void leavesSignedPathUnsetWhenHostIsNotACustomDomain() {
ApiGatewayService service = mock(ApiGatewayService.class);
@@ -63,10 +63,13 @@ class ApiGatewayExecuteApiHostFilterTest {
"abc123.execute-api.localhost.floci.io",
URI.create("http://abc123.execute-api.localhost.floci.io/files/a%2Fb?value=a%2Fb"));
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"))
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
new ApiGatewayExecuteApiHostFilter(lookup, new RegionResolver(REGION, "000000000000"), routeContext)
.filter(request.context());
assertEquals("/execute-api/abc123/$default/files/a%2Fb", request.routedUri().getRawPath());
assertEquals("/execute-api/abc123/$default/files/a%252Fb", request.routedUri().getRawPath());
assertEquals("/execute-api/abc123/$default/files/a%2Fb", routeContext.rewrittenRequestPath());
assertEquals("/files/a%2Fb", routeContext.signedRequestPath());
assertEquals("value=a%2Fb", request.routedUri().getRawQuery());
}
@@ -101,6 +104,7 @@ class ApiGatewayExecuteApiHostFilterTest {
assertEquals("value=a%2Fb", request.routedUri().getRawQuery());
assertNull(routeContext.httpApiRegion());
assertEquals("/dev/deep/path", routeContext.signedRequestPath());
assertEquals("/execute-api/abc123/dev/deep/path", routeContext.rewrittenRequestPath());
}
@Test
@@ -49,12 +49,45 @@ import static org.mockito.Mockito.when;
class ApiGatewayExecuteControllerTest {
private static ApiGatewayExecuteController controller(ObjectMapper objectMapper) {
return controller(objectMapper, new ApiGatewayExecuteRouteContext());
}
private static ApiGatewayExecuteController controller(ObjectMapper objectMapper,
ApiGatewayExecuteRouteContext routeContext) {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn("000000000000");
return new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, objectMapper, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, routeContext, null, null, null, null, null, null);
}
@ParameterizedTest
@ValueSource(strings = {
"/index/a%25b", "/index/a%2Fb", "/index/a%252Fb", "/index/a//b", "/index/a/./b", "/index/a/../b"
})
void recordedRewritePathTakesPrecedenceOverTheEscapedRoutingUri(String resourcePath) {
ApiGatewayExecuteRouteContext routeContext = new ApiGatewayExecuteRouteContext();
String intendedPath = "/execute-api/api1/prod" + resourcePath;
routeContext.recordSignedRequestPath("/public" + resourcePath);
routeContext.recordRewrittenRequestPath(intendedPath);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566"
+ intendedPath.replace("%", "%25")));
ApiGatewayExecuteController controller = controller(new ObjectMapper(), routeContext);
assertEquals(intendedPath, controller.rawRequestPath(uriInfo));
assertEquals(resourcePath,
ApiGatewayExecuteController.rawStagelessPath(resourcePath, controller.rawRequestPath(uriInfo)));
assertEquals("/public" + resourcePath, routeContext.signedRequestPath());
}
@Test
void pathStyleRequestsUseTheRawUriPathWhenNoRewriteWasRecorded() {
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566/execute-api/api1/prod/a%2525b"));
assertEquals("/execute-api/api1/prod/a%2525b", controller(new ObjectMapper()).rawRequestPath(uriInfo));
}
@Test
@@ -15,6 +15,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
@@ -131,7 +132,9 @@ class ApiGatewayHttpProxyEncodedPathIntegrationTest {
}
@ParameterizedTest
@ValueSource(strings = {"NIFTY%2050/constituents", "a%3Fb", "a%23b", "a%2Fb", "a%252Fb", "a+b", "caf%C3%A9"})
@ValueSource(strings = {
"NIFTY%2050/constituents", "x%25y", "trailing%25", "a%3Fb", "a%23b", "a%2Fb", "a%252Fb", "a+b", "caf%C3%A9"
})
void restGreedyParameterKeepsTheCallersEncoding(String suffix) {
String apiId = createRestApi("HTTP_PROXY", "/index/{proxy+}", "/backend/{proxy}", null);
@@ -141,6 +144,60 @@ class ApiGatewayHttpProxyEncodedPathIntegrationTest {
assertNull(lastRawQuery.get());
}
@ParameterizedTest
@ValueSource(strings = {"plain", "x%25y", "trailing%25", "a%252Fb", "a%2Fb", "a%20b", "a%3Fb%23c", "caf%C3%A9"})
void restRoutedHostsKeepTheCallersEncoding(String suffix) {
String apiId = createRestApi("HTTP_PROXY", "/index/{proxy+}", "/backend/{proxy}", null);
String domain = createCustomDomain(apiId);
try {
given().contentType(ContentType.JSON)
.body(Map.of("restApiId", apiId, "stage", "test", "basePath", "public"))
.when().post("/domainnames/" + domain + "/basepathmappings").then().statusCode(201);
assertRoutedHostsForwardPath(apiId, domain, suffix, "/backend/" + suffix);
} finally {
given().delete("/domainnames/" + domain).then().statusCode(202);
}
}
@ParameterizedTest
@ValueSource(strings = {"plain", "x%25y", "trailing%25", "a%252Fb", "a%2Fb", "a%20b", "a%3Fb%23c", "caf%C3%A9"})
void httpApiRoutedHostsKeepTheCallersEncoding(String suffix) {
String apiId = HttpProxyApiFixtures.createProxyApi("encoded-host-http-api", "", "test", backendPort,
"/index/{proxy+}");
String domain = createCustomDomain(apiId);
try {
given().contentType(ContentType.JSON)
.body(Map.of("apiId", apiId, "stage", "test", "apiMappingKey", "public"))
.when().post("/v2/domainnames/" + domain + "/apimappings").then().statusCode(201);
assertRoutedHostsForwardPath(apiId, domain, suffix, "/index/" + suffix);
} finally {
given().delete("/domainnames/" + domain).then().statusCode(202);
}
}
private static String createCustomDomain(String apiId) {
String domain = apiId + ".example.test";
given().contentType(ContentType.JSON)
.body(Map.of("domainName", domain, "endpointConfiguration", Map.of("types", List.of("REGIONAL"))))
.when().post("/domainnames").then().statusCode(201);
return domain;
}
private static void assertRoutedHostsForwardPath(String apiId, String domain, String suffix, String expectedPath) {
given().urlEncodingEnabled(false).header("Host", domain)
.when().get("/public/index/" + suffix)
.then().statusCode(200);
assertEquals(expectedPath, lastRawPath.get());
lastRawPath.set(null);
given().urlEncodingEnabled(false).header("Host", apiId + ".execute-api.localhost.floci.io")
.when().get("/test/index/" + suffix)
.then().statusCode(200);
assertEquals(expectedPath, lastRawPath.get());
}
@ParameterizedTest
@ValueSource(strings = {
"/execute-api/{apiId}/test/index/NIFTY%2050",
@@ -23,6 +23,8 @@ import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.BufferedReader;
import java.io.IOException;
@@ -88,6 +90,25 @@ class CloudFrontCustomOriginServingTest {
private HttpServer originServer;
private CountDownLatch originRelease;
@ParameterizedTest
@ValueSource(strings = {"plain", "x%25y", "trailing%25", "a%252Fb", "a%2Fb", "a%20b", "a%3Fb%23c", "caf%C3%A9"})
void routedHostsPreserveTheRawOriginPath(String suffix) throws IOException {
AtomicReference<ReceivedRequest> received = new AtomicReference<>();
startRecordingOrigin(received, new AtomicInteger());
Distribution distribution = customOriginDistribution(defaultBehavior("custom-origin"));
String alias = "encoded-path.example.test";
distribution.getConfig().setAliases(List.of(alias));
Distribution created = createDistribution(distribution);
for (String host : List.of(alias, created.getDomainName())) {
given().urlEncodingEnabled(false).header("Host", host)
.when().get("/" + suffix)
.then().statusCode(200).body(equalTo("ok"));
assertEquals("/" + suffix, received.get().path());
}
}
@AfterEach
void stopOrigin() {
if (originRelease != null) {