fix(s3): return x-amz-version-id from presigned POST (#2608)

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
This commit is contained in:
Ricky Rattlesnake authored and GitHub committed 2026-08-28 08:55:07 -04:00
1 parent e471946f44
commit 066e6c904b
2 files changed
+54 -3

No files matched your search

@@ -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,
@@ -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("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.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));
}
}