fix(s3): prevent S3VirtualHostFilter from hijacking non-S3 requests (#199)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-03 20:08:51 -05:00
1 parent 665af53149
commit 59cdc3fea6
6 files changed
+170

No files matched your search

@@ -16,6 +16,21 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
String host = requestContext.getHeaderString("Host");
if (host == null) return;
// 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")) {
return;
}
// S3 does not use these content types for bucket/object operations,
// but other AWS services (AwsQuery, JSON protocols) do.
String contentType = requestContext.getHeaderString("Content-Type");
if (contentType != null && (
contentType.startsWith("application/x-www-form-urlencoded") ||
contentType.startsWith("application/x-amz-json-"))) {
return;
}
String bucket = extractBucket(host);
if (bucket == null) return;
@@ -0,0 +1,24 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class CloudFormationHijackTest {
@Test
void testCloudFormationHijack() {
// Mock a CloudFormation request sent with Host: cloudformation.us-west-1.amazonaws.com
given()
.header("Host", "cloudformation.us-west-1.amazonaws.com")
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "DescribeStacks")
.formParam("Version", "2010-05-15")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("DescribeStacksResponse")); // Should be routed to CloudFormation, not S3
}
}
@@ -0,0 +1,32 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class FilterTest {
@Test
void testFilterWithQuery() {
given()
.header("Host", "my-bucket.localhost:4566")
.when()
.put("/")
.then()
.statusCode(200);
String xml = "<Delete><Object><Key>test</Key></Object></Delete>";
given()
.header("Host", "my-bucket.localhost:4566")
.queryParam("delete", "")
.contentType("application/xml")
.body(xml)
.log().all()
.when()
.post("/")
.then()
.log().all()
.statusCode(200);
}
}
@@ -0,0 +1,18 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
@QuarkusTest
class S3DumpTest {
@Test
void dump() {
given()
.header("Host", "mybucket.s3.amazonaws.com")
.when()
.post("/?delete")
.then()
.log().all();
}
}
@@ -0,0 +1,65 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class S3VirtualHostTest {
@Test
void testVirtualHostPostDelete() {
given()
.header("Host", "mybucket.s3.amazonaws.com")
.when()
.put("/")
.then()
.statusCode(200);
String xml = "<Delete><Object><Key>test</Key></Object></Delete>";
given()
.header("Host", "mybucket.s3.amazonaws.com")
// Empty value adds equal sign in RestAssured: ?delete=
.queryParam("delete", "")
.contentType("application/xml")
.body(xml)
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("DeleteResult"));
// Test with raw query string ?delete
given()
.header("Host", "mybucket.s3.amazonaws.com")
.contentType("application/xml")
.body(xml)
.when()
.post("/?delete")
.then()
.statusCode(200)
.body(containsString("DeleteResult"));
// What about path-style ?delete
given()
.contentType("application/xml")
.body(xml)
.when()
.post("/mybucket?delete")
.then()
.statusCode(200)
.body(containsString("DeleteResult"));
// Path style with delete=
given()
.contentType("application/xml")
.body(xml)
.when()
.post("/mybucket?delete=")
.then()
.statusCode(200)
.body(containsString("DeleteResult"));
}
}
@@ -0,0 +1,16 @@
package io.github.hectorvent.floci.services.s3;
import jakarta.ws.rs.core.UriBuilder;
import org.junit.jupiter.api.Test;
import java.net.URI;
import static org.junit.jupiter.api.Assertions.assertEquals;
class UriBuilderTest {
@Test
void testUriBuilder() {
URI uri = URI.create("http://host/?delete");
URI newUri = UriBuilder.fromUri(uri).replacePath("/b/").build();
System.out.println("NEW URI: " + newUri.toString());
System.out.println("NEW URI QUERY: " + newUri.getQuery());
}
}