Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): omit body-description headers and user metadata from PutObject response (#948)

Darrell Draney70958b10917f
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 1c89cbd6e1..f4cf335217 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
@@ -37,7 +37,10 @@ import software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.Tagging;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.zip.GZIPOutputStream;
import static org.assertj.core.api.Assertions.*;
@@ -387,4 +390,41 @@ class S3Test {
void deleteBucket() {
s3.deleteBucket(DeleteBucketRequest.builder().bucket(BUCKET).build());
}
+
+ @Test
+ @Order(26)
+ void putObjectWithContentEncodingDoesNotBreakSdkDecompression() throws IOException {
+ String bucket = TestFixtures.uniqueName("gzip-bucket");
+ String key = "gzipped.json";
+ byte[] body;
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ GZIPOutputStream gz = new GZIPOutputStream(baos)) {
+ gz.write("{\"hello\":\"world\"}".getBytes(StandardCharsets.UTF_8));
+ gz.finish();
+ body = baos.toByteArray();
+ }
+ s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());
+ try {
+ var putResponse = s3.putObject(PutObjectRequest.builder()
+ .bucket(bucket).key(key)
+ .contentEncoding("gzip")
+ .contentType("application/json")
+ .build(),
+ RequestBody.fromBytes(body));
+
+ assertThat(putResponse.eTag()).isNotBlank();
+
+ // HEAD response has a body, so Content-Encoding belongs there.
+ HeadObjectResponse head = s3.headObject(HeadObjectRequest.builder()
+ .bucket(bucket).key(key).build());
+ assertThat(head.contentEncoding()).isEqualTo("gzip");
+ } finally {
+ try {
+ s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build());
+ } catch (Exception ignored) {}
+ try {
+ s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());
+ } catch (Exception ignored) {}
+ }
+ }
}
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 5ce6e593ab..aa9dbcbba5 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
@@ -502,7 +502,7 @@ public class S3Controller {
if (obj.getVersionId() != null) {
resp.header("x-amz-version-id", obj.getVersionId());
}
- appendObjectHeaders(resp, obj);
+ appendPutObjectResponseHeaders(resp, obj);
return resp.build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -1516,6 +1516,19 @@ public class S3Controller {
appendObjectHeaders(resp, obj, ResponseHeaderOverrides.NONE);
}
+ // PutObject's response body is empty — body-describing headers and x-amz-meta-*
+ // must not be emitted, or SDK clients will try to decompress and fail.
+ private void appendPutObjectResponseHeaders(Response.ResponseBuilder resp, S3Object obj) {
+ if (obj.getStorageClass() != null) {
+ resp.header("x-amz-storage-class", obj.getStorageClass());
+ }
+ if (obj.getServerSideEncryption() != null) {
+ resp.header("x-amz-server-side-encryption", obj.getServerSideEncryption());
+ }
+ appendChecksumHeaders(resp, obj.getChecksum());
+ appendLockHeaders(resp, obj);
+ }
+
private void appendObjectHeaders(Response.ResponseBuilder resp, S3Object obj, ResponseHeaderOverrides overrides) {
if (obj.getStorageClass() != null) {
resp.header("x-amz-storage-class", obj.getStorageClass());
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
index 39d647dc18..ac1c66795a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java
@@ -817,7 +817,10 @@ class S3IntegrationTest {
.put("/encoding-test-bucket/encoded.txt")
.then()
.statusCode(200)
- .header("ETag", notNullValue());
+ .header("ETag", notNullValue())
+ .header("Content-Encoding", nullValue())
+ .header("Content-Disposition", nullValue())
+ .header("Cache-Control", nullValue());
}
@Test