mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(s3): validate multipart completion parts (#3312)
* fix(s3): validate multipart completion parts * fix(s3): accept CLI multipart ETags
This commit is contained in:
1 parent
8b25e5003d
commit
31f8860c11
7 files changed
+304
-93
No files matched your search
@@ -1366,7 +1366,8 @@ public class S3Controller {
|
||||
String checksumType = httpHeaders.getHeaderString("x-amz-checksum-type");
|
||||
S3Checksum expectedChecksum = extractChecksumFromHeaders(httpHeaders);
|
||||
S3Object obj = s3Service.completeMultipartUpload(bucket, key, uploadId, partNumbers,
|
||||
completedPartChecksums(completedParts), checksumType, expectedChecksum);
|
||||
completedPartETags(completedParts), completedPartChecksums(completedParts), checksumType,
|
||||
expectedChecksum);
|
||||
String baseUrl = uriInfo.getBaseUri().toString();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
@@ -1539,7 +1540,7 @@ public class S3Controller {
|
||||
return Response.ok(xml.build()).build();
|
||||
}
|
||||
|
||||
private record CompletedPart(int partNumber, S3Checksum checksum) {}
|
||||
private record CompletedPart(int partNumber, String eTag, S3Checksum checksum) {}
|
||||
|
||||
private List<CompletedPart> parseCompleteMultipartBody(String xml) {
|
||||
List<Map<String, String>> parts = XmlParser.extractGroups(xml, "Part");
|
||||
@@ -1554,11 +1555,12 @@ public class S3Controller {
|
||||
} catch (NumberFormatException e) {
|
||||
throw malformedCompleteMultipartXml();
|
||||
}
|
||||
String eTag = part.getOrDefault("ETag", "").trim();
|
||||
S3Checksum checksum = new S3Checksum();
|
||||
for (ChecksumAlgorithm algorithm : ChecksumAlgorithm.values()) {
|
||||
checksum.setValueFor(algorithm, part.get("Checksum" + algorithm.name()));
|
||||
}
|
||||
completedParts.add(new CompletedPart(partNumber, checksum.hasAnyValue() ? checksum : null));
|
||||
completedParts.add(new CompletedPart(partNumber, eTag, checksum.hasAnyValue() ? checksum : null));
|
||||
}
|
||||
return completedParts;
|
||||
}
|
||||
@@ -1577,6 +1579,14 @@ public class S3Controller {
|
||||
return checksums;
|
||||
}
|
||||
|
||||
private Map<Integer, String> completedPartETags(List<CompletedPart> parts) {
|
||||
Map<Integer, String> eTags = new HashMap<>();
|
||||
for (CompletedPart part : parts) {
|
||||
eTags.put(part.partNumber(), part.eTag());
|
||||
}
|
||||
return eTags;
|
||||
}
|
||||
|
||||
// --- Versioning Operations ---
|
||||
|
||||
private Response handlePutBucketVersioning(String bucket, byte[] body) {
|
||||
|
||||
@@ -2477,12 +2477,20 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
return completeMultipartUpload(bucket, key, uploadId, partNumbers, Map.of(), checksumType, expectedChecksum);
|
||||
return completeMultipartUpload(bucket, key, uploadId, partNumbers, Map.of(), Map.of(), checksumType,
|
||||
expectedChecksum);
|
||||
}
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
Map<Integer, S3Checksum> partChecksums,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
return completeMultipartUpload(bucket, key, uploadId, partNumbers, Map.of(), partChecksums, checksumType,
|
||||
expectedChecksum);
|
||||
}
|
||||
|
||||
public S3Object completeMultipartUpload(String bucket, String key, String uploadId, List<Integer> partNumbers,
|
||||
Map<Integer, String> partETags, Map<Integer, S3Checksum> partChecksums,
|
||||
String checksumType, S3Checksum expectedChecksum) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
throw new AwsException("NoSuchUpload",
|
||||
@@ -2492,13 +2500,23 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
ChecksumAlgorithm algorithm = upload.getChecksumAlgorithm() != null ? upload.getChecksumAlgorithm() : ChecksumAlgorithm.CRC64NVME;
|
||||
ChecksumType storedChecksumType = upload.getChecksumType() != null ? upload.getChecksumType() : ChecksumType.FULL_OBJECT;
|
||||
|
||||
// Verify all requested parts exist and carry the checksums the upload requires
|
||||
int previousPartNumber = 0;
|
||||
for (int num : partNumbers) {
|
||||
if (num <= previousPartNumber) {
|
||||
throw new AwsException("InvalidPartOrder",
|
||||
"The list of parts was not in ascending order.", 400);
|
||||
}
|
||||
previousPartNumber = num;
|
||||
Part part = upload.getParts().get(num);
|
||||
if (part == null) {
|
||||
throw new AwsException("InvalidPart",
|
||||
"One or more of the specified parts could not be found. Part " + num + " is missing.", 400);
|
||||
}
|
||||
if (!partETags.isEmpty() && !etagsMatch(part.getETag(), partETags.get(num))) {
|
||||
throw new AwsException("InvalidPart",
|
||||
"One or more of the specified parts could not be found. Part " + num
|
||||
+ " has an invalid ETag.", 400);
|
||||
}
|
||||
validatePartChecksum(upload.getChecksumAlgorithm(), storedChecksumType, num, part, partChecksums.get(num));
|
||||
}
|
||||
|
||||
@@ -2563,6 +2581,20 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean etagsMatch(String storedETag, String submittedETag) {
|
||||
return stripSurroundingQuotes(storedETag).equals(stripSurroundingQuotes(submittedETag));
|
||||
}
|
||||
|
||||
private String stripSurroundingQuotes(String eTag) {
|
||||
if (eTag == null) {
|
||||
return null;
|
||||
}
|
||||
if (eTag.length() >= 2 && eTag.startsWith("\"") && eTag.endsWith("\"")) {
|
||||
return eTag.substring(1, eTag.length() - 1);
|
||||
}
|
||||
return eTag;
|
||||
}
|
||||
|
||||
public void abortMultipartUpload(String bucket, String key, String uploadId) {
|
||||
MultipartUpload upload = multipartUploads.get(uploadId);
|
||||
if (upload == null || !upload.getBucket().equals(bucket) || !upload.getKey().equals(key)) {
|
||||
|
||||
Reference in new issue
Block a user