fix(s3): route signed multipart requests by virtual host (#3464)

* fix(s3): route signed multipart requests by virtual host

* fix(s3): recognize presigned virtual-host requests

---------

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-13 00:42:15 -04:00
1 parent ae7548a70c
commit 01ec7af8d5
3 files changed
+102 -4

No files matched your search

@@ -107,14 +107,20 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
// Do not hijack requests meant for other AWS services
String auth = requestContext.getHeaderString("Authorization");
if (auth != null && auth.contains("Credential=") && !auth.contains("/s3/aws4_request")) {
boolean hasAuthorizationCredential = auth != null && auth.contains("Credential=");
boolean headerSignedForS3 = hasAuthorizationCredential && auth.contains("/s3/aws4_request");
if (hasAuthorizationCredential && !headerSignedForS3) {
return;
}
String queryCredential = requestContext.getUriInfo().getQueryParameters()
.getFirst("X-Amz-Credential");
boolean signedForS3 = headerSignedForS3
|| queryCredential != null && queryCredential.contains("/s3/aws4_request");
// S3 does not use these content types for bucket/object operations,
// but other AWS services (AwsQuery, JSON protocols) do.
// Other AWS services use these content types on the shared edge endpoint. An explicit
// S3 SigV4 scope is authoritative, including empty-body multipart initiation requests.
String contentType = requestContext.getHeaderString("Content-Type");
if (contentType != null && (
if (!signedForS3 && contentType != null && (
contentType.startsWith("application/x-www-form-urlencoded") ||
contentType.startsWith("application/x-amz-json-"))) {
return;
@@ -1,6 +1,8 @@
package io.github.hectorvent.floci.services.s3;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -18,7 +20,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -293,6 +297,7 @@ class S3VirtualHostFilterTest {
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(requestUri);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getHeaderString("Host")).thenReturn(null); // HTTP/2: no Host header
@@ -429,6 +434,7 @@ class S3VirtualHostFilterTest {
URI requestUri = URI.create("http://www.example.com.localhost:4566/index.html");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(requestUri);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getHeaderString("Host")).thenReturn("www.example.com.localhost:4566");
@@ -440,6 +446,64 @@ class S3VirtualHostFilterTest {
assertEquals("/www.example.com/index.html", rewritten.getValue().getRawPath());
}
@Test
void filterRewritesFormRequestExplicitlySignedForS3() {
URI requestUri = URI.create("http://vhost-bucket.localhost:4566/archive.zip?uploads=");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(requestUri);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getHeaderString("Host")).thenReturn("vhost-bucket.localhost:4566");
when(ctx.getHeaderString("Authorization")).thenReturn(
"AWS4-HMAC-SHA256 Credential=test/20260912/us-east-1/s3/aws4_request, Signature=fake");
when(ctx.getHeaderString("Content-Type")).thenReturn("application/x-www-form-urlencoded; charset=UTF-8");
new S3VirtualHostFilter().filter(ctx);
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
verify(ctx).setRequestUri(rewritten.capture());
assertEquals("/vhost-bucket/archive.zip", rewritten.getValue().getRawPath());
assertEquals("uploads=", rewritten.getValue().getRawQuery());
}
@Test
void filterRewritesFormRequestPresignedForS3() {
URI requestUri = URI.create("http://vhost-bucket.localhost:4566/archive.zip?uploads=");
MultivaluedMap<String, String> queryParameters = new MultivaluedHashMap<>();
queryParameters.add("X-Amz-Credential", "test/20260913/us-east-1/s3/aws4_request");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(requestUri);
when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getHeaderString("Host")).thenReturn("vhost-bucket.localhost:4566");
when(ctx.getHeaderString("Content-Type")).thenReturn("application/x-www-form-urlencoded");
new S3VirtualHostFilter().filter(ctx);
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
verify(ctx).setRequestUri(rewritten.capture());
assertEquals("/vhost-bucket/archive.zip", rewritten.getValue().getRawPath());
assertEquals("uploads=", rewritten.getValue().getRawQuery());
}
@Test
void filterStillIgnoresUnsignedFormRequests() {
URI requestUri = URI.create("http://vhost-bucket.localhost:4566/archive.zip?uploads=");
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(requestUri);
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
when(ctx.getUriInfo()).thenReturn(uriInfo);
when(ctx.getHeaderString("Host")).thenReturn("vhost-bucket.localhost:4566");
when(ctx.getHeaderString("Content-Type")).thenReturn("application/x-www-form-urlencoded");
new S3VirtualHostFilter().filter(ctx);
verify(ctx, never()).setRequestUri(any(URI.class));
}
// --- Other services' virtual-host schemes must not be swallowed as dotted buckets ---
@ParameterizedTest
@@ -299,6 +299,34 @@ class S3VirtualHostIntegrationTest {
@Test
@Order(20)
void createMultipartUploadViaVirtualHost() {
String key = "virtual-multipart.zip";
var response = given()
.header("Host", HOST)
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260912/us-east-1/s3/aws4_request, Signature=fake")
.contentType("application/x-www-form-urlencoded")
.queryParam("uploads", "")
.when()
.post("/" + key)
.then()
.statusCode(200)
.body(containsString("<Bucket>" + BUCKET + "</Bucket>"))
.body(containsString("<Key>" + key + "</Key>"))
.extract().response();
String uploadId = response.xmlPath().getString("InitiateMultipartUploadResult.UploadId");
given()
.header("Host", HOST)
.queryParam("uploadId", uploadId)
.when()
.delete("/" + key)
.then()
.statusCode(204);
}
@Test
@Order(21)
void cleanupAndDeleteBucket() {
given().header("Host", HOST).delete("/hello.txt");
given().header("Host", HOST).delete("/path/to/nested.json");