Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

multipart: fix chunked uploads storing truncated objects when the source ends early

Nick Craig-Wood5b7cc097e330
diff --git a/lib/multipart/multipart.go b/lib/multipart/multipart.go
index b8d0a6a84..f7e3734ec 100644
--- a/lib/multipart/multipart.go
+++ b/lib/multipart/multipart.go
@@ -121,6 +121,13 @@ func UploadMultipart(ctx context.Context, src fs.ObjectInfo, in io.Reader, opt U
return nil, err
}
+ // Check the source supplied the number of bytes it declared before
+ // finalising, otherwise a truncated object would be created on the
+ // backend. Returning an error here aborts the upload.
+ if size >= 0 && off != size {
+ return nil, fmt.Errorf("multipart upload: expected %d bytes in input, but got %d: %w", size, off, io.ErrUnexpectedEOF)
+ }
+
err = chunkWriter.Close(ctx)
if err != nil {
return nil, fmt.Errorf("multipart upload: failed to finalise: %w", err)