|
| 1 | +package cvrfcve |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/xml" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "path/filepath" |
| 10 | + "regexp" |
| 11 | + "strings" |
| 12 | + "unicode/utf8" |
| 13 | + |
| 14 | + "github.com/cheggaaa/pb" |
| 15 | + "github.com/spf13/afero" |
| 16 | + "golang.org/x/xerrors" |
| 17 | + |
| 18 | + "github.com/aquasecurity/vuln-list-update/utils" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + cvrfCVEURL = "http://ftp.suse.com/pub/projects/security/cvrf-cve/" |
| 23 | + fileRegexp = regexp.MustCompile(`<a href="(cvrf-(CVE-\d{4}-\d+)\.xml)">.*`) |
| 24 | + retry = 5 |
| 25 | + concurrency = 20 |
| 26 | + wait = 1 |
| 27 | + cvrfDir = "cvrf" |
| 28 | + suseCVEDir = "suse-cves" |
| 29 | +) |
| 30 | + |
| 31 | +type Config struct { |
| 32 | + VulnListDir string |
| 33 | + URL string |
| 34 | + AppFs afero.Fs |
| 35 | + Retry int |
| 36 | +} |
| 37 | + |
| 38 | +func NewConfig() Config { |
| 39 | + return Config{ |
| 40 | + VulnListDir: utils.VulnListDir(), |
| 41 | + URL: cvrfCVEURL, |
| 42 | + AppFs: afero.NewOsFs(), |
| 43 | + Retry: retry, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (c Config) Update() error { |
| 48 | + log.Print("Fetching SUSE CVE CVRF data...") |
| 49 | + |
| 50 | + res, err := utils.FetchURL(c.URL, "", c.Retry) |
| 51 | + if err != nil { |
| 52 | + return xerrors.Errorf("cannot download SUSE CVE CVRF list: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + cveURLs := make(map[string]string) |
| 56 | + scanner := bufio.NewScanner(bytes.NewReader(res)) |
| 57 | + for scanner.Scan() { |
| 58 | + line := scanner.Text() |
| 59 | + if match := fileRegexp.FindStringSubmatch(line); len(match) != 0 { |
| 60 | + cveURLs[match[2]] = c.URL + match[1] |
| 61 | + } |
| 62 | + } |
| 63 | + if err := scanner.Err(); err != nil { |
| 64 | + return xerrors.Errorf("failed reading SUSE CVE CVRF list: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + urls := make([]string, 0, len(cveURLs)) |
| 68 | + for _, u := range cveURLs { |
| 69 | + urls = append(urls, u) |
| 70 | + } |
| 71 | + |
| 72 | + cvrfXMLs, err := utils.FetchConcurrently(urls, concurrency, wait, c.Retry) |
| 73 | + if err != nil { |
| 74 | + log.Printf("failed to fetch CVE CVRF data from SUSE. err: %s", err) |
| 75 | + } |
| 76 | + |
| 77 | + log.Printf("Saving SUSE CVE CVRF data...") |
| 78 | + bar := pb.StartNew(len(cvrfXMLs)) |
| 79 | + for _, cvrfXML := range cvrfXMLs { |
| 80 | + var cv Cvrf |
| 81 | + if len(cvrfXML) == 0 { |
| 82 | + log.Println("empty CVE CVRF xml") |
| 83 | + bar.Increment() |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + if !utf8.Valid(cvrfXML) { |
| 88 | + log.Println("invalid UTF-8") |
| 89 | + cvrfXML = []byte(strings.ToValidUTF8(string(cvrfXML), "")) |
| 90 | + } |
| 91 | + |
| 92 | + if err = xml.Unmarshal(cvrfXML, &cv); err != nil { |
| 93 | + return xerrors.Errorf("failed to decode SUSE CVE CVRF XML: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + cveID := extractCVEID(cv) |
| 97 | + if cveID == "" { |
| 98 | + log.Printf("invalid CVE CVRF document ID: %s", cv.Tracking.ID) |
| 99 | + bar.Increment() |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + if err = c.saveCVEPerYear(cveID, cv); err != nil { |
| 104 | + return xerrors.Errorf("failed to save SUSE CVE CVRF: %w", err) |
| 105 | + } |
| 106 | + bar.Increment() |
| 107 | + } |
| 108 | + bar.Finish() |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func extractCVEID(cv Cvrf) string { |
| 113 | + for _, v := range cv.Vulnerabilities { |
| 114 | + cve := strings.TrimSpace(v.CVE) |
| 115 | + if cve != "" { |
| 116 | + return cve |
| 117 | + } |
| 118 | + } |
| 119 | + return strings.TrimSpace(cv.Title) |
| 120 | +} |
| 121 | + |
| 122 | +func (c Config) saveCVEPerYear(cveID string, data interface{}) error { |
| 123 | + s := strings.Split(cveID, "-") |
| 124 | + if len(s) < 3 { |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + yearDir := filepath.Join(c.VulnListDir, cvrfDir, suseCVEDir, s[1]) |
| 129 | + fileName := fmt.Sprintf("%s.json", cveID) |
| 130 | + if err := utils.WriteJSON(c.AppFs, yearDir, fileName, data); err != nil { |
| 131 | + return xerrors.Errorf("failed to write file: %w", err) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
0 commit comments