fix(s3): stream PutObject and UploadPart bodies to disk (#5106)

The object PUT endpoint took its body as a byte array, so a single
PutObject or UploadPart was held whole in the heap and capped near 2 GiB.

The body is now read as a stream. A streaming aws-chunked payload is
decoded as it arrives, and Content-MD5 and x-amz-checksum-* are checked
on the way through. In the disk-backed modes the body is staged under
.multipart/.incoming with no lock held, then a part is moved into its
upload under the upload's lock and an object is stored like an
assembled multipart object. Checks the request alone decides (bucket,
SSE-C headers, checksum algorithm, write preconditions, a malformed
Content-MD5) run before any of the body is read. Memory mode reads the
body into a capped array and fails past about 2 GiB with EntityTooLarge.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-05 17:11:47 -05:00
1 parent f157d354f2
commit 583de3010c
8 files changed
+1022 -111

No files matched your search

@@ -0,0 +1,96 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
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"));
}
@Test
void skipsTrailerLinesAfterTheFinalChunk() throws IOException {
assertEquals("hello", decode("5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n"
+ "x-amz-trailer-signature:ghi\r\n\r\n"));
}
@Test
void acceptsBareLineFeeds() throws IOException {
assertEquals("hello", decode("5\nhello\n0\n\n"));
}
@Test
void decodesManyChunksWhateverTheReadSize() throws IOException {
Random random = new Random(5080);
byte[] payload = new byte[200_003];
random.nextBytes(payload);
ByteArrayOutputStream framed = new ByteArrayOutputStream();
for (int offset = 0; offset < payload.length; offset += 8192) {
int size = Math.min(8192, payload.length - offset);
framed.writeBytes((Integer.toHexString(size) + ";chunk-signature=sig\r\n").getBytes(StandardCharsets.US_ASCII));
framed.write(payload, offset, size);
framed.writeBytes("\r\n".getBytes(StandardCharsets.US_ASCII));
}
framed.writeBytes("0;chunk-signature=sig\r\n\r\n".getBytes(StandardCharsets.US_ASCII));
for (int readSize : List.of(1, 7, 4096, 65_536)) {
try (InputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(framed.toByteArray()))) {
ByteArrayOutputStream decoded = new ByteArrayOutputStream();
byte[] buffer = new byte[readSize];
for (int read = in.read(buffer); read >= 0; read = in.read(buffer)) {
decoded.write(buffer, 0, read);
}
assertArrayEquals(payload, decoded.toByteArray(), "read size " + readSize);
}
}
}
@Test
void readsOneByteAtATime() throws IOException {
try (InputStream in = new AwsChunkedInputStream(new ByteArrayInputStream(
"3\r\nabc\r\n0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)))) {
assertEquals('a', in.read());
assertEquals('b', in.read());
assertEquals('c', in.read());
assertEquals(-1, in.read());
assertEquals(-1, in.read(), "the stream stays at its end");
}
}
@Test
void brokenFramingIsAnIncompleteBody() {
for (String body : List.of(
"10\r\nonly-five\r\n",
"5\r\nhello\r\n",
"five\r\nhello\r\n0\r\n\r\n",
"5\r\nhello0\r\n\r\n",
"5\r\nhello\r\n0\r\nx-amz-checksum-crc32:NhCmhg==\r\n",
"5;chunk-signature=abc\r\nhello\r\n0;chunk-signature=def\r\n",
"-5\r\nhello\r\n0\r\n\r\n",
"")) {
AwsException error = assertThrows(AwsException.class, () -> decode(body), body);
assertEquals("IncompleteBody", error.getErrorCode(), body);
assertEquals(400, error.getHttpStatus(), body);
}
}
private static String decode(String framed) throws IOException {
try (InputStream in = new AwsChunkedInputStream(
new ByteArrayInputStream(framed.getBytes(StandardCharsets.ISO_8859_1)))) {
return new String(in.readAllBytes(), StandardCharsets.ISO_8859_1);
}
}
}
@@ -0,0 +1,337 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.ChecksumAlgorithm;
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.PutObjectOptions;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.time.Duration;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
/**
* PutObject and UploadPart bodies read as a stream, as the controller hands them over, so a
* disk-backed upload is never held in memory whole.
*/
class S3StreamedUploadTest {
private static final byte[] BODY = "a body that arrives as a stream".getBytes(StandardCharsets.UTF_8);
private static final UploadChecksums NO_CHECKSUMS = new UploadChecksums(null, Map.of());
@TempDir
Path tempDir;
private S3Service s3Service;
private Path dataRoot;
@BeforeEach
void setUp() {
dataRoot = tempDir.resolve("s3");
s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, false);
s3Service.createBucket("bucket", "us-east-1");
}
@Test
void aStreamedPutStoresWhatTheBytePathStores() {
S3Object fromBytes = s3Service.putObject("bucket", "bytes.txt", BODY, "text/plain", Map.of());
S3Object streamed = s3Service.putObject("bucket", "streamed.txt", new ByteArrayInputStream(BODY),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions());
assertEquals(fromBytes.getETag(), streamed.getETag());
assertEquals(fromBytes.getChecksum().getChecksumCRC64NVME(), streamed.getChecksum().getChecksumCRC64NVME());
assertEquals(ChecksumType.FULL_OBJECT, streamed.getChecksum().getChecksumType());
assertEquals(BODY.length, streamed.getSize());
assertArrayEquals(BODY, s3Service.getObject("bucket", "streamed.txt").getData());
assertNothingStaged();
}
@Test
void aStreamedPutStoresTheDeclaredAlgorithmsChecksum() {
S3Object streamed = s3Service.putObject("bucket", "sha.txt", new ByteArrayInputStream(BODY),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions().withChecksumAlgorithm("SHA256"));
assertEquals(ChecksumAlgorithm.SHA256.compute(BODY), streamed.getChecksum().getChecksumSHA256());
assertNull(streamed.getChecksum().getChecksumCRC64NVME());
}
@Test
void aStreamedPutIntoAVersionedBucketKeepsEachVersion() {
s3Service.putBucketVersioning("bucket", "Enabled");
byte[] second = "the second version".getBytes(StandardCharsets.UTF_8);
S3Object first = s3Service.putObject("bucket", "versioned.txt", new ByteArrayInputStream(BODY),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions());
s3Service.putObject("bucket", "versioned.txt", new ByteArrayInputStream(second),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions());
assertArrayEquals(second, s3Service.getObject("bucket", "versioned.txt").getData());
assertArrayEquals(BODY, s3Service.getObject("bucket", "versioned.txt", first.getVersionId()).getData());
assertNothingStaged();
}
@Test
void aBodyThatFailsItsChecksumIsNotStored() {
UploadChecksums wrong = new UploadChecksums(null,
Map.of(ChecksumAlgorithm.CRC32, ChecksumAlgorithm.CRC32.compute("something else".getBytes(StandardCharsets.UTF_8))));
AwsException error = assertThrows(AwsException.class, () -> s3Service.putObject("bucket", "bad.txt",
new ByteArrayInputStream(BODY), wrong, "text/plain", Map.of(), new PutObjectOptions()));
assertEquals("BadDigest", error.getErrorCode());
assertEquals("NoSuchKey", assertThrows(AwsException.class,
() -> s3Service.headObject("bucket", "bad.txt")).getErrorCode());
assertNothingStaged();
}
@Test
void aBodyThatMatchesItsContentMd5AndChecksumIsStored() throws Exception {
String md5 = Base64.getEncoder().encodeToString(MessageDigest.getInstance("MD5").digest(BODY));
UploadChecksums right = new UploadChecksums(md5, Map.of(ChecksumAlgorithm.SHA1, ChecksumAlgorithm.SHA1.compute(BODY)));
s3Service.putObject("bucket", "good.txt", new ByteArrayInputStream(BODY), right,
"text/plain", Map.of(), new PutObjectOptions());
assertArrayEquals(BODY, s3Service.getObject("bucket", "good.txt").getData());
}
@Test
void aWriteThatCannotSucceedFailsBeforeTheBodyIsRead() {
s3Service.putObject("bucket", "existing.txt", BODY, "text/plain", Map.of());
assertEquals("PreconditionFailed", assertThrows(AwsException.class, () -> s3Service.putObject("bucket",
"existing.txt", unreadable(), NO_CHECKSUMS, "text/plain", Map.of(),
new PutObjectOptions().withIfNoneMatch("*"))).getErrorCode());
assertEquals("NoSuchBucket", assertThrows(AwsException.class, () -> s3Service.putObject("missing-bucket",
"key.txt", unreadable(), NO_CHECKSUMS, "text/plain", Map.of(),
new PutObjectOptions())).getErrorCode());
assertEquals("InvalidDigest", assertThrows(AwsException.class, () -> s3Service.putObject("bucket",
"key.txt", unreadable(), new UploadChecksums("not-an-md5", Map.of()), "text/plain", Map.of(),
new PutObjectOptions())).getErrorCode());
assertEquals("InvalidArgument", assertThrows(AwsException.class, () -> s3Service.putObject("bucket",
"key.txt", unreadable(), NO_CHECKSUMS, "text/plain", Map.of(),
new PutObjectOptions().withChecksumAlgorithm("NOT-AN-ALGORITHM"))).getErrorCode());
}
@Test
void aBodyThatBreaksPartwayLeavesNothingBehind() {
assertThrows(UncheckedIOException.class, () -> s3Service.putObject("bucket", "broken.txt",
breaksAfter(BODY), NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions()));
AwsException framing = assertThrows(AwsException.class, () -> s3Service.putObject("bucket", "framing.txt",
new AwsChunkedInputStream(new ByteArrayInputStream("10\r\nonly-five\r\n".getBytes(StandardCharsets.US_ASCII))),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions()));
assertEquals("IncompleteBody", framing.getErrorCode());
assertEquals("NoSuchKey", assertThrows(AwsException.class,
() -> s3Service.headObject("bucket", "broken.txt")).getErrorCode());
assertNothingStaged();
}
@Test
void aStreamedPartStoresWhatTheBytePathStores() {
byte[] part2 = "and a second part".getBytes(StandardCharsets.UTF_8);
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "multipart.bin", null);
Part fromBytes = s3Service.storePart("bucket", "multipart.bin", upload.getUploadId(), 1, BODY, null, null, null);
Part streamed = s3Service.storePart("bucket", "multipart.bin", upload.getUploadId(), 2,
new ByteArrayInputStream(part2), NO_CHECKSUMS, null, null, null);
Part streamedAgain = s3Service.storePart("bucket", "multipart.bin", upload.getUploadId(), 1,
new ByteArrayInputStream(BODY), NO_CHECKSUMS, null, null, null);
assertEquals(fromBytes.getETag(), streamedAgain.getETag());
assertEquals(fromBytes.getChecksum().getChecksumCRC64NVME(), streamedAgain.getChecksum().getChecksumCRC64NVME());
assertEquals(part2.length, streamed.getSize());
s3Service.completeMultipartUpload("bucket", "multipart.bin", upload.getUploadId(), List.of(1, 2), null, null);
byte[] whole = new byte[BODY.length + part2.length];
System.arraycopy(BODY, 0, whole, 0, BODY.length);
System.arraycopy(part2, 0, whole, BODY.length, part2.length);
assertArrayEquals(whole, s3Service.getObject("bucket", "multipart.bin").getData());
assertNothingStaged();
}
@Test
void aPartThatFailsItsChecksumIsNotRecorded() {
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "multipart.bin", null);
UploadChecksums wrong = new UploadChecksums(null,
Map.of(ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA256.compute("other".getBytes(StandardCharsets.UTF_8))));
assertEquals("BadDigest", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
"multipart.bin", upload.getUploadId(), 1, new ByteArrayInputStream(BODY), wrong,
null, null, null)).getErrorCode());
assertTrue(s3Service.getMultipartUpload("bucket", "multipart.bin", upload.getUploadId()).getParts().isEmpty());
assertNothingStaged();
}
@Test
void aPartForAnUploadThatCannotTakeItFailsBeforeItsBodyIsRead() {
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "multipart.bin", null);
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
"multipart.bin", "no-such-upload", 1, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
assertEquals("InvalidArgument", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
"multipart.bin", upload.getUploadId(), 10001, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
}
@Test
void anUploadAbortedWhileAPartArrivesFailsThePartAndLeavesNothingBehind() {
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "multipart.bin", null);
InputStream body = onFirstRead(BODY,
() -> s3Service.abortMultipartUpload("bucket", "multipart.bin", upload.getUploadId()));
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> s3Service.storePart("bucket",
"multipart.bin", upload.getUploadId(), 1, body, NO_CHECKSUMS, null, null, null)).getErrorCode());
assertFalse(Files.exists(dataRoot.resolve(".multipart").resolve(upload.getUploadId())));
assertNothingStaged();
}
@Test
void aPartStillArrivingDoesNotHoldUpDeletingItsBucket() {
MultipartUpload upload = s3Service.initiateMultipartUpload("bucket", "multipart.bin", null);
// Deleting the bucket takes its multipart lock exclusively, so this would never return if the
// part held that lock while its body arrived.
InputStream body = onFirstRead(BODY, () -> s3Service.deleteBucket("bucket"));
AwsException error = assertTimeoutPreemptively(Duration.ofSeconds(30), () -> assertThrows(AwsException.class,
() -> s3Service.storePart("bucket", "multipart.bin", upload.getUploadId(), 1, body,
NO_CHECKSUMS, null, null, null)));
assertTrue(List.of("NoSuchUpload", "NoSuchBucket").contains(error.getErrorCode()), error.getErrorCode());
assertNothingStaged();
}
@Test
void memoryModeReadsTheStreamIntoTheBytePaths() {
S3Service memory = memoryService();
S3Object fromBytes = memory.putObject("bucket", "bytes.txt", BODY, "text/plain", Map.of());
S3Object streamed = memory.putObject("bucket", "streamed.txt", new ByteArrayInputStream(BODY),
NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions());
MultipartUpload upload = memory.initiateMultipartUpload("bucket", "multipart.bin", null);
memory.storePart("bucket", "multipart.bin", upload.getUploadId(), 1, new ByteArrayInputStream(BODY),
NO_CHECKSUMS, null, null, null);
memory.completeMultipartUpload("bucket", "multipart.bin", upload.getUploadId(), List.of(1), null, null);
assertEquals(fromBytes.getETag(), streamed.getETag());
assertArrayEquals(BODY, memory.getObject("bucket", "streamed.txt").getData());
assertArrayEquals(BODY, memory.getObject("bucket", "multipart.bin").getData());
UploadChecksums wrong = new UploadChecksums(null,
Map.of(ChecksumAlgorithm.CRC32C, ChecksumAlgorithm.CRC32C.compute("other".getBytes(StandardCharsets.UTF_8))));
assertEquals("BadDigest", assertThrows(AwsException.class, () -> memory.putObject("bucket", "bad.txt",
new ByteArrayInputStream(BODY), wrong, "text/plain", Map.of(), new PutObjectOptions())).getErrorCode());
}
@Test
void aBucketRecreatedWhileABodyArrivesDoesNotReceiveTheObject() {
for (S3Service service : List.of(s3Service, memoryService())) {
service.createBucket("recreated", "us-east-1");
// The request was checked against the first bucket; the second has its own policy.
InputStream body = onFirstRead(BODY, () -> {
service.deleteBucket("recreated");
service.createBucket("recreated", "us-east-1");
});
assertEquals("NoSuchBucket", assertThrows(AwsException.class, () -> service.putObject("recreated",
"object.txt", body, NO_CHECKSUMS, "text/plain", Map.of(), new PutObjectOptions())).getErrorCode());
assertEquals("NoSuchKey", assertThrows(AwsException.class,
() -> service.headObject("recreated", "object.txt")).getErrorCode());
}
assertNothingStaged();
}
@Test
void memoryModeChecksAPartsUploadBeforeReadingItsBody() {
S3Service memory = memoryService();
MultipartUpload upload = memory.initiateMultipartUpload("bucket", "multipart.bin", null);
assertEquals("NoSuchUpload", assertThrows(AwsException.class, () -> memory.storePart("bucket",
"multipart.bin", "no-such-upload", 1, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
assertEquals("InvalidArgument", assertThrows(AwsException.class, () -> memory.storePart("bucket",
"multipart.bin", upload.getUploadId(), 0, unreadable(), NO_CHECKSUMS, null, null, null)).getErrorCode());
}
private S3Service memoryService() {
S3Service memory = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, true);
memory.createBucket("bucket", "us-east-1");
return memory;
}
private void assertNothingStaged() {
Path incoming = dataRoot.resolve(".multipart").resolve(".incoming");
if (!Files.isDirectory(incoming)) {
return;
}
try (Stream<Path> files = Files.list(incoming)) {
assertEquals(List.of(), files.toList(), "staged bodies left behind");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/** A body the write should never get to reading. */
private static InputStream unreadable() {
return new InputStream() {
@Override
public int read() {
throw new AssertionError("the body was read");
}
@Override
public int read(byte[] buffer, int offset, int length) {
throw new AssertionError("the body was read");
}
};
}
/** {@code data}, then a failure, as when a client disconnects partway. */
private static InputStream breaksAfter(byte[] data) {
return new FilterInputStream(new ByteArrayInputStream(data)) {
@Override
public int read(byte[] buffer, int offset, int length) throws IOException {
int read = super.read(buffer, offset, length);
if (read < 0) {
throw new IOException("connection reset");
}
return read;
}
};
}
/** {@code data}, running {@code action} the first time any of it is read. */
private static InputStream onFirstRead(byte[] data, Runnable action) {
return new ByteArrayInputStream(data) {
private boolean started;
@Override
public synchronized int read(byte[] buffer, int offset, int length) {
if (!started) {
started = true;
action.run();
}
return super.read(buffer, offset, length);
}
};
}
}