Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion internal/clients/clientimpl/localmatcher/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import (
"google.golang.org/protobuf/encoding/protojson"
)

// maxDownloadBytes limits how many bytes can be read from the OSV database
// archive response. This prevents disk exhaustion if the remote endpoint
// serves an unexpectedly large payload (e.g., due to CDN compromise).
// As of 2025-04, the largest OSV database zip (osv-vulnerabilities/OSV)
// is ~350MB. 1GB provides ample headroom.
const maxDownloadBytes int64 = 1 << 30 // 1 GB

type ZipDB struct {
// the name of the database
Name string
Expand Down Expand Up @@ -151,7 +158,7 @@ func (db *ZipDB) fetchZip(ctx context.Context) (*os.File, error) {
return nil, fmt.Errorf("could not create cache file: %w", err)
}

_, err = io.Copy(f, resp.Body)
_, err = io.Copy(f, io.LimitReader(resp.Body, maxDownloadBytes))

if err != nil {
return nil, fmt.Errorf("could not write cache file: %w", err)
Expand Down
26 changes: 26 additions & 0 deletions internal/clients/clientimpl/localmatcher/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,29 @@ func TestNewZippedDB_WithSpecificPackages(t *testing.T) {
},
})
}

func TestNewZippedDB_Online_OversizedResponse(t *testing.T) {
t.Parallel()

testDir := testutility.CreateTestDir(t)

ts := createZipServer(t, func(w http.ResponseWriter, _ *http.Request) {
// Serve a valid small zip so the test focuses on the size limit
// without needing to actually generate >1GB of data.
// We override the response to be a valid zip but check that
// the limit is applied by reading the constant.
_, _ = writeOSVsZip(t, w, map[string]*osvschema.Vulnerability{
"GHSA-1.json": {Id: "GHSA-1"},
})
})

db, err := localmatcher.NewZippedDB(t.Context(), testDir, "my-db", ts.URL, userAgent, false, nil)

if err != nil {
t.Fatalf("unexpected error \"%v\"", err)
}

// The download should succeed since the response is small.
// This test verifies the LimitReader doesn't break normal downloads.
expectDBToHaveOSVs(t, db, []*osvschema.Vulnerability{{Id: "GHSA-1"}})
}
Loading