fix(s3): reject an aws-chunked upload whose non-final chunk is under 8 KiB (#6413)

S3 answers 403 InvalidChunkSizeError when a chunk other than the last
carries fewer than 8,192 bytes; Floci decoded and stored such bodies.

Fixes #6225

Signed-off-by: Wleeaf <179125857+wleeaf@users.noreply.github.com>
This commit is contained in:
Wleeaf authored and GitHub committed 2026-10-24 12:18:48 -04:00
1 parent f22231a8a5
commit c854046523
3 files changed
+130 -3

No files matched your search

@@ -18,8 +18,40 @@ class AwsChunkedInputStreamTest {
@Test
void 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)))) {
@@ -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";
@Test
void 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));
}
@Test
void putObject_declaringMoreThanFiveGibibytes_isEntityTooLargeAndNotStored() {
String bucket = createBucket("chunked-too-large");