Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

TATSUNO “Taz” Yasuhiro2bb69a030b53
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 "/<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)
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 /<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;
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"));
+ }
}