Skip to content

feat(s3): validate the parts list on CompleteMultipartUpload (#400) - #407

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/400-multipart-complete-validation
Jul 31, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/400-multipart-complete-validation

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #400.

completeMultipartUpload assembled whatever parts it was handed. EntityTooSmall appeared nowhere in the repo, and the ETag in each <Part> was parsed into a struct field that was never read.

What this changes

Condition Before After
Non-final part under 5,242,880 bytes 200 400 EntityTooSmall
Part never uploaded 400 InvalidPart unchanged
Supplied ETag ≠ stored part ETag 200 400 InvalidPart
Part numbers not strictly ascending 400 InvalidPartOrder unchanged
No Part elements 400 InvalidPart 400 MalformedXML
uploadId valid but wrong bucket/key 200, object written under the wrong key 404 NoSuchUpload

The last part is exempt from the minimum and may be zero bytes; a single-part upload is nothing but its own final part, so it is exempt too.

The EntityTooSmall body carries the offending <PartNumber>, its <ETag>, <ProposedSize> and <MinSizeAllowed> — the capability #396 added to the error builder, which is why this was sequenced after it.

Why the failure path matters more than the validation

A consumer computing multipart chunk sizes from a file size can land under 5 MB and have every upload in that band fail at Complete on real S3 — after every part has been uploaded and paid for. Substrate accepted those uploads, so the only thing holding such a consumer’s size floor correct was a unit test asserting its own arithmetic against itself.

Just as important is what a rejected Complete does not do. It writes no object and leaves the upload open, so ListMultipartUploads still reports it until AbortMultipartUpload ends it. That makes no orphan upload was left behind after a failed Complete a property a test can assert by observing the emulator, rather than by asserting the consumer called Abort. Orphaned parts bill indefinitely, and the issue reporter is fixing exactly that leak.

Details worth a second look

  • ETag comparison ignores quoting, hex case and whitespace. An ETag is opaque, but clients differ on whether they echo back the quotes S3 sends, and a false InvalidPart over quoting would send a consumer hunting a data bug that does not exist. Covered by a 4-case table test.
  • Existence and ETag are checked across all parts before any size check — the size of a part that was never uploaded is not a meaningful complaint.
  • Zero parts is MalformedXML, not InvalidPart. Per the API reference: "If you do not supply a valid Part with your request, the service sends back an HTTP 400 response." The old InvalidPart was a 400 too, so this changes the code, not the status.
  • Error codes, messages and statuses are verbatim from the AWS CompleteMultipartUpload API reference, which also replaced the abbreviated NoSuchUpload / MalformedXML paraphrases used across the plugin. No test asserted the old strings.
  • validateCompleteParts returns ([]S3Part, *AWSResponse, error): the third value is an internal failure, the second is an S3 error to serve. It writes nothing.

Test changes

TestS3_MultipartUpload_Complete completed two 13-byte parts with the made-up ETags e1/e2; it now uses a 5 MB first part and the ETags UploadPart actually returned, as a real client would. TestS3_MultipartUpload_UserMetadata likewise dropped its e1.

New emulator/s3_multipart_test.go covers the three acceptance criteria from the issue (1 KiB first part of two → EntityTooSmall and no object at the key; 1 KiB final part succeeds; the upload is still listed after the failed Complete and gone after Abort) plus the InvalidPart, InvalidPartOrder, MalformedXML, ETag-quoting, wrong-bucket/key, and complete-then-NoSuchUpload cases.

Verification

make test (race), golangci-lint run ./... (0 issues), go vet ./..., test/e2e, make docs-reference-check — all green. docs/services.md gains a Multipart upload validation section with the condition table and the EntityTooSmall body.

CompleteMultipartUpload assembled whatever parts it was handed, so
EntityTooSmall was unreachable and a supplied ETag was parsed and then
ignored. A consumer that computes chunk sizes from a file size can land
under S3's 5 MB non-final-part minimum and have every upload in that band
fail at Complete on real AWS — after every part has been uploaded and paid
for — while substrate accepted it. The only thing holding such a consumer's
size floor correct was a unit test asserting its own arithmetic against
itself, which tracks a bug rather than catching it.

Complete now validates before assembling anything:

  - a non-final part under 5,242,880 bytes is 400 EntityTooSmall, carrying
    the offending <PartNumber>, its <ETag>, <ProposedSize> and
    <MinSizeAllowed>
  - a part never uploaded, or one whose supplied ETag does not match the
    stored one, is 400 InvalidPart
  - a body naming no parts is 400 MalformedXML, not InvalidPart
  - an upload ID presented against a different bucket or key is 404
    NoSuchUpload, matching the guard UploadPart and AbortMultipartUpload
    already applied

The final part is exempt from the minimum and may be zero bytes; a
single-part upload is nothing but its own final part, so it is exempt too.
ETags compare ignoring surrounding quotes, hex case and whitespace, since
clients differ on whether they echo back the quotes S3 sends and a false
InvalidPart would send a consumer hunting a data bug that does not exist.

Equally important is what a rejected Complete does not do: it writes no
object and leaves the upload open, so ListMultipartUploads still reports it
until AbortMultipartUpload ends it. That makes "no orphan upload was left
behind after a failed Complete" a property a test can assert by observing
the emulator rather than by asserting the consumer called Abort — orphaned
parts bill indefinitely.

Error codes, messages and statuses are taken verbatim from the S3
CompleteMultipartUpload API reference, which also replaces the abbreviated
NoSuchUpload and MalformedXML paraphrases used across the plugin.

TestS3_MultipartUpload_Complete previously completed two 13-byte parts with
made-up ETags; it now uses a 5 MB first part and the ETags UploadPart
returned, as a real client would.

Closes #400
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.11940% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_plugin.go 76.11% 12 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit eb3214b into main Jul 31, 2026
15 checks passed
@scttfrdmn
scttfrdmn deleted the feat/400-multipart-complete-validation branch July 31, 2026 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S3: CompleteMultipartUpload does not enforce the 5 MB non-final part minimum (EntityTooSmall unreachable)

1 participant