Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): reject an aws-chunked upload whose non-final chunk is under 8 KiB (#6413)
Wleeafc854046523a8
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStream.java b/src/main/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStream.javaindex 03f61d75d4..1dc459ba3f 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStream.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStream.java@@ -17,13 +17,16 @@ import java.util.Locale;* checked. Only the trailing checksum named up front is kept, so the caller can compare it once* the body has been read; any other {@code x-amz-checksum-*} line is recorded as undeclared and* every other trailer line is discarded. A body that breaks the framing or ends early fails with- * S3's {@code IncompleteBody}.+ * S3's {@code IncompleteBody}, and one whose chunk other than the last is under 8 KiB fails with+ * S3's {@code InvalidChunkSizeError}.*/final class AwsChunkedInputStream extends InputStream {// A chunk header or trailer line is a size, a signature and a few separators; anything longer// is not framing.private static final int MAX_LINE_LENGTH = 8192;+ // S3 requires every chunk but the last to carry at least this much data.+ private static final int MIN_CHUNK_SIZE = 8192;private static final String CHECKSUM_PREFIX = "x-amz-checksum-";private final InputStream in;@@ -32,6 +35,7 @@ final class AwsChunkedInputStream extends InputStream {private ChecksumAlgorithm undeclaredTrailer;private boolean undeclaredChecksum;private long remainingInChunk;+ private long previousChunkSize;private boolean finished;AwsChunkedInputStream(InputStream in) {@@ -114,6 +118,11 @@ final class AwsChunkedInputStream extends InputStream {throw incompleteBody();}if (size > 0) {+ // Another data chunk follows, so the previous one was not the last.+ if (previousChunkSize > 0 && previousChunkSize < MIN_CHUNK_SIZE) {+ throw invalidChunkSize();+ }+ previousChunkSize = size;remainingInChunk = size;return true;}@@ -177,4 +186,9 @@ final class AwsChunkedInputStream extends InputStream {return new AwsException("IncompleteBody","You did not provide the number of bytes specified by the Content-Length HTTP header.", 400);}++ private static AwsException invalidChunkSize() {+ return new AwsException("InvalidChunkSizeError",+ "Only the last chunk is allowed to have a size less than " + MIN_CHUNK_SIZE + " bytes", 403);+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStreamTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStreamTest.javaindex 45da38cd39..8abfc372fe 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStreamTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/AwsChunkedInputStreamTest.java@@ -18,8 +18,40 @@ class AwsChunkedInputStreamTest {@Testvoid decodesSignedChunksUpToTheFinalChunk() throws IOException {- assertEquals("hello world", decode("5;chunk-signature=abc\r\nhello\r\n6;chunk-signature=def\r\n world\r\n"- + "0;chunk-signature=ghi\r\n\r\n"));+ String first = "a".repeat(8192);+ assertEquals(first + " world", decode("2000;chunk-signature=abc\r\n" + first + "\r\n6;chunk-signature=def\r\n"+ + " world\r\n0;chunk-signature=ghi\r\n\r\n"));+ }++ @Test+ void aChunkBeforeTheLastUnder8KiBIsAnInvalidChunkSize() {+ assertInvalidChunkSize("5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n");+ assertInvalidChunkSize("5;chunk-signature=abc\r\nhello\r\n6;chunk-signature=def\r\n world\r\n"+ + "0;chunk-signature=ghi\r\n\r\n");+ assertInvalidChunkSize("1fff\r\n" + "a".repeat(8191) + "\r\n1\r\nb\r\n0\r\n\r\n");+ assertInvalidChunkSize("2000\r\n" + "a".repeat(8192) + "\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n");+ }++ @Test+ void onlyTheLastChunkMayBeUnder8KiB() throws IOException {+ String full = "a".repeat(8192);+ assertEquals("hello", decode("5\r\nhello\r\n0\r\n\r\n"));+ assertEquals(full, decode("2000\r\n" + full + "\r\n0\r\n\r\n"));+ assertEquals(full + "b", decode("2000\r\n" + full + "\r\n1\r\nb\r\n0\r\n\r\n"));+ assertEquals(full + full + "hello", decode("2000\r\n" + full + "\r\n2000\r\n" + full+ + "\r\n5\r\nhello\r\n0\r\n\r\n"));+ }++ @Test+ void readsTheTrailerAfterALastChunkUnder8KiB() throws IOException {+ String full = "a".repeat(8192);+ try (AwsChunkedInputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(+ ("2000\r\n" + full + "\r\n5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n\r\n")+ .getBytes(StandardCharsets.US_ASCII)),+ ChecksumAlgorithm.CRC32)) {+ assertEquals(full + "hello", new String(in.readAllBytes(), StandardCharsets.US_ASCII));+ assertEquals("NhCmhg==", in.trailerChecksum());+ }}@Test@@ -130,6 +162,13 @@ class AwsChunkedInputStreamTest {}}+ private static void assertInvalidChunkSize(String framed) {+ AwsException error = assertThrows(AwsException.class, () -> decode(framed));+ assertEquals("InvalidChunkSizeError", error.getErrorCode());+ assertEquals("Only the last chunk is allowed to have a size less than 8192 bytes", error.getMessage());+ assertEquals(403, error.getHttpStatus());+ }+private static String decode(String framed) throws IOException {try (InputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(framed.getBytes(StandardCharsets.ISO_8859_1)))) {diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AwsChunkedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AwsChunkedIntegrationTest.javaindex d885e9a68b..10f6deba25 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AwsChunkedIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AwsChunkedIntegrationTest.java@@ -1,8 +1,10 @@package io.github.hectorvent.floci.services.s3;+import io.github.hectorvent.floci.services.s3.model.S3Checksum;import io.quarkus.test.junit.QuarkusTest;import org.junit.jupiter.api.Test;+import java.nio.charset.StandardCharsets;import java.util.UUID;import static io.restassured.RestAssured.given;@@ -20,6 +22,11 @@ class S3AwsChunkedIntegrationTest {// S3 names the size proposed and the most allowed, in this order, between the message and the request id.private static final String ENTITY_TOO_LARGE_SIZES ="</Message><ProposedSize>5368709121</ProposedSize><MaxSizeAllowed>5368709120</MaxSizeAllowed><RequestId>";+ private static final String INVALID_CHUNK_SIZE_MESSAGE =+ "Only the last chunk is allowed to have a size less than 8192 bytes";+ // Two 100-byte chunks and the CRC32 of the 200 bytes: the first chunk is not the last, so it is under 8 KiB.+ private static final String SMALL_CHUNKS = "64\r\n" + "a".repeat(100) + "\r\n64\r\n" + "b".repeat(100)+ + "\r\n0\r\nx-amz-checksum-crc32:0+vo5w==\r\n\r\n";@Testvoid putObject_chunkShorterThanItsDeclaredSize_isIncompleteBodyAndNotStored() {@@ -305,6 +312,73 @@ class S3AwsChunkedIntegrationTest {.body(not(containsString("<Part>")));}+ @Test+ void putObject_chunkBeforeTheLastUnder8KiB_isInvalidChunkSizeErrorAndNotStored() {+ String bucket = createBucket("chunked-small");++ given()+ .header("x-amz-content-sha256", STREAMING_UNSIGNED)+ .header("Content-Encoding", "aws-chunked")+ .header("x-amz-decoded-content-length", "200")+ .header("x-amz-trailer", "x-amz-checksum-crc32")+ .body(SMALL_CHUNKS)+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(403)+ .body(containsString("<Code>InvalidChunkSizeError</Code>"))+ .body(containsString("<Message>" + INVALID_CHUNK_SIZE_MESSAGE + "</Message>"));++ assertObjectAbsent(bucket, "object.txt");+ }++ @Test+ void uploadPart_chunkBeforeTheLastUnder8KiB_isInvalidChunkSizeErrorAndNoPartIsStored() {+ String bucket = createBucket("chunked-part-small");+ 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("Content-Encoding", "aws-chunked")+ .header("x-amz-decoded-content-length", "200")+ .header("x-amz-trailer", "x-amz-checksum-crc32")+ .body(SMALL_CHUNKS)+ .when()+ .put("/" + bucket + "/object.txt?partNumber=1&uploadId=" + uploadId)+ .then()+ .statusCode(403)+ .body(containsString("<Code>InvalidChunkSizeError</Code>"))+ .body(containsString("<Message>" + INVALID_CHUNK_SIZE_MESSAGE + "</Message>"));++ given()+ .when().get("/" + bucket + "/object.txt?uploadId=" + uploadId)+ .then().statusCode(200)+ .body(not(containsString("<Part>")));+ }++ @Test+ void putObject_lastChunkUnder8KiB_storesTheDecodedPayload() {+ String bucket = createBucket("chunked-small-last");+ String payload = "a".repeat(8192) + "b".repeat(100);++ given()+ .header("x-amz-content-sha256", STREAMING_UNSIGNED)+ .header("Content-Encoding", "aws-chunked")+ .header("x-amz-decoded-content-length", String.valueOf(payload.length()))+ .header("x-amz-trailer", "x-amz-checksum-crc32")+ .body("2000\r\n" + "a".repeat(8192) + "\r\n64\r\n" + "b".repeat(100) + "\r\n0\r\nx-amz-checksum-crc32:"+ + S3Checksum.crc32Base64(payload.getBytes(StandardCharsets.US_ASCII)) + "\r\n\r\n")+ .when()+ .put("/" + bucket + "/object.txt")+ .then()+ .statusCode(200);++ given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo(payload));+ }+@Testvoid putObject_declaringMoreThanFiveGibibytes_isEntityTooLargeAndNotStored() {String bucket = createBucket("chunked-too-large");