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
+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, "")