mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
fix(s3): reject aws-chunked S3 uploads whose trailing checksum mismatches (#5176)
* fix(s3): reject aws-chunked S3 uploads whose trailing checksum mismatches The decoder now keeps each trailer line, keyed by lower-case header name, and exposes that map once the body has been read. After PutObject, UploadPart, and the in-memory body read hash the decoded payload, the checksum named by x-amz-trailer is compared with the computed digest; a mismatch, a missing trailer line, or a line for a different algorithm raises 400 BadDigest and the object or part is not published. An aws-chunked body is hashed with every checksum algorithm so the trailer can be checked even when it is not the algorithm stored on the object. Header mismatches use the same text, "The CRC32 you specified did not match the calculated checksum.", and x-amz-trailer-signature is not treated as a checksum. Fixes #5136 * fix(s3): hash only the declared trailer checksum and frame trailer bodies An aws-chunked body is no longer hashed with every checksum algorithm. Only the algorithm named up front in x-amz-trailer is added to the digest set, so a chunked upload with no trailer does no extra hashing. The decoder keeps just the declared x-amz-checksum-* trailer line and discards other trailer lines, so trailer storage is bounded. Any other x-amz-checksum-* line, including one sent with no x-amz-trailer at all, fails the request with 400 BadDigest. A body with Content-Encoding: aws-chunked and an x-amz-trailer header but no streaming x-amz-content-sha256 is now decoded through AwsChunkedInputStream, so its trailer is verified like a streaming upload. Without x-amz-trailer the lenient decode is unchanged. * refactor(s3): resolve x-amz-checksum-* names in one ChecksumAlgorithm helper Add ChecksumAlgorithm.fromChecksumHeader, which maps an x-amz-checksum-* header name to its algorithm and returns null for a name it does not know. S3Controller.trailerChecksumAlgorithm and AwsChunkedInputStream now both use it, and the stream's private knownChecksum lookup is gone. fromWireValue shares the same lookup and still throws for unsupported or unknown algorithm values. --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
1 parent
63ef2d3a2d
commit
37e66ee371
10 files changed
+567
-38
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.s3.model.ChecksumAlgorithm;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -27,6 +28,48 @@ class AwsChunkedInputStreamTest {
|
||||
+ "x-amz-trailer-signature:ghi\r\n\r\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void retainsOnlyTheDeclaredChecksumTrailer() throws IOException {
|
||||
StringBuilder framed = new StringBuilder("5\r\nhello\r\n0\r\n");
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
framed.append("x-amz-trailer-signature:").append(i).append("\r\n");
|
||||
}
|
||||
framed.append("x-amz-checksum-crc32:NhCmhg==\r\n\r\n");
|
||||
|
||||
try (AwsChunkedInputStream in = new AwsChunkedInputStream(
|
||||
new ByteArrayInputStream(framed.toString().getBytes(StandardCharsets.US_ASCII)),
|
||||
ChecksumAlgorithm.CRC32)) {
|
||||
assertEquals("hello", new String(in.readAllBytes(), StandardCharsets.US_ASCII));
|
||||
assertEquals("NhCmhg==", in.trailerChecksum());
|
||||
assertNull(in.undeclaredTrailer());
|
||||
assertFalse(in.hasUndeclaredChecksum());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordsAChecksumTrailerOtherThanTheDeclaredOne() throws IOException {
|
||||
try (AwsChunkedInputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(
|
||||
("5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n"
|
||||
+ "x-amz-checksum-sha256:abc\r\n\r\n").getBytes(StandardCharsets.US_ASCII)),
|
||||
ChecksumAlgorithm.CRC32)) {
|
||||
assertEquals("hello", new String(in.readAllBytes(), StandardCharsets.US_ASCII));
|
||||
assertEquals("NhCmhg==", in.trailerChecksum());
|
||||
assertEquals(ChecksumAlgorithm.SHA256, in.undeclaredTrailer());
|
||||
assertTrue(in.hasUndeclaredChecksum());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChecksumTrailerWithNoDeclaredAlgorithmIsUndeclared() throws IOException {
|
||||
try (AwsChunkedInputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(
|
||||
"5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n\r\n".getBytes(StandardCharsets.US_ASCII)))) {
|
||||
assertEquals("hello", new String(in.readAllBytes(), StandardCharsets.US_ASCII));
|
||||
assertNull(in.trailerChecksum());
|
||||
assertEquals(ChecksumAlgorithm.CRC32, in.undeclaredTrailer());
|
||||
assertTrue(in.hasUndeclaredChecksum());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsBareLineFeeds() throws IOException {
|
||||
assertEquals("hello", decode("5\nhello\n0\n\n"));
|
||||
|
||||
+209
@@ -161,6 +161,7 @@ class S3AwsChunkedIntegrationTest {
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER")
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("5;chunk-signature=abc\r\nhello\r\n0;chunk-signature=def\r\n"
|
||||
+ "x-amz-checksum-crc32:NhCmhg==\r\nx-amz-trailer-signature:ghi\r\n\r\n")
|
||||
.when()
|
||||
@@ -171,6 +172,111 @@ class S3AwsChunkedIntegrationTest {
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_trailingChecksumMismatch_isBadDigestAndNotStored() {
|
||||
String bucket = createBucket("chunked-bad-digest");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_trailingChecksumMatch_storesTheDecodedPayload() {
|
||||
String bucket = createBucket("chunked-good-digest");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("hello world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_declaredTrailerMissingFromTheBody_isBadDigestAndNotStored() {
|
||||
String bucket = createBucket("chunked-trailer-missing");
|
||||
for (String body : new String[] {
|
||||
"b\r\nhello world\r\n0\r\nx-amz-checksum-crc32c:AAAAAA==\r\n\r\n",
|
||||
"b\r\nhello world\r\n0\r\n\r\n"}) {
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body(body)
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
}
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPart_trailingChecksumMismatch_isBadDigestAndNoPartIsStored() {
|
||||
String bucket = createBucket("chunked-part-digest");
|
||||
String uploadId = given()
|
||||
.when().post("/" + bucket + "/object.txt?uploads")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt?partNumber=1&uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
given()
|
||||
.when().get("/" + bucket + "/object.txt?uploadId=" + uploadId)
|
||||
.then().statusCode(200)
|
||||
.body(not(containsString("<Part>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPart_trailingChecksumMatch_storesThePart() {
|
||||
String bucket = createBucket("chunked-part-ok");
|
||||
String uploadId = given()
|
||||
.when().post("/" + bucket + "/object.txt?uploads")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n"
|
||||
+ "x-amz-trailer-signature:ghi\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt?partNumber=1&uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when().get("/" + bucket + "/object.txt?uploadId=" + uploadId)
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<Part>"))
|
||||
.body(containsString("<Size>11</Size>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPart_malformedBody_isIncompleteBodyAndNoPartIsStored() {
|
||||
String bucket = createBucket("chunked-part");
|
||||
@@ -195,6 +301,109 @@ class S3AwsChunkedIntegrationTest {
|
||||
.body(not(containsString("<Part>")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_chunkedBodyWithoutATrailer_storesTheDecodedPayload() {
|
||||
String bucket = createBucket("chunked-no-trailer");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.body("5\r\nhello\r\n0\r\nx-amz-trailer-signature:ghi\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_undeclaredChecksumTrailer_isBadDigestAndNotStored() {
|
||||
String bucket = createBucket("chunked-undeclared");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n"
|
||||
+ "x-amz-checksum-sha256:abc\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The SHA256 you specified did not match the calculated checksum."));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_awsChunkedTrailerWithoutStreamingSha256_rejectsAMismatchedTrailer() {
|
||||
String bucket = createBucket("chunked-lenient-trailer");
|
||||
|
||||
given()
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_awsChunkedTrailerWithoutStreamingSha256_storesAMatchingTrailer() {
|
||||
String bucket = createBucket("chunked-lenient-ok");
|
||||
|
||||
given()
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("hello world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPart_awsChunkedTrailerWithoutStreamingSha256_rejectsAMismatchedTrailer() {
|
||||
String bucket = createBucket("chunked-part-lenient");
|
||||
String uploadId = given()
|
||||
.when().post("/" + bucket + "/object.txt?uploads")
|
||||
.then().statusCode(200)
|
||||
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
|
||||
|
||||
given()
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.header("x-amz-trailer", "x-amz-checksum-crc32")
|
||||
.body("b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt?partNumber=1&uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>BadDigest</Code>"))
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
given()
|
||||
.when().get("/" + bucket + "/object.txt?uploadId=" + uploadId)
|
||||
.then().statusCode(200)
|
||||
.body(not(containsString("<Part>")));
|
||||
}
|
||||
|
||||
// Without a streaming x-amz-content-sha256, a Content-Encoding naming aws-chunked does not
|
||||
// mean the body is framed, so a body that is not framed is stored as sent.
|
||||
@Test
|
||||
|
||||
@@ -255,6 +255,13 @@ class S3ChecksumTest {
|
||||
assertEquals("InvalidRequest", knownButUnsupported.getErrorCode());
|
||||
AwsException unknown = assertThrows(AwsException.class, () -> ChecksumAlgorithm.fromWireValue("MURMUR3"));
|
||||
assertEquals("InvalidArgument", unknown.getErrorCode());
|
||||
|
||||
assertEquals(ChecksumAlgorithm.CRC32, ChecksumAlgorithm.fromChecksumHeader("x-amz-checksum-crc32"));
|
||||
assertEquals(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.fromChecksumHeader("X-Amz-Checksum-SHA256"));
|
||||
assertNull(ChecksumAlgorithm.fromChecksumHeader("x-amz-checksum-sha512"));
|
||||
assertNull(ChecksumAlgorithm.fromChecksumHeader("x-amz-checksum-murmur3"));
|
||||
assertNull(ChecksumAlgorithm.fromChecksumHeader("x-amz-trailer-signature"));
|
||||
assertNull(ChecksumAlgorithm.fromChecksumHeader(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+3
-3
@@ -52,7 +52,7 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The CRC32 checksum you specified did not match the payload."));
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
|
||||
// Xác nhận object không được lưu vào storage
|
||||
given()
|
||||
@@ -137,7 +137,7 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The SHA256 checksum you specified did not match the payload."));
|
||||
.body(containsString("The SHA256 you specified did not match the calculated checksum."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -170,7 +170,7 @@ class S3PresignedChecksumIntegrationTest {
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("BadDigest"))
|
||||
.body(containsString("The CRC32 checksum you specified did not match the payload."));
|
||||
.body(containsString("The CRC32 you specified did not match the calculated checksum."));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -42,6 +42,17 @@ class S3StreamedUploadTest {
|
||||
|
||||
private static final byte[] BODY = "a body that arrives as a stream".getBytes(StandardCharsets.UTF_8);
|
||||
private static final UploadChecksums NO_CHECKSUMS = new UploadChecksums(null, Map.of());
|
||||
private static final String CHUNKED_MISMATCH =
|
||||
"b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:AAAAAA==\r\n\r\n";
|
||||
private static final String CHUNKED_MATCH =
|
||||
"b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\n\r\n";
|
||||
private static final String CHUNKED_EXTRA =
|
||||
"b\r\nhello world\r\n0\r\nx-amz-checksum-crc32:DUoRhQ==\r\nx-amz-checksum-sha256:abc\r\n\r\n";
|
||||
private static final String CHUNKED_PLAIN = "b\r\nhello world\r\n0\r\nx-amz-trailer-signature:ghi\r\n\r\n";
|
||||
private static final String TRAILER_MISMATCH =
|
||||
"The CRC32 you specified did not match the calculated checksum.";
|
||||
private static final String UNDECLARED_TRAILER =
|
||||
"The SHA256 you specified did not match the calculated checksum.";
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
@@ -304,6 +315,102 @@ class S3StreamedUploadTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChunkedTrailerChecksumIsCheckedBeforeTheObjectOrPartIsStored() {
|
||||
byte[] payload = "hello world".getBytes(StandardCharsets.US_ASCII);
|
||||
UploadChecksums declared = new UploadChecksums(null, Map.of(), ChecksumAlgorithm.CRC32);
|
||||
|
||||
for (S3Service service : List.of(s3Service, memoryService())) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.putObject("bucket", "bad.txt",
|
||||
chunked(CHUNKED_MISMATCH), declared, "text/plain", Map.of(), new PutObjectOptions()));
|
||||
assertEquals("BadDigest", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertEquals(TRAILER_MISMATCH, error.getMessage());
|
||||
assertEquals("NoSuchKey", assertThrows(AwsException.class,
|
||||
() -> service.headObject("bucket", "bad.txt")).getErrorCode());
|
||||
|
||||
S3Object stored = service.putObject("bucket", "good.txt", chunked(CHUNKED_MATCH), declared,
|
||||
"text/plain", Map.of(), new PutObjectOptions());
|
||||
assertEquals(payload.length, stored.getSize());
|
||||
assertArrayEquals(payload, service.getObject("bucket", "good.txt").getData());
|
||||
|
||||
MultipartUpload upload = service.initiateMultipartUpload("bucket", "part.bin", null);
|
||||
assertEquals("BadDigest", assertThrows(AwsException.class, () -> service.storePart("bucket", "part.bin",
|
||||
upload.getUploadId(), 1, chunked(CHUNKED_MISMATCH), declared, null, null, null)).getErrorCode());
|
||||
assertTrue(service.getMultipartUpload("bucket", "part.bin", upload.getUploadId()).getParts().isEmpty());
|
||||
|
||||
Part part = service.storePart("bucket", "part.bin", upload.getUploadId(), 1, chunked(CHUNKED_MATCH),
|
||||
declared, null, null, null);
|
||||
assertEquals(payload.length, part.getSize());
|
||||
assertEquals(1, service.getMultipartUpload("bucket", "part.bin", upload.getUploadId()).getParts().size());
|
||||
}
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDeclaredTrailerMissingFromTheChunkIsNotStored() {
|
||||
UploadChecksums declared = new UploadChecksums(null, Map.of(), ChecksumAlgorithm.CRC32);
|
||||
|
||||
for (S3Service service : List.of(s3Service, memoryService())) {
|
||||
for (String framed : List.of(
|
||||
"b\r\nhello world\r\n0\r\nx-amz-checksum-crc32c:AAAAAA==\r\n\r\n",
|
||||
"b\r\nhello world\r\n0\r\n\r\n")) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.putObject("bucket", "missing.txt",
|
||||
chunked(framed), declared, "text/plain", Map.of(), new PutObjectOptions()));
|
||||
assertEquals("BadDigest", error.getErrorCode());
|
||||
assertEquals(TRAILER_MISMATCH, error.getMessage());
|
||||
assertEquals("NoSuchKey", assertThrows(AwsException.class,
|
||||
() -> service.headObject("bucket", "missing.txt")).getErrorCode());
|
||||
}
|
||||
}
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChunkedUploadWithoutATrailerIsStored() {
|
||||
byte[] payload = "hello world".getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
for (S3Service service : List.of(s3Service, memoryService())) {
|
||||
S3Object stored = service.putObject("bucket", "plain.txt",
|
||||
new AwsChunkedInputStream(new ByteArrayInputStream(CHUNKED_PLAIN.getBytes(StandardCharsets.US_ASCII))),
|
||||
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions());
|
||||
|
||||
assertEquals(payload.length, stored.getSize());
|
||||
assertArrayEquals(payload, service.getObject("bucket", "plain.txt").getData());
|
||||
}
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUndeclaredChecksumTrailerIsNotStored() {
|
||||
UploadChecksums declared = new UploadChecksums(null, Map.of(), ChecksumAlgorithm.CRC32);
|
||||
|
||||
for (S3Service service : List.of(s3Service, memoryService())) {
|
||||
AwsException extra = assertThrows(AwsException.class, () -> service.putObject("bucket", "extra.txt",
|
||||
chunked(CHUNKED_EXTRA), declared, "text/plain", Map.of(), new PutObjectOptions()));
|
||||
assertEquals("BadDigest", extra.getErrorCode());
|
||||
assertEquals(400, extra.getHttpStatus());
|
||||
assertEquals(UNDECLARED_TRAILER, extra.getMessage());
|
||||
|
||||
AwsException undeclared = assertThrows(AwsException.class, () -> service.putObject("bucket", "none.txt",
|
||||
new AwsChunkedInputStream(new ByteArrayInputStream(CHUNKED_MATCH.getBytes(StandardCharsets.US_ASCII))),
|
||||
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions()));
|
||||
assertEquals("BadDigest", undeclared.getErrorCode());
|
||||
assertEquals(TRAILER_MISMATCH, undeclared.getMessage());
|
||||
|
||||
assertEquals("NoSuchKey", assertThrows(AwsException.class,
|
||||
() -> service.headObject("bucket", "extra.txt")).getErrorCode());
|
||||
assertEquals("NoSuchKey", assertThrows(AwsException.class,
|
||||
() -> service.headObject("bucket", "none.txt")).getErrorCode());
|
||||
|
||||
MultipartUpload upload = service.initiateMultipartUpload("bucket", "part.bin", null);
|
||||
assertEquals("BadDigest", assertThrows(AwsException.class, () -> service.storePart("bucket", "part.bin",
|
||||
upload.getUploadId(), 1, chunked(CHUNKED_EXTRA), declared, null, null, null)).getErrorCode());
|
||||
assertTrue(service.getMultipartUpload("bucket", "part.bin", upload.getUploadId()).getParts().isEmpty());
|
||||
}
|
||||
assertNothingStaged();
|
||||
}
|
||||
|
||||
@Test
|
||||
void memoryModeReadsTheStreamIntoTheBytePaths() {
|
||||
S3Service memory = memoryService();
|
||||
@@ -373,6 +480,11 @@ class S3StreamedUploadTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static AwsChunkedInputStream chunked(String framed) {
|
||||
return new AwsChunkedInputStream(new ByteArrayInputStream(framed.getBytes(StandardCharsets.US_ASCII)),
|
||||
ChecksumAlgorithm.CRC32);
|
||||
}
|
||||
|
||||
/** A body the write should never get to reading. */
|
||||
private static InputStream unreadable() {
|
||||
return new InputStream() {
|
||||
|
||||
Reference in new issue
Block a user