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

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