|
| 1 | +// package main implements vulnupdatelist script. |
| 2 | +// |
| 3 | +// Run this script to list all the vulnerable Go modules to upgrade. |
| 4 | +// Compared to govulncheck binary, it also checks severity and groups the results |
| 5 | +// into clear table per module to upgrade. |
| 6 | +// |
| 7 | +// Example use: |
| 8 | +// |
| 9 | +// go run ./... \ |
| 10 | +// -go-version=1.23.0 \ |
| 11 | +// -only-fixed \ |
| 12 | +// -dir=../../../prometheus \ |
| 13 | +// -nvd-api-key="$(cat ./api.text)" | tee vuln.txt |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "flag" |
| 19 | + "fmt" |
| 20 | + "log" |
| 21 | + "log/slog" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + |
| 26 | + "github.com/Masterminds/semver/v3" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + goVersion = flag.String("go-version", "", "Go version to test vulnerabilities in (stdlib). Otherwise the `go env GOVERSION` is used") |
| 31 | + dir = flag.String("dir", ".", "Where to run the script from") |
| 32 | + nvdAPIKey = flag.String("nvd-api-key", "", "API Key for avoiding rate-limiting on severity checks; see https://nvd.nist.gov/developers/request-an-api-key") |
| 33 | + onlyFixed = flag.Bool("only-fixed", false, "Don't print vulnerable modules without fixed version; note: fixed version often means sometimes that a new major version contains a fix.") |
| 34 | +) |
| 35 | + |
| 36 | +// UpdateList presents the minimum version to upgrade to solve all CVEs with |
| 37 | +// a fixed version. The CVE refers to the important CVE. |
| 38 | +// For example critical CVE 1 is fixed in v0.5.1 and low is fixed in v0.10.1. |
| 39 | +// TODO(bwplotka): Logically, there might be cases where low contains heavy breaking changes that we can't fix easily; add option to print those too. |
| 40 | +type UpdateList struct { |
| 41 | + CVE CVE // If CVE has +<number> suffix, it means the top CVE. |
| 42 | + AdditionalCVEs int // Lower priority CVEs included in the "fixed" version. |
| 43 | + Module string |
| 44 | + FixedVersion *semver.Version |
| 45 | + Version string |
| 46 | +} |
| 47 | + |
| 48 | +func (u UpdateList) String() string { |
| 49 | + fixedVersion := "???" |
| 50 | + if u.FixedVersion != nil { |
| 51 | + fixedVersion = u.FixedVersion.String() |
| 52 | + } |
| 53 | + if u.AdditionalCVEs > 0 { |
| 54 | + return fmt.Sprintf("%s %s@%s %s(+%d more) now@%s", u.CVE.Severity, u.Module, fixedVersion, u.CVE.ID, u.AdditionalCVEs, u.Version) |
| 55 | + } |
| 56 | + return fmt.Sprintf("%s %s@%s %s now@%s", u.CVE.Severity, u.Module, fixedVersion, u.CVE.ID, u.Version) |
| 57 | +} |
| 58 | + |
| 59 | +func main() { |
| 60 | + flag.Parse() |
| 61 | + |
| 62 | + workDir, err := filepath.Abs(*dir) |
| 63 | + if err != nil { |
| 64 | + log.Fatalf("Failed to resolve work dir: %v", err) |
| 65 | + } |
| 66 | + slog.Info("Running vulnupdatelist", "dir", workDir) |
| 67 | + |
| 68 | + if err := ensureGovulncheck(workDir); err != nil { |
| 69 | + log.Fatalf("Failed to ensure govulncheck is installed: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + slog.Info("Running govulncheck... ") |
| 73 | + vulnJSON, err := runGovulncheck(workDir, *goVersion) |
| 74 | + if err != nil { |
| 75 | + log.Fatalf("Error running govulncheck: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + if len(vulnJSON) == 0 { |
| 79 | + slog.Info("govulncheck produced no output; no vulnerabilities found.") |
| 80 | + os.Exit(0) |
| 81 | + } |
| 82 | + |
| 83 | + slog.Info("Parsing vulnerabilities and finding updates...") |
| 84 | + updates, err := compileUpdateList(bytes.NewReader(vulnJSON), *onlyFixed) |
| 85 | + if err != nil { |
| 86 | + log.Fatalf("Error parsing govulncheck output: %v", err) |
| 87 | + } |
| 88 | + if len(updates) == 0 { |
| 89 | + slog.Info("No actionable vulnerabilities with fixed versions found.") |
| 90 | + os.Exit(0) |
| 91 | + } |
| 92 | + for _, up := range updates { |
| 93 | + fmt.Println(up.String()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// ensureGovulncheck checks if govulncheck is in the PATH, and installs it if not. |
| 98 | +func ensureGovulncheck(dir string) error { |
| 99 | + _, err := exec.LookPath("govulncheck") |
| 100 | + if err == nil { |
| 101 | + slog.Info("govulncheck is already installed") |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + slog.Info("govulncheck not found. Installing...") |
| 106 | + cmd := exec.Command("go", "install", "golang.org/x/vuln/cmd/govulncheck@latest") |
| 107 | + cmd.Dir = dir |
| 108 | + cmd.Stdout = os.Stdout |
| 109 | + cmd.Stderr = os.Stderr |
| 110 | + if err := cmd.Run(); err != nil { |
| 111 | + return fmt.Errorf("failed to run 'go install': %w", err) |
| 112 | + } |
| 113 | + slog.Info("govulncheck installed successfully.") |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +// runGovulncheck executes `govulncheck -json ./...` and returns the output. |
| 118 | +func runGovulncheck(dir string, goVersion string) ([]byte, error) { |
| 119 | + cmd := exec.Command("govulncheck", "--format=json", "./...") |
| 120 | + if goVersion != "" { |
| 121 | + cmd.Env = append(os.Environ(), "GOVERSION="+goVersion) |
| 122 | + } |
| 123 | + |
| 124 | + cmd.Dir = dir |
| 125 | + var out bytes.Buffer |
| 126 | + var stderr bytes.Buffer |
| 127 | + cmd.Stdout = &out |
| 128 | + cmd.Stderr = &stderr |
| 129 | + |
| 130 | + // govulncheck exits with a non-zero status code if vulns are found. |
| 131 | + // We ignore the exit code and check stderr instead. If stderr is empty, |
| 132 | + // it's a successful run (even with vulnerabilities). |
| 133 | + _ = cmd.Run() |
| 134 | + |
| 135 | + if stderr.Len() > 0 { |
| 136 | + // Only return an error if stderr is not empty, as this indicates a real execution problem. |
| 137 | + return nil, fmt.Errorf("govulncheck execution error: %s", stderr.String()) |
| 138 | + } |
| 139 | + return out.Bytes(), nil |
| 140 | +} |
0 commit comments