Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): preserve leading slashes in object keys to prevent key colli… (#286)
Dixit R Jainc7d5c554ecbe
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.javaindex 4502aec497..e913e85fc2 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@@ -64,13 +64,16 @@ public class S3Controller {private final S3Service s3Service;private final S3SelectService s3SelectService;private final RegionResolver regionResolver;+ private final io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest;@Injectpublic S3Controller(S3Service s3Service, S3SelectService s3SelectService,- RegionResolver regionResolver) {+ RegionResolver regionResolver,+ io.quarkus.vertx.http.runtime.CurrentVertxRequest currentVertxRequest) {this.s3Service = s3Service;this.s3SelectService = s3SelectService;this.regionResolver = regionResolver;+ this.currentVertxRequest = currentVertxRequest;}// --- Bucket operations ---@@ -353,6 +356,8 @@ public class S3Controller {@Context HttpHeaders httpHeaders,byte[] body) {try {+ key = extractObjectKey(uriInfo, bucket);+if (hasQueryParam(uriInfo, "tagging")) {return handlePutObjectTagging(bucket, key, body);}@@ -428,6 +433,8 @@ public class S3Controller {@Context UriInfo uriInfo,@Context HttpHeaders httpHeaders) {try {+ key = extractObjectKey(uriInfo, bucket);+if (uploadId != null) {return handleListParts(bucket, key, uploadId, maxPartsQuery, partNumberMarkerQuery);}@@ -551,8 +558,10 @@ public class S3Controller {@HeaderParam("If-Match") String ifMatch,@HeaderParam("If-None-Match") String ifNoneMatch,@HeaderParam("If-Modified-Since") String ifModifiedSince,- @HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince) {+ @HeaderParam("If-Unmodified-Since") String ifUnmodifiedSince,+ @Context UriInfo uriInfo) {try {+ key = extractObjectKey(uriInfo, bucket);S3Object obj = s3Service.headObject(bucket, key, versionId);Response preconditionResponse = checkPreconditions(obj, ifMatch, ifNoneMatch, ifModifiedSince, ifUnmodifiedSince);if (preconditionResponse != null) {@@ -655,6 +664,8 @@ public class S3Controller {@Context UriInfo uriInfo,@Context HttpHeaders httpHeaders) {try {+ key = extractObjectKey(uriInfo, bucket);+if (hasQueryParam(uriInfo, "tagging")) {s3Service.deleteObjectTagging(bucket, key);return Response.noContent().build();@@ -712,6 +723,8 @@ public class S3Controller {@Context UriInfo uriInfo,byte[] body) {try {+ key = extractObjectKey(uriInfo, bucket);+if (hasQueryParam(uriInfo, "uploads")) {MultipartUpload upload = s3Service.initiateMultipartUpload(bucket, key, contentType,extractUserMetadata(httpHeaders), httpHeaders.getHeaderString("x-amz-storage-class"));@@ -1890,4 +1903,22 @@ public class S3Controller {if (query == null) return false;return query.equals(param) || query.contains(param + "&") || query.contains("&" + param);}++ /**+ * Extracts the object key from the raw Vert.x request URI, preserving leading slashes+ * that JAX-RS path normalization would otherwise strip.+ */+ private String extractObjectKey(UriInfo uriInfo, String bucket) {+ String rawUri = currentVertxRequest.getCurrent().request().uri();+ 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 — route already matched /{bucket}/{key:.+}+ return uriInfo.getPathParameters().getFirst("key");+ }+ String rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());+ return URLDecoder.decode(rawKey, StandardCharsets.UTF_8);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3LeadingSlashKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3LeadingSlashKeyIntegrationTest.javanew file mode 100644index 0000000000..5cbdf780ad--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3LeadingSlashKeyIntegrationTest.java@@ -0,0 +1,284 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import java.net.URI;+import java.net.http.HttpClient;+import java.net.http.HttpRequest;+import java.net.http.HttpResponse;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.*;+import static org.junit.jupiter.api.Assertions.*;++/**+ * Bug condition exploration tests for leading-slash key collision.+ *+ * These tests verify that S3 object keys with leading slashes (e.g., /file.txt)+ * are treated as distinct from keys without leading slashes (e.g., file.txt).+ *+ * The bug is that JAX-RS normalizes // to / in the URL path, so+ * PUT /bucket//file.txt stores under "file.txt" instead of "/file.txt".+ *+ * We use java.net.http.HttpClient for requests with leading-slash keys+ * because RestAssured normalizes double slashes in URL paths.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class S3LeadingSlashKeyIntegrationTest {++ private static final String BUCKET = "test-slash-bucket";++ @io.quarkus.test.common.http.TestHTTPResource("/")+ URI baseUri;++ // --- Setup ---++ @Test+ @Order(0)+ void createBucket() {+ given()+ .when()+ .put("/" + BUCKET)+ .then()+ .statusCode(200);+ }++ // --- Test 1: Distinct Objects ---+ // PUT "content-A" to /test-slash-bucket/file.txt and "content-B" to /test-slash-bucket//file.txt+ // then GET each and assert they return different content.+ // Bug: both map to "file.txt", so content-B overwrites content-A.++ @Test+ @Order(1)+ void distinctObjects_leadingSlashKeyIsSeparateFromNormalKey() throws Exception {+ HttpClient client = HttpClient.newHttpClient();+ String base = baseUri.toString().replaceAll("/$", "");++ // PUT content-A to normal key: /test-slash-bucket/file.txt+ HttpRequest putNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "/file.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("content-A"))+ .build();+ HttpResponse<String> putNormalResp = client.send(putNormal, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, putNormalResp.statusCode(), "PUT normal key should succeed");++ // PUT content-B to leading-slash key: /test-slash-bucket//file.txt (key = /file.txt)+ HttpRequest putSlash = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//file.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("content-B"))+ .build();+ HttpResponse<String> putSlashResp = client.send(putSlash, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, putSlashResp.statusCode(), "PUT leading-slash key should succeed");++ // GET normal key+ HttpRequest getNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "/file.txt"))+ .GET().build();+ HttpResponse<String> getNormalResp = client.send(getNormal, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, getNormalResp.statusCode());+ String normalContent = getNormalResp.body();++ // GET leading-slash key+ HttpRequest getSlash = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//file.txt"))+ .GET().build();+ HttpResponse<String> getSlashResp = client.send(getSlash, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, getSlashResp.statusCode());+ String slashContent = getSlashResp.body();++ // They must be different — /file.txt and file.txt are distinct S3 keys+ assertNotEquals(normalContent, slashContent,+ "Keys 'file.txt' and '/file.txt' should store distinct objects, " ++ "but both returned: " + normalContent);+ }++ // --- Test 2: PUT/GET Round-Trip ---+ // PUT content to /test-slash-bucket//leading.txt, GET it back, assert correct content.+ // Bug: PUT stores under "leading.txt", GET retrieves "leading.txt" — round-trip works+ // but the key is wrong (leading slash stripped).++ @Test+ @Order(2)+ void putGetRoundTrip_leadingSlashKeyPreservesContent() throws Exception {+ HttpClient client = HttpClient.newHttpClient();+ String base = baseUri.toString().replaceAll("/$", "");+ String content = "leading-slash-content-12345";++ // PUT to leading-slash key: /test-slash-bucket//leading.txt+ HttpRequest put = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//leading.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString(content))+ .build();+ HttpResponse<String> putResp = client.send(put, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, putResp.statusCode(), "PUT leading-slash key should succeed");++ // GET the leading-slash key+ HttpRequest get = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//leading.txt"))+ .GET().build();+ HttpResponse<String> getResp = client.send(get, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, getResp.statusCode(), "GET leading-slash key should return 200");+ assertEquals(content, getResp.body(),+ "GET should return the exact content PUT to the leading-slash key");++ // Also verify the normal key "leading.txt" does NOT have this content+ // (it shouldn't exist unless the bug causes collision)+ HttpRequest getNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "/leading.txt"))+ .GET().build();+ HttpResponse<String> getNormalResp = client.send(getNormal, HttpResponse.BodyHandlers.ofString());++ // If the bug exists, this will be 200 with the same content (collision).+ // If fixed, this should be 404 (no object at "leading.txt").+ assertNotEquals(200, getNormalResp.statusCode(),+ "Normal key 'leading.txt' should NOT exist — only '/leading.txt' was uploaded. " ++ "Bug: leading slash was stripped, storing under 'leading.txt' instead of '/leading.txt'");+ }++ // --- Test 3: HEAD Leading Slash ---+ // PUT to /test-slash-bucket//meta.txt, HEAD /test-slash-bucket//meta.txt,+ // assert correct Content-Length and Content-Type.++ @Test+ @Order(3)+ void headLeadingSlashKey_returnsCorrectMetadata() throws Exception {+ HttpClient client = HttpClient.newHttpClient();+ String base = baseUri.toString().replaceAll("/$", "");+ String content = "metadata-test-content";++ // PUT to leading-slash key+ HttpRequest put = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//meta.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString(content))+ .build();+ HttpResponse<String> putResp = client.send(put, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, putResp.statusCode(), "PUT leading-slash key should succeed");++ // HEAD the leading-slash key+ HttpRequest head = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//meta.txt"))+ .method("HEAD", HttpRequest.BodyPublishers.noBody())+ .build();+ HttpResponse<String> headResp = client.send(head, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, headResp.statusCode(), "HEAD leading-slash key should return 200");++ String contentLength = headResp.headers().firstValue("content-length").orElse(null);+ assertNotNull(contentLength, "HEAD should return Content-Length");+ assertEquals(String.valueOf(content.length()), contentLength,+ "Content-Length should match the PUT body size");++ String contentType = headResp.headers().firstValue("content-type").orElse(null);+ assertNotNull(contentType, "HEAD should return Content-Type");+ assertTrue(contentType.contains("text/plain"),+ "Content-Type should be text/plain but was: " + contentType);+ }++ // --- Test 4: DELETE Isolation ---+ // PUT to both /test-slash-bucket/data.txt and /test-slash-bucket//data.txt,+ // DELETE /test-slash-bucket//data.txt, GET /test-slash-bucket/data.txt should still succeed.+ // Bug: DELETE //data.txt actually deletes "data.txt" (the normal key).++ @Test+ @Order(4)+ void deleteIsolation_deletingLeadingSlashKeyDoesNotAffectNormalKey() throws Exception {+ HttpClient client = HttpClient.newHttpClient();+ String base = baseUri.toString().replaceAll("/$", "");++ // PUT to normal key: data.txt+ HttpRequest putNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "/data.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("normal-data"))+ .build();+ client.send(putNormal, HttpResponse.BodyHandlers.ofString());++ // PUT to leading-slash key: /data.txt+ HttpRequest putSlash = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//data.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("slash-data"))+ .build();+ client.send(putSlash, HttpResponse.BodyHandlers.ofString());++ // DELETE the leading-slash key: /data.txt+ HttpRequest deleteSlash = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "//data.txt"))+ .DELETE().build();+ HttpResponse<String> deleteResp = client.send(deleteSlash, HttpResponse.BodyHandlers.ofString());+ assertEquals(204, deleteResp.statusCode(), "DELETE leading-slash key should return 204");++ // GET the normal key — it should still exist+ HttpRequest getNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + BUCKET + "/data.txt"))+ .GET().build();+ HttpResponse<String> getNormalResp = client.send(getNormal, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, getNormalResp.statusCode(),+ "Normal key 'data.txt' should still exist after deleting '/data.txt'. " ++ "Bug: DELETE //data.txt deleted 'data.txt' instead of '/data.txt'");+ assertEquals("normal-data", getNormalResp.body(),+ "Normal key content should be unchanged");+ }++ // --- Test 5: List Shows Both ---+ // PUT to both file.txt and /file.txt in same bucket, list objects,+ // assert both keys appear as separate entries.+ // Bug: both map to "file.txt", so list shows only one entry.++ @Test+ @Order(5)+ void listShowsBoth_leadingSlashAndNormalKeyAppearSeparately() throws Exception {+ HttpClient client = HttpClient.newHttpClient();+ String base = baseUri.toString().replaceAll("/$", "");++ // Use a dedicated bucket to avoid interference from other tests+ String listBucket = "test-slash-list-bucket";+ given().when().put("/" + listBucket).then().statusCode(200);++ // PUT to normal key: file.txt+ HttpRequest putNormal = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + listBucket + "/file.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("normal"))+ .build();+ client.send(putNormal, HttpResponse.BodyHandlers.ofString());++ // PUT to leading-slash key: /file.txt+ HttpRequest putSlash = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + listBucket + "//file.txt"))+ .header("Content-Type", "text/plain")+ .PUT(HttpRequest.BodyPublishers.ofString("slash"))+ .build();+ client.send(putSlash, HttpResponse.BodyHandlers.ofString());++ // List objects in the bucket+ HttpRequest list = HttpRequest.newBuilder()+ .uri(URI.create(base + "/" + listBucket))+ .GET().build();+ HttpResponse<String> listResp = client.send(list, HttpResponse.BodyHandlers.ofString());+ assertEquals(200, listResp.statusCode());++ String body = listResp.body();++ // Count how many <Key> entries appear+ int keyCount = 0;+ int idx = 0;+ while ((idx = body.indexOf("<Key>", idx)) != -1) {+ keyCount++;+ idx += 5;+ }++ // Should have at least 2 entries: "file.txt" and "/file.txt"+ assertTrue(keyCount >= 2,+ "List should show at least 2 objects (file.txt and /file.txt) but found " ++ keyCount + " <Key> entries. Bug: both keys collapsed into one. Response: " + body);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PreservationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PreservationTest.javanew file mode 100644index 0000000000..49eab1dcc1--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PreservationTest.java@@ -0,0 +1,281 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.*;++/**+ * These tests verify that normal S3 object operations (keys without leading slashes)+ * continue to work correctly. They establish a baseline on UNFIXED code and must+ * continue to pass after the leading-slash key collision fix is applied.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class S3PreservationTest {++ private static final String BUCKET = "preserve-bucket";++ // --- Setup ---++ @Test+ @Order(0)+ void createBucket() {+ given()+ .when()+ .put("/" + BUCKET)+ .then()+ .statusCode(200);+ }++ // --- Test 1: Normal Key PUT/GET ---+ // For keys without leading slashes, PUT content and GET returns identical content with correct ETag.++ @Test+ @Order(1)+ void normalKeyPutGet_simpleKey() {+ String content = "hello-simple";++ // PUT+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/simple.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ // GET+ given()+ .when()+ .get("/" + BUCKET + "/simple.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue())+ .body(equalTo(content));+ }++ @Test+ @Order(2)+ void normalKeyPutGet_nestedKey() {+ String content = "hello-nested";++ // PUT with nested path key+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/a/b/c.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ // GET+ given()+ .when()+ .get("/" + BUCKET + "/a/b/c.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue())+ .body(equalTo(content));+ }++ @Test+ @Order(3)+ void normalKeyPutGet_hyphenatedKey() {+ String content = "hello-hyphen";++ // PUT with hyphenated key+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/my-key")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ // GET+ given()+ .when()+ .get("/" + BUCKET + "/my-key")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue())+ .body(equalTo(content));+ }++ // --- Test 2: Interior Slash Preservation ---+ // For keys with interior slashes, PUT and GET round-trip correctly.++ @Test+ @Order(4)+ void interiorSlashPreservation_pathToFile() {+ String content = "interior-slash-content";++ // PUT+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/path/to/file.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ // GET+ given()+ .when()+ .get("/" + BUCKET + "/path/to/file.txt")+ .then()+ .statusCode(200)+ .body(equalTo(content));+ }++ @Test+ @Order(5)+ void interiorSlashPreservation_deeplyNestedPath() {+ String content = "deep-nested-content";++ // PUT+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/a/b/c/d.txt")+ .then()+ .statusCode(200)+ .header("ETag", notNullValue());++ // GET+ given()+ .when()+ .get("/" + BUCKET + "/a/b/c/d.txt")+ .then()+ .statusCode(200)+ .body(equalTo(content));+ }++ // --- Test 3: HEAD Normal Key ---+ // For normal keys, HEAD returns correct Content-Length and Content-Type matching the PUT.++ @Test+ @Order(6)+ void headNormalKey_returnsCorrectMetadata() {+ String content = "head-test-content-12345";++ // PUT+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/head-test.txt")+ .then()+ .statusCode(200);++ // HEAD+ given()+ .when()+ .head("/" + BUCKET + "/head-test.txt")+ .then()+ .statusCode(200)+ .header("Content-Length", equalTo(String.valueOf(content.length())))+ .header("Content-Type", containsString("text/plain"));+ }++ // --- Test 4: DELETE Normal Key ---+ // For normal keys, DELETE removes the object and subsequent GET returns 404.++ @Test+ @Order(7)+ void deleteNormalKey_removesObjectAndGetReturns404() {+ String content = "delete-me";++ // PUT+ given()+ .contentType("text/plain")+ .body(content)+ .when()+ .put("/" + BUCKET + "/to-delete.txt")+ .then()+ .statusCode(200);++ // Verify it exists+ given()+ .when()+ .get("/" + BUCKET + "/to-delete.txt")+ .then()+ .statusCode(200)+ .body(equalTo(content));++ // DELETE+ given()+ .when()+ .delete("/" + BUCKET + "/to-delete.txt")+ .then()+ .statusCode(204);++ // GET should now return 404+ given()+ .when()+ .get("/" + BUCKET + "/to-delete.txt")+ .then()+ .statusCode(404);+ }++ // --- Test 5: List Objects Normal Keys ---+ // PUT multiple normal-key objects, list returns all with correct keys.++ @Test+ @Order(8)+ void listObjectsNormalKeys_returnsAllPutObjects() {+ String listBucket = "preserve-list-bucket";++ // Create bucket+ given()+ .when()+ .put("/" + listBucket)+ .then()+ .statusCode(200);++ // PUT three objects+ given()+ .contentType("text/plain")+ .body("alpha")+ .when()+ .put("/" + listBucket + "/alpha.txt")+ .then()+ .statusCode(200);++ given()+ .contentType("text/plain")+ .body("beta")+ .when()+ .put("/" + listBucket + "/beta.txt")+ .then()+ .statusCode(200);++ given()+ .contentType("text/plain")+ .body("gamma")+ .when()+ .put("/" + listBucket + "/nested/gamma.txt")+ .then()+ .statusCode(200);++ // List objects+ given()+ .when()+ .get("/" + listBucket)+ .then()+ .statusCode(200)+ .body(containsString("<Key>alpha.txt</Key>"))+ .body(containsString("<Key>beta.txt</Key>"))+ .body(containsString("<Key>nested/gamma.txt</Key>"));+ }+}