mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(s3): validate FULL_OBJECT checksum on CompleteMultipartUpload (#1980)
* fix(s3): validate FULL_OBJECT checksum on CompleteMultipartUpload * fix(s3): align FULL_OBJECT checksum validation with AWS's CRC-only support - Reject SHA1/SHA256 combined with ChecksumType=FULL_OBJECT on multipart completion with InvalidRequest, since AWS only supports full-object checksums for CRC32/CRC32C/CRC64NVME (SHA is composite-only). - Report ChecksumType=FULL_OBJECT instead of COMPOSITE once a requested full-object checksum has been validated. - Abort the multipart upload left dangling by the mismatched-checksum test, and add regression coverage for both fixes above.
This commit is contained in:
1 parent
fa9ed9100e
commit
d039780e3a
4 files changed
+214
-9
No files matched your search
@@ -1201,7 +1201,10 @@ public class S3Controller {
|
||||
if (preconditionResponse != null) {
|
||||
return preconditionResponse;
|
||||
}
|
||||
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers);
|
||||
String checksumType = httpHeaders.getHeaderString("x-amz-checksum-type");
|
||||
S3Checksum expectedChecksum = extractChecksumFromHeaders(httpHeaders);
|
||||
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers,
|
||||
checksumType, expectedChecksum);
|
||||
String baseUrl = uriInfo.getBaseUri().toString();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
|
||||
@@ -1463,7 +1463,8 @@ public class S3Service implements Resettable {
|
||||
sseCustomerHeaders.algorithm(), sseCustomerHeaders.key(), sseCustomerHeaders.keyMd5());
|
||||
}
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers) {
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
throw new AwsException("NoSuchUpload",
|
||||
@@ -1494,6 +1495,12 @@ public class S3Service implements Resettable {
|
||||
|
||||
byte[] allData = combined.toByteArray();
|
||||
|
||||
boolean fullObjectChecksumRequested = "FULL_OBJECT".equalsIgnoreCase(checksumType)
|
||||
&& expectedChecksum != null && expectedChecksum.hasAnyValue();
|
||||
if (fullObjectChecksumRequested) {
|
||||
validateFullObjectChecksum(allData, expectedChecksum);
|
||||
}
|
||||
|
||||
// Composite ETag: MD5 of concatenated part MD5s, suffixed with part count
|
||||
String compositeETag = "\"" + bytesToHex(md.digest()) + "-" + partNumbers.size() + "\"";
|
||||
|
||||
@@ -1501,6 +1508,9 @@ public class S3Service implements Resettable {
|
||||
.map(num -> copyPart(upload.getParts().get(num)))
|
||||
.toList();
|
||||
S3Checksum checksum = buildChecksum(allData, completedParts, true, upload.getChecksumAlgorithm());
|
||||
if (fullObjectChecksumRequested) {
|
||||
checksum.setChecksumType("FULL_OBJECT");
|
||||
}
|
||||
S3Object object = storeObject(bucket, key, allData, upload.getContentType(), upload.getMetadata(),
|
||||
checksum, completedParts,
|
||||
new PutObjectOptions()
|
||||
@@ -2398,6 +2408,25 @@ public class S3Service implements Resettable {
|
||||
throw new AwsException("InvalidArgument", "The checksum algorithm you specified is not supported.", 400);
|
||||
}
|
||||
|
||||
private static void validateFullObjectChecksum(byte[] data, S3Checksum expected) {
|
||||
if (expected.getChecksumSHA1() != null || expected.getChecksumSHA256() != null) {
|
||||
throw new AwsException("InvalidRequest",
|
||||
"The FULL_OBJECT checksum type is not supported with the SHA1 or SHA256 checksum algorithm. "
|
||||
+ "Full object checksums are only supported with the CRC32, CRC32C, and CRC64NVME checksum algorithms.",
|
||||
400);
|
||||
}
|
||||
if (expected.getChecksumCRC32() != null && !expected.getChecksumCRC32().equals(S3Checksum.crc32Base64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC32 checksum you specified did not match the payload.", 400);
|
||||
}
|
||||
if (expected.getChecksumCRC32C() != null && !expected.getChecksumCRC32C().equals(S3Checksum.crc32cBase64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC32C checksum you specified did not match the payload.", 400);
|
||||
}
|
||||
if (expected.getChecksumCRC64NVME() != null
|
||||
&& !expected.getChecksumCRC64NVME().equals(S3Checksum.crc64NvmeBase64(data))) {
|
||||
throw new AwsException("BadDigest", "The CRC64NVME checksum you specified did not match the payload.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static S3Checksum buildChecksum(byte[] data, List<Part> parts, boolean multipartUpload) {
|
||||
return buildChecksum(data, parts, multipartUpload, null);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
@@ -559,6 +560,176 @@ class S3MultipartIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void completeMultipartUploadRejectsMismatchedFullObjectChecksum() {
|
||||
String key = "checksum-mismatch-multipart.bin";
|
||||
String newUploadId = given()
|
||||
.header("x-amz-checksum-algorithm", "CRC32")
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.body("Part1Data-Hello")
|
||||
.when()
|
||||
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String completeXml = """
|
||||
<CompleteMultipartUpload>
|
||||
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
|
||||
</CompleteMultipartUpload>""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.header("x-amz-checksum-type", "FULL_OBJECT")
|
||||
.header("x-amz-checksum-crc32", "AAAAAA==")
|
||||
.body(completeXml)
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"));
|
||||
|
||||
// The checksum mismatch rejects completion before the upload is consumed, so abort it explicitly.
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(21)
|
||||
void completeMultipartUploadAcceptsMatchingFullObjectChecksum() {
|
||||
String key = "checksum-match-multipart.bin";
|
||||
String data = "Part1Data-Hello";
|
||||
String correctCrc32 = S3Checksum.crc32Base64(data.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
String newUploadId = given()
|
||||
.header("x-amz-checksum-algorithm", "CRC32")
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.body(data)
|
||||
.when()
|
||||
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String completeXml = """
|
||||
<CompleteMultipartUpload>
|
||||
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
|
||||
</CompleteMultipartUpload>""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.header("x-amz-checksum-type", "FULL_OBJECT")
|
||||
.header("x-amz-checksum-crc32", correctCrc32)
|
||||
.body(completeXml)
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<CompleteMultipartUploadResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(22)
|
||||
void completeMultipartUploadRejectsFullObjectShaChecksum() {
|
||||
String key = "checksum-sha-full-object-multipart.bin";
|
||||
String newUploadId = given()
|
||||
.header("x-amz-checksum-algorithm", "SHA256")
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.body("Part1Data-Hello")
|
||||
.when()
|
||||
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String completeXml = """
|
||||
<CompleteMultipartUpload>
|
||||
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
|
||||
</CompleteMultipartUpload>""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.header("x-amz-checksum-type", "FULL_OBJECT")
|
||||
.header("x-amz-checksum-sha256", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
|
||||
.body(completeXml)
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidRequest"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(23)
|
||||
void completeMultipartUploadReportsFullObjectChecksumType() {
|
||||
String key = "checksum-type-multipart.bin";
|
||||
String data = "Part1Data-Hello";
|
||||
String correctCrc32 = S3Checksum.crc32Base64(data.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
String newUploadId = given()
|
||||
.header("x-amz-checksum-algorithm", "CRC32")
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.body(data)
|
||||
.when()
|
||||
.put("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId + "&partNumber=1")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String completeXml = """
|
||||
<CompleteMultipartUpload>
|
||||
<Part><PartNumber>1</PartNumber><ETag>etag1</ETag></Part>
|
||||
</CompleteMultipartUpload>""";
|
||||
|
||||
given()
|
||||
.contentType("application/xml")
|
||||
.header("x-amz-checksum-type", "FULL_OBJECT")
|
||||
.header("x-amz-checksum-crc32", correctCrc32)
|
||||
.body(completeXml)
|
||||
.when()
|
||||
.post("/" + BUCKET + "/" + key + "?uploadId=" + newUploadId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("x-amz-object-attributes", "Checksum")
|
||||
.when()
|
||||
.get("/" + BUCKET + "/" + key + "?attributes")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ChecksumType>FULL_OBJECT</ChecksumType>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(24)
|
||||
void cleanUp() {
|
||||
given().when().delete("/" + BUCKET + "/" + KEY).then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET + "/tagged-multipart.bin").then().statusCode(204);
|
||||
@@ -566,6 +737,8 @@ class S3MultipartIntegrationTest {
|
||||
given().when().delete("/" + BUCKET + "/copy-dest.bin").then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET + "/sse-c-multipart.bin").then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET + "/sse-c-source-for-copy.bin").then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET + "/checksum-match-multipart.bin").then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET + "/checksum-type-multipart.bin").then().statusCode(204);
|
||||
given().when().delete("/" + BUCKET).then().statusCode(204);
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ class S3MultipartServiceTest {
|
||||
s3Service.uploadPart("test-bucket", "file.bin", upload.getUploadId(), 2, "part2".getBytes());
|
||||
|
||||
S3Object result = s3Service.completeMultipartUpload("test-bucket", "file.bin",
|
||||
upload.getUploadId(), List.of(1, 2));
|
||||
upload.getUploadId(), List.of(1, 2), null, null);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("text/plain", result.getContentType());
|
||||
@@ -101,7 +101,7 @@ class S3MultipartServiceTest {
|
||||
Map.of("token", "abc-123", "teamId", "42"));
|
||||
s3Service.uploadPart("test-bucket", "tagged.bin", upload.getUploadId(), 1, "part1".getBytes());
|
||||
|
||||
s3Service.completeMultipartUpload("test-bucket", "tagged.bin", upload.getUploadId(), List.of(1));
|
||||
s3Service.completeMultipartUpload("test-bucket", "tagged.bin", upload.getUploadId(), List.of(1), null, null);
|
||||
|
||||
Map<String, String> tags = s3Service.getObjectTagging("test-bucket", "tagged.bin");
|
||||
assertEquals(2, tags.size());
|
||||
@@ -114,7 +114,7 @@ class S3MultipartServiceTest {
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "untagged.bin", null);
|
||||
s3Service.uploadPart("test-bucket", "untagged.bin", upload.getUploadId(), 1, "part1".getBytes());
|
||||
|
||||
s3Service.completeMultipartUpload("test-bucket", "untagged.bin", upload.getUploadId(), List.of(1));
|
||||
s3Service.completeMultipartUpload("test-bucket", "untagged.bin", upload.getUploadId(), List.of(1), null, null);
|
||||
|
||||
assertTrue(s3Service.getObjectTagging("test-bucket", "untagged.bin").isEmpty());
|
||||
}
|
||||
@@ -126,7 +126,7 @@ class S3MultipartServiceTest {
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
s3Service.completeMultipartUpload("test-bucket", "file.bin",
|
||||
upload.getUploadId(), List.of(1, 2)));
|
||||
upload.getUploadId(), List.of(1, 2), null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -163,7 +163,7 @@ class S3MultipartServiceTest {
|
||||
s3Service.uploadPart("test-bucket", "versioned.bin", upload.getUploadId(), 1, "data".getBytes());
|
||||
|
||||
S3Object result = s3Service.completeMultipartUpload("test-bucket", "versioned.bin",
|
||||
upload.getUploadId(), List.of(1));
|
||||
upload.getUploadId(), List.of(1), null, null);
|
||||
|
||||
assertNotNull(result.getVersionId(), "Versioned bucket should produce a versionId");
|
||||
}
|
||||
@@ -172,7 +172,7 @@ class S3MultipartServiceTest {
|
||||
void completeMultipartUploadCleansUp() {
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "file.bin", null);
|
||||
s3Service.uploadPart("test-bucket", "file.bin", upload.getUploadId(), 1, "data".getBytes());
|
||||
s3Service.completeMultipartUpload("test-bucket", "file.bin", upload.getUploadId(), List.of(1));
|
||||
s3Service.completeMultipartUpload("test-bucket", "file.bin", upload.getUploadId(), List.of(1), null, null);
|
||||
|
||||
// Should no longer be in active uploads
|
||||
List<MultipartUpload> uploads = s3Service.listMultipartUploads("test-bucket");
|
||||
@@ -184,7 +184,7 @@ class S3MultipartServiceTest {
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "parts.bin", "application/octet-stream");
|
||||
s3Service.uploadPart("test-bucket", "parts.bin", upload.getUploadId(), 1, "abc".getBytes(StandardCharsets.UTF_8));
|
||||
s3Service.uploadPart("test-bucket", "parts.bin", upload.getUploadId(), 2, "def".getBytes(StandardCharsets.UTF_8));
|
||||
s3Service.completeMultipartUpload("test-bucket", "parts.bin", upload.getUploadId(), List.of(1, 2));
|
||||
s3Service.completeMultipartUpload("test-bucket", "parts.bin", upload.getUploadId(), List.of(1, 2), null, null);
|
||||
|
||||
GetObjectAttributesResult attributes = s3Service.getObjectAttributes("test-bucket", "parts.bin", null,
|
||||
Set.of(ObjectAttributeName.OBJECT_PARTS, ObjectAttributeName.CHECKSUM),
|
||||
|
||||
Reference in new issue
Block a user