-
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.
Merge branch 'main' into cactus-rename-to-cacti
- Loading branch information
Showing
20 changed files
with
1,140 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 OpenSSF 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 ( | ||
"github.com/ossf/scorecard/v5/checker" | ||
sce "github.com/ossf/scorecard/v5/errors" | ||
"github.com/ossf/scorecard/v5/finding" | ||
"github.com/ossf/scorecard/v5/probes/hasReleaseSBOM" | ||
"github.com/ossf/scorecard/v5/probes/hasSBOM" | ||
) | ||
|
||
// SBOM applies the score policy for the SBOM check. | ||
func SBOM(name string, | ||
findings []finding.Finding, | ||
dl checker.DetailLogger, | ||
) checker.CheckResult { | ||
// We have 4 unique probes, each should have a finding. | ||
expectedProbes := []string{ | ||
hasSBOM.Probe, | ||
hasReleaseSBOM.Probe, | ||
} | ||
|
||
if !finding.UniqueProbesEqual(findings, expectedProbes) { | ||
e := sce.WithMessage(sce.ErrScorecardInternal, "invalid probe results") | ||
return checker.CreateRuntimeErrorResult(name, e) | ||
} | ||
|
||
// Compute the score. | ||
score := 0 | ||
m := make(map[string]bool) | ||
var logLevel checker.DetailType | ||
for i := range findings { | ||
f := &findings[i] | ||
switch f.Outcome { | ||
case finding.OutcomeTrue: | ||
logLevel = checker.DetailInfo | ||
switch f.Probe { | ||
case hasSBOM.Probe: | ||
score += scoreProbeOnce(f.Probe, m, 5) | ||
case hasReleaseSBOM.Probe: | ||
score += scoreProbeOnce(f.Probe, m, 5) | ||
} | ||
case finding.OutcomeFalse: | ||
logLevel = checker.DetailWarn | ||
default: | ||
continue // for linting | ||
} | ||
checker.LogFinding(dl, f, logLevel) | ||
} | ||
|
||
_, defined := m[hasSBOM.Probe] | ||
if !defined { | ||
return checker.CreateMinScoreResult(name, "SBOM file not detected") | ||
} | ||
|
||
_, defined = m[hasReleaseSBOM.Probe] | ||
if defined { | ||
return checker.CreateMaxScoreResult(name, "SBOM file found in release artifacts") | ||
} | ||
|
||
return checker.CreateResultWithScore(name, "SBOM file found in project", score) | ||
} |
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,95 @@ | ||
// Copyright 2024 OpenSSF 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 ( | ||
"testing" | ||
|
||
"github.com/ossf/scorecard/v5/checker" | ||
"github.com/ossf/scorecard/v5/finding" | ||
scut "github.com/ossf/scorecard/v5/utests" | ||
) | ||
|
||
func TestSBOM(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
findings []finding.Finding | ||
result scut.TestReturn | ||
}{ | ||
{ | ||
name: "No SBOM. Min Score", | ||
findings: []finding.Finding{ | ||
{ | ||
Probe: "hasSBOM", | ||
Outcome: finding.OutcomeFalse, | ||
}, | ||
{ | ||
Probe: "hasReleaseSBOM", | ||
Outcome: finding.OutcomeFalse, | ||
}, | ||
}, | ||
result: scut.TestReturn{ | ||
Score: checker.MinResultScore, | ||
NumberOfInfo: 0, | ||
NumberOfWarn: 2, | ||
}, | ||
}, | ||
{ | ||
name: "Only Source SBOM. Half Points", | ||
findings: []finding.Finding{ | ||
{ | ||
Probe: "hasSBOM", | ||
Outcome: finding.OutcomeTrue, | ||
}, | ||
{ | ||
Probe: "hasReleaseSBOM", | ||
Outcome: finding.OutcomeFalse, | ||
}, | ||
}, | ||
result: scut.TestReturn{ | ||
Score: 5, | ||
NumberOfInfo: 1, | ||
NumberOfWarn: 1, | ||
}, | ||
}, | ||
{ | ||
name: "SBOM in Release Assets. Max score", | ||
findings: []finding.Finding{ | ||
{ | ||
Probe: "hasSBOM", | ||
Outcome: finding.OutcomeTrue, | ||
}, | ||
{ | ||
Probe: "hasReleaseSBOM", | ||
Outcome: finding.OutcomeTrue, | ||
}, | ||
}, | ||
result: scut.TestReturn{ | ||
Score: checker.MaxResultScore, | ||
NumberOfInfo: 2, | ||
NumberOfWarn: 0, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
dl := scut.TestDetailLogger{} | ||
got := SBOM(tt.name, tt.findings, &dl) | ||
scut.ValidateTestReturn(t, tt.name, &tt.result, &got, &dl) | ||
}) | ||
} | ||
} |
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,106 @@ | ||
// Copyright 2024 OpenSSF 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 ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/ossf/scorecard/v5/checker" | ||
"github.com/ossf/scorecard/v5/clients" | ||
"github.com/ossf/scorecard/v5/finding" | ||
) | ||
|
||
var ( | ||
reRootFile = regexp.MustCompile(`^[^.]([^//]*)$`) | ||
reSBOMFile = regexp.MustCompile( | ||
`(?i).+\.(cdx.json|cdx.xml|spdx|spdx.json|spdx.xml|spdx.y[a?]ml|spdx.rdf|spdx.rdf.xml)`, | ||
) | ||
) | ||
|
||
const releaseLookBack = 5 | ||
|
||
// SBOM retrieves the raw data for the SBOM check. | ||
func SBOM(c *checker.CheckRequest) (checker.SBOMData, error) { | ||
var results checker.SBOMData | ||
|
||
releases, lerr := c.RepoClient.ListReleases() | ||
if lerr != nil { | ||
return results, fmt.Errorf("RepoClient.ListReleases: %w", lerr) | ||
} | ||
|
||
results.SBOMFiles = append(results.SBOMFiles, checkSBOMReleases(releases)...) | ||
|
||
// Look for SBOMs in source | ||
repoFiles, err := c.RepoClient.ListFiles(func(file string) (bool, error) { | ||
return reSBOMFile.MatchString(file) && reRootFile.MatchString(file), nil | ||
}) | ||
if err != nil { | ||
return results, fmt.Errorf("error during ListFiles: %w", err) | ||
} | ||
|
||
results.SBOMFiles = append(results.SBOMFiles, checkSBOMSource(repoFiles)...) | ||
|
||
return results, nil | ||
} | ||
|
||
func checkSBOMReleases(releases []clients.Release) []checker.SBOM { | ||
var foundSBOMs []checker.SBOM | ||
|
||
for i := range releases { | ||
if i >= releaseLookBack { | ||
break | ||
} | ||
|
||
v := releases[i] | ||
|
||
for _, link := range v.Assets { | ||
if !reSBOMFile.MatchString(link.Name) { | ||
continue | ||
} | ||
|
||
foundSBOMs = append(foundSBOMs, | ||
checker.SBOM{ | ||
File: checker.File{ | ||
Path: link.URL, | ||
Type: finding.FileTypeURL, | ||
}, | ||
Name: link.Name, | ||
}) | ||
|
||
// Only want one sbom from each release | ||
break | ||
} | ||
} | ||
return foundSBOMs | ||
} | ||
|
||
func checkSBOMSource(fileList []string) []checker.SBOM { | ||
var foundSBOMs []checker.SBOM | ||
|
||
for _, file := range fileList { | ||
// TODO: parse matching file contents to determine schema & version | ||
foundSBOMs = append(foundSBOMs, | ||
checker.SBOM{ | ||
File: checker.File{ | ||
Path: file, | ||
Type: finding.FileTypeSource, | ||
}, | ||
Name: file, | ||
}) | ||
} | ||
|
||
return foundSBOMs | ||
} |
Oops, something went wrong.