Skip to content

fix: limit the size of downloaded package archives - #638

Merged
eitsupi merged 8 commits into
mainfrom
fix/archive-download-limit
Dec 4, 2026
Merged

eitsupi merged 8 commits into
mainfrom
fix/archive-download-limit

Conversation

@eitsupi

@eitsupi eitsupi commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

dbc could save an arbitrarily large archive to disk, and dbc lock can pin a downloaded archive without extracting it (#623).

Registry and HTTP archive downloads are now capped at 512 MiB. dbc rejects a Content-Length above the cap before reading the body, enforces the cap as it reads, and removes partial downloads. The largest archive measured in the central registry was about 174 MiB (182,358,568 bytes). mise also caps its self-update downloads at 512 MiB.

The cap also applies to the released Go APIs PkgInfo.DownloadPackage (deprecated) and Client.Install. Transfer errors from PkgInfo.DownloadPackage now start with "failed to download driver", just like request errors. The unreleased Go API Client.OpenAnonymous now returns the declared response length when known. Client.Download remains an uncapped stream.

@eitsupi
eitsupi added this pull request to stack #641 December 2, 2026 13:54
@eitsupi eitsupi changed the title fix/archive download limit fix: limit the size of downloaded package archives Dec 2, 2026
@eitsupi
eitsupi marked this pull request as ready for review December 2, 2026 15:20
@eitsupi
eitsupi requested review from amoeba and zeroshade December 2, 2026 15:20

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Every network path that saves an archive is capped:

  • Registry installs go through PkgInfo.DownloadPackage (cmd/dbc/main.go).
  • Client.Install goes through Client.downloadPackage.
  • HTTP sources go through downloadHTTPPkg.

Each rejects an oversized Content-Length before creating the temp dir, then enforces the cap while copying and removes the temp dir on failure. CopyLimited's one-byte probe handles a body of exactly limit bytes and passes through read errors that aren't EOF. The tests cover under, exact, one over, empty, and errors other than "too large". The 512 MiB figure and how it was chosen are well documented on MaxArchiveBytes. Client.Download staying uncapped is documented. The since_version('v0.4.0') marker is correct: archive hash verification isn't in v0.3.1.

Three minor notes inline, none blocking.

Comment thread drivers.go
@@ -283,6 +284,12 @@ type PkgInfo struct {

// Deprecated: Use Client.Download instead.
func (p PkgInfo) DownloadPackage(prog ProgressFunc) (*os.File, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a released (deprecated) API, and it now refuses archives over 512 MiB. The description only mentions the OpenAnonymous change; please add a line that PkgInfo.DownloadPackage and Client.Install are now capped too. Separately, every error from this function now gets the prefix "failed to download driver:", including local write errors that used to come back unwrapped. That's fine, but it changes the error text.

Comment thread client_methods.go
}

f, err := c.downloadPackage(ctx, pkg)
f, err := c.downloadPackage(ctx, pkg, download.MaxArchiveBytes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Install wraps this as failed to download driver %s: %w, and downloadPackage already starts with failed to download driver:. An oversized archive therefore reads "failed to download driver foo: failed to download driver: package archive exceeds the download limit of 536870912 bytes". Dropping one of the prefixes would avoid the repetition.

Comment thread internal/download/limit.go Outdated
// tooLarge wraps ErrArchiveTooLarge with the limit, so the message tells
// how large an archive may be.
func tooLarge(limit int64) error {
return fmt.Errorf("%w of %d bytes", ErrArchiveTooLarge, limit)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "the download limit of 536870912 bytes" is hard to read. Consider "512 MiB", or the byte count followed by "(512 MiB)".

Registry and HTTP source downloads copied the whole response to a
temporary file with no bound; only extraction was limited. Cap the
saved archive at 512 MiB, rejecting a larger declared Content-Length
before reading and enforcing the limit on the bytes actually read.
A rejected download leaves no temporary file.
OpenAnonymous now also returns the declared length of the response, so
the HTTP source download rejects a Content-Length over the limit before
creating a temporary file, like registry downloads do.
Neither the archive hash check nor the download limit is in v0.3.1.
The private download helpers return the size error as is, so callers that
name the driver no longer repeat "failed to download driver". The limit
is shown in MiB when it is a whole number of them. PkgInfo.DownloadPackage
again returns other copy errors unwrapped, and its doc and Client.Install's
mention the limit.
PkgInfo.DownloadPackage already says that the download failed, so sync
only redacts its registry errors instead of adding the same prefix.
Sync relies on the prefix to name a failed registry download, so a read
or write error during the transfer gets it too.
@zeroshade

Copy link
Copy Markdown
Member

Re-reviewed at c51cdca. The follow-up commits address all three earlier comments, so the approval still stands.

Checks run: go vet . ./internal/download/ ./cmd/dbc/ is clean. With go test -race, these pass:

  • ./internal/download/
  • in the root package: TestDownloadPackageLimit, TestOpenAnonymous*, TestInstall*
  • in cmd/dbc: TestDownloadHTTPPkgLimit, TestDownloadArchiveReportsRegistryErrorsOnce, TestReadHTTPFile*, and the suite's TestSyncKeepsRegistrySecrets* and HTTP sync tests

Earlier comments

Comment Now
The description should say PkgInfo.DownloadPackage and Client.Install are capped Done, and both doc comments say "An archive larger than 512 MiB is refused."
Install repeated "failed to download driver" for oversized archives Client.downloadPackage returns ErrArchiveTooLarge unwrapped, so the prefix appears once.
"536870912 bytes" was hard to read formatSize prints "512 MiB" for whole MiB values, and tests check that text.

The other follow-ups also look good:

  • PkgInfo.DownloadPackage now adds the prefix to every request and transfer error. dbc install passes these errors through, so its messages are more consistent.
  • downloadArchive in sync no longer adds a second prefix.

New notes, none blocking

  1. Sync loses some context. Not every error from downloadPkg starts with the prefix. failed to create temp dir: …, failed to create temp file to download to: …, cannot download package for X: no url set and startupRegistryConfigErr used to read "failed to download driver: …" in sync and now appear without it. This matches the doc comment, which only covers "a failed request or transfer". Wrapping the two temp-file errors in PkgInfo.downloadPackage would bring the context back.
  2. Install still repeats the prefix for other errors. This was already the case before this PR: a request failure reads "failed to download driver foo: failed to download driver: …", and a bad status adds a second "failed to download driver : 404 …". It would be easy to fix in the same place if you want.
  3. A misleading label, also from before this PR. Client.downloadPackage reports network read errors during the copy as "failed to write driver file: …". This is out of scope here.
  4. Tests.
    • cmd/dbc/http_fetch_test.go repeats the archiveServer handler from download_limit_test.go line for line. That's reasonable, since they're in different packages.
    • In TestDownloadPackageLimit, defer os.RemoveAll(tmp) is redundant because t.TempDir() already cleans up.

@eitsupi
eitsupi merged commit d7761e6 into main Dec 4, 2026
32 checks passed
@eitsupi
eitsupi deleted the fix/archive-download-limit branch December 4, 2026 00:53

This branch was previously deployed

1 inactive deployment
snapshot — c51cdcaf Deployed Dec 3, 2026 by eitsupi via snapshot #1468
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.

2 participants