From 55c33d7ad38a84e98bcac2028c869838acbdcbcd Mon Sep 17 00:00:00 2001 From: Tomas Kislan Date: Fri, 30 Oct 2026 13:52:53 +0100 Subject: [PATCH] fix(s3): keep Content-Encoding and Cache-Control on multipart uploads (#7000) * fix(s3): keep Content-Encoding and Cache-Control on multipart uploads CreateMultipartUpload ignored both headers, so an object completed from a multipart upload came back from HeadObject and GetObject without them while a single PutObject kept them. A client that decodes by Content-Encoding (a browser, curl --compressed) then received raw gzip bytes for any object large enough to be uploaded in parts. The controller now reads both headers on the ?uploads branch, stripping the aws-chunked token as PutObject does. S3Service records them on the MultipartUpload and applies them to the object when the upload completes. An upload created without either header still produces an object with neither. * test(s3): use .bin keys and text/plain in the multipart encoding tests Match the neighbouring tests, which name objects *.bin; the behaviour under test is not specific to YAML. The content type is text/plain rather than application/octet-stream because MultipartUpload falls back to application/octet-stream when CreateMultipartUpload carries no Content-Type, so asserting that value could not catch a dropped header. --------- Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> --- .../src/test/java/com/floci/test/S3Test.java | 40 +++++++++++++ docs/services/s3.md | 1 + .../floci/services/s3/S3Controller.java | 3 + .../floci/services/s3/S3Service.java | 10 +++- .../services/s3/model/MultipartUpload.java | 8 +++ .../s3/S3MultipartIntegrationTest.java | 59 +++++++++++++++++++ .../services/s3/S3MultipartServiceTest.java | 14 +++++ 7 files changed, 132 insertions(+), 3 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java index a4c441ac44..03d935dcfb 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java @@ -671,6 +671,46 @@ class S3Test { } } + @Test + @Order(36) + void multipartUploadKeepsContentEncodingAndCacheControl() throws IOException { + String bucket = TestFixtures.uniqueName("mpu-encoding-bucket"); + String key = "encoded.bin"; + byte[] body; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); + GZIPOutputStream gz = new GZIPOutputStream(baos)) { + gz.write("version: 1.0.0".getBytes(StandardCharsets.UTF_8)); + gz.finish(); + body = baos.toByteArray(); + } + s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build()); + try { + String uploadId = s3.createMultipartUpload(CreateMultipartUploadRequest.builder() + .bucket(bucket).key(key) + .contentType("text/plain") + .contentEncoding("gzip") + .cacheControl("no-cache") + .build()).uploadId(); + String eTag = s3.uploadPart(UploadPartRequest.builder() + .bucket(bucket).key(key).uploadId(uploadId).partNumber(1).build(), + RequestBody.fromBytes(body)).eTag(); + s3.completeMultipartUpload(CompleteMultipartUploadRequest.builder() + .bucket(bucket).key(key).uploadId(uploadId) + .multipartUpload(CompletedMultipartUpload.builder() + .parts(CompletedPart.builder().partNumber(1).eTag(eTag).build()).build()) + .build()); + + HeadObjectResponse head = s3.headObject(HeadObjectRequest.builder() + .bucket(bucket).key(key).build()); + + assertThat(head.contentEncoding()).isEqualTo("gzip"); + assertThat(head.cacheControl()).isEqualTo("no-cache"); + assertThat(head.contentType()).isEqualTo("text/plain"); + } finally { + deleteObjectAndBucketQuietly(bucket, key); + } + } + private void assertInlineTaggingHeaderRejected(String rawTaggingHeader, int expectedStatus, String expectedErrorCode) { String bucket = TestFixtures.uniqueName("inline-tag-reject"); String key = "rejected.txt"; diff --git a/docs/services/s3.md b/docs/services/s3.md index 41d76047a7..0c83279e01 100644 --- a/docs/services/s3.md +++ b/docs/services/s3.md @@ -740,6 +740,7 @@ Floci now persists and returns the following object attribute state on S3 object - user metadata from `x-amz-meta-*` - storage class from `x-amz-storage-class` +- `Content-Encoding` (without the `aws-chunked` token) and `Cache-Control` from `PutObject` and multipart initiation, replayed on `GetObject` and `HeadObject` - checksum metadata for object reads and `GetObjectAttributes` - multipart part manifests for `GetObjectAttributes(ObjectParts)` - `CompleteMultipartUpload` validates `x-amz-mp-object-size` against the total size of the selected parts. A mismatch returns `InvalidRequest` without replacing the object or discarding the upload, so the request can be retried with the correct size. Blank, malformed, overflowing, or negative sizes also return `InvalidRequest` and preserve the object and upload 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 52e0ef2533..f902d50d77 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 @@ -1651,6 +1651,9 @@ public class S3Controller { new PutObjectOptions() .withStorageClass(httpHeaders.getHeaderString("x-amz-storage-class")) .withContentDisposition(httpHeaders.getHeaderString("Content-Disposition")) + .withContentEncoding(toPersistedContentEncoding( + httpHeaders.getHeaderString("Content-Encoding"))) + .withCacheControl(httpHeaders.getHeaderString("Cache-Control")) .withServerSideEncryption(httpHeaders.getHeaderString("x-amz-server-side-encryption")) .withSseKmsKeyId(httpHeaders.getHeaderString( "x-amz-server-side-encryption-aws-kms-key-id")) diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index 3baa802a82..b7f0fbf0a0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -4603,9 +4603,9 @@ public class S3Service implements Resettable, ResourceProvider { /** * Starts a multipart upload of an object that gets {@code objectOptions} when it completes, as a - * PutObject takes them: its storage class, encryption, checksum algorithm, canned ACL or grants, - * tags and Object Lock settings. The ACL is checked here, so one S3 refuses fails the upload's - * creation rather than its completion. + * PutObject takes them: its storage class, content encoding, cache control, encryption, checksum + * algorithm, canned ACL or grants, tags and Object Lock settings. The ACL is checked here, so one + * S3 refuses fails the upload's creation rather than its completion. */ public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType, Map metadata, String checksumType, @@ -4644,6 +4644,8 @@ public class S3Service implements Resettable, ResourceProvider { } upload.setStorageClass(ObjectAttributeName.normalizeStorageClass(storageClass)); upload.setContentDisposition(contentDisposition); + upload.setContentEncoding(objectOptions.getContentEncoding()); + upload.setCacheControl(objectOptions.getCacheControl()); upload.setServerSideEncryption(normalizedServerSideEncryption); upload.setSseKmsKeyId("aws:kms".equals(normalizedServerSideEncryption) ? sseKmsKeyId : null); if (customerKey != null) { @@ -5119,6 +5121,8 @@ public class S3Service implements Resettable, ResourceProvider { new PutObjectOptions() .withStorageClass(upload.getStorageClass()) .withContentDisposition(upload.getContentDisposition()) + .withContentEncoding(upload.getContentEncoding()) + .withCacheControl(upload.getCacheControl()) .withServerSideEncryption(upload.getServerSideEncryption()) .withSseKmsKeyId(upload.getSseKmsKeyId()) .withAcl(upload.getAcl()) diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/MultipartUpload.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/MultipartUpload.java index ccaa6f7284..62e47822ff 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/model/MultipartUpload.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/MultipartUpload.java @@ -17,6 +17,8 @@ public class MultipartUpload { private String contentType; private String storageClass; private String contentDisposition; + private String contentEncoding; + private String cacheControl; private String serverSideEncryption; private String sseKmsKeyId; private String sseCustomerAlgorithm; @@ -73,6 +75,12 @@ public class MultipartUpload { public String getContentDisposition() { return contentDisposition; } public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } + public String getContentEncoding() { return contentEncoding; } + public void setContentEncoding(String contentEncoding) { this.contentEncoding = contentEncoding; } + + public String getCacheControl() { return cacheControl; } + public void setCacheControl(String cacheControl) { this.cacheControl = cacheControl; } + public String getServerSideEncryption() { return serverSideEncryption; } public void setServerSideEncryption(String serverSideEncryption) { this.serverSideEncryption = serverSideEncryption; } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java index df7acbdb73..5d6778c3b3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3MultipartIntegrationTest.java @@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.s3; import io.github.hectorvent.floci.services.s3.model.S3Checksum; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.config.DecoderConfig; +import io.restassured.config.RestAssuredConfig; import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; @@ -215,6 +217,8 @@ class S3MultipartIntegrationTest { .then() .statusCode(200) .header("Content-Disposition", equalTo("attachment; filename=\"multipart.bin\"")) + .header("Content-Encoding", nullValue()) + .header("Cache-Control", nullValue()) .header("x-amz-server-side-encryption", equalTo("AES256")) .header("x-amz-meta-owner", equalTo("team-a")) .header("x-amz-storage-class", equalTo("STANDARD_IA")) @@ -1559,6 +1563,60 @@ class S3MultipartIntegrationTest { @Test @Order(40) + void completedObjectKeepsContentEncodingAndCacheControl() { + String key = "encoded-multipart.bin"; + String encodedUploadId = given() + .header("Content-Type", "text/plain") + .header("Content-Encoding", "gzip,aws-chunked") + .header("Cache-Control", "no-cache") + .when() + .post("/" + BUCKET + "/" + key + "?uploads") + .then() + .statusCode(200) + .extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId"); + + String eTag = given() + .body(PART_1) + .when() + .put("/" + BUCKET + "/" + key + "?uploadId=" + encodedUploadId + "&partNumber=1") + .then() + .statusCode(200) + .extract().header("ETag"); + + given() + .contentType("application/xml") + .body(singlePartCompleteXml(eTag, "")) + .when() + .post("/" + BUCKET + "/" + key + "?uploadId=" + encodedUploadId) + .then() + .statusCode(200) + .body(containsString("