diff --git a/common/debug.go b/common/debug.go index d0584b0..9ac59d9 100644 --- a/common/debug.go +++ b/common/debug.go @@ -1,7 +1,6 @@ package common import ( - "fmt" "strings" "time" @@ -11,7 +10,7 @@ import ( func firstN(s string, n int) string { if len(s) > n { - return fmt.Sprintf("%s...", s[:n]) + return s[:n] + "..." } return s } diff --git a/share/set.go b/share/set.go index dc23b50..d526e18 100644 --- a/share/set.go +++ b/share/set.go @@ -314,7 +314,7 @@ func (set *threadUnsafeSet) String() string { for elem := range *set { items = append(items, fmt.Sprintf("%v", elem)) } - return fmt.Sprintf("{%s}", strings.Join(items, ", ")) + return "{" + strings.Join(items, ", ") + "}" } func (pair orderedPair) String() string { diff --git a/share/tar.go b/share/tar.go index ad3ea8d..aaaee0f 100644 --- a/share/tar.go +++ b/share/tar.go @@ -25,7 +25,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -119,7 +118,7 @@ func SelectivelyExtractArchive(r io.Reader, selected func(string) bool, maxFileS log.WithFields(log.Fields{"size": size, "filename": filename}).Error("file too big") return nil } - d, _ := ioutil.ReadAll(reader) + d, _ := io.ReadAll(reader) data[filename] = d return nil } @@ -142,7 +141,7 @@ func SelectivelyExtractToFiles(r io.Reader, dir string, selected func(string) bo log.WithFields(log.Fields{"size": size, "filename": filename}).Error("file too big") return nil } - tmpfile, err := ioutil.TempFile(dir, "extract") + tmpfile, err := os.CreateTemp(dir, "extract") if err != nil { log.WithFields(log.Fields{"err": err, "filename": filename}).Error("write to temp file fail") return nil @@ -203,13 +202,13 @@ func ExtractAllArchiveToFiles(path string, r io.Reader, maxFileSize int64, encry // Extract the element if hdr.Typeflag == tar.TypeReg { - data, _ := ioutil.ReadAll(tr) + data, _ := io.ReadAll(tr) if encryptKey != nil { data, _ = Encrypt(encryptKey, data) } - err = ioutil.WriteFile(path+filename, data, 0400) + err = os.WriteFile(path+filename, data, 0400) if err != nil { return err } @@ -330,7 +329,7 @@ func getTarReader(r io.Reader) (*TarReadCloser, error) { } return &TarReadCloser{tar.NewReader(gr), gr}, nil case bytes.HasPrefix(header, bzip2Header): - bzip2r := ioutil.NopCloser(bzip2.NewReader(br)) + bzip2r := io.NopCloser(bzip2.NewReader(br)) return &TarReadCloser{tar.NewReader(bzip2r), bzip2r}, nil case bytes.HasPrefix(header, xzHeader): xzr, err := NewXzReader(br) @@ -341,7 +340,7 @@ func getTarReader(r io.Reader) (*TarReadCloser, error) { } } - dr := ioutil.NopCloser(br) + dr := io.NopCloser(br) return &TarReadCloser{tar.NewReader(dr), dr}, nil } @@ -401,7 +400,7 @@ func SelectivelyExtractModules(r io.Reader, lastfix string, maxFileSize int64) ( // Extract the element if hdr.Typeflag == tar.TypeReg { - d, _ := ioutil.ReadAll(tr) + d, _ := io.ReadAll(tr) data[filename] = d } } @@ -409,12 +408,12 @@ func SelectivelyExtractModules(r io.Reader, lastfix string, maxFileSize int64) ( return data, nil } -//func SelectivelyExtractToFile(r io.Reader, prefix string, toExtract []string, dir string) (map[string]string, error) { +// func SelectivelyExtractToFile(r io.Reader, prefix string, toExtract []string, dir string) (map[string]string, error) { func SelectivelyExtractToFile(r io.Reader, selected func(string) bool, dir string) (map[string]string, error) { files := make(map[string]string) extract := func(filename string, size int64, reader io.ReadCloser) error { - fpath := dir + "/" + strings.Replace(filename, "/", "_", -1) + fpath := dir + "/" + strings.ReplaceAll(filename, "/", "_") f, err := os.Create(fpath) if err != nil { return ErrCouldNotWriteToDisk diff --git a/share/utils.go b/share/utils.go index 436453a..e29b67f 100644 --- a/share/utils.go +++ b/share/utils.go @@ -8,7 +8,6 @@ import ( "crypto/rand" "fmt" "io" - "io/ioutil" "os/exec" "runtime" "sort" @@ -48,7 +47,7 @@ func GunzipBytes(buf []byte) []byte { return nil } defer r.Close() - uzb, _ := ioutil.ReadAll(r) + uzb, _ := io.ReadAll(r) return uzb } diff --git a/updater/fetchers/alpine/alpine.go b/updater/fetchers/alpine/alpine.go index b745012..70bf8a7 100644 --- a/updater/fetchers/alpine/alpine.go +++ b/updater/fetchers/alpine/alpine.go @@ -2,8 +2,7 @@ package alpine import ( "encoding/json" - "fmt" - "io/ioutil" + "io" "net/http" "os" "regexp" @@ -22,11 +21,13 @@ const ( updaterFlag = "alpine-secdbUpdater" cveURLPrefix = "https://cve.mitre.org/cgi-bin/cvename.cgi?name=" ) + const ( aportsGitURL = "git://git.alpinelinux.org/aports" ) var cveRegex = regexp.MustCompile(`^CVE-\d{4}-\d{4,}$`) +var nsRegexp = regexp.MustCompile(`(.*)/.*-`) type AlpineFetcher struct { repositoryLocalPath string @@ -134,10 +135,14 @@ func (u *AlpineFetcher) downloadSecDB(url string) ([]common.Vulnerability, error log.WithError(err).WithFields(log.Fields{"url": url}).Error("Failed to download alpine db") return nil, err } - - body, _ := ioutil.ReadAll(r.Body) defer r.Body.Close() + body, err := io.ReadAll(r.Body) + if err != nil { + log.WithError(err).WithFields(log.Fields{"url": url}).Error("Failed to read alpine db response") + return nil, err + } + return parseSecDB(body, url) } @@ -147,17 +152,20 @@ func (u *AlpineFetcher) downloadSecDBNamespaces() ([]string, error) { log.WithError(err).WithFields(log.Fields{"url": secdbURL}).Error("Failed to download alpine db") return nil, err } - defer r.Body.Close() - body, _ := ioutil.ReadAll(r.Body) - nss := make([]string, 0) + body, err := io.ReadAll(r.Body) + if err != nil { + log.WithError(err).WithFields(log.Fields{"url": secdbURL}).Error("Failed to read alpine db response") + return nil, err + } - // locate folder from the list - var nsRegexp = regexp.MustCompile(`(.*)/.*-`) - matches := nsRegexp.FindAllStringSubmatch(string(body[:]), -1) + nss := make([]string, 0) + matches := nsRegexp.FindAllSubmatch(body, -1) for _, m := range matches { - nss = append(nss, m[1]) + if len(m) == 2 && len(m[1]) > 0 { + nss = append(nss, string(m[1])) + } } return nss, nil @@ -171,10 +179,10 @@ func (u *AlpineFetcher) FetchUpdate() (resp updater.FetcherResponse, err error) for _, ns := range nss { log.WithFields(log.Fields{"namespace": ns}).Debug() - if vulns, err := u.downloadSecDB(fmt.Sprintf("%s/%s/main.json", secdbURL, ns)); err == nil { + if vulns, err := u.downloadSecDB(secdbURL + "/" + ns + "/main.json"); err == nil { resp.Vulnerabilities = append(resp.Vulnerabilities, vulns...) } - if vulns, err := u.downloadSecDB(fmt.Sprintf("%s/%s/community.json", secdbURL, ns)); err == nil { + if vulns, err := u.downloadSecDB(secdbURL + "/" + ns + "/community.json"); err == nil { resp.Vulnerabilities = append(resp.Vulnerabilities, vulns...) } } @@ -182,7 +190,7 @@ func (u *AlpineFetcher) FetchUpdate() (resp updater.FetcherResponse, err error) vulsMap := make(map[string]common.Vulnerability) for _, vul := range resp.Vulnerabilities { - key := fmt.Sprintf("%s:%s", vul.FixedIn[0].Feature.Namespace, vul.Name) + key := vul.FixedIn[0].Feature.Namespace + ":" + vul.Name vulsMap[key] = vul } @@ -190,7 +198,7 @@ func (u *AlpineFetcher) FetchUpdate() (resp updater.FetcherResponse, err error) /* if vuls, err := u.fromAports(); err == nil { for _, vul := range vuls { - key := fmt.Sprintf("%s:%s", vul.FixedIn[0].Feature.Namespace, vul.Name) + key := vul.FixedIn[0].Feature.Namespace + ":" + vul.Name if _, ok := vulsMap[key]; !ok { correctVulRecord(&vul) resp.Vulnerabilities = append(resp.Vulnerabilities, vul) diff --git a/updater/fetchers/alpine/alpine_test.go b/updater/fetchers/alpine/alpine_test.go index 497acc6..0a2c5d2 100644 --- a/updater/fetchers/alpine/alpine_test.go +++ b/updater/fetchers/alpine/alpine_test.go @@ -1,10 +1,8 @@ package alpine import ( + "os" "testing" - - "io/ioutil" - "regexp" ) func TestParseSecDbIndex(t *testing.T) { @@ -27,7 +25,6 @@ func TestParseSecDbIndex(t *testing.T) {