mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): support virtual-hosted routing via X-Forwarded-Host and prevent unintended bucket creation (#3621)
* fix(s3): support virtual-hosted routing via X-Forwarded-Host and prevent unintended bucket creation - Respect X-Forwarded-Host when resolving host for reverse proxy compatibility - Support tail-agnostic .s3.<region>.<domain> qualifier and fallback lookup against existing buckets - Reroute misplaced single-segment object/multipart uploads or reject with InvalidBucketName/MalformedXML - Add unit and integration tests for virtual-hosted requests forwarded through proxies * docs(s3): translate virtual host routing comment
This commit is contained in:
1 parent
ce5c5b4cfa
commit
c63c834c0e
7 files changed
+471
-16
No files matched your search
@@ -488,6 +488,68 @@ class S3IntegrationTest {
|
||||
.body(containsString("InvalidLocationConstraint"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBucketRejectsOverlyLongBucketName() {
|
||||
String longBucketName = "30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json";
|
||||
given()
|
||||
.when()
|
||||
.put("/" + longBucketName)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidBucketName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBucketRejectsNonXmlPayload() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body("{\"key\":\"value\"}")
|
||||
.when()
|
||||
.put("/malformed-xml-bucket")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("MalformedXML"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBucketReroutesMisplacedVirtualHostedPutObject() {
|
||||
String targetBucket = "reroute-target-bucket";
|
||||
String objectKey = "misplaced-file.json";
|
||||
String content = "{\"message\":\"hello from misplaced put\"}";
|
||||
|
||||
given().put("/" + targetBucket).then().statusCode(200);
|
||||
|
||||
// PutObject request arriving on single path segment with custom Host
|
||||
given()
|
||||
.header("Host", targetBucket + ".floci.apps.custom.com")
|
||||
.header("x-amz-content-sha256", "UNSIGNED-PAYLOAD")
|
||||
.contentType("application/json")
|
||||
.body(content)
|
||||
.when()
|
||||
.put("/" + objectKey)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Verify stored in targetBucket
|
||||
given()
|
||||
.when()
|
||||
.get("/" + targetBucket + "/" + objectKey)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo(content));
|
||||
|
||||
// Verify objectKey was not created as bucket
|
||||
given()
|
||||
.when()
|
||||
.head("/" + objectKey)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
// Clean up
|
||||
given().delete("/" + targetBucket + "/" + objectKey).then().statusCode(204);
|
||||
given().delete("/" + targetBucket).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void copyObjectWithNonAsciiKeySucceeds() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
@@ -758,4 +759,102 @@ class S3VirtualHostFilterTest {
|
||||
assertEquals("data.us-east-1", S3VirtualHostFilter.extractBucket(
|
||||
"data.us-east-1.s3.localhost", "localhost", DEFAULT_SUFFIXES));
|
||||
}
|
||||
|
||||
// --- Reverse Proxy & Custom Domain Resolution Tests ---
|
||||
|
||||
@Test
|
||||
void resolveHostPrefersXForwardedHost() {
|
||||
URI uri = URI.create("http://floci:4566/key");
|
||||
assertEquals("my-bucket.localhost:4566",
|
||||
S3VirtualHostFilter.resolveHost("floci:4566", "my-bucket.localhost:4566", uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveHostExtractsFirstHostFromCommaSeparatedXForwardedHost() {
|
||||
URI uri = URI.create("http://floci:4566/key");
|
||||
assertEquals("client-bucket.localhost:4566",
|
||||
S3VirtualHostFilter.resolveHost("floci:4566", "client-bucket.localhost:4566, proxy1:4566, proxy2", uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveHostFallsBackToHostWhenXForwardedHostBlankOrNull() {
|
||||
URI uri = URI.create("http://localhost:4566/key");
|
||||
assertEquals("my-bucket.localhost:4566",
|
||||
S3VirtualHostFilter.resolveHost("my-bucket.localhost:4566", null, uri));
|
||||
assertEquals("my-bucket.localhost:4566",
|
||||
S3VirtualHostFilter.resolveHost("my-bucket.localhost:4566", " ", uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
void bucketBeforeS3QualifierExtractsBucketOnCustomDomains() {
|
||||
assertEquals("my-bucket",
|
||||
S3VirtualHostFilter.bucketBeforeS3Qualifier("my-bucket.s3.us-east-1.floci.apps.mesirendon.com"));
|
||||
assertEquals("my-bucket",
|
||||
S3VirtualHostFilter.bucketBeforeS3Qualifier("my-bucket.s3-fips.dualstack.us-west-2.mycorp.internal"));
|
||||
assertEquals("dotted.name.bucket",
|
||||
S3VirtualHostFilter.bucketBeforeS3Qualifier("dotted.name.bucket.s3.eu-west-1.custom.org"));
|
||||
assertNull(S3VirtualHostFilter.bucketBeforeS3Qualifier("s3.us-east-1.floci.apps.mesirendon.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRewritesVirtualHostWithXForwardedHost() {
|
||||
URI requestUri = URI.create("http://floci:4566/30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json");
|
||||
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("floci:4566");
|
||||
when(ctx.getHeaderString("X-Forwarded-Host")).thenReturn("cdk-assets-bucket.localhost:4566");
|
||||
|
||||
new S3VirtualHostFilter().filter(ctx);
|
||||
|
||||
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
|
||||
verify(ctx).setRequestUri(rewritten.capture());
|
||||
assertEquals("/cdk-assets-bucket/30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json",
|
||||
rewritten.getValue().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRewritesVirtualHostWithCustomDomainS3Qualifier() {
|
||||
URI requestUri = URI.create("http://floci:4566/archive.zip");
|
||||
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("custom-bucket.s3.us-east-1.floci.apps.mesirendon.com");
|
||||
|
||||
new S3VirtualHostFilter().filter(ctx);
|
||||
|
||||
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
|
||||
verify(ctx).setRequestUri(rewritten.capture());
|
||||
assertEquals("/custom-bucket/archive.zip", rewritten.getValue().getRawPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterRewritesUsingExistingBucketFallbackWhenSignedForS3() {
|
||||
S3Service s3Service = mock(S3Service.class);
|
||||
when(s3Service.bucketExists("existing-cdk-bucket")).thenReturn(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<S3Service> s3ServiceInstance = mock(Instance.class);
|
||||
when(s3ServiceInstance.isResolvable()).thenReturn(true);
|
||||
when(s3ServiceInstance.get()).thenReturn(s3Service);
|
||||
|
||||
URI requestUri = URI.create("http://floci:4566/template.json");
|
||||
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("existing-cdk-bucket.floci.apps.mesirendon.com");
|
||||
when(ctx.getHeaderString("Authorization")).thenReturn(
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260914/us-east-1/s3/aws4_request, Signature=fake");
|
||||
|
||||
new S3VirtualHostFilter(s3ServiceInstance).filter(ctx);
|
||||
|
||||
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
|
||||
verify(ctx).setRequestUri(rewritten.capture());
|
||||
assertEquals("/existing-cdk-bucket/template.json", rewritten.getValue().getRawPath());
|
||||
}
|
||||
}
|
||||
+69
@@ -327,6 +327,75 @@ class S3VirtualHostIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void putObjectViaVirtualHostWithXForwardedHost() {
|
||||
String hashKey = "30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json";
|
||||
String content = "{\"asset\":\"cdk-template-data\"}";
|
||||
|
||||
// Simulate request through Traefik/reverse proxy rewriting Host to floci:4566
|
||||
// and forwarding the client's virtual-hosted Host in X-Forwarded-Host
|
||||
given()
|
||||
.header("Host", "floci:4566")
|
||||
.header("X-Forwarded-Host", HOST)
|
||||
.contentType("application/json")
|
||||
.body(content)
|
||||
.when()
|
||||
.put("/" + hashKey)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Verify object was stored in vhost-bucket
|
||||
given()
|
||||
.header("Host", HOST)
|
||||
.when()
|
||||
.get("/" + hashKey)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo(content));
|
||||
|
||||
// Verify hashKey was NOT created as a bucket
|
||||
given()
|
||||
.header("Host", "localhost:4566")
|
||||
.when()
|
||||
.head("/" + hashKey)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
// Clean up object
|
||||
given().header("Host", HOST).delete("/" + hashKey)
|
||||
.then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void createMultipartUploadViaVirtualHostWithXForwardedHost() {
|
||||
String key = "multipart-xfh.zip";
|
||||
io.restassured.response.Response response = given()
|
||||
.header("Host", "floci:4566")
|
||||
.header("X-Forwarded-Host", HOST)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260914/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(25)
|
||||
void cleanupAndDeleteBucket() {
|
||||
given().header("Host", HOST).delete("/hello.txt");
|
||||
given().header("Host", HOST).delete("/path/to/nested.json");
|
||||
|
||||
Reference in new issue
Block a user