2020-10-26 23:22:13 +03:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2020-10-19 19:58:51 +03:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/go-github/v32/github"
|
2020-10-27 22:23:48 +03:00
|
|
|
"github.com/ossf/scorecard/checker"
|
2020-10-19 19:58:51 +03:00
|
|
|
)
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
const sastStr = "SAST"
|
|
|
|
|
2020-12-01 17:32:37 +03:00
|
|
|
var sastTools map[string]bool = map[string]bool{"github-code-scanning": true, "sonarcloud": true}
|
|
|
|
|
2020-10-19 19:58:51 +03:00
|
|
|
func init() {
|
2021-04-10 15:26:56 +03:00
|
|
|
registerCheck(sastStr, SAST)
|
2021-01-15 23:18:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
func SAST(c checker.CheckRequest) checker.CheckResult {
|
2021-01-15 23:18:43 +03:00
|
|
|
return checker.MultiCheck(
|
|
|
|
CodeQLInCheckDefinitions,
|
|
|
|
SASTToolInCheckRuns,
|
|
|
|
)(c)
|
2020-10-19 19:58:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
func SASTToolInCheckRuns(c checker.CheckRequest) checker.CheckResult {
|
2020-10-19 19:58:51 +03:00
|
|
|
prs, _, err := c.Client.PullRequests.List(c.Ctx, c.Owner, c.Repo, &github.PullRequestListOptions{
|
|
|
|
State: "closed",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeRetryResult(sastStr, err)
|
2020-10-19 19:58:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
totalMerged := 0
|
|
|
|
totalTested := 0
|
|
|
|
for _, pr := range prs {
|
|
|
|
if pr.MergedAt == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalMerged++
|
|
|
|
crs, _, err := c.Client.Checks.ListCheckRunsForRef(c.Ctx, c.Owner, c.Repo, pr.GetHead().GetSHA(), &github.ListCheckRunsOptions{})
|
|
|
|
if err != nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeRetryResult(sastStr, err)
|
2020-10-19 19:58:51 +03:00
|
|
|
}
|
2021-01-18 22:43:54 +03:00
|
|
|
if crs == nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeInconclusiveResult(sastStr)
|
2021-01-18 22:43:54 +03:00
|
|
|
}
|
2020-10-19 19:58:51 +03:00
|
|
|
for _, cr := range crs.CheckRuns {
|
|
|
|
if cr.GetStatus() != "completed" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cr.GetConclusion() != "success" {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-01 17:32:37 +03:00
|
|
|
if sastTools[cr.GetApp().GetSlug()] {
|
|
|
|
c.Logf("SAST Tool found: %s", cr.GetHTMLURL())
|
2020-10-19 19:58:51 +03:00
|
|
|
totalTested++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if totalTested == 0 {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeInconclusiveResult(sastStr)
|
2020-10-19 19:58:51 +03:00
|
|
|
}
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeProportionalResult(sastStr, totalTested, totalMerged, .75)
|
2020-10-19 19:58:51 +03:00
|
|
|
}
|
2020-12-15 19:07:45 +03:00
|
|
|
|
2021-04-10 15:26:56 +03:00
|
|
|
func CodeQLInCheckDefinitions(c checker.CheckRequest) checker.CheckResult {
|
2020-12-15 19:07:45 +03:00
|
|
|
searchQuery := ("github/codeql-action path:/.github/workflows repo:" + c.Owner + "/" + c.Repo)
|
|
|
|
results, _, err := c.Client.Search.Code(c.Ctx, searchQuery, &github.SearchOptions{})
|
|
|
|
if err != nil {
|
2021-04-10 15:26:56 +03:00
|
|
|
return checker.MakeRetryResult(sastStr, err)
|
2020-12-15 19:07:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, result := range results.CodeResults {
|
|
|
|
c.Logf("found CodeQL definition: %s", result.GetPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
return checker.CheckResult{
|
2021-04-10 15:26:56 +03:00
|
|
|
Name: sastStr,
|
2020-12-15 19:07:45 +03:00
|
|
|
Pass: *results.Total > 0,
|
2021-04-10 15:26:56 +03:00
|
|
|
Confidence: checker.MaxResultConfidence,
|
2020-12-15 19:07:45 +03:00
|
|
|
}
|
|
|
|
}
|