Skip to content

fix(cache): refuse whole-object hits whose body length mismatches metadata - #269

Merged
ovaistariq merged 6 commits into
mainfrom
fix-whole-object-length-check
Oct 22, 2026
Merged

ovaistariq merged 6 commits into
mainfrom
fix-whole-object-length-check

Conversation

@ovaistariq

@ovaistariq ovaistariq commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Nothing on the read side verified cached whole-body bytes against meta.ContentLength, so a truncated cache entry was served with the full Content-Length until TTL. The write-side cause is closed separately (#263); this is the read-side guard.

serveFromCache now counts the bytes delivered from the cache body stream:

  • Small/buffered bodies (≤64 KiB) are checked before any header is written; a mismatch is a cache read failure, the entry is invalidated with the existing ETag-guarded delete, and the request falls through to upstream.
  • Streamed bodies are checked after commit; a mismatch takes the existing post-commit error branch, invalidates with the same guarded delete, and leaves the declared Content-Length unmet so the client sees an unexpected EOF rather than a clean end.
  • ContentLength < 0 (unknown length) skips the check. The range path is unchanged.

A tag_cache_operations_total{operation="get",result="length_mismatch"} sample is recorded on mismatch. No new persisted keys, no pre-serve probe, no extra store round trip on the hit path.

Replaces the serve-side part of #249; the hint-key family, pre-serve probe and HEAD/304 range reads from that PR are intentionally not adopted.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6


Note

Medium Risk
Changes core GetObject cache serve behavior for all whole-object hits; post-commit mismatches can still deliver partial bodies to clients, but corrupt entries are evicted instead of repeating until TTL.

Overview
Adds a read-side guard so whole-object cache hits are only served when streamed bytes match meta.ContentLength, fixing truncated or corrupt entries being returned with a full Content-Length.

Buffered (small) objects: serveFromCache compares the buffered body to metadata before writing headers; mismatch returns errCacheBodyLengthMismatch, which bodyGone treats as unusable so callers invalidate (ETag-guarded) and fall through to upstream.

Streamed (large) objects: A new lengthBoundedWriter refuses chunks past the limit and holds the last promised byte until the stream ends cleanly—so aligned chunk boundaries cannot deliver a “complete” corrupt body. After commit, mismatches leave the response short of Content-Length, record get / length_mismatch metrics, and invalidate the entry in serveFromCache (including http.ErrContentLength as a mismatch). Unknown length (ContentLength < 0) skips the check.

Extensive tests cover pre/post-commit paths, holdback behavior, and no extra cache round trips on valid hits.

Reviewed by Cursor Bugbot for commit 48e7c65. Bugbot is set up for automated code reviews on this repo. Configure here.

…adata

Nothing on the read side compared the cached whole body against
meta.ContentLength, so a truncated entry was served as a full-length hit
until its TTL expired. serveFromCache now counts the bytes the cache body
produced and treats a short or long body as an unusable-body error:

- Small (buffered) path: the check runs before any header is written, so
  the existing callers' bodyGone gate invalidates the entry with the
  ETag/version-guarded delete and falls through to upstream, exactly as
  for a missing body.
- Large (streamed) path: headers commit on the first chunk, so a clean
  stream that ends short is detected after the fact. The response is
  left incomplete like any committed-then-failed stream (declared
  Content-Length unmet, no upstream append), the request is counted as
  an error, and the entry is invalidated in place with the same guarded
  delete.

A mismatch is recorded as tag_cache_operations_total{get,length_mismatch}.
No new persisted keys, no pre-serve probe: a correct entry still costs one
meta read and one body read.

Read-side half of #249; the write-side cause is closed separately (#263).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
@greptile-apps

greptile-apps Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous download-completion issue is fixed.

Summary

Checks whole-object cache bodies against their saved length.

  • Whole-object cache responses check that the body matches its declared length.

Reviews (5) · Last reviewed commit: "fix(cache): hold back the last promised ..." · Reviewed by Greptile

Comment thread proxy/revalidation.go Resolved
ovaistariq and others added 4 commits October 21, 2026 10:24
A streamed cache body longer than its metadata's Content-Length was never
caught: net/http rejected the excess write with http.ErrContentLength, which
serveFromCache took for a transient stream failure, so the corrupt entry
stayed cached and failed every later request until its TTL.

Put a lengthBoundedWriter in front of the lazy-commit writer. It refuses the
chunk that would overrun Content-Length and reports errCacheBodyLengthMismatch
before anything reaches the response writer, so the committed path invalidates
the entry with the ETag-guarded delete exactly as for a short body, and a body
whose first chunk is already too long is rejected before commit and falls
through to upstream. As belt and braces, an http.ErrContentLength that does
slip through on a committed stream is classified as the same mismatch.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
Keep the length-bounded streamed path; take main's RecordRequestResult call
(#251) in place of the status string.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
The merge cleanup removed the status variables from every function in the
file; only serveFromCache switched to RecordRequestResult.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
Comment thread proxy/revalidation.go Resolved
…nds cleanly

Refusing only the chunk that overruns Content-Length is not enough when the
cache stream's chunks align with the declared length: the client has already
received every promised byte by the time the excess arrives, so it sees a
clean, complete response from an entry the proxy detected as corrupt and
deleted.

lengthBoundedWriter now withholds the last promised byte (the one that would
land at offset ContentLength-1) and only releases it after GetBodyStream has
returned cleanly with exactly ContentLength bytes. An over-run, short, or
failed stream never releases it, so the declared Content-Length is never met
and the client sees an aborted body. The small/buffered path is unchanged: it
already checks the whole body before any header is written.

Tests cover the aligned-chunk overrun (client receives Content-Length-1
bytes, entry invalidated, mismatch counted), the aligned exact-length hit
(all bytes delivered, entry kept), and the 1-byte writer contract.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
@ovaistariq
ovaistariq merged commit 34fdfcb into main Oct 22, 2026
9 checks passed
@ovaistariq
ovaistariq deleted the fix-whole-object-length-check branch October 22, 2026 03:36
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 22, 2026
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.22.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants