mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
@@ -2102,11 +2102,13 @@ public class S3Controller {
|
||||
* Format: hex-size;chunk-signature=sig\r\n data \r\n ... 0;chunk-signature=sig\r\n
|
||||
*/
|
||||
private byte[] decodeAwsChunked(byte[] body, String contentEncoding, String contentSha256) {
|
||||
boolean isAwsChunked = (contentEncoding != null
|
||||
&& contentEncoding.toLowerCase(Locale.ROOT).contains("aws-chunked"))
|
||||
|| "STREAMING-AWS4-HMAC-SHA256-PAYLOAD".equals(contentSha256)
|
||||
boolean declaresStreamingPayload = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD".equals(contentSha256)
|
||||
|| "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER".equals(contentSha256)
|
||||
|| "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD".equals(contentSha256)
|
||||
|| "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER".equals(contentSha256)
|
||||
|| "STREAMING-UNSIGNED-PAYLOAD-TRAILER".equals(contentSha256);
|
||||
boolean isAwsChunked = declaresStreamingPayload || (contentEncoding != null
|
||||
&& contentEncoding.toLowerCase(Locale.ROOT).contains("aws-chunked"));
|
||||
if (!isAwsChunked) {
|
||||
return body;
|
||||
}
|
||||
@@ -2115,6 +2117,7 @@ public class S3Controller {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
String raw = new String(body, StandardCharsets.ISO_8859_1);
|
||||
int pos = 0;
|
||||
boolean complete = false;
|
||||
while (pos < raw.length()) {
|
||||
int lineEnd = raw.indexOf('\n', pos);
|
||||
if (lineEnd < 0) break;
|
||||
@@ -2122,7 +2125,11 @@ public class S3Controller {
|
||||
int semiColon = line.indexOf(';');
|
||||
String hexSize = semiColon >= 0 ? line.substring(0, semiColon) : line;
|
||||
int chunkSize = Integer.parseInt(hexSize.trim(), 16);
|
||||
if (chunkSize == 0) break;
|
||||
if (chunkSize == 0) {
|
||||
// A streaming body ends with an empty line, after any trailer headers.
|
||||
complete = !declaresStreamingPayload || reachesEmptyLine(raw, lineEnd + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
int dataStart = lineEnd + 1;
|
||||
byte[] chunkData = new byte[chunkSize];
|
||||
@@ -2131,15 +2138,45 @@ public class S3Controller {
|
||||
|
||||
pos = dataStart + chunkSize;
|
||||
if (pos < raw.length() && raw.charAt(pos) == '\r') pos++;
|
||||
if (pos < raw.length() && raw.charAt(pos) == '\n') pos++;
|
||||
boolean lineBreak = pos < raw.length() && raw.charAt(pos) == '\n';
|
||||
if (lineBreak) pos++;
|
||||
if (declaresStreamingPayload && !lineBreak) {
|
||||
throw new IllegalArgumentException("aws-chunked chunk is not followed by a line break");
|
||||
}
|
||||
}
|
||||
if (!complete) {
|
||||
throw new IllegalArgumentException("aws-chunked body ends before its final chunk");
|
||||
}
|
||||
return out.toByteArray();
|
||||
} catch (Exception e) {
|
||||
// A streaming payload is always framed, so storing it raw would save the chunk
|
||||
// headers as the object's content. Without that declaration, a Content-Encoding
|
||||
// naming aws-chunked does not guarantee framing, and the body is kept as sent.
|
||||
if (declaresStreamingPayload) {
|
||||
LOG.debugv("Rejecting malformed aws-chunked body: {0}", e.getMessage());
|
||||
throw new AwsException("IncompleteBody",
|
||||
"You did not provide the number of bytes specified by the Content-Length HTTP header.",
|
||||
400);
|
||||
}
|
||||
LOG.debugv("Failed to decode aws-chunked body, using raw: {0}", e.getMessage());
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean reachesEmptyLine(String raw, int pos) {
|
||||
while (pos < raw.length()) {
|
||||
int lineEnd = raw.indexOf('\n', pos);
|
||||
if (lineEnd < 0) {
|
||||
return false;
|
||||
}
|
||||
if (raw.substring(pos, lineEnd).trim().isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
pos = lineEnd + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* S3's CreateBucket region rules, the same in every partition: every regional endpoint but
|
||||
* {@code us-east-1} requires a constraint naming exactly its region and answers
|
||||
|
||||
+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