cli/context/store: limit decompressed size of TLS files on zip import - #7105
Open
UditDewan wants to merge 1 commit into
Open
cli/context/store: limit decompressed size of TLS files on zip import#7105UditDewan wants to merge 1 commit into
UditDewan wants to merge 1 commit into
Conversation
The meta.json entry of an imported zip archive was read through limitedReader, but TLS entries were read with a bare io.ReadAll. Only the compressed archive size was capped, so a crafted zip entry could decompress to many times the allowed import size and exhaust memory. Apply the same size cap to TLS entries, and make limitedReader return its limit error instead of a clean EOF when a read lands exactly on the limit while more data remains, so oversized content fails instead of being silently truncated. Fixes docker#6917 Signed-off-by: uditDewan <udit.dewan21@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This PR hardens docker context import (zip) against decompression-based memory exhaustion by enforcing size limits on decompressed TLS material and tightening limitedReader behavior.
Changes:
- Apply
limitedReaderwhen readingtls/zip entries to cap decompressed TLS file size. - Adjust
limitedReader.Readsemantics to avoid silently truncating when reads land exactly on the limit. - Add regression tests covering oversized TLS zip entries and a boundary read case for
limitedReader.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
cli/context/store/store.go |
Caps decompressed reads for tls/ zip entries (and highlights loop-close/memory considerations). |
cli/context/store/store_test.go |
Adds a regression test for oversized decompressed TLS zip entries. |
cli/context/store/io_utils.go |
Updates limitedReader.Read to avoid truncation-at-limit behavior. |
cli/context/store/io_utils_test.go |
Extends tests to cover an exact-limit boundary case using iotest.OneByteReader. |
Comments suppressed due to low confidence (2)
cli/context/store/store.go:485
defer f.Close()inside the zip entry loop can accumulate many deferred closes when importing archives with lots of files, potentially exhausting file descriptors. Close the entry reader immediately afterio.ReadAllinstead of deferring.
data, err := io.ReadAll(&limitedReader{R: f, N: maxAllowedFileSizeToImport})
defer f.Close()
if err != nil {
return err
}
cli/context/store/store.go:487
- This change caps each TLS entry’s decompressed size, but
importZipstill accumulates all TLS file contents in memory (tlsData.Endpoints[...].Files[...] = data) with no overall decompressed-size budget. A crafted zip with many highly-compressible TLS entries could still OOM the process even though each individual entry stays under the per-file limit.
data, err := io.ReadAll(&limitedReader{R: f, N: maxAllowedFileSizeToImport})
defer f.Close()
if err != nil {
return err
}
err = importEndpointTLS(&tlsData, zf.Name, data)
if err != nil {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+263
to
+266
| tf, err := w.Create(path.Join("tls", "docker", "ca.pem")) | ||
| assert.NilError(t, err) | ||
| _, err = tf.Write(make([]byte, 2*maxAllowedFileSizeToImport)) | ||
| assert.NilError(t, err) |
Comment on lines
16
to
20
| if l.N < 0 { | ||
| return 0, errors.New("read exceeds the defined limit") | ||
| } | ||
| if l.N == 0 { | ||
| return 0, io.EOF | ||
| } | ||
| // have to cap N + 1 otherwise we won't hit limit err | ||
| if int64(len(p)) > l.N+1 { |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
- What I did
Fixed a memory exhaustion issue in
docker context importwith zip archives (fixes #6917).The
meta.jsonentry of an imported zip archive is read throughlimitedReader(10MB cap), buttls/entries were read with a bareio.ReadAll. Only the compressed archive size was capped, so a small crafted zip containing a highly compressible TLS entry could decompress to gigabytes and OOM the CLI.- How I did it
importZipthrough the samelimitedReaderalready used formeta.json.l.N == 0early-EOF branch fromlimitedReader.Read. Without this, content exceeding the limit was silently truncated (instead of rejected) whenever a read landed exactly on the remaining budget — which is deterministic for zip imports, since flate delivers 32KiB chunks that divide the 10MB cap evenly. True end-of-data still returns a cleanio.EOFvia the underlying reader.- How to verify it
New regression tests:
TestImportZipTLSDataTooLarge— imports a zip whose TLS entry inflates past the cap while the archive itself stays well under it, and asserts the import errors.TestLimitReaderReadAllcase usingiotest.OneByteReaderthat lands a read exactly on the limit with data remaining, and asserts the limit error is returned.- Human readable description for the release notes