mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(s3): reject a malformed aws-chunked upload instead of storing its framing (#5082)
* fix(s3): reject a malformed aws-chunked upload instead of storing its framing When an aws-chunked body could not be decoded, the decoder logged the failure and returned the raw body, so PutObject and UploadPart stored the chunk headers and signatures as the object's content. A body that ended before its final zero-length chunk was silently truncated. A request whose x-amz-content-sha256 declares a streaming payload is always framed, so a body that does not decode, or that ends before its final chunk, now fails with a 400 IncompleteBody and nothing is stored. The SigV4a (ECDSA) streaming variants count as streaming payloads too. A Content-Encoding naming aws-chunked without that declaration does not guarantee framing, so a body that does not decode is still kept as sent, rather than decoding to an empty object as before. * fix(s3): require each chunk's line break and the closing line of a streaming upload A streaming body whose chunk data was not followed by a line break, or that ended right after its final chunk line or partway through its trailer, was still accepted and stored. For a request that declares a streaming payload, each chunk's data must now be followed by a line break and the body must reach the empty line that ends it; otherwise the upload fails with a 400 IncompleteBody.
This commit is contained in:
1 parent
66f04cde25
commit
a11040e841
2 files changed
+266
-5
No files matched your search
+224
@@ -0,0 +1,224 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
@QuarkusTest
|
||||
class S3AwsChunkedIntegrationTest {
|
||||
|
||||
private static final String STREAMING_UNSIGNED = "STREAMING-UNSIGNED-PAYLOAD-TRAILER";
|
||||
private static final String INCOMPLETE_BODY_MESSAGE =
|
||||
"You did not provide the number of bytes specified by the Content-Length HTTP header.";
|
||||
|
||||
@Test
|
||||
void putObject_chunkShorterThanItsDeclaredSize_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-short");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("10\r\nonly-five\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"))
|
||||
.body(containsString("<Message>" + INCOMPLETE_BODY_MESSAGE + "</Message>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_bodyEndingBeforeItsFinalChunk_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-truncated");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("5\r\nhello\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_chunkSizeThatIsNotHex_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-size");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("five\r\nhello\r\n0\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_chunkNotFollowedByItsLineBreak_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-delimiter");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("5\r\nhello0\r\n\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_bodyEndingBeforeItsTrailerIsComplete_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-trailer");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_bodyEndingRightAfterItsFinalChunkLine_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-final-line");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", "STREAMING-AWS4-HMAC-SHA256-PAYLOAD")
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("5;chunk-signature=abc\r\nhello\r\n0;chunk-signature=def\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_malformedBodyDoesNotReplaceExistingObject() {
|
||||
String bucket = createBucket("chunked-overwrite");
|
||||
given().body("original").when().put("/" + bucket + "/object.txt").then().statusCode(200);
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", STREAMING_UNSIGNED)
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("10\r\nonly-five\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("original"));
|
||||
}
|
||||
|
||||
// SigV4a signs streaming uploads with ECDSA; the body is framed the same way.
|
||||
@Test
|
||||
void putObject_malformedSigV4aStreamingBody_isIncompleteBodyAndNotStored() {
|
||||
String bucket = createBucket("chunked-sigv4a");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD")
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("10;chunk-signature=abc\r\nonly-five\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
assertObjectAbsent(bucket, "object.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_sigV4aStreamingBodyWithTrailer_storesTheDecodedPayload() {
|
||||
String bucket = createBucket("chunked-sigv4a-ok");
|
||||
|
||||
given()
|
||||
.header("x-amz-content-sha256", "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER")
|
||||
.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()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadPart_malformedBody_isIncompleteBodyAndNoPartIsStored() {
|
||||
String bucket = createBucket("chunked-part");
|
||||
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")
|
||||
.body("10\r\nonly-five\r\n")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt?partNumber=1&uploadId=" + uploadId)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>IncompleteBody</Code>"));
|
||||
|
||||
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
|
||||
void putObject_awsChunkedEncodingWithoutStreamingPayload_storesTheBodyAsSent() {
|
||||
String bucket = createBucket("chunked-lenient");
|
||||
|
||||
given()
|
||||
.header("Content-Encoding", "aws-chunked")
|
||||
.body("plain-content")
|
||||
.when()
|
||||
.put("/" + bucket + "/object.txt")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt").then().statusCode(200).body(equalTo("plain-content"));
|
||||
}
|
||||
|
||||
private static String createBucket(String prefix) {
|
||||
String bucket = prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static void assertObjectAbsent(String bucket, String key) {
|
||||
given().when().get("/" + bucket + "/" + key).then().statusCode(404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user