fix(s3): keep a percent sign in a virtual-hosted object key (#6000)

* fix(s3): keep a percent sign in a virtual-hosted object key

A virtual-hosted request for a key such as a%/b failed with 400 or 500.
The same request in path style worked. S3 stores the key as sent in
both styles.

RESTEasy keeps the decoded path of a rewritten request URI and later
decodes it again. S3VirtualHostFilter now escapes the raw path once more,
so routing sees the same path as a path-style request. The request URI
then decodes to /<bucket> plus the raw path, so the virtual-hosted key
check compares against the decoded path.

Fixes #5997

* test(s3): keep only the percent-key cases that guard the fix
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-19 10:57:03 -04:00
1 parent ca47df9ec5
commit 2bb69a030b
6 files changed
+57 -10

No files matched your search

@@ -149,6 +149,20 @@ class S3VirtualHostStyleTest {
@Test
@Order(10)
void keyWithPercentSign() {
String key = "a%/b";
s3.putObject(PutObjectRequest.builder().bucket(BUCKET).key(key).build(),
RequestBody.fromString(CONTENT));
ListObjectsV2Response listed = s3.listObjectsV2(
ListObjectsV2Request.builder().bucket(BUCKET).build());
assertThat(listed.contents()).extracting(S3Object::key).containsExactly(key);
s3.deleteObject(DeleteObjectRequest.builder().bucket(BUCKET).key(key).build());
}
@Test
@Order(11)
void deleteBucket() {
s3.deleteBucket(DeleteBucketRequest.builder().bucket(BUCKET).build());
}
@@ -28,6 +28,7 @@ import org.jboss.logging.Logger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@@ -441,11 +442,12 @@ public class IamConditionContextResolver {
if (bucket == null) {
return null;
}
String requestUriPath = ctx.getUriInfo().getRequestUri() == null
? null : ctx.getUriInfo().getRequestUri().getRawPath();
URI requestUri = ctx.getUriInfo().getRequestUri();
RoutingContext current = currentVertxRequest == null ? null : currentVertxRequest.getCurrent();
String rawPath = current != null && current.request() != null ? current.request().path() : requestUriPath;
String key = rawPath == null ? null : S3ObjectKeys.fromRawPath(rawPath, requestUriPath, bucket);
String rawPath = current != null && current.request() != null ? current.request().path()
: requestUri == null ? null : requestUri.getRawPath();
String key = rawPath == null ? null
: S3ObjectKeys.fromRawPath(rawPath, requestUri == null ? null : requestUri.getPath(), bucket);
if (key == null) {
key = s3PathObjectKey(ctx.getUriInfo().getPath());
}
@@ -4321,7 +4321,7 @@ public class S3Controller {
// A virtual-hosted raw path is the key alone (see S3ObjectKeys.isVirtualHostedRawPath):
// 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.
String key = S3ObjectKeys.fromRawPath(rawPath, uriInfo.getRequestUri().getRawPath(), bucket);
String key = S3ObjectKeys.fromRawPath(rawPath, uriInfo.getRequestUri().getPath(), bucket);
if (key == null) {
// Should not happen on standard /{bucket}/{key:.+} routes, but can happen when
// requests are rerouted from a single-segment route (e.g. createBucket / handleBucketPost)
@@ -16,10 +16,10 @@ public final class S3ObjectKeys {
/**
* The decoded key in {@code rawPath}, the path as the client sent it, for a request on
* {@code bucket}. {@code requestUriPath} is the request URI's raw path, which
* {@link S3VirtualHostFilter} rewrites to {@code /<bucket>/<key>} for a virtual-hosted request,
* whose raw path is then the key alone. Returns null when the raw path does not carry the
* bucket, or does not decode.
* {@code bucket}. {@code requestUriPath} is the request URI's decoded path, which
* {@link S3VirtualHostFilter} rewrites to {@code /<bucket>} followed by the raw path for a
* virtual-hosted request, whose raw path is then the key alone. Returns null when the raw path
* does not carry the bucket, or does not decode.
*/
public static String fromRawPath(String rawPath, String requestUriPath, String bucket) {
String rawKey;
@@ -175,8 +175,10 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
// Rewrite path from /key to /bucket/key
String newPath = "/" + bucket + (path.startsWith("/") ? "" : "/") + path;
// RESTEasy keeps the decoded path of the URI it is given and decodes it again to route,
// so escape it once more to leave the raw path in place, as a path-style request has it.
URI newUri = UriBuilder.fromUri(uri)
.replacePath(newPath)
.replacePath(newPath.replace("%", "%25"))
.build();
requestContext.setProperty(ORIGINAL_REQUEST_URI_PROPERTY, uri);
@@ -473,4 +473,33 @@ class S3VirtualHostIntegrationTest {
.then()
.statusCode(404);
}
@Test
@Order(91)
void keyWithPercentSignIsStoredLiterally() {
String percentBucket = "vhost-percent-key";
String percentHost = percentBucket + ".localhost";
given().header("Host", percentHost).when().put("/").then().statusCode(200);
for (String wirePath : new String[] {"/a%25/b", "/archive/" + percentBucket + "/a%25b"}) {
given()
.urlEncodingEnabled(false)
.header("Host", percentHost)
.contentType("text/plain")
.body("x")
.when()
.put(wirePath)
.then()
.statusCode(200);
}
given()
.header("Host", percentHost)
.queryParam("list-type", "2")
.when()
.get("/")
.then()
.statusCode(200)
.body("ListBucketResult.Contents.Key", containsInAnyOrder("a%/b", "archive/" + percentBucket + "/a%b"));
}
}