Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): keep a virtual-hosted key whole when it contains the bucket name (#3944)
lex00c62db2de1ebb
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex c78819dd0c..83508e5f75 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -3733,21 +3733,45 @@ public class S3Controller {int qIdx = rawUri.indexOf('?');String rawPath = qIdx >= 0 ? rawUri.substring(0, qIdx) : rawUri;String bucketPrefix = "/" + bucket + "/";- int prefixIndex = rawPath.indexOf(bucketPrefix);- if (prefixIndex < 0) {- // Should not happen on standard /{bucket}/{key:.+} routes, but can happen when- // requests are rerouted from a single-segment route (e.g. createBucket / handleBucketPost)- String pathKey = uriInfo.getPathParameters().getFirst("key");- String resolvedKey = pathKey != null ? pathKey : fallbackKey;- validateKeyNoTraversal(resolvedKey);- return resolvedKey;- }- String rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());+ String rawKey;+ if (isVirtualHostedRawPath(uriInfo, bucket, rawPath)) {+ // Virtual-hosted style: S3VirtualHostFilter rewrote the JAX-RS URI to "/<bucket>" ++ // path, but the RAW path is still the key alone. Searching it for "/<bucket>/" finds+ // that string only when the KEY happens to contain the bucket's name as a segment,+ // and everything up to that segment was then dropped from the key.+ rawKey = rawPath.substring(1);+ } else {+ int prefixIndex = rawPath.indexOf(bucketPrefix);+ if (prefixIndex < 0) {+ // Should not happen on standard /{bucket}/{key:.+} routes, but can happen when+ // requests are rerouted from a single-segment route (e.g. createBucket / handleBucketPost)+ String pathKey = uriInfo.getPathParameters().getFirst("key");+ String resolvedKey = pathKey != null ? pathKey : fallbackKey;+ validateKeyNoTraversal(resolvedKey);+ return resolvedKey;+ }+ rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());+ }String key = URLDecoder.decode(rawKey.replace("+", "%2B"), StandardCharsets.UTF_8);validateKeyNoTraversal(key);return key;}+ /**+ * True when the raw request path carries no bucket prefix because the bucket came from the+ * Host header: the rewritten request URI is then exactly "/" + bucket + rawPath, which is+ * what {@link S3VirtualHostFilter} produces and what a path-style request never is.+ */+ private static boolean isVirtualHostedRawPath(UriInfo uriInfo, String bucket, String rawPath) {+ if (rawPath.isEmpty() || rawPath.charAt(0) != '/') {+ return false;+ }+ String rewritten = uriInfo.getRequestUri().getRawPath();+ return rewritten != null+ && !rewritten.equals(rawPath)+ && rewritten.equals("/" + bucket + rawPath);+ }+private void validateKeyNoTraversal(String key) {if (key == null) return;try {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.javaindex 114594d677..cb2c7c7d24 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@@ -415,4 +415,54 @@ class S3VirtualHostIntegrationTest {.then().statusCode(204);}++ // A key may contain the bucket's own name as a path segment, for example+ // "tofu-records/<bucket>/...". The raw request path of a virtual-hosted request is the key+ // alone, so the "/<bucket>/" inside it is part of the key and not the path-style bucket+ // prefix.+ @Test+ @Order(90)+ void keyContainingTheBucketNameAsASegmentIsStoredWhole() {+ // Its own bucket: the shared one is deleted by an earlier ordered test, and a 404 on+ // the PUT below would fail this test without saying anything about keys.+ String segmentBucket = "vhost-key-segment";+ String segmentHost = segmentBucket + ".localhost";+ given().header("Host", segmentHost).when().put("/").then().statusCode(200);++ String key = "tofu-records/" + segmentBucket + "/.store-sentinel";+ given()+ .header("Host", segmentHost)+ .contentType("text/plain")+ .body("sentinel")+ .when()+ .put("/" + key)+ .then()+ .statusCode(200);++ given()+ .header("Host", segmentHost)+ .queryParam("list-type", "2")+ .queryParam("prefix", "tofu-records/" + segmentBucket + "/")+ .when()+ .get("/")+ .then()+ .statusCode(200)+ .body(containsString("<Key>" + key + "</Key>"));++ given()+ .header("Host", segmentHost)+ .when()+ .get("/" + key)+ .then()+ .statusCode(200)+ .body(equalTo("sentinel"));++ // And nothing was stored under the truncated key.+ given()+ .header("Host", segmentHost)+ .when()+ .get("/.store-sentinel")+ .then()+ .statusCode(404);+ }}