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:
Niels Kootstra authored and GitHub committed 2026-10-04 13:50:45 -04:00
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