-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Separate check from policies for the Vulnerabilities check (#1532)
* raw vulnerabilities seperation * update year * missing files * tests
- Loading branch information
1 parent
7a6eb28
commit 5f9fff3
Showing
7 changed files
with
173 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2022 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package evaluation | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ossf/scorecard/v4/checker" | ||
sce "github.com/ossf/scorecard/v4/errors" | ||
) | ||
|
||
// Vulnerabilities applies the score policy for the Vulnerabilities check. | ||
func Vulnerabilities(name string, dl checker.DetailLogger, | ||
r *checker.VulnerabilitiesData) checker.CheckResult { | ||
if r == nil { | ||
e := sce.WithMessage(sce.ErrScorecardInternal, "empty raw data") | ||
return checker.CreateRuntimeErrorResult(name, e) | ||
} | ||
|
||
score := checker.MaxResultScore | ||
IDs := []string{} | ||
for _, vuln := range r.Vulnerabilities { | ||
IDs = append(IDs, vuln.ID) | ||
score-- | ||
} | ||
|
||
if score < 0 { | ||
score = 0 | ||
} | ||
|
||
if len(IDs) > 0 { | ||
dl.Warn3(&checker.LogMessage{ | ||
Text: fmt.Sprintf("HEAD is vulnerable to %s", strings.Join(IDs, ", ")), | ||
}) | ||
return checker.CreateResultWithScore(name, | ||
fmt.Sprintf("%v existing vulnerabilities detected", len(IDs)), score) | ||
} | ||
|
||
return checker.CreateMaxScoreResult(name, "no vulnerabilities detected") | ||
} |
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,60 @@ | ||
// Copyright 2022 Security Scorecard Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package raw | ||
|
||
import ( | ||
"github.com/ossf/scorecard/v4/checker" | ||
"github.com/ossf/scorecard/v4/clients" | ||
sce "github.com/ossf/scorecard/v4/errors" | ||
) | ||
|
||
// Vulnerabilities retrieves the raw data for the Vulnerabilities check. | ||
func Vulnerabilities(c *checker.CheckRequest) (checker.VulnerabilitiesData, error) { | ||
commits, err := c.RepoClient.ListCommits() | ||
if err != nil { | ||
return checker.VulnerabilitiesData{}, | ||
sce.WithMessage(sce.ErrScorecardInternal, "Client.Repositories.ListCommits") | ||
} | ||
|
||
if len(commits) < 1 || commits[0].SHA == "" { | ||
return checker.VulnerabilitiesData{}, | ||
sce.WithMessage(sce.ErrScorecardInternal, "no commits found") | ||
} | ||
|
||
resp, err := c.VulnerabilitiesClient.HasUnfixedVulnerabilities(c.Ctx, commits[0].SHA) | ||
if err != nil { | ||
return checker.VulnerabilitiesData{}, | ||
sce.WithMessage(sce.ErrScorecardInternal, "VulnerabilitiesClient.HasUnfixedVulnerabilities") | ||
} | ||
|
||
vulnIDs := getVulnerabilities(&resp) | ||
vulns := []checker.Vulnerability{} | ||
for _, id := range vulnIDs { | ||
v := checker.Vulnerability{ | ||
ID: id, | ||
// Note: add fields if needed. | ||
} | ||
vulns = append(vulns, v) | ||
} | ||
return checker.VulnerabilitiesData{Vulnerabilities: vulns}, nil | ||
} | ||
|
||
func getVulnerabilities(resp *clients.VulnerabilitiesResponse) []string { | ||
ids := make([]string, 0, len(resp.Vulns)) | ||
for _, vuln := range resp.Vulns { | ||
ids = append(ids, vuln.ID) | ||
} | ||
return ids | ||
} |
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