mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): validate multipart upload part numbers (#5655)
* fix(s3): validate multipart upload part numbers * test(s3): verify rejected upload parts are not stored
This commit is contained in:
1 parent
bb3629105b
commit
4c3d9cad2d
7 files changed
+143
-16
No files matched your search
+8
@@ -956,6 +956,14 @@ class S3AuthEnforcementIntegrationTest {
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
|
||||
given()
|
||||
.body("part data")
|
||||
.when()
|
||||
.put("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId + "&partNumber=0")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
|
||||
String eTag = given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.body("part data")
|
||||
|
||||
@@ -95,10 +95,15 @@ class S3MultipartServiceTest {
|
||||
@Test
|
||||
void uploadPartInvalidNumber() {
|
||||
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "file.bin", null);
|
||||
assertThrows(AwsException.class, () ->
|
||||
s3Service.uploadPart("test-bucket", "file.bin", upload.getUploadId(), 0, new byte[1]));
|
||||
assertThrows(AwsException.class, () ->
|
||||
s3Service.uploadPart("test-bucket", "file.bin", upload.getUploadId(), 10001, new byte[1]));
|
||||
for (int partNumber : new int[] {0, 10001}) {
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
s3Service.uploadPart("test-bucket", "file.bin", upload.getUploadId(), partNumber, new byte[1]));
|
||||
assertEquals("InvalidArgument", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals("Part number must be an integer between 1 and 10000, inclusive", error.getMessage());
|
||||
assertEquals(Map.of("ArgumentName", "partNumber", "ArgumentValue", Integer.toString(partNumber)),
|
||||
error.getExtendedData());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -207,8 +207,13 @@ class S3StreamedUploadTest {
|
||||
|
||||
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
|
||||
"multipart.bin", "no-such-upload", 1, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
|
||||
assertEquals("InvalidArgument", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
|
||||
"multipart.bin", upload.getUploadId(), 10001, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
|
||||
AwsException invalidPart = assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
|
||||
"multipart.bin", upload.getUploadId(), 10001, unreadable(), NO_CHECKSUMS, null, null, null));
|
||||
assertEquals("InvalidArgument", invalidPart.getErrorCode());
|
||||
assertEquals(400, invalidPart.getHttpStatus());
|
||||
assertEquals("Part number must be an integer between 1 and 10000, inclusive", invalidPart.getMessage());
|
||||
assertEquals(Map.of("ArgumentName", "partNumber", "ArgumentValue", "10001"),
|
||||
invalidPart.getExtendedData());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** Regression coverage for AWS's InvalidArgument response for an invalid multipart part number. */
|
||||
@QuarkusTest
|
||||
class S3UploadPartValidationIntegrationTest {
|
||||
|
||||
private static final String[] INVALID_PART_NUMBERS = {"-1", "0", "10001", "abc", "4294967297"};
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
@Inject
|
||||
S3Service s3Service;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void uploadPartRejectsInvalidPartNumbersWithTheAwsErrorDetails() {
|
||||
String bucket = createBucket("upload-part-validation");
|
||||
String key = "object.bin";
|
||||
String uploadId = createUpload(bucket, key);
|
||||
cleanup.register(() -> abortUpload(bucket, key, uploadId));
|
||||
|
||||
for (String partNumber : INVALID_PART_NUMBERS) {
|
||||
assertInvalidPartNumber(bucket, key, uploadId, partNumber, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPartCopyRejectsInvalidPartNumbersWithTheAwsErrorDetails() {
|
||||
String bucket = createBucket("upload-part-copy-validation");
|
||||
String key = "copy.bin";
|
||||
given().body("source").when().put("/" + bucket + "/source.bin").then().statusCode(200);
|
||||
cleanup.register(() -> given().when().delete("/" + bucket + "/source.bin").then().statusCode(204));
|
||||
String uploadId = createUpload(bucket, key);
|
||||
cleanup.register(() -> abortUpload(bucket, key, uploadId));
|
||||
|
||||
for (String partNumber : INVALID_PART_NUMBERS) {
|
||||
assertInvalidPartNumber(bucket, key, uploadId, partNumber, "/" + bucket + "/source.bin");
|
||||
}
|
||||
}
|
||||
|
||||
private void assertInvalidPartNumber(String bucket, String key, String uploadId,
|
||||
String partNumber, String copySource) {
|
||||
RequestSpecification request = given().body("part");
|
||||
if (copySource != null) {
|
||||
request.header("x-amz-copy-source", copySource);
|
||||
}
|
||||
request.when()
|
||||
.put("/" + bucket + "/" + key + "?uploadId=" + uploadId + "&partNumber=" + partNumber)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Error.Code", equalTo("InvalidArgument"))
|
||||
.body("Error.Message", equalTo("Part number must be an integer between 1 and 10000, inclusive"))
|
||||
.body("Error.ArgumentName", equalTo("partNumber"))
|
||||
.body("Error.ArgumentValue", equalTo(partNumber));
|
||||
|
||||
assertTrue(s3Service.listParts(bucket, key, uploadId).getParts().isEmpty(),
|
||||
"Rejected part " + partNumber + " must not be recorded");
|
||||
}
|
||||
|
||||
private String createBucket(String prefix) {
|
||||
String bucket = prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), auth("test", "s3"), bucket));
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static String createUpload(String bucket, String key) {
|
||||
return given().when()
|
||||
.post("/" + bucket + "/" + key + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
}
|
||||
|
||||
private static void abortUpload(String bucket, String key, String uploadId) {
|
||||
given().when().delete("/" + bucket + "/" + key + "?uploadId=" + uploadId).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user