Don't load files in tarball exceeding size_limit_bytes

Previously we'd check the size limit *after* the file was read, which
mostly defeats the point of the limit. Now we check the size from the
header in the tar archive and exit early if it exceeds the configured
limit.

In order to do this, I had to extend the `DirectoryLoader` interface
with a method to set the max size. While I added implementations for
the other (than tarball) loader types, the limit is not currently set
anywhere for those. Perhaps we'll want to do that at some later point
but it feels like this is mainly relevant when files are loaded via
remote bundles.

Fixes #6514

Signed-off-by: Anders Eknert <anders@styra.com>
This commit is contained in:
Anders Eknert
2024-01-09 15:55:41 +01:00
committed by Ashutosh Narkar
parent b5eeaaa2e3
commit c0589c1272
6 changed files with 100 additions and 23 deletions
+7
View File
@@ -326,6 +326,12 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
loader = bundle.NewTarballLoaderWithBaseURL(r, baseURL)
}
// Setting the size limit on the loader allows early exit in the case
// of any file exceeding the limit, without the file getting loaded
if d.sizeLimitBytes != nil {
loader = loader.WithSizeLimitBytes(*d.sizeLimitBytes)
}
etag := resp.Header.Get("ETag")
reader := bundle.NewCustomReader(loader).
@@ -335,6 +341,7 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
WithLazyLoadingMode(d.lazyLoadingMode).
WithBundleName(d.bundleName).
WithBundlePersistence(d.persist)
if d.sizeLimitBytes != nil {
reader = reader.WithSizeLimitBytes(*d.sizeLimitBytes)
}