Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

XuJian01ec7af8d5c9
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
index 6a307afe61..d199a22fa1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java
@@ -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;
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
index d7f496139b..93464d231e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilterTest.java
@@ -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
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java
index 4e51fa6548..03a1af2863 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VirtualHostIntegrationTest.java
@@ -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");