fix(s3): validate multipart upload object size (#5643)

* fix(s3): validate multipart upload object size

* fix(s3): reject invalid multipart object sizes
This commit is contained in:
Mohith authored and GitHub committed 2026-10-14 13:10:12 -04:00
1 parent 7c4d1c913a
commit 4d6ecc1ac2
6 files changed
+240 -2

No files matched your search

@@ -1645,7 +1645,7 @@ public class S3Controller {
S3Checksum expectedChecksum = extractChecksum(httpHeaders, uriInfo);
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers,
completedPartETags(completedParts), completedPartChecksums(completedParts), checksumType,
expectedChecksum);
expectedChecksum, multipartObjectSize(httpHeaders, uriInfo));
String baseUrl = uriInfo.getBaseUri().toString();
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
@@ -2298,6 +2298,35 @@ public class S3Controller {
}
}
private static Long multipartObjectSize(HttpHeaders httpHeaders, UriInfo uriInfo) {
String name = "x-amz-mp-object-size";
String value = httpHeaders != null ? httpHeaders.getHeaderString(name) : null;
if (value == null && uriInfo != null && uriInfo.getQueryParameters() != null) {
for (Map.Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
if (entry.getKey().equalsIgnoreCase(name) && !entry.getValue().isEmpty()) {
value = entry.getValue().getFirst();
break;
}
}
}
if (value == null) {
return null;
}
String normalized = value.trim();
long size;
try {
size = Long.parseLong(normalized);
} catch (NumberFormatException e) {
throw new AwsException("InvalidRequest", "Value for x-amz-mp-object-size header is invalid: '"
+ normalized + "'", 400);
}
if (size < 0) {
throw new AwsException("InvalidRequest", "Value for x-amz-mp-object-size header is less than zero: '"
+ normalized + "'", 400);
}
return size;
}
private static String trailerHeader(HttpHeaders httpHeaders, UriInfo uriInfo) {
return resolveHeaderOrQueryParam(httpHeaders, uriInfo, "x-amz-trailer");
}
@@ -4197,6 +4197,13 @@ public class S3Service implements Resettable, ResourceProvider {
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
Map<Integer, String> partETags, Map<Integer, S3Checksum> partChecksums,
String checksumType, S3Checksum expectedChecksum) {
return completeMultipartUpload(bucket, key, uploadId, partNumbers, partETags, partChecksums,
checksumType, expectedChecksum, null);
}
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
Map<Integer, String> partETags, Map<Integer, S3Checksum> partChecksums,
String checksumType, S3Checksum expectedChecksum, Long expectedObjectSize) {
S3Object completed = withMultipartOperationLock(bucket, uploadId, () -> {
MultipartUpload upload = getMultipartUpload(bucket, key, uploadId);
@@ -4227,8 +4234,13 @@ public class S3Service implements Resettable, ResourceProvider {
}
validateCompleteChecksumType(algorithm, storedChecksumType, ChecksumType.fromWireValue(checksumType));
long objectSize = parts.stream().mapToLong(Part::getSize).sum();
if (expectedObjectSize != null && expectedObjectSize != objectSize) {
throw new AwsException("InvalidRequest", "The provided 'x-amz-mp-object-size' header value "
+ expectedObjectSize + " does not match what was computed: " + objectSize, 400);
}
if (inMemory) {
requireFitsInMemory("Multipart upload " + uploadId, parts.stream().mapToLong(Part::getSize).sum());
requireFitsInMemory("Multipart upload " + uploadId, objectSize);
}
// Concatenate parts in order