-
Notifications
You must be signed in to change notification settings - Fork 129
feat(suse): add cvrf CVE feed #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manojkrishna-nomula
wants to merge
1
commit into
aquasecurity:main
Choose a base branch
from
manojkrishnanomula:feat/suse-cvrf-cve-feed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Package cvrfarchive walks SUSE CVRF tar archives published at | ||
| // http://ftp.suse.com/pub/projects/security/. It hides the | ||
| // download/decompress/tar plumbing so feed-specific code only needs | ||
| // to handle XML decoding and persistence. | ||
| package cvrfarchive | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "compress/bzip2" | ||
| "compress/gzip" | ||
| "errors" | ||
| "io" | ||
| "log" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| "unicode/utf8" | ||
|
|
||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/aquasecurity/vuln-list-update/utils" | ||
| ) | ||
|
|
||
| // Entry is a single XML document extracted from a SUSE CVRF archive. | ||
| // Data is guaranteed to be valid UTF-8 (invalid bytes are stripped). | ||
| type Entry struct { | ||
| Filename string | ||
| Data []byte | ||
| } | ||
|
|
||
| // Walk downloads a SUSE CVRF archive from url, decompresses it | ||
| // (bzip2 or gzip, detected by the URL suffix) and invokes handler | ||
| // for every .xml entry whose base name matches nameRegexp. | ||
| // Non-regular tar entries, empty files and non-XML files are skipped; | ||
| // invalid UTF-8 byte sequences are stripped from the data. | ||
| func Walk(url string, retries int, nameRegexp *regexp.Regexp, handler func(Entry) error) error { | ||
| // The SUSE server is sometimes unstable, so download the whole archive into | ||
| // memory before processing. Streaming directly from the HTTP response would | ||
| // make it hard to distinguish a mid-transfer disconnection (which surfaces | ||
| // as a truncated tar) from a legitimate parse error. The archive is only a | ||
| // few hundred MB, which fits comfortably in memory on CI runners. | ||
| body, err := utils.FetchURL(url, "", retries) | ||
| if err != nil { | ||
| return xerrors.Errorf("failed to download archive: %w", err) | ||
| } | ||
|
|
||
| decompressed, err := decompress(url, body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| tr := tar.NewReader(decompressed) | ||
| for { | ||
| hdr, err := tr.Next() | ||
| switch { | ||
| case errors.Is(err, io.EOF): | ||
| return nil | ||
| case err != nil: | ||
| return xerrors.Errorf("failed to read tar entry: %w", err) | ||
| case hdr.Typeflag != tar.TypeReg: | ||
| continue | ||
| } | ||
|
|
||
| filename := filepath.Base(hdr.Name) | ||
| if !strings.HasSuffix(filename, ".xml") { | ||
| continue | ||
| } | ||
| if nameRegexp != nil && !nameRegexp.MatchString(filename) { | ||
| continue | ||
| } | ||
|
|
||
| data, err := io.ReadAll(tr) | ||
| if err != nil { | ||
| return xerrors.Errorf("failed to read tar entry data: %w", err) | ||
| } | ||
| if len(data) == 0 { | ||
| log.Printf("empty xml: %s", filename) | ||
| continue | ||
| } | ||
| if !utf8.Valid(data) { | ||
| log.Printf("invalid UTF-8: %s", filename) | ||
| data = []byte(strings.ToValidUTF8(string(data), "")) | ||
| } | ||
|
|
||
| if err := handler(Entry{Filename: filename, Data: data}); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func decompress(url string, body []byte) (io.Reader, error) { | ||
| switch { | ||
| case strings.HasSuffix(url, ".tar.bz2"): | ||
| // The upstream archive is .tar.bz2, which is the only format used in production. | ||
| return bzip2.NewReader(bytes.NewReader(body)), nil | ||
| case strings.HasSuffix(url, ".tar.gz"): | ||
| // Go's compress/bzip2 lacks a Writer, so tests use .tar.gz instead. | ||
| return gzip.NewReader(bytes.NewReader(body)) | ||
| default: | ||
| return nil, xerrors.Errorf("unsupported archive format: %s", url) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package cvrfcve | ||
|
|
||
| import ( | ||
| "encoding/xml" | ||
| "fmt" | ||
| "log" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/afero" | ||
| "golang.org/x/xerrors" | ||
|
|
||
| "github.com/aquasecurity/vuln-list-update/suse/cvrfarchive" | ||
| "github.com/aquasecurity/vuln-list-update/utils" | ||
| ) | ||
|
|
||
| const ( | ||
| cvrfCVEArchiveURL = "http://ftp.suse.com/pub/projects/security/cvrf-cve.tar.bz2" | ||
| cvrfDir = "cvrf" | ||
| suseCVEDir = "suse-cves" | ||
| retries = 5 | ||
| ) | ||
|
|
||
| var fileRegexp = regexp.MustCompile(`^cvrf-(CVE-\d{4}-\d+)\.xml$`) | ||
|
|
||
| type Config struct { | ||
| VulnListDir string | ||
| URL string | ||
| AppFs afero.Fs | ||
| } | ||
|
|
||
| func NewConfig() Config { | ||
| return Config{ | ||
| VulnListDir: utils.VulnListDir(), | ||
| URL: cvrfCVEArchiveURL, | ||
| AppFs: afero.NewOsFs(), | ||
| } | ||
| } | ||
|
|
||
| func (c Config) Update() error { | ||
| log.Print("Fetching SUSE CVE CVRF archive...") | ||
|
|
||
| return cvrfarchive.Walk(c.URL, retries, fileRegexp, func(e cvrfarchive.Entry) error { | ||
| // CVE ID is taken from the file name, already validated by fileRegexp. | ||
| cveID := fileRegexp.FindStringSubmatch(e.Filename)[1] | ||
|
|
||
| var cv Cvrf | ||
| if err := xml.Unmarshal(e.Data, &cv); err != nil { | ||
| return xerrors.Errorf("failed to decode SUSE CVE CVRF XML (%s): %w", e.Filename, err) | ||
| } | ||
|
|
||
| return c.saveCVEPerYear(cveID, cv) | ||
| }) | ||
| } | ||
|
|
||
| func (c Config) saveCVEPerYear(cveID string, data Cvrf) error { | ||
| year := strings.Split(cveID, "-")[1] | ||
| yearDir := filepath.Join(c.VulnListDir, cvrfDir, suseCVEDir, year) | ||
| fileName := fmt.Sprintf("%s.json", cveID) | ||
| if err := utils.WriteJSON(c.AppFs, yearDir, fileName, data); err != nil { | ||
| return xerrors.Errorf("failed to write file: %w", err) | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add suse-cvrf-cve into README.md?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done