-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
507fac9
commit 0c11078
Showing
8 changed files
with
186 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,111 @@ | ||
package cargo | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/BurntSushi/toml" | ||
|
||
"github.com/etcd-io/bbolt" | ||
|
||
"github.com/knqyf263/trivy/pkg/db" | ||
"github.com/knqyf263/trivy/pkg/vulnsrc/vulnerability" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/knqyf263/trivy/pkg/git" | ||
"github.com/knqyf263/trivy/pkg/utils" | ||
) | ||
|
||
const ( | ||
dbURL = "https://github.com/RustSec/advisory-db.git" | ||
) | ||
|
||
var ( | ||
repoPath = filepath.Join(utils.CacheDir(), "rust-advisory-db") | ||
) | ||
|
||
type AdvisoryDB map[string][]Lockfile | ||
|
||
type Lockfile struct { | ||
Advisory `toml:"advisory"` | ||
} | ||
|
||
type Advisory struct { | ||
Id string | ||
Package string | ||
Title string `toml:"title"` | ||
Url string | ||
Date string | ||
Description string | ||
Keywords []string | ||
PatchedVersions []string `toml:"patched_versions"` | ||
AffectedFunctions []string `toml:"affected_functions"` | ||
} | ||
|
||
func (s *Scanner) UpdateDB() (err error) { | ||
if _, err := git.CloneOrPull(dbURL, repoPath); err != nil { | ||
return xerrors.Errorf("error in %s security DB update: %w", s.Type(), err) | ||
} | ||
s.db, err = s.walk() | ||
return err | ||
} | ||
|
||
func (s *Scanner) walk() (AdvisoryDB, error) { | ||
advisoryDB := AdvisoryDB{} | ||
root := filepath.Join(repoPath, "crates") | ||
|
||
var vulns []vulnerability.Vulnerability | ||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
buf, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return xerrors.Errorf("failed to read a file: %w", err) | ||
} | ||
|
||
advisory := Lockfile{} | ||
err = toml.Unmarshal(buf, &advisory) | ||
if err != nil { | ||
return xerrors.Errorf("failed to unmarshal TOML: %w", err) | ||
} | ||
|
||
// for detecting vulnerabilities | ||
advisories, ok := advisoryDB[advisory.Package] | ||
if !ok { | ||
advisories = []Lockfile{} | ||
} | ||
advisoryDB[advisory.Package] = append(advisories, advisory) | ||
|
||
// for displaying vulnerability detail | ||
vulns = append(vulns, vulnerability.Vulnerability{ | ||
ID: advisory.Id, | ||
References: []string{advisory.Url}, | ||
Title: advisory.Title, | ||
Description: advisory.Description, | ||
}) | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("error in file walk: %w", err) | ||
} | ||
|
||
if err = s.saveVulnerabilities(vulns); err != nil { | ||
return nil, err | ||
} | ||
return advisoryDB, nil | ||
} | ||
|
||
func (s *Scanner) saveVulnerabilities(vulns []vulnerability.Vulnerability) error { | ||
return vulnerability.BatchUpdate(func(b *bbolt.Bucket) error { | ||
for _, vuln := range vulns { | ||
if err := db.Put(b, vuln.ID, vulnerability.RustSec, vuln); err != nil { | ||
return xerrors.Errorf("failed to save %s vulnerability: %w", s.Type(), err) | ||
} | ||
} | ||
return nil | ||
}) | ||
} |
This file contains 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,56 @@ | ||
package cargo | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/knqyf263/go-dep-parser/pkg/cargo" | ||
ptypes "github.com/knqyf263/go-dep-parser/pkg/types" | ||
"github.com/knqyf263/go-version" | ||
"github.com/knqyf263/trivy/pkg/scanner/utils" | ||
"github.com/knqyf263/trivy/pkg/types" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
const ( | ||
scannerType = "cargo" | ||
) | ||
|
||
type Scanner struct { | ||
db AdvisoryDB | ||
} | ||
|
||
func NewScanner() *Scanner { | ||
return &Scanner{} | ||
} | ||
|
||
func (s *Scanner) Detect(pkgName string, pkgVer *version.Version) ([]types.Vulnerability, error) { | ||
var vulns []types.Vulnerability | ||
for _, advisory := range s.db[pkgName] { | ||
if utils.MatchVersions(pkgVer, advisory.PatchedVersions) { | ||
continue | ||
} | ||
|
||
vuln := types.Vulnerability{ | ||
VulnerabilityID: advisory.Id, | ||
PkgName: strings.TrimSpace(advisory.Package), | ||
Title: strings.TrimSpace(advisory.Title), | ||
InstalledVersion: pkgVer.String(), | ||
FixedVersion: strings.Join(advisory.PatchedVersions, ", "), | ||
} | ||
vulns = append(vulns, vuln) | ||
} | ||
return vulns, nil | ||
} | ||
|
||
func (s *Scanner) ParseLockfile(f *os.File) ([]ptypes.Library, error) { | ||
libs, err := cargo.Parse(f) | ||
if err != nil { | ||
return nil, xerrors.Errorf("invalid Cargo.lock format: %w", err) | ||
} | ||
return libs, nil | ||
} | ||
|
||
func (s *Scanner) Type() string { | ||
return scannerType | ||
} |
This file contains 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 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 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