From c62db2de1ebb0dceee5cbab17808bbd60222cda6 Mon Sep 17 00:00:00 2001 From: lex00 <121451605+lex00@users.noreply.github.com> Date: Mon, 21 Sep 2026 06:33:51 -0600 Subject: [PATCH] fix(s3): keep a virtual-hosted key whole when it contains the bucket name (#3944) * fix(s3): keep a virtual-hosted key whole when it contains the bucket name extractObjectKey searched the raw request path for "//". For a virtual-hosted request the raw path is the key alone, so that string is there only when the key happens to contain the bucket's name as a segment, and everything up to and including that segment was dropped: "tofu-records/b/.store-sentinel" in bucket "b" was stored as ".store-sentinel". A list under the real prefix then came back empty. The raw path is treated as the whole key when the rewritten request URI is exactly "/" + rawPath, which is what S3VirtualHostFilter produces and a path-style request never is. Path-style is unchanged. Closes #208 (cherry picked from commit e0dccb85f514d59f4f3c24d59d54ccc035821097) * test(s3): describe the scenario rather than naming a downstream project Review feedback on #3944. The comment named a specific project and its key layout, which goes stale and tells the next reader nothing they need. It now describes the shape that matters, a key carrying the bucket's own name as a segment, and keeps the two sentences that explain why a virtual-hosted raw path makes that key data rather than a bucket prefix. --- .../floci/services/s3/S3Controller.java | 42 ++++++++++++---- .../s3/S3VirtualHostIntegrationTest.java | 50 +++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) 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.java index 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; + if (isVirtualHostedRawPath(uriInfo, bucket, rawPath)) { + // Virtual-hosted style: S3VirtualHostFilter rewrote the JAX-RS URI to "/" + + // path, but the RAW path is still the key alone. Searching it for "//" 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 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.java index 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//...". The raw request path of a virtual-hosted request is the key + // alone, so the "//" 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 + "")); + + 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); + } }