Fix(s3)/1560 fix copy object (#1670)

* fix(s3): add support for S3 CopyObject operation and multipart uploads

* fix(s3): correct checksum type when copying multipart objects in S3

* fix(s3): validate checksum algorithms and support standard header fallbacks

* fix(s3): handle integration tests

* fix(s3): return InvalidRequest for valid but unsupported checksum algorithms
This commit is contained in:
Prakhar Kapoor authored and GitHub committed 2026-07-06 21:44:36 -05:00
1 parent 2288da7f23
commit caacbc8afa
6 files changed
+273 -110

No files matched your search

@@ -517,7 +517,7 @@ public class S3Controller {
return handleUploadPartCopy(copySource, bucket, key, uploadId, partNumber, httpHeaders);
}
byte[] partData = decodeAwsChunked(body, contentEncoding, contentSha256);
validateChecksumHeaders(httpHeaders, partData, httpHeaders.getHeaderString("x-amz-sdk-checksum-algorithm"));
validateChecksumHeaders(httpHeaders, partData, getChecksumAlgorithm(httpHeaders));
String eTag = s3Service.uploadPart(bucket, key, uploadId, partNumber, partData,
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"),
@@ -541,7 +541,7 @@ public class S3Controller {
Instant retainUntil = retainUntilStr != null ? Instant.parse(retainUntilStr) : null;
byte[] data = decodeAwsChunked(body, contentEncoding, contentSha256);
String checksumAlgorithm = httpHeaders.getHeaderString("x-amz-sdk-checksum-algorithm");
String checksumAlgorithm = getChecksumAlgorithm(httpHeaders);
validateChecksumHeaders(httpHeaders, data, checksumAlgorithm);
String persistedEncoding = toPersistedContentEncoding(contentEncoding);
String contentDisposition = httpHeaders.getHeaderString("Content-Disposition");
@@ -989,7 +989,8 @@ public class S3Controller {
httpHeaders.getHeaderString("x-amz-acl"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-algorithm"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key"),
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"));
httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5"),
getChecksumAlgorithm(httpHeaders));
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("InitiateMultipartUploadResult", AwsNamespaces.S3)
@@ -1804,6 +1805,7 @@ public class S3Controller {
.withCopySourceSseCustomerAlgorithm(httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-algorithm"))
.withCopySourceSseCustomerKey(httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-key"))
.withCopySourceSseCustomerKeyMd5(httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-key-MD5"))
.withChecksumAlgorithm(getChecksumAlgorithm(httpHeaders))
.withAcl(cannedAcl));
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
@@ -1993,6 +1995,14 @@ public class S3Controller {
return checksum;
}
private String getChecksumAlgorithm(HttpHeaders httpHeaders) {
String algorithm = httpHeaders.getHeaderString("x-amz-checksum-algorithm");
if (algorithm == null || algorithm.isBlank()) {
algorithm = httpHeaders.getHeaderString("x-amz-sdk-checksum-algorithm");
}
return algorithm;
}
private void validateChecksumHeaders(HttpHeaders httpHeaders, byte[] data, String algorithm) {
String sha1 = httpHeaders.getHeaderString("x-amz-checksum-sha1");
if (sha1 != null && !sha1.equals(S3Checksum.sha1Base64(data))) {
@@ -342,9 +342,10 @@ public class S3Service implements Resettable {
object.getMetadata().putAll(metadata);
}
object.setStorageClass(ObjectAttributeName.normalizeStorageClass(effectiveOptions.getStorageClass()));
String validatedChecksumAlgorithm = validateAndNormalizeChecksumAlgorithm(effectiveOptions.getChecksumAlgorithm());
S3Checksum resolvedChecksum = checksum != null ? copyChecksum(checksum)
: effectiveOptions.getClientChecksum() != null ? copyChecksum(effectiveOptions.getClientChecksum())
: buildChecksum(data, parts, false, effectiveOptions.getChecksumAlgorithm());
: buildChecksum(data, parts, false, validatedChecksumAlgorithm);
object.setChecksum(resolvedChecksum);
object.setParts(copyParts(parts));
object.setContentEncoding(effectiveOptions.getContentEncoding());
@@ -1207,13 +1208,14 @@ public class S3Service implements Resettable {
Map<String, String> metadata, String storageClass,
String contentDisposition, String serverSideEncryption, String acl) {
return initiateMultipartUpload(bucket, key, contentType, metadata, storageClass, contentDisposition,
serverSideEncryption, acl, null, null, null);
serverSideEncryption, acl, null, null, null, null);
}
public MultipartUpload initiateMultipartUpload(String bucket, String key, String contentType,
Map<String, String> metadata, String storageClass,
String contentDisposition, String serverSideEncryption, String acl,
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5) {
String sseCustomerAlgorithm, String sseCustomerKey, String sseCustomerKeyMd5,
String checksumAlgorithm) {
ensureBucketExists(bucket);
if (acl != null && !acl.isBlank()) {
cannedObjectAclXml(acl);
@@ -1233,6 +1235,7 @@ public class S3Service implements Resettable {
upload.setSseCustomerKeyMd5(customerKey.keyMd5());
}
upload.setAcl(acl);
upload.setChecksumAlgorithm(validateAndNormalizeChecksumAlgorithm(checksumAlgorithm));
if (inMemory) {
memoryMultipartStore.put(upload.getUploadId(), new ConcurrentHashMap<>());
@@ -1279,7 +1282,7 @@ public class S3Service implements Resettable {
String eTag = computeETag(data);
Part part = new Part(partNumber, eTag, data.length);
part.setChecksum(buildChecksum(data, List.of(part), true));
part.setChecksum(buildChecksum(data, List.of(part), true, upload.getChecksumAlgorithm()));
upload.getParts().put(partNumber, part);
LOG.debugv("Uploaded part {0} for upload {1} ({2} bytes)", partNumber, uploadId, data.length);
return eTag;
@@ -1357,7 +1360,7 @@ public class S3Service implements Resettable {
List<Part> completedParts = partNumbers.stream()
.map(num -> copyPart(upload.getParts().get(num)))
.toList();
S3Checksum checksum = buildChecksum(allData, completedParts, true);
S3Checksum checksum = buildChecksum(allData, completedParts, true, upload.getChecksumAlgorithm());
S3Object object = storeObject(bucket, key, allData, upload.getContentType(), upload.getMetadata(),
checksum, completedParts,
new PutObjectOptions()
@@ -2158,6 +2161,20 @@ public class S3Service implements Resettable {
}
}
public static String validateAndNormalizeChecksumAlgorithm(String algorithm) {
if (algorithm == null || algorithm.isBlank()) {
return null;
}
String normalized = algorithm.trim().toUpperCase(java.util.Locale.ROOT);
if (normalized.equals("CRC32") || normalized.equals("CRC32C") || normalized.equals("SHA1") || normalized.equals("SHA256") || normalized.equals("CRC64NVME")) {
return normalized;
}
if (normalized.equals("SHA512") || normalized.equals("MD5") || normalized.equals("XXHASH3") || normalized.equals("XXHASH64") || normalized.equals("XXHASH128")) {
throw new AwsException("InvalidRequest", "The checksum algorithm you specified is a valid AWS checksum algorithm, but is not currently supported by Floci (supported: CRC32, CRC32C, CRC64NVME, SHA1, SHA256).", 400);
}
throw new AwsException("InvalidArgument", "The checksum algorithm you specified is not supported.", 400);
}
private static S3Checksum buildChecksum(byte[] data, List<Part> parts, boolean multipartUpload) {
return buildChecksum(data, parts, multipartUpload, null);
}
@@ -2438,8 +2455,14 @@ public class S3Service implements Resettable {
? effectiveOptions.getReplacementTagging()
: source.getTags();
S3Checksum effectiveChecksum = source.getChecksum();
String copyChecksumAlgorithm = validateAndNormalizeChecksumAlgorithm(effectiveOptions.getChecksumAlgorithm());
if (copyChecksumAlgorithm != null) {
effectiveChecksum = null;
}
S3Object copy = storeObject(destBucket, destKey, source.getData(), effectiveContentType, metadata,
source.getChecksum(), source.getParts(),
effectiveChecksum, copyChecksumAlgorithm != null ? null : source.getParts(),
new PutObjectOptions()
.withStorageClass(effectiveStorageClass)
.withContentEncoding(effectiveContentEncoding)
@@ -2450,6 +2473,7 @@ public class S3Service implements Resettable {
.withSseCustomerKey(effectiveOptions.getSseCustomerKey())
.withSseCustomerKeyMd5(effectiveOptions.getSseCustomerKeyMd5())
.withAcl(effectiveOptions.getAcl())
.withChecksumAlgorithm(copyChecksumAlgorithm)
.withTagging(effectiveTags));
copy.setETag(source.getETag());
LOG.debugv("Copied object: {0}/{1} -> {2}/{3}", sourceBucket, sourceKey, destBucket, destKey);
@@ -20,6 +20,7 @@ public class CopyObjectOptions {
private String copySourceSseCustomerKey;
private String copySourceSseCustomerKeyMd5;
private String acl;
private String checksumAlgorithm;
public String getMetadataDirective() { return metadataDirective; }
public CopyObjectOptions withMetadataDirective(String metadataDirective) { this.metadataDirective = metadataDirective; return this; }
@@ -71,4 +72,7 @@ public class CopyObjectOptions {
public String getAcl() { return acl; }
public CopyObjectOptions withAcl(String acl) { this.acl = acl; return this; }
public String getChecksumAlgorithm() { return checksumAlgorithm; }
public CopyObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; }
}
@@ -21,6 +21,7 @@ public class MultipartUpload {
private String sseCustomerAlgorithm;
private String sseCustomerKeyMd5;
private String acl;
private String checksumAlgorithm;
private Map<String, String> metadata;
private Instant initiated;
private final Map<Integer, Part> parts = new ConcurrentHashMap<>();
@@ -70,6 +71,9 @@ public class MultipartUpload {
public String getAcl() { return acl; }
public void setAcl(String acl) { this.acl = acl; }
public String getChecksumAlgorithm() { return checksumAlgorithm; }
public void setChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; }
public Map<String, String> getMetadata() { return metadata; }
public void setMetadata(Map<String, String> metadata) { this.metadata = metadata; }