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>
This commit is contained in:
Tomas KislanandPriamo Germosen authored and GitHub committed 2026-10-30 08:52:53 -04:00
1 parent ca3599f8c9
commit 55c33d7ad3
7 files changed
+132 -3

No files matched your search

@@ -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";
+1
View File
@@ -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
@@ -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"))
@@ -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<String, String> 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())
@@ -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; }
@@ -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("<CompleteMultipartUploadResult"));
given()
.when()
.head("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("Content-Encoding", equalTo("gzip"))
.header("Cache-Control", equalTo("no-cache"))
// RestAssured appends a default charset to the Content-Type it sends
.header("Content-Type", startsWith("text/plain"));
RestAssuredConfig noDecompress = RestAssuredConfig.config()
.decoderConfig(DecoderConfig.decoderConfig().noContentDecoders());
given()
.config(noDecompress)
.when()
.get("/" + BUCKET + "/" + key)
.then()
.statusCode(200)
.header("Content-Encoding", equalTo("gzip"))
.header("Cache-Control", equalTo("no-cache"))
.body(equalTo(PART_1));
}
@Test
@Order(41)
void cleanUp() {
given().when().delete("/" + BUCKET + "/copy-of-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/" + KEY).then().statusCode(204);
@@ -1577,6 +1635,7 @@ class S3MultipartIntegrationTest {
given().when().delete("/" + BUCKET + "/range-source.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/one-part-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/one-part-composite.bin").then().statusCode(204);
given().when().delete("/" + BUCKET + "/encoded-multipart.bin").then().statusCode(204);
given().when().delete("/" + BUCKET).then().statusCode(204);
}
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.PutObjectOptions;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.BeforeEach;
@@ -347,6 +348,19 @@ class S3MultipartServiceTest {
assertTrue(s3Service.getObjectTagging("test-bucket", "untagged.bin").isEmpty());
}
@Test
void completeMultipartUploadAppliesContentEncodingAndCacheControl() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "encoded.bin", "text/plain",
null, null, new PutObjectOptions().withContentEncoding("gzip").withCacheControl("no-cache"));
s3Service.uploadPart("test-bucket", "encoded.bin", upload.getUploadId(), 1, "part1".getBytes());
s3Service.completeMultipartUpload("test-bucket", "encoded.bin", upload.getUploadId(), List.of(1), null, null);
S3Object object = s3Service.headObject("test-bucket", "encoded.bin");
assertEquals("gzip", object.getContentEncoding());
assertEquals("no-cache", object.getCacheControl());
}
@Test
void completeMultipartUploadMissingPart() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "file.bin", null);