Skip to content

fix: a mount reads every compression algorithm it can write (#230) - #243

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/230-decode-any-algorithm
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/230-decode-any-algorithm

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #230.

The defect

A bucket outlives its configuration. Compressor held exactly one codec and Decompress compared the object's stored Content-Encoding against that codec's token, returning the data unchanged on any mismatch — so a mount could read back only what it was currently configured to write.

The issue's verified table, one object written algorithm: zstd and read by four backends differing only in that field:

reader config before after
algorithm: zstd 22,000 bytes, correct correct
algorithm: gzip DATA_CORRUPTION correct
algorithm: lz4 DATA_CORRUPTION correct
enabled: false DATA_CORRUPTION correct

The last row is the one that matters most. Setting enabled: false is how an operator turns compression off after deciding the read amplification was not worth it — the change most likely to be made and least likely to be expected to break anything. Turning compression off stops new objects being compressed; it does not make the existing ones uncompressed.

Nobody got wrong bytes: checkFullyDecoded cross-checks the decoded length against objectfs-original-size and fails closed. But that guard was compensating for a dispatch that could have succeeded — every codec was already linked into the same binary.

The fix

Compressor holds a decoder per Content-Encoding token, built at construction from pkg/compression.SupportedAlgorithms rather than listed by hand. The derivation is the fix rather than the map: #230's actual shape was a set of encoders and a set of decoders maintained independently, and a hand-listed table would reintroduce exactly that. Adding an algorithm to SupportedAlgorithms makes it readable with no second edit.

Decompress gained a second return reporting whether a codec was found, because two cases have to be told apart and only the caller can do it:

  • Unknown token → bytes through, decoded=false, no error. A coding ObjectFS does not implement belongs to whatever tool wrote it, and aws s3 cp hands back the same bytes. checkFullyDecoded is what refuses it if the object is one ObjectFS compressed, and it can tell, because only those carry objectfs-original-size.
  • Claimed token over a body the codec rejects → non-retryable DATA_CORRUPTION, rather than the bare wrapped error the retry layer would previously have taken at face value. Retrying decodes the same frame and fails the same way.

Tests, at both layers

internal/compression/decoders_test.go — the write × read matrix from SupportedAlgorithms in both directions; a disabled compressor that still decodes; the derivation itself (every writable token has a decoder, sorted, no empty token); the two failure modes separated.

internal/storage/s3/compression_algorithm_change_test.go — the issue's table generalized to every writable algorithm × (every algorithm + disabled), through a real HTTP endpoint. It asserts the stored object actually compressed before reading it back, so a pass cannot come from an object that was never encoded. Plus the ranged-read shape, which is where a silent failure hides best: checkFullyDecoded deliberately does not fire on a partial read, because a short range is legitimately short and the length alone cannot distinguish it from a still-encoded body.

Two existing tests changed rather than added to, both asserting the old behavior:

  • TestUndecodableObjectFailsClosed's "cannot decode" case used lz4, now decodable — it would have asserted that a readable object is refused. Replaced with br: a registered coding ObjectFS does not implement, which is the realistic version (a bucket shared with a tool that writes brotli).
  • TestRoundTripAcrossACodecChange accepted a fail-closed read as acceptable. It is a failure now. There is no configuration of this mount that legitimately cannot read an object this mount's codecs can decode.

Mutation verification

Per the project's practice of breaking the code and watching the test fail rather than predicting a signature. Five mutants, all caught:

mutant failures
M1 dispatch on the configured codec (the original defect) 32 subtests
M2 decoder table hand-listed as zstd only 19 subtests
M3 swallow a corrupt frame, return the bytes 1
M4 a disabled compressor decodes nothing 7 subtests
M5 backend gates the decode on its own configured encoding 14 subtests

M5 is the one that matters for test placement: it confirms the S3-layer tests are not riding on the compression package's coverage.

Documentation

docs/features/compression.md said "do not change algorithm once a bucket holds compressed objects" and named the fail-closed error as the consequence. It now says the algorithm is safe to change and enabled: false is safe, dates that to v0.11.0, and keeps the one-way door that is real: the format, not the algorithm — a compressed object is still only readable by something that understands its Content-Encoding, which is cost #1 on that page.

Three stale comments corrected in the same pass — backend.go's fail-closed rationale, vfs/errors.go's ErrIntegrity, and the compression package's own doc. A codec mismatch is no longer among the ways to reach an integrity error, and saying it is would send an operator to change a setting that cannot help. What can still reach it: a coding ObjectFS does not implement, and a header stripped after the write — a CopyObject or a tier transition does not carry it.

Gates

  • go build ./..., go vet ./... clean
  • go test -race ./internal/... ./pkg/... all pass
  • golangci-lint run --new-from-merge-base=origin/main → 0 issues
  • coverage: internal/compression 88.6% (floor raised 87 → 88), internal/storage/s3 82.3% (floor 81)

…red one

A bucket outlives its configuration. Objects accumulate across mounts, across
releases, and across whatever `compression.algorithm` was set to at the time, so
the algorithm a mount is configured to *write* says nothing about the algorithm
of the object in front of it.

The read path did not accept that. `Compressor` held exactly one codec and
`Decompress` compared the object's stored `Content-Encoding` against that
codec's token, returning the data unchanged on any mismatch — so a mount could
read back only what it was currently configured to write. Switching
`algorithm: zstd` to `lz4` made every existing zstd object unreadable, and so
did `enabled: false`, which is how an operator turns compression off after
deciding the read amplification was not worth it. That is the change most likely
to be made and least likely to be expected to break anything: turning
compression off stops new objects being compressed, it does not make the
existing ones uncompressed.

Nobody got wrong bytes. `checkFullyDecoded` cross-checks the decoded length
against the recorded `objectfs-original-size` and fails closed, so the four
readings in the issue produced one correct result and three DATA_CORRUPTION
errors rather than three raw frames with exit status 0. But that guard was
compensating for a dispatch that could have succeeded — every codec was already
linked into the same binary.

`Compressor` now holds a decoder per Content-Encoding token, built at
construction from `pkg/compression.SupportedAlgorithms` rather than listed by
hand. The derivation is the fix rather than the map: #230's actual shape was a
set of encoders and a set of decoders maintained independently, and a
hand-listed table would reintroduce exactly that. Keyed on the token because
that is what the object carries; `ContentEncoding()` is a separate method from
`Algorithm()` precisely so a codec may have them differ.

`Decompress` returns a second value reporting whether a codec was found. False
with a nil error means the bytes came back untouched, and only the caller can
say what that means: a foreign coding is another tool's format and passing it
through is what every S3 client does, while an ObjectFS-compressed object
arriving undecoded is an integrity failure. `checkFullyDecoded` is where those
are told apart, because `objectfs-original-size` is only in hand there. A body
its own declared codec *rejects* is now reported as non-retryable corruption
rather than a bare wrapped error the retry layer would take at face value —
retrying decodes the same frame and fails the same way.

Tests, at both layers:

- `internal/compression` — the full write × read matrix from
  SupportedAlgorithms in both directions, a disabled compressor that still
  decodes, the derivation itself (every writable token has a decoder), and the
  two failure modes separated: unknown token passes through, claimed token over
  a bad body errors.
- `internal/storage/s3` — the issue's failure table generalized, one object per
  writable algorithm read by every algorithm and by a disabled mount, with an
  assertion that the stored object actually compressed so a pass cannot come
  from an object that was never encoded. Plus the ranged-read shape, which is
  where a silent failure hides best: `checkFullyDecoded` deliberately does not
  fire on a partial read, since a short range is legitimately short.

Two existing tests changed rather than being added to, and both were asserting
the old behavior:

- `TestUndecodableObjectFailsClosed`'s "cannot decode" case used `lz4`, which is
  now decodable; it would have been asserting that a readable object is refused.
  Replaced with `br` — a registered coding ObjectFS does not implement, which is
  the realistic version: a bucket shared with a tool that writes brotli.
- `TestRoundTripAcrossACodecChange` accepted a fail-closed read as an acceptable
  outcome. It is a failure now. There is no configuration of this mount that
  legitimately cannot read an object this mount's codecs can decode.

Verified by mutation, five mutants, all caught:

  M1 dispatch on the configured codec (the original defect)  32 subtests
  M2 decoder table hand-listed as zstd only                  19 subtests
  M3 swallow a corrupt frame, return the bytes                1
  M4 a disabled compressor decodes nothing                    7 subtests
  M5 backend gates the decode on its own encoding            14 subtests

`docs/features/compression.md` said "do not change `algorithm` once a bucket
holds compressed objects" and named the fail-closed error as the consequence.
It now says the algorithm is safe to change and `enabled: false` is safe, dates
that to v0.11.0, and keeps the one-way door that is real: the *format*, not the
algorithm — a compressed object is still only readable by something that
understands its Content-Encoding. Three stale comments corrected in the same
pass, in `backend.go`'s fail-closed rationale, `vfs/errors.go`'s ErrIntegrity,
and the compression package's own doc: a codec mismatch is no longer among the
ways to reach an integrity error, and saying it is would send an operator to
change a setting that cannot help.

Closes #230
@scttfrdmn
scttfrdmn merged commit 9a33f44 into main Aug 3, 2026
23 checks passed
@scttfrdmn
scttfrdmn deleted the fix/230-decode-any-algorithm branch August 3, 2026 07:20
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.

A mount can only read the one compression algorithm it is configured for, so changing algorithm orphans existing objects

1 participant