From 066e6c904b4c623b56f14ee01f5a8e44e7fe684f Mon Sep 17 00:00:00 2001 From: Ricky Rattlesnake <4549582+rickyrattlesnake@users.noreply.github.com> Date: Fri, 28 Aug 2026 22:55:07 +1000 Subject: [PATCH] fix(s3): return x-amz-version-id from presigned POST (#2608) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A presigned POST to a versioning-enabled bucket creates a new version but the response omits x-amz-version-id, so the caller cannot discover it. A browser upload has no other channel — there is no SDK response to read — which breaks the common direct-to-S3 pattern where the browser uploads, reads the version from the response, and reports it back to the application. The handler already has the S3Object returned by putObject; it simply never read getVersionId(). Every other write path in S3Controller sets the header behind the same null guard. Fixes #2607 --- .../floci/services/s3/S3Controller.java | 9 ++-- .../s3/S3PresignedPostIntegrationTest.java | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) 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 0b89eb5b16..2393efd5be 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 @@ -2711,10 +2711,13 @@ public class S3Controller { .elem("ETag", obj.getETag()) .end("PostResponse") .build(); - return Response.status(204) + var response = Response.status(204) .header("ETag", obj.getETag()) - .header("Location", bucket + "/" + key) - .build(); + .header("Location", bucket + "/" + key); + if (obj.getVersionId() != null) { + response.header("x-amz-version-id", obj.getVersionId()); + } + return response.build(); } private void validatePolicyConditions(String policyBase64, String bucket, diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java index b9d4663ff4..3d15dfc774 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.Matchers.*; class S3PresignedPostIntegrationTest { private static final String BUCKET = "presigned-post-bucket"; + private static final String VERSIONED_BUCKET = "presigned-post-versioned-bucket"; private static final DateTimeFormatter AMZ_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC); @@ -672,4 +673,51 @@ class S3PresignedPostIntegrationTest { } """.formatted(expiration, bucket, keyPrefix, contentTypePrefix, minSize, maxSize); } + + @Test + @Order(110) + void presignedPostReturnsVersionIdOnAVersionedBucket() { + given() + .when() + .put("/" + VERSIONED_BUCKET) + .then() + .statusCode(200); + + given() + .body("Enabled") + .when() + .put("/" + VERSIONED_BUCKET + "?versioning") + .then() + .statusCode(200); + + String key = "uploads/versioned.txt"; + String contentType = "text/plain"; + String policy = buildPolicy(VERSIONED_BUCKET, key, contentType, 0, 10485760); + String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8)); + + String versionId = + given() + .multiPart("key", key) + .multiPart("Content-Type", contentType) + .multiPart("policy", policyBase64) + .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256") + .multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now())) + .multiPart("file", "versioned.txt", + "Hello from a versioned presigned POST!".getBytes(StandardCharsets.UTF_8), contentType) + .when() + .post("/" + VERSIONED_BUCKET) + .then() + .statusCode(204) + .extract() + .header("x-amz-version-id"); + + assertThat(versionId, is(notNullValue())); + + given() + .when() + .head("/" + VERSIONED_BUCKET + "/" + key) + .then() + .statusCode(200) + .header("x-amz-version-id", is(versionId)); + } }