Repository navigation
Description
Summary
PutObject compresses object bodies but the CargoShip upload path stores the
Content-Encoding value as user metadata (x-amz-meta-content-encoding) instead of the
real HTTP header. GetObject keys decompression off the real header only, so objects
uploaded through that path are returned to callers as raw zstd bytes.
Background
In internal/storage/s3/backend.go PutObject, the standard path sets the header correctly:
if contentEncoding != "" {
input.ContentEncoding = aws.String(contentEncoding)
}So does initiateMultipartUpload in internal/storage/s3/multipart_upload.go. The CargoShip
branch does not — it puts the value in the metadata map that becomes x-amz-meta-*:
if contentEncoding != "" {
cargoMeta["content-encoding"] = contentEncoding
}This is a limitation of the dependency, not an oversight that can be worked around in the
archive struct: in [email protected]/pkg/aws/s3/transporter.go the Archive struct has no
ContentEncoding field, and the PutObjectInput it builds sets only Bucket, Key, Body,
StorageClass, and Metadata.
The read path confirms the data is unrecoverable. GetObject decompresses only when the
real header came back:
if result.ContentEncoding != nil {
contentEncoding = aws.ToString(result.ContentEncoding)
}
// ...
if b.compressor != nil && contentEncoding != "" {
decompressed, decompErr := b.compressor.Decompress(data, contentEncoding)
// ...
}Nothing consults user metadata. Two consequences:
- A full read returns raw compressed bytes to the application.
- Worse, the range slice that runs after the decompress step then slices compressed
bytes at uncompressed offsets — so every partial read of such an object returns
silent garbage from the wrong position.
Implementation
Hoist the wasCompressed result out of the compression block and skip the CargoShip
transporter when it is true, taking the standard PutObject path that sets the header
correctly. This is correct-by-construction and needs no upstream change. Log at INFO when
CargoShip is bypassed for this reason so the throughput trade-off stays visible.
Upstreaming a ContentEncoding field to CargoShip's Archive is the longer-term fix and
should be filed separately; it does not block this one.
Note while in this code: the CargoShip branch is unreachable for objects at or above
MultipartThreshold, because the multipart early-return precedes it. The transporter only
ever handles objects smaller than 32 MB by default, so the "4.6x performance" comment above
it overstates its reach. Correct the comment.
Acceptance Criteria
- When compression compresses an object, the upload takes the standard path and
HeadObjectreportsContentEncoding: "zstd"— notx-amz-meta-content-encoding - A full
GetObjectreturns bytes byte-identical to the input - A partial
GetObjectat a non-zero offset returns the correct slice - Unit test asserts the CargoShip path is not selected when
wasCompressedis true - The misleading throughput comment is corrected
-
go test -race ./internal/storage/s3/...passes -
AWS_PROFILE=aws AWS_REGION=us-west-2 go test -race -tags=integration ./internal/storage/s3/...passes
The reproducing case needs an object between MinSize (4KB) and MultipartThreshold
(32MB): below 4KB nothing is compressed, and at or above 32MB the CargoShip branch is
unreachable.
Dependencies
None. Must land before the issue that plumbs EnableCargoShipOptimization through
buildS3Config — today that flag never reaches the backend on the mount path, so the
CargoShip branch is dead code there. Plumbing it first would activate this corruption.
Activity
scttfrdmn commentedon Aug 2, 2026
Moved to v0.11.0. Not fixed in v0.10.1, and it is worth being precise about why, because the release did touch this code.
internal/storage/s3/backend.go:830-885now bypasses CargoShip entirely for compressed objects and takes the plain SDK put path instead. That closes the corruption — aContent-Encodingcannot be turned into user metadata by a path that is no longer taken — but it does so by routing around the defect, not by fixing it. The CargoShip upload path still cannot carryContent-Encodingcorrectly, so the moment anything else needs that path for an encoded object, the bug is live again.So the fix in the release is a containment, and this issue stays open for the real one: make the CargoShip path preserve
Content-Encodingas a header, and then remove the bypass.scttfrdmn commentedon Aug 3, 2026
The corruption this issue describes is already prevented, though not in the way the title proposes — worth recording, because the remaining work is smaller than the issue reads and the ordering hazard it created is gone.
Measured
CargoShip enabled and compression enabled, zstd,
min_size: 1B— the configuration this issue says corrupts:98,304 bytes in, 98,304 identical bytes out, 37 bytes on the wire. No corruption.
Why
internal/storage/s3/backend.go:874bypasses the transporter entirely when the object carries an encoding:So a compressed object takes the direct path, which does set the header. The fix chosen was "do not use the transporter for objects it cannot represent" rather than "teach the transporter to carry the header" — which is why this issue is still open while the defect is not reachable. The same pattern was applied a few lines down for encryption (
cargoShipCanEncrypt), where the transporter hardcodesaws:kmsand has no bucket-key field.What that changes about scheduling
The v0.10.0 audit plan listed this as an ordering hazard: plumbing config before fixing the encoding path would activate
EnableCargoShipOptimizationand thereby activate the corruption. That constraint no longer holds. The flag is now plumbed —internal/adapter/adapter.go:447maps it froms3cfg.UseCargoShip, so it is reachable on the real mount path, and the round-trip above is that path. Config plumbing work no longer needs to wait on this.What is actually left
The title's literal ask: make the transporter able to carry
Content-Encoding, so compressed objects can use CargoShip's throughput path instead of falling back. That is an optimization, not a correctness fix, and it belongs upstream in cargoship (transporter.go:184puts the encoding in user metadata and nothing sets the HTTP header) before it can be used here.Suggest relabeling from
priority: criticalto something lower and rewriting the summary, since the current text says compressed objects are stored corrupt and they are not. Leaving the label change to @scttfrdmn rather than making it unilaterally — but a critical-priority issue whose defect is unreachable will keep pulling attention away from ones that are live, which is the same failure mode as a green check for work that did not run.Whichever way it goes, the bypass needs a regression test pinning it: the probe above was thrown away, and nothing currently fails if
backend.go:874is deleted.scttfrdmn commentedon Aug 3, 2026
Correcting the last paragraph of my previous comment: I said the bypass has no regression test and that nothing fails if
backend.go:874is deleted. Both are wrong.TestDefaultConfigDoesNotCorruptCompressedObjectsininternal/storage/s3/compression_integrity_test.go:116already pins it, and it pins the right thing — the storedContent-Encodingheader read back through a raw client, not just the round-trip, so it catches the metadata-instead-of-header failure specifically.Verified by mutation rather than by reading it. Disabling the bypass so compressed objects route back through the transporter:
backend.gorestored, confirmed withgit diff.The test's own doc comment also already records the ordering point I made above, and more precisely than I did — including that the audit's "latent" classification was wrong twice over, and that
use_cargoshipmaps straight to the flag as of v0.10.1. Everything else in my previous comment stands: the corruption is unreachable, the ordering hazard is gone, and what remains is the upstream optimization.[-]C0-1: Preserve Content-Encoding on the CargoShip upload path[/-][+]C0-1: Restore CargoShip throughput on the compressed write path[/+]scttfrdmn commentedon Aug 3, 2026
Relabelled
priority: critical→priority: mediumand retitled, because the defect this was filed for is not reachable.backend.go:874bypasses the CargoShip transporter whenevercontentEncoding != "", so a compressed object never travels the path that would have storedContent-Encodingas user metadata. Measured: a 98,304-byte round trip withEnableCargoShipOptimizationand compression both on stored 37 bytes and read back byte-identical.That bypass is pinned by a test with teeth —
TestDefaultConfigDoesNotCorruptCompressedObjects(internal/storage/s3/compression_integrity_test.go:116), which asserts the storedContent-Encodingheader through a raw client. Verified by mutation: changing the guard toif false && contentEncoding != ""fails it withstored Content-Encoding header = "", want "zstd".So there is no correctness work left here. What remains is that compressed writes forgo CargoShip's throughput optimization entirely — a performance gap, and the fix belongs upstream in cargoship (teaching the transporter to carry
Content-Encodingas a real header) rather than here. Retitled to name that.Also note this removes the ordering hazard the audit plan recorded: "plumbing config first would activate CargoShip corruption" no longer applies.
adapter.go:447already maps the flag froms3cfg.UseCargoShipand the round trip is clean.scttfrdmn commentedon Aug 3, 2026
Closed by 851af8e. The corruption itself was fixed in v0.10.1; what remained was the fourth acceptance criterion — a test asserting CargoShip is not selected when
wasCompressedis true.Why that criterion was worth keeping separate.
TestDefaultConfigDoesNotCorruptCompressedObjectsalready asserted the stored object carries a realContent-Encodingheader, which is the property users need. But it is one step removed from the mechanism: it would also pass if the transporter had acquired header support, and it would keep passing if the bypass were replaced by anything else that happened to produce a correct object. The comment inPutObjectexplaining why a compressed body must not go through the transporter had nothing asserting the branch it describes.What the new test uses.
cargoship-created-by, whichbuildMetadatastamps on everything the transporter uploads. It is the only observable that distinguishes the two paths — both end in aPutObjectto the same endpoint with the same body, so asserting on the object alone cannot tell them apart, and asserting on a log line would test the logging rather than the routing.The control half is equally load-bearing. A 1 KiB object, below the 4 KB compression threshold, must carry the stamp. Without that, the test would pass on a build where the transporter never runs at all — silently measuring a disabled feature instead of the bypass.
Both halves verified by mutation. Neutering the bypass condition fails the assertion, and the metadata in the failure output shows the defect exactly:
content-encodingas user metadata beside an empty real header. SettingEnableCargoShipOptimizationfalse fails the control with its reason.Status of the other criteria, all already met:
HeadObjectreportsContentEncoding: "zstd"—TestDefaultConfigDoesNotCorruptCompressedObjectsGetObjectis byte-identical — same testGetObjectat a non-zero offset returns the correct slice —TestPartialReadSurvivesAnAlgorithmChange, at offset 9001 of a 22000-byte object, deliberately away from any boundary so a skipped decode cannot coincide with plaintextbenchmarks/go test -race ./internal/storage/s3/...passesThe ordering constraint this issue named held:
use_cargoshipwas plumbed throughbuildS3Configonly after the bypass existed, so the plumbing never activated the corruption.Upstream follow-up: scttfrdmn/cargoship#353.
Archivestill has no field that maps toContent-Encodingand neither transporter sets the header, as of v0.20.0 (we pin v0.13.0).CompressionTypelooks like the field and is not —buildMetadataputs it in user metadata, and CargoShip's own tests use"none"for it, which is not a valid HTTP content coding, so deriving the header from it would not work either. Until that lands, ObjectFS gives up the throughput optimization for exactly the objects that compressed.v0.11.0: subcommands, a security gate, and prices that know their region