Skip to content

feat(compression): seekable zstd framing, format layer (#185) - #509

Merged
scttfrdmn merged 3 commits into
mainfrom
feat/seekable-zstd-frame-format
Sep 16, 2026
Merged

scttfrdmn merged 3 commits into
mainfrom
feat/seekable-zstd-frame-format

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Stage 1 of #185: the format layer in internal/compression, self-contained and
testable without touching the storage backend. Nothing calls it yet, so no
object this build writes is framed and nothing user-facing changes — the
CHANGELOG entry belongs with the stage that makes the format reachable, not with
this one.

What the format is

The stored object becomes a concatenation of independently decodable zstd frames
behind a skippable frame holding an index over them. A reader that wants
[offset, offset+size) fetches the index, finds the covering frames, and
ranged-GETs only those, instead of the whole stored body as it does today.

Three decisions here are not obvious, and each is recorded at the point it
applies rather than only in the PR:

The index leads, it does not trail. CargoShip's equivalent format (2.1) has
to trail, because it streams a tar pipeline through the encoder and cannot know
the index until the stream is done. objectfs does not stream — Compress takes a
whole buffer and prepareUpload compresses before the multipart dispatch — so
one prefix GET bytes=0-K can carry the index and the first data frames, where
a trailing index needs two requests. Most reads of a file start at offset 0.

Per-frame checksums are over the compressed bytes, verified before the decoder
runs.
That is exactly what a ranged GET returns, so a corrupt or hostile frame
never reaches the decompressor — code fed attacker-controlled length fields. It
also catches every way a store can mis-serve a range: a clamped unsatisfiable
range, an off-by-one range, and a range served from a different generation of
an overwritten object all fail the frame hash, which is why this is safe to
attempt on an endpoint that ignores If-Match.

Skippable ID 0xD, not 0xE. The Zstandard Seekable Format convention claims
0x184D2A5E for its own seek table, whose per-frame checksum is a 32-bit XXH64
truncation over each frame's decompressed bytes — weaker than SHA-256 and on
the wrong side of the decoder to protect it. Claiming 0xE would make two
incompatible layouts answer to one ID. An in-payload "OFSI" magic backs the ID
up, since a skippable ID alone cannot establish whose frame it is.

The object is the authority and the S3 descriptor is only an accelerator:
SetObjectMetadata is a CopyObject with MetadataDirective=REPLACE, which
discards anything not restated — the same mechanism that once let a chmod drop
Content-Encoding and render a compressed object permanently unreadable. If the
descriptor were authoritative, a chmod would silently de-seek an object.

Frame size is derived, not picked

F = sqrt(40·S·r) minimizes what a cold read of one frame transfers: index cost
falls as 1/F, frame cost rises as F. Neither #185's original guess of 1–4 MiB
nor CargoShip's 16 MiB is simply adopted — both are right for their own cost
model, and CargoShip's 16 MiB is in fact what this formula gives for a single
object of roughly 2 TiB, where objectfs pays for an index on the cold read of
each object.

int64 in memory, fixed-width on the wire

The in-memory types are int64 while the format keeps its uint32/uint64
fields. Every caller of this package works in int64 — a Range header, a
ReadAt, a slice bound — so unsigned fields would buy nothing and cost a
conversion at each of those sites, and conversions are where truncation bugs
live. All narrowing happens in AppendFrameIndex, range-checked rather than
trusted, because the index is what the per-frame checksums are verified against:
a value that wrapped on the way onto the wire would surface as corruption
arbitrarily later, on another machine.

This also took gosec from 9 G115 findings on the new file to zero without a
baseline slot. The three suppressions that remain each sit on a statement whose
bound is checked immediately above it.

Two bounds are load-bearing rather than defensive:

  • maxIndexPayload stops an oversized index being written with a truncated
    uint32 SkippableSize, which would leave the frame claiming to end inside
    itself and every CompressedOffset behind it wrong, in an object that looks
    fine.
  • ParseFrameIndex rejects a wire size above MaxInt64. Without it, a
    single-frame index carrying MaxUint64 arrives as -1, FramesCovering
    computes offset / -1, and the slice bound after it panics.

Testing

go test -race ./internal/compression/ green; golangci-lint and standalone
gosec both clean.

Compatibility is a tested property, not a claim: a framed object still decodes
whole through klauspost and through zstd(1) (asserted against the CLI's
actual output, # Skippable Frames: 1, not a predicted string), and a plain
unframed object is reported as ErrNotFramed rather than as corrupt — so
nothing already stored needs migrating.

FuzzFramedRangeRead: 3,375,624 execs, 217 corpus entries, no failures. An
earlier version reported 0 execs/sec because it t.Skip()ped on an out-of-range
random frameSize, and Go does not count skipped inputs as execs; normalizing
the input instead of skipping it is what made the fuzzer actually run.

TestFramedRangeReadMatchesTheSource is exhaustive over every (offset, size)
pair of a 200-byte object at 8 frame sizes. TestFramedReadTransfersOnlyTheCoveringFrames
asserts bytes transferred, not latency, against a budget computed from the
largest frame in the object rather than from the frames the read happened to
fetch — the first version was true by construction.

Two tests exist to prove they are guarding something and fail loudly if not:
the slack-slice test asserts that the slack slice really does decode to more than
one frame, and the MaxInt64 test asserts its fixture has exactly one frame, since
with two the tiling check would answer first and the test would silently stop
testing the bound.

Mutation-verified: 18 mutations, 15 killed. The three survivors are
equivalent mutants and each now says so in the code — DecompressFrame's length
check is subsumed by the checksum, DeriveFrameSize's ceiling arm is
unobservable while MaxFrameSize is a power of two, and the index-length
assertion fires only if indexPayloadLen and AppendFrameIndex disagree, which
no input can cause. The second commit is what that run found: three checks that
had no test, and one duplicate guard deleted rather than tested because
AppendFrameIndex's equivalent is reachable and it was not.

Also corrected against the library rather than assumed: #185's design comment
claims klauspost/compress exports no skippable-frame writer and that objectfs
must emit the 8-byte header by hand. zstd.Header.AppendTo is exported as of
v1.20.0 (which is what this repo is on, not v1.18.0) and is the exact inverse of
Header.Decode, which the reader uses. I'll correct the issue comment.

Not in this PR

Stage 2 wires the backend: prepareUpload, SetObjectMetadata, GetObject's
two encoded arms, a frame-relative offset at sliceRange's call site,
checkFullyDecoded comparing against the frame's UncompressedSize, the
objectfs-seekable descriptor, and the resulting XattrBudget change.

The stored object becomes a concatenation of independently decodable zstd
frames behind a skippable frame holding an index over them, so a reader that
wants [offset, offset+size) can range-GET only the covering frames instead of
the whole body. This commit is the format layer only: nothing in the storage
backend calls it yet, so no object written by this build is framed and nothing
user-facing changes.

Three decisions in here are not obvious and each is recorded at the point it
applies:

The index leads rather than trails. CargoShip's equivalent format has to trail
because it streams a tar pipeline through the encoder; objectfs does not stream
— Compress takes a whole buffer and prepareUpload compresses before the
multipart dispatch — so one prefix GET can carry the index and the first data
frames, where a trailing index needs two requests.

Per-frame checksums are over the compressed bytes and are verified before the
decoder runs. That is exactly what a ranged GET returns, so a corrupt or
hostile frame never reaches the decompressor, and it catches every way a store
can mis-serve a range: clamped, off-by-one, or served from a different
generation of an overwritten object.

Skippable ID 0xD, not 0xE. The Zstandard Seekable Format convention claims 0xE
for its own seek table, whose checksums are a 32-bit XXH64 truncation over each
frame's decompressed bytes — weaker than SHA-256 and on the wrong side of the
decoder.

The in-memory types are int64 while the wire format keeps its uint32/uint64
fields. Every caller of this package works in int64 — a Range header, a ReadAt,
a slice bound — so unsigned fields would buy nothing and cost a conversion at
each of those sites. All narrowing happens in AppendFrameIndex, range-checked
rather than trusted, since the index is what the per-frame checksums are
verified against and a value that wrapped on the way onto the wire would be
reported as corruption arbitrarily later, on another machine.

Two bounds are load-bearing rather than defensive. maxIndexPayload keeps an
oversized index from being written with a truncated uint32 SkippableSize, which
would leave every CompressedOffset behind it wrong in an object that looks
fine. ParseFrameIndex rejects a wire size above MaxInt64, which would otherwise
arrive as a negative in-memory length and be divided by in FramesCovering.

Compatibility is a tested property, not a claim: a framed object still decodes
whole through klauspost and through zstd(1), and a plain unframed object is
reported as unframed rather than corrupt, so nothing already stored needs
migrating.
…plicate guard

Mutation testing the format layer: 18 mutations, 10 killed on the first pass.
Four of the survivors were tests that were not asserting what they looked like
they asserted, and this commit fixes those. The rest are recorded as what they
are rather than papered over.

The MaxInt64 bound in ParseFrameIndex was covered by two rows in the corruption
table, and both passed with the bound deleted. The table's fixture has five
frames, and with more than one frame the tiling check rejects an absurd frame
size several lines before the bound is consulted — so the rows were exercising
the tiling check twice. Moved to a test with a single-frame fixture, where
FrameSize is never compared against a span and a wire value of MaxUint64 really
does reach FramesCovering as -1, which makes `offset / -1` negative and panics
on the slice bound after it. The new test asserts its fixture has exactly one
frame, so it cannot quietly stop testing the bound if the fixture changes.

The check after the decoder, that the frame decoded to the length the index
claimed, had no test at all. It is reachable because Frame is a plain struct a
caller can build: the compressed bytes and their checksum can agree with each
other while UncompressedSize disagrees with both.

The total-coverage check at the end of ParseFrameIndex likewise had none, and
could not get one from the corruption table — zeroing the declared count there
leaves the record bytes in place, which an earlier check rejects first. An index
with no frames at all is the only shape that reaches it, since every other
inconsistency is caught by the tiling check inside the loop.

CompressFramed's own per-frame uint32 check is deleted rather than tested.
AppendFrameIndex now range-checks every extent on the way onto the wire, that
check is reachable from a test, and this one required a 4 GiB compressed frame
to reach. Two guards where one is testable is worse than one guard.

Three survivors are equivalent mutants, and each now says so where it lives:
DecompressFrame's length check is subsumed by the checksum (any length change
also changes the hash) and is kept as a fast reject that refuses a hostile
CompressedSize before hashing a large buffer; DeriveFrameSize's ceiling arm is
unobservable while MaxFrameSize is a power of two; and the index-frame length
assertion in CompressFramed fires only if indexPayloadLen and AppendFrameIndex
disagree, which no input can cause.
CI's lint job flagged this and a local `golangci-lint run` did not. The reason is
worth recording: ci.yml pins golangci-lint to v2.12.2 and this machine has
v2.13.2, so a locally clean run is not evidence about CI. Checked against v2.12.2
directly rather than guessing — clean there too now.
@scttfrdmn
scttfrdmn force-pushed the feat/seekable-zstd-frame-format branch from f6b1101 to c0b2345 Compare September 16, 2026 02:37
@scttfrdmn
scttfrdmn merged commit 700098b into main Sep 16, 2026
43 checks passed
@scttfrdmn
scttfrdmn deleted the feat/seekable-zstd-frame-format branch September 16, 2026 23:43
scttfrdmn added a commit that referenced this pull request Sep 17, 2026
…ver existed (#523)

The next release is 0.15.0, not the 0.16.0 the milestone was titled. Four lines in the
Unreleased section described the CargoShip upload-path removal as having happened "in
v0.15.0" — it shipped in 0.12.0 (#362, 2026-08-08) — and CLAUDE.md repeated the claim.
A fifth said a tier defect was "the v0.15.0 defect"; that one was v0.10.1. No v0.15.0
tag, changelog section, or module-proxy entry has ever existed, so the number is free
and skipping it would have preserved five wrong references to justify a milestone title.

Promotes Unreleased to [0.15.0] - 2026-09-17: seekable compression framing end to end
(#185, via #509/#513/#516) and the GoReleaser convergence (#502) with what it let us
delete. Bumps the version constant, which is the authority the changelog link test and
the packaging tests read.

Also corrects the signing claims across four documents (#505). README.md, docs/index.md
and docs-platform/guide/getting-started.md all said the project "does not sign releases
yet"; #501 signs checksums.txt keylessly with cosign. Each now states the two positions
separately, because neither covers the other: the release is signed, the .deb and .rpm
files are not. The verify command is the one release.yml both runs and prints into the
notes — a bundle, not the --certificate/--signature pair a first draft of this invented.
README's claim that the apt/yum repository is "built and tested on every pull request"
also went with #520, which deleted that machinery.
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.

1 participant