Repository navigation
fix(cache): refuse whole-object hits whose body length mismatches metadata - #269
Merged
Merged
Conversation
…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
[Medium impact] The PR appears safe to merge; the previous download-completion issue is fixed. SummaryChecks whole-object cache bodies against their saved length.
Reviews (5) · Last reviewed commit: "fix(cache): hold back the last promised ..." · Reviewed by Greptile |
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
Keep both the length-bound note and #270's flat stream read deadline. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01NGTev5ePU5jHubGtdoUfK6
…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
|
🎉 This PR is included in version 1.22.3 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Nothing on the read side verified cached whole-body bytes against
meta.ContentLength, so a truncated cache entry was served with the fullContent-Lengthuntil TTL. The write-side cause is closed separately (#263); this is the read-side guard.serveFromCachenow counts the bytes delivered from the cache body stream:Content-Lengthunmet 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 fullContent-Length.Buffered (small) objects:
serveFromCachecompares the buffered body to metadata before writing headers; mismatch returnserrCacheBodyLengthMismatch, whichbodyGonetreats as unusable so callers invalidate (ETag-guarded) and fall through to upstream.Streamed (large) objects: A new
lengthBoundedWriterrefuses 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 ofContent-Length, recordget/length_mismatchmetrics, and invalidate the entry inserveFromCache(includinghttp.ErrContentLengthas 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.