Skip to content

Commit 6d481e8

Browse files
leodidoona-agent
andcommitted
fix: skip vulnerability scanning for packages that failed to build
When SLSA verification fails for a package and it needs to be built locally, but the local build also fails, vulnerability scanning was attempting to scan the package anyway and failing with 'Package not found in local cache'. This fix: - Passes the package build status map to vulnerability scanning - Only scans packages with status PackageBuilt or PackageDownloaded - Skips packages that failed verification, download, or build - Logs which packages are skipped and why This prevents fatal errors when a package fails to build but vulnerability scanning is enabled. The build will still fail due to the package build failure, but vulnerability scanning won't cause an additional error. Fixes the issue in gitpod-next PR #11869 where api/go:lib SLSA verification failed, local build was attempted, and vulnerability scanning crashed trying to scan a package that wasn't in cache. Co-authored-by: Ona <[email protected]>
1 parent cba003b commit 6d481e8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

pkg/leeway/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,9 @@ func Build(pkg *Package, opts ...BuildOption) (err error) {
748748

749749
// Scan all packages for vulnerabilities after the build completes
750750
// This ensures we scan even cached packages that weren't rebuilt
751+
// Only scan packages that were successfully built or downloaded
751752
if pkg.C.W.SBOM.Enabled && pkg.C.W.SBOM.ScanVulnerabilities {
752-
if err := scanAllPackagesForVulnerabilities(ctx, allpkg); err != nil {
753+
if err := scanAllPackagesForVulnerabilities(ctx, allpkg, pkgstatus); err != nil {
753754
return err
754755
}
755756
}

pkg/leeway/sbom-scan.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ type PackageVulnerabilityStats struct {
4949
// This function is called after the build process completes to identify security issues
5050
// in all built packages, including those loaded from cache. It generates comprehensive
5151
// vulnerability reports in multiple formats and collects statistics across all packages.
52-
func scanAllPackagesForVulnerabilities(buildctx *buildContext, packages []*Package, customOutputDir ...string) error {
52+
// Only packages with successful build status (PackageBuilt or PackageDownloaded) are scanned.
53+
func scanAllPackagesForVulnerabilities(buildctx *buildContext, packages []*Package, pkgstatus map[*Package]PackageBuildStatus, customOutputDir ...string) error {
5354
if len(packages) == 0 {
5455
return nil
5556
}
@@ -74,6 +75,12 @@ func scanAllPackagesForVulnerabilities(buildctx *buildContext, packages []*Packa
7475

7576
// Process each package
7677
for _, p := range packages {
78+
// Skip packages that were not successfully built or downloaded
79+
status := pkgstatus[p]
80+
if status != PackageBuilt && status != PackageDownloaded {
81+
buildctx.Reporter.PackageBuildLog(p, false, []byte(fmt.Sprintf("Skipping vulnerability scan for package %s (status: %s)\n", p.FullName(), status)))
82+
continue
83+
}
7784
if !p.C.W.SBOM.Enabled {
7885
errMsg := fmt.Append(nil, "SBOM feature is disabled, cannot scan for vulnerabilities")
7986
buildctx.Reporter.PackageBuildLog(p, false, errMsg)
@@ -88,9 +95,10 @@ func scanAllPackagesForVulnerabilities(buildctx *buildContext, packages []*Packa
8895

8996
location, exists := buildctx.LocalCache.Location(p)
9097
if !exists {
91-
errMsg := fmt.Appendf(nil, "Package %s not found in local cache, cannot scan for vulnerabilities\n", p.FullName())
92-
buildctx.Reporter.PackageBuildLog(p, false, errMsg)
93-
return xerrors.Errorf(string(errMsg))
98+
// This should not happen since we already filtered by build status
99+
// but handle it gracefully just in case
100+
buildctx.Reporter.PackageBuildLog(p, false, []byte(fmt.Sprintf("Package %s not found in local cache, skipping vulnerability scan\n", p.FullName())))
101+
continue
94102
}
95103

96104
// Create temporary file for SBOM content
@@ -194,6 +202,7 @@ func scanAllPackagesForVulnerabilities(buildctx *buildContext, packages []*Packa
194202
// ScanAllPackagesForVulnerabilities provides a public API for scanning packages for vulnerabilities.
195203
// It creates a build context with the provided local cache and reporter, then calls the internal
196204
// scanAllPackagesForVulnerabilities function to perform the actual scanning.
205+
// This function assumes all provided packages are already built and available in the local cache.
197206
func ScanAllPackagesForVulnerabilities(localCache cache.LocalCache, packages []*Package, customOutputDir ...string) error {
198207
buildctx := &buildContext{
199208
buildOptions: buildOptions{
@@ -202,7 +211,14 @@ func ScanAllPackagesForVulnerabilities(localCache cache.LocalCache, packages []*
202211
},
203212
}
204213

205-
return scanAllPackagesForVulnerabilities(buildctx, packages, customOutputDir...)
214+
// Create a status map marking all packages as built (since this is a public API
215+
// that expects packages to already be in cache)
216+
pkgstatus := make(map[*Package]PackageBuildStatus)
217+
for _, p := range packages {
218+
pkgstatus[p] = PackageBuilt
219+
}
220+
221+
return scanAllPackagesForVulnerabilities(buildctx, packages, pkgstatus, customOutputDir...)
206222
}
207223

208224
// scanSBOMForVulnerabilities scans an SBOM file for vulnerabilities and generates reports.

0 commit comments

Comments
 (0)