fix(scanner): delete the modules govulncheck downloads after each scan

Each govulncheck scan resolves the full dependency tree of the module.
The scanner kept these modules in the shared Go module cache. On the
deployed host that cache grew to 13G, which is nine times the size of
the packages the proxy serves.

The scan now sets GOMODCACHE to a directory inside its temp dir. The
directory is deleted when the scan ends. GOFLAGS adds -modcacherw,
because Go makes the module cache read-only and RemoveAll cannot
delete it otherwise. A failed delete is logged.
This commit is contained in:
2026-11-27 13:51:31 +00:00
parent 2de0bf029d
commit 7fcbbcab0b
2 changed files with 37 additions and 3 deletions
+9 -2
View File
@@ -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)
+28 -1
View File
@@ -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/[email protected]"
touch "$GOMODCACHE/example.com/[email protected]/dep.go"
case "$GOFLAGS" in *-modcacherw*) ;; *) chmod 0555 "$GOMODCACHE/example.com/[email protected]" ;; 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, "")