Skip to content

Commit f983480

Browse files
⚠️ OSV scanner integration (#2509)
* Improve OSV scanning integration (squashed) Signed-off-by: Rex P <rexpan@google.com> * Add support for grouping vulnerabilities and aliases Signed-off-by: Rex P <rexpan@google.com> * Updated documentation, spit vulnerability output to multiple warnings Signed-off-by: Rex P <rexpan@google.com> * Updated documentation, spit vulnerability output to multiple warnings Signed-off-by: Rex P <rexpan@google.com> * Add its own codebase into docs Signed-off-by: Rex P <rexpan@google.com> * Update scorecard test to not prevent known vulns Signed-off-by: Rex P <rexpan@google.com> Signed-off-by: Rex P <rexpan@google.com> Co-authored-by: laurentsimon <64505099+laurentsimon@users.noreply.github.com>
1 parent 7206a2b commit f983480

File tree

23 files changed

+260
-106
lines changed

23 files changed

+260
-106
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ e2e-coverage.out
4242
.vscode/
4343
*.iml
4444
.idea
45+
.history
4546

4647
# tools
4748
bin

checker/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func GetClients(ctx context.Context, repoURI, localURI string, logger *log.Logge
4545
localdir.CreateLocalDirClient(ctx, logger), /*repoClient*/
4646
nil, /*ossFuzzClient*/
4747
nil, /*ciiClient*/
48-
nil, /*vulnClient*/
48+
clients.DefaultVulnerabilitiesClient(), /*vulnClient*/
4949
retErr
5050
}
5151

checks/evaluation/vulnerabilities.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"fmt"
1919
"strings"
2020

21+
"github.com/google/osv-scanner/pkg/grouper"
22+
2123
"github.com/ossf/scorecard/v4/checker"
2224
sce "github.com/ossf/scorecard/v4/errors"
2325
)
@@ -31,21 +33,25 @@ func Vulnerabilities(name string, dl checker.DetailLogger,
3133
return checker.CreateRuntimeErrorResult(name, e)
3234
}
3335

34-
score := checker.MaxResultScore
35-
IDs := []string{}
36+
aliasVulnerabilities := []grouper.IDAliases{}
3637
for _, vuln := range r.Vulnerabilities {
37-
IDs = append(IDs, vuln.ID)
38-
score--
38+
aliasVulnerabilities = append(aliasVulnerabilities, grouper.IDAliases(vuln))
3939
}
4040

41+
IDs := grouper.Group(aliasVulnerabilities)
42+
score := checker.MaxResultScore - len(IDs)
43+
4144
if score < checker.MinResultScore {
4245
score = checker.MinResultScore
4346
}
4447

4548
if len(IDs) > 0 {
46-
dl.Warn(&checker.LogMessage{
47-
Text: fmt.Sprintf("HEAD is vulnerable to %s", strings.Join(IDs, ", ")),
48-
})
49+
for _, v := range IDs {
50+
dl.Warn(&checker.LogMessage{
51+
Text: fmt.Sprintf("Project is vulnerable to: %s", strings.Join(v.IDs, " / ")),
52+
})
53+
}
54+
4955
return checker.CreateResultWithScore(name,
5056
fmt.Sprintf("%v existing vulnerabilities detected", len(IDs)), score)
5157
}

