Repository navigation
fix: limit the size of downloaded package archives - #638
Conversation
zeroshade
left a comment
There was a problem hiding this comment.
Looks good. Every network path that saves an archive is capped:
- Registry installs go through
PkgInfo.DownloadPackage(cmd/dbc/main.go). Client.Installgoes throughClient.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.
| @@ -283,6 +284,12 @@ type PkgInfo struct { | |||
|
|
|||
| // Deprecated: Use Client.Download instead. | |||
| func (p PkgInfo) DownloadPackage(prog ProgressFunc) (*os.File, error) { | |||
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| f, err := c.downloadPackage(ctx, pkg) | ||
| f, err := c.downloadPackage(ctx, pkg, download.MaxArchiveBytes) |
There was a problem hiding this comment.
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.
| // 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) |
There was a problem hiding this comment.
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.
d9aae0c to
c51cdca
Compare
|
Re-reviewed at c51cdca. The follow-up commits address all three earlier comments, so the approval still stands. Checks run:
Earlier comments
The other follow-ups also look good:
New notes, none blocking
|
dbc could save an arbitrarily large archive to disk, and
dbc lockcan 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) andClient.Install. Transfer errors fromPkgInfo.DownloadPackagenow start with "failed to download driver", just like request errors. The unreleased Go APIClient.OpenAnonymousnow returns the declared response length when known.Client.Downloadremains an uncapped stream.