|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
| 4 | + "bufio" |
| 5 | + "errors" |
5 | 6 | "fmt" |
6 | | - "io" |
7 | 7 | "os" |
| 8 | + "slices" |
| 9 | + "strings" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | // Vulnerabilities with no upstream fix — remove entries once fixed. |
11 | 13 | var ignore = map[string]string{ |
12 | 14 | "GO-2026-4518": "pgproto3/v2 DoS, no fix available (EOL). Transitive via pgconn v1 + pop/v6.", |
13 | 15 | } |
14 | 16 |
|
15 | | -type message struct { |
16 | | - Finding *struct { |
17 | | - OSV *struct { |
18 | | - ID string `json:"id"` |
19 | | - } `json:"osv"` |
20 | | - } `json:"finding"` |
| 17 | +func main() { |
| 18 | + if err := run(); err != nil { |
| 19 | + fmt.Fprintf(os.Stderr, "vulncheck-filter: %v\n", err) |
| 20 | + os.Exit(1) |
| 21 | + } |
21 | 22 | } |
22 | 23 |
|
23 | | -func main() { |
24 | | - dec := json.NewDecoder(os.Stdin) |
| 24 | +func run() error { |
| 25 | + const ( |
| 26 | + stInit = iota |
| 27 | + stVulnOpen |
| 28 | + ) |
25 | 29 |
|
26 | | - var unignored []string |
27 | | - seen := make(map[string]bool) |
28 | | - for { |
29 | | - var m message |
30 | | - if err := dec.Decode(&m); err != nil { |
31 | | - if err == io.EOF { |
32 | | - break |
| 30 | + type vuln struct { |
| 31 | + ID string `json:"id"` |
| 32 | + Text string |
| 33 | + } |
| 34 | + |
| 35 | + var ( |
| 36 | + cur vuln |
| 37 | + vulns []*vuln |
| 38 | + ) |
| 39 | + st := stInit |
| 40 | + sc := bufio.NewScanner(os.Stdin) |
| 41 | + for sc.Scan() { |
| 42 | + v := sc.Text() |
| 43 | + switch st { |
| 44 | + case stInit: |
| 45 | + if strings.HasPrefix(v, "Vulnerability ") { |
| 46 | + st = stVulnOpen |
| 47 | + _, id, ok := strings.Cut(v, ": ") |
| 48 | + if !ok { |
| 49 | + return errors.New("no longer able to parse format") |
| 50 | + } |
| 51 | + cur = vuln{ |
| 52 | + ID: id, |
| 53 | + } |
| 54 | + } |
| 55 | + case stVulnOpen: |
| 56 | + cur.Text += v + "\n" |
| 57 | + if v == "" { |
| 58 | + st = stInit |
| 59 | + cpy := cur |
| 60 | + vulns = append(vulns, &cpy) |
33 | 61 | } |
34 | | - // govulncheck JSON stream may contain objects we don't care about; skip decode errors |
35 | | - continue |
36 | | - } |
37 | | - if m.Finding == nil { |
38 | | - continue |
39 | | - } |
40 | | - if m.Finding.OSV == nil { |
41 | | - continue |
42 | | - } |
43 | | - id := m.Finding.OSV.ID |
44 | | - if seen[id] { |
45 | | - continue |
46 | 62 | } |
47 | | - seen[id] = true |
48 | | - |
49 | | - if reason, ok := ignore[id]; ok { |
50 | | - fmt.Fprintf(os.Stderr, "ignoring %s: %s\n", id, reason) |
51 | | - } else { |
52 | | - fmt.Fprintf(os.Stderr, "ERROR: %s (not in ignore list)\n", id) |
53 | | - unignored = append(unignored, id) |
| 63 | + } |
| 64 | + if err := sc.Err(); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + vulns = slices.DeleteFunc(vulns, func(v *vuln) bool { |
| 68 | + reason, ok := ignore[v.ID] |
| 69 | + if ok { |
| 70 | + fmt.Fprintf(os.Stderr, "ignoring %s: %s\n", v.ID, reason) |
54 | 71 | } |
| 72 | + return ok |
| 73 | + }) |
| 74 | + if len(vulns) == 0 { |
| 75 | + return nil |
55 | 76 | } |
56 | 77 |
|
57 | | - if len(unignored) > 0 { |
58 | | - fmt.Fprintf(os.Stderr, "\n%d unignored vulnerability(ies) found\n", len(unignored)) |
59 | | - os.Exit(1) |
| 78 | + fmt.Fprintf(os.Stderr, "\n") |
| 79 | + for idx, vuln := range vulns { |
| 80 | + msg := "Vulnerability #%d: %v\n%v" |
| 81 | + fmt.Fprintf(os.Stderr, msg, idx+1, vuln.ID, vuln.Text) |
60 | 82 | } |
| 83 | + return fmt.Errorf("%d unignored vulnerability(ies) found", len(vulns)) |
61 | 84 | } |
0 commit comments