checks/raw/vulnerabilities.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ import (
2323

2424
// Vulnerabilities retrieves the raw data for the Vulnerabilities check.
2525
func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, error) {
26+
commitHash := ""
2627
commits, err := c.RepoClient.ListCommits()
27-
if err != nil {
28-
return checker.VulnerabilitiesData{}, fmt.Errorf("repoClient.ListCommits: %w", err)
28+
if err == nil && len(commits) > 0 && !allOf(commits, hasEmptySHA) {
29+
commitHash = commits[0].SHA
2930
}
3031

31-
if len(commits) < 1 || allOf(commits, hasEmptySHA) {
32-
return checker.VulnerabilitiesData{}, nil
32+
localPath, err := c.RepoClient.LocalPath()
33+
if err != nil {
34+
return checker.VulnerabilitiesData{}, fmt.Errorf("RepoClient.LocalPath: %w", err)
3335
}
34-
35-
resp, err := c.VulnerabilitiesClient.HasUnfixedVulnerabilities(c.Ctx, commits[0].SHA)
36+
resp, err := c.VulnerabilitiesClient.ListUnfixedVulnerabilities(c.Ctx, commitHash, localPath)
3637
if err != nil {
37-
return checker.VulnerabilitiesData{}, fmt.Errorf("vulnerabilitiesClient.HasUnfixedVulnerabilities: %w", err)
38+
return checker.VulnerabilitiesData{}, fmt.Errorf("vulnerabilitiesClient.ListUnfixedVulnerabilities: %w", err)
3839
}
3940
return checker.VulnerabilitiesData{
4041
Vulnerabilities: resp.Vulnerabilities,

checks/raw/vulnerabilities_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ func TestVulnerabilities(t *testing.T) {
8585
return []clients.Commit{{SHA: "test"}}, nil
8686
}).AnyTimes()
8787

88+
mockRepo.EXPECT().LocalPath().DoAndReturn(func() (string, error) {
89+
return "test_path", nil
90+
}).AnyTimes()
91+
8892
mockVulnClient := mockrepo.NewMockVulnerabilitiesClient(ctrl)
89-
mockVulnClient.EXPECT().HasUnfixedVulnerabilities(context.TODO(), gomock.Any()).DoAndReturn(
90-
func(ctx context.Context, repo string) (clients.VulnerabilitiesResponse, error) {
93+
mockVulnClient.EXPECT().ListUnfixedVulnerabilities(context.TODO(), gomock.Any(), gomock.Any()).DoAndReturn(
94+
func(ctx context.Context, commit string, localPath string) (clients.VulnerabilitiesResponse, error) {
9195
if tt.vulnsError {
9296
//nolint
9397
return clients.VulnerabilitiesResponse{}, errors.New("error")

checks/vulnerabilities.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const CheckVulnerabilities = "Vulnerabilities"
2828
func init() {
2929
supportedRequestTypes := []checker.RequestType{
3030
checker.CommitBased,
31+
checker.FileBased,
3132
}
3233
if err := registerCheck(CheckVulnerabilities, Vulnerabilities, supportedRequestTypes); err != nil {
3334
// this should never happen

checks/vulnerabilities_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ func TestVulnerabilities(t *testing.T) {
5555
return []clients.Commit{{SHA: "test"}}, nil
5656
}).MinTimes(1)
5757

58+
mockRepo.EXPECT().LocalPath().DoAndReturn(func() (string, error) {
59+
return "test_path", nil
60+
}).AnyTimes()
61+
5862
mockVulnClient := mockrepo.NewMockVulnerabilitiesClient(ctrl)
59-
mockVulnClient.EXPECT().HasUnfixedVulnerabilities(context.TODO(), gomock.Any()).DoAndReturn(
60-
func(ctx context.Context, repo string) (clients.VulnerabilitiesResponse, error) {
63+
mockVulnClient.EXPECT().ListUnfixedVulnerabilities(context.TODO(), gomock.Any(), gomock.Any()).DoAndReturn(
64+
func(ctx context.Context, commit string, localPath string) (clients.VulnerabilitiesResponse, error) {
6165
return tt.expected, tt.err
6266
}).MinTimes(1)
6367

clients/githubrepo/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ func (client *Client) URI() string {
130130
return fmt.Sprintf("github.com/%s/%s", client.repourl.owner, client.repourl.repo)
131131
}
132132

133+
// LocalPath implements RepoClient.LocalPath.
134+
func (client *Client) LocalPath() (string, error) {
135+
return client.tarball.getLocalPath()
136+
}
137+
133138
// ListFiles implements RepoClient.ListFiles.
134139
func (client *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) {
135140
return client.tarball.listFiles(predicate)

clients/githubrepo/tarball.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ func (handler *tarballHandler) listFiles(predicate func(string) (bool, error)) (
243243
return ret, nil
244244
}
245245

246+
func (handler *tarballHandler) getLocalPath() (string, error) {
247+
if err := handler.setup(); err != nil {
248+
return "", fmt.Errorf("error during tarballHandler.setup: %w", err)
249+
}
250+
absTempDir, err := filepath.Abs(handler.tempDir)
251+
if err != nil {
252+
return "", fmt.Errorf("error during filepath.Abs: %w", err)
253+
}
254+
return absTempDir, nil
255+
}
256+
246257
func (handler *tarballHandler) getFileContent(filename string) ([]byte, error) {
247258
if err := handler.setup(); err != nil {
248259
return nil, fmt.Errorf("error during tarballHandler.setup: %w", err)

clients/gitlabrepo/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (client *Client) URI() string {
136136
return fmt.Sprintf("%s/%s/%s", client.repourl.hostname, client.repourl.owner, client.repourl.projectID)
137137
}
138138

139+
func (client *Client) LocalPath() (string, error) {
140+
return "", nil
141+
}
142+
139143
func (client *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) {
140144
return nil, nil
141145
}

0 commit comments

Comments
 (0)