mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): ignore hoisted upload checksum query parameters (#5977)
* fix(s3): ignore hoisted upload checksum query parameters * fix(s3): ignore hoisted multipart checksum types
This commit is contained in:
1 parent
49d2d74f3f
commit
3a913ca8d6
4 files changed
+279
-53
No files matched your search
+215
-35
@@ -23,6 +23,7 @@ import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3EnforceAuthProfile.class)
|
||||
@@ -36,12 +37,14 @@ class S3PresignedChecksumIntegrationTest {
|
||||
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
|
||||
|
||||
@Test
|
||||
void presignedPutWithMismatchedChecksumCrc32InQueryFailsWith400BadDigest() throws Exception {
|
||||
void presignedPutWithSdkEmptyBodyChecksumInQueryIgnoresIt() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/mismatched-crc32.txt";
|
||||
// SDK JS v3 presigner ký CRC32 của empty payload (AAAAAA==) khi chưa có body tại thời điểm ký
|
||||
// JS SDK v3 presigns before the body is known; AWS ignores this hoisted empty-body checksum.
|
||||
String emptyPayloadCrc32 = S3Checksum.crc32Base64(new byte[0]);
|
||||
String url = presign("PUT", path, Map.of("x-amz-checksum-crc32", emptyPayloadCrc32), Map.of());
|
||||
String url = presign("PUT", path, Map.of(
|
||||
"x-amz-checksum-crc32", emptyPayloadCrc32,
|
||||
"x-amz-sdk-checksum-algorithm", "CRC32"), Map.of());
|
||||
|
||||
byte[] body = "not an empty body".getBytes(StandardCharsets.UTF_8);
|
||||
given()
|
||||
@@ -50,22 +53,30 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.when()
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", nullValue());
|
||||
|
||||
// Xác nhận object không được lưu vào storage
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchKey"));
|
||||
.statusCode(200)
|
||||
.body(equalTo(new String(body, StandardCharsets.UTF_8)));
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.head(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", nullValue())
|
||||
.header("x-amz-checksum-crc64nvme", equalTo(S3Checksum.crc64NvmeBase64(body)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithMatchingChecksumCrc32InQuerySucceedsAndStoresChecksum() throws Exception {
|
||||
void presignedPutWithMatchingChecksumInQueryDoesNotStoreIt() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/matching-crc32.txt";
|
||||
byte[] body = "hello presigned checksum world".getBytes(StandardCharsets.UTF_8);
|
||||
@@ -80,9 +91,8 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("ETag", notNullValue())
|
||||
.header("x-amz-checksum-crc32", equalTo(expectedCrc32));
|
||||
.header("x-amz-checksum-crc32", nullValue());
|
||||
|
||||
// Kiểm tra HeadObject với x-amz-checksum-mode=ENABLED trả về checksum đã lưu
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
@@ -90,15 +100,16 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.head(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", equalTo(expectedCrc32));
|
||||
.header("x-amz-checksum-crc32", nullValue())
|
||||
.header("x-amz-checksum-crc64nvme", equalTo(S3Checksum.crc64NvmeBase64(body)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithChecksumAlgorithmInQueryComputesAndStoresChecksum() throws Exception {
|
||||
void presignedPutWithChecksumAlgorithmInQueryUsesDefaultChecksum() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/algorithm-query.txt";
|
||||
byte[] body = "calculate crc32 from algorithm query param".getBytes(StandardCharsets.UTF_8);
|
||||
String expectedCrc32 = S3Checksum.crc32Base64(body);
|
||||
String expectedChecksum = S3Checksum.crc64NvmeBase64(body);
|
||||
String url = presign("PUT", path, Map.of("x-amz-checksum-algorithm", "CRC32"), Map.of());
|
||||
|
||||
given()
|
||||
@@ -108,9 +119,9 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", equalTo(expectedCrc32));
|
||||
.header("x-amz-checksum-crc32", nullValue())
|
||||
.header("x-amz-checksum-crc64nvme", equalTo(expectedChecksum));
|
||||
|
||||
// Query param x-amz-checksum-mode=ENABLED trên presigned GET cũng phải trả về checksum
|
||||
String getUrl = presign("GET", path, Map.of("x-amz-checksum-mode", "ENABLED"), Map.of());
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
@@ -118,12 +129,12 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.get(getUrl)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", equalTo(expectedCrc32))
|
||||
.header("x-amz-checksum-crc64nvme", equalTo(expectedChecksum))
|
||||
.body(equalTo(new String(body, StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithMismatchedSha256InQueryFailsWith400BadDigest() throws Exception {
|
||||
void presignedPutWithMismatchedSha256InQueryIgnoresIt() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/mismatched-sha256.txt";
|
||||
String invalidSha256 = S3Checksum.sha256Base64("other content".getBytes(StandardCharsets.UTF_8));
|
||||
@@ -135,18 +146,16 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.when()
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The SHA256 you specified did not match the calculated checksum."));
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-sha256", nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedUploadPartWithMismatchedChecksumQueryParamFailsWith400BadDigest() throws Exception {
|
||||
void presignedUploadPartWithMismatchedChecksumQueryParamIgnoresIt() throws Exception {
|
||||
createBucket();
|
||||
String key = "multipart-checksum.txt";
|
||||
String path = "/" + BUCKET + "/" + key;
|
||||
|
||||
// Khởi tạo multipart upload
|
||||
String uploadId = given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
@@ -155,26 +164,95 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
// Tạo presigned URL cho upload part có x-amz-checksum-crc32 sai
|
||||
String partPath = path;
|
||||
String wrongCrc32 = S3Checksum.crc32Base64(new byte[0]);
|
||||
String partUrl = presign("PUT", partPath,
|
||||
String partUrl = presign("PUT", path,
|
||||
Map.of("uploadId", uploadId, "partNumber", "1", "x-amz-checksum-crc32", wrongCrc32),
|
||||
Map.of());
|
||||
|
||||
given()
|
||||
String etag = given()
|
||||
.urlEncodingEnabled(false)
|
||||
.body("part data not matching empty crc32".getBytes(StandardCharsets.UTF_8))
|
||||
.when()
|
||||
.put(partUrl)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", nullValue())
|
||||
.extract().header("ETag");
|
||||
|
||||
String completeUrl = presign("POST", path,
|
||||
Map.of("uploadId", uploadId, "x-amz-checksum-type", "COMPOSITE"), Map.of());
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.body("<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>" + etag
|
||||
+ "</ETag></Part></CompleteMultipartUpload>")
|
||||
.when()
|
||||
.post(completeUrl)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.get(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("part data not matching empty crc32"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedCopyObjectWithChecksumAlgorithmInQueryComputesAndStoresChecksum() throws Exception {
|
||||
void presignedCreateMultipartUploadWithQueryChecksumSettingsIgnoresThem() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/multipart-query-settings.txt";
|
||||
String url = presign("POST", path, Map.of("uploads", "",
|
||||
"x-amz-checksum-algorithm", "CRC32", "x-amz-checksum-type", "FULL_OBJECT"), Map.of());
|
||||
|
||||
String uploadId = given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.post(url)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-algorithm", nullValue())
|
||||
.header("x-amz-checksum-type", nullValue())
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.delete(path + "?uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedCreateMultipartUploadWithChecksumHeadersPreservesThem() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/multipart-header-settings.txt";
|
||||
String url = presign("POST", path, Map.of("uploads", ""),
|
||||
Map.of("x-amz-checksum-algorithm", "CRC32", "x-amz-checksum-type", "FULL_OBJECT"));
|
||||
|
||||
String uploadId = given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("x-amz-checksum-algorithm", "CRC32")
|
||||
.header("x-amz-checksum-type", "FULL_OBJECT")
|
||||
.when()
|
||||
.post(url)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-algorithm", equalTo("CRC32"))
|
||||
.header("x-amz-checksum-type", equalTo("FULL_OBJECT"))
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.delete(path + "?uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedCopyObjectWithChecksumAlgorithmInQueryPreservesSourceChecksum() throws Exception {
|
||||
createBucket();
|
||||
String sourcePath = "/" + BUCKET + "/copy-source.txt";
|
||||
byte[] body = "copy source content for checksum test".getBytes(StandardCharsets.UTF_8);
|
||||
@@ -187,7 +265,7 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.statusCode(200);
|
||||
|
||||
String destPath = "/" + BUCKET + "/copy-dest.txt";
|
||||
String expectedSha256 = S3Checksum.sha256Base64(body);
|
||||
String expectedChecksum = S3Checksum.crc64NvmeBase64(body);
|
||||
String copyUrl = presign("PUT", destPath,
|
||||
Map.of("x-amz-checksum-algorithm", "SHA256"),
|
||||
Map.of("x-amz-copy-source", "/" + BUCKET + "/copy-source.txt"));
|
||||
@@ -200,7 +278,7 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("CopyObjectResult"))
|
||||
.body(containsString("<ChecksumSHA256>" + expectedSha256 + "</ChecksumSHA256>"));
|
||||
.body(containsString("<ChecksumCRC64NVME>" + expectedChecksum + "</ChecksumCRC64NVME>"));
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
@@ -209,7 +287,109 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.head(destPath)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-sha256", equalTo(expectedSha256));
|
||||
.header("x-amz-checksum-sha256", nullValue())
|
||||
.header("x-amz-checksum-crc64nvme", equalTo(expectedChecksum));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithMatchingChecksumHeaderStoresItDespiteMismatchedQuery() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/matching-header.txt";
|
||||
byte[] body = "checksum header takes precedence".getBytes(StandardCharsets.UTF_8);
|
||||
String checksum = S3Checksum.crc32Base64(body);
|
||||
String url = presign("PUT", path,
|
||||
Map.of("x-amz-checksum-crc32", "AAAAAA=="),
|
||||
Map.of("x-amz-checksum-crc32", checksum, "x-amz-sdk-checksum-algorithm", "CRC32"));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("x-amz-checksum-crc32", checksum)
|
||||
.header("x-amz-sdk-checksum-algorithm", "CRC32")
|
||||
.body(body)
|
||||
.when()
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", equalTo(checksum));
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.header("x-amz-checksum-mode", "ENABLED")
|
||||
.when()
|
||||
.head(path)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("x-amz-checksum-crc32", equalTo(checksum));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithMismatchedChecksumHeaderFailsDespiteMatchingQuery() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/mismatched-header.txt";
|
||||
byte[] body = "body does not match the header".getBytes(StandardCharsets.UTF_8);
|
||||
String url = presign("PUT", path,
|
||||
Map.of("x-amz-checksum-crc32", S3Checksum.crc32Base64(body)),
|
||||
Map.of("x-amz-checksum-crc32", "AAAAAA=="));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("x-amz-checksum-crc32", "AAAAAA==")
|
||||
.body(body)
|
||||
.when()
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"));
|
||||
|
||||
given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.head(path)
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedPutWithTamperedChecksumQueryIsRejected() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/tampered-query.txt";
|
||||
String url = presign("PUT", path, Map.of("x-amz-checksum-crc32", "AAAAAA=="), Map.of());
|
||||
String tampered = url.replace("x-amz-checksum-crc32=" + uriEncode("AAAAAA=="),
|
||||
"x-amz-checksum-crc32=" + uriEncode("BBBBBB=="));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.body("not an empty body")
|
||||
.when()
|
||||
.put(tampered)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("SignatureDoesNotMatch"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedUploadPartWithMismatchedChecksumHeaderFails() throws Exception {
|
||||
createBucket();
|
||||
String path = "/" + BUCKET + "/multipart-header.txt";
|
||||
String uploadId = given()
|
||||
.filter(LOCAL_SIGNER)
|
||||
.when()
|
||||
.post(path + "?uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
String url = presign("PUT", path, Map.of("uploadId", uploadId, "partNumber", "1"),
|
||||
Map.of("x-amz-checksum-crc32", "AAAAAA=="));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("x-amz-checksum-crc32", "AAAAAA==")
|
||||
.body("not an empty body")
|
||||
.when()
|
||||
.put(url)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user