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 "/<bucket>/". 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 "/<bucket>" + 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.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-21 08:33:51 -04:00
1 parent 25a74be198
commit c62db2de1e
2 files changed
+83 -9

No files matched your search

@@ -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);
}
}