From 2bb69a030b534f6695423d69ef477f78cd20d89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Mon, 19 Oct 2026 23:57:03 +0900 Subject: [PATCH] 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 / 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 --- .../floci/test/S3VirtualHostStyleTest.java | 14 +++++++++ .../common/IamConditionContextResolver.java | 10 ++++--- .../floci/services/s3/S3Controller.java | 2 +- .../floci/services/s3/S3ObjectKeys.java | 8 ++--- .../services/s3/S3VirtualHostFilter.java | 4 ++- .../s3/S3VirtualHostIntegrationTest.java | 29 +++++++++++++++++++ 6 files changed, 57 insertions(+), 10 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3VirtualHostStyleTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3VirtualHostStyleTest.java index d8d42fd534..6ceba76d25 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3VirtualHostStyleTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3VirtualHostStyleTest.java @@ -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()); } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java index 88b8fafcfd..d4cf09f472 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java @@ -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()); } 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 2e05442d3a..b294f388c5 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 @@ -4321,7 +4321,7 @@ public class S3Controller { // A virtual-hosted raw path is the key alone (see S3ObjectKeys.isVirtualHostedRawPath): // 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. - 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) diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java index 6a190fbcb3..f92dee426b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java @@ -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 //} 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 /} 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; diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java index 73e083e207..f59e2e4968 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java @@ -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); 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 404bf4b8df..5850194578 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 @@ -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")); + } }