diff --git a/pkg/scanner/govulncheck/govulncheck.go b/pkg/scanner/govulncheck/govulncheck.go index d8ab137..e305234 100644 --- a/pkg/scanner/govulncheck/govulncheck.go +++ b/pkg/scanner/govulncheck/govulncheck.go @@ -83,14 +83,21 @@ func (s *Scanner) Scan(ctx context.Context, registry, packageName, version strin if err != nil { return nil, fmt.Errorf("failed to create temp dir: %w", err) } - defer func() { _ = os.RemoveAll(tmpDir) }() + defer func() { + if rmErr := os.RemoveAll(tmpDir); rmErr != nil { + log.Warn().Err(rmErr).Str("scanner", ScannerName).Str("dir", tmpDir).Msg("Cannot remove scan directory") + } + }() // govulncheck never reports advisories against the main module of the tree it scans, so // scanning the module's own source reports a vulnerable module clean. The module is // scanned as a dependency of a wrapper module instead, and the wrapper imports its // packages because module-mode scanning only considers modules that provide an import. // Resolving the module reaches the module proxy. - env := append(os.Environ(), "GOFLAGS=-mod=mod", "GOTOOLCHAIN=local", "GOWORK=off") + // The dependency tree goes to a module cache inside tmpDir, so the scan keeps nothing on disk; + // -modcacherw leaves that cache writable, or RemoveAll cannot delete it. + env := append(os.Environ(), "GOFLAGS=-mod=mod -modcacherw", "GOTOOLCHAIN=local", "GOWORK=off", + "GOMODCACHE="+filepath.Join(tmpDir, "modcache")) goMod := fmt.Sprintf("module gohoarder.audit\n\nrequire %s %s\n", module, version) if err = os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(goMod), 0o600); err != nil { return nil, fmt.Errorf("failed to write go.mod: %w", err) diff --git a/pkg/scanner/govulncheck/govulncheck_test.go b/pkg/scanner/govulncheck/govulncheck_test.go index 982122a..2df90f5 100644 --- a/pkg/scanner/govulncheck/govulncheck_test.go +++ b/pkg/scanner/govulncheck/govulncheck_test.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "github.com/lukaszraczylo/gohoarder/pkg/config" @@ -132,7 +133,7 @@ func TestScanAuditsTheModuleItself(t *testing.T) { env, err := os.ReadFile(filepath.Join(rec, "env")) // #nosec G304 -- test temp dir require.NoError(t, err) - assert.Equal(t, "-mod=mod|local\n", string(env)) + assert.Equal(t, "-mod=mod -modcacherw|local\n", string(env)) assert.Equal(t, []string{"-json", "-scan", "module"}, scannertest.Args(t, args)) @@ -144,6 +145,32 @@ func TestScanAuditsTheModuleItself(t *testing.T) { assert.NotContains(t, imports, "/internal", "internal packages cannot be imported") } +func TestScanRemovesTheModulesItDownloaded(t *testing.T) { + rec := t.TempDir() + // Like go, the fake leaves the module cache read-only unless GOFLAGS has -modcacherw. + scannertest.Script(t, "go", fmt.Sprintf(` +[ -n "$GOMODCACHE" ] || exit 1 +mkdir -p "$GOMODCACHE/example.com/dep@v1.0.0" +touch "$GOMODCACHE/example.com/dep@v1.0.0/dep.go" +case "$GOFLAGS" in *-modcacherw*) ;; *) chmod 0555 "$GOMODCACHE/example.com/dep@v1.0.0" ;; esac +echo "$GOMODCACHE" > %q/go +echo "github.com/google/uuid uuid 12 0 "`, rec)) + scannertest.Script(t, "govulncheck", fmt.Sprintf(` +echo "$GOMODCACHE" > %q/govulncheck +cat %q`, rec, abs(t, "google-uuid-v1.6.0.stream"))) + + res, err := New(config.GovulncheckConfig{}).Scan(context.Background(), "go", "github.com/google/uuid/@v/v1.6.0.zip", "v1.6.0", module(t)) + + require.NoError(t, err) + require.Equal(t, metadata.ScanStatusClean, res.Status) + listCache, err := os.ReadFile(filepath.Join(rec, "go")) // #nosec G304 -- test temp dir + require.NoError(t, err) + scanCache, err := os.ReadFile(filepath.Join(rec, "govulncheck")) // #nosec G304 -- test temp dir + require.NoError(t, err) + assert.Equal(t, string(listCache), string(scanCache), "govulncheck reads the modules go list downloaded") + assert.NoDirExists(t, strings.TrimSpace(string(listCache)), "the scan keeps no downloaded modules") +} + func TestScanCleanModule(t *testing.T) { scannertest.Script(t, "go", `echo "github.com/google/uuid uuid 12 0 "`) _, _ = fakeGovulncheck(t, abs(t, "google-uuid-v1.6.0.stream"), 0, "")