Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add experimental check for published SBOM #3903

Merged
merged 21 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ff83c06
Sbom check MVP
ashearin Feb 22, 2024
e59a2a8
Merge branch 'main' of https://github.com/ashearin/scorecard-sandbox …
ashearin Feb 22, 2024
d928ede
PR suggestion fixes
ashearin Feb 27, 2024
718d798
fix line length
ashearin Feb 27, 2024
3c09694
update gitlab client to check 20 latest pipelines in default branch
ashearin Mar 6, 2024
bc11501
Merge branch 'main' into check-for-published-sbom
ashearin Mar 6, 2024
a43a169
correct issues
ashearin Mar 6, 2024
fc4f183
add unit tests for sbom client code
ashearin Apr 3, 2024
ed58add
Merge branch 'main' of https://github.com/ashearin/scorecard-sandbox …
ashearin Apr 3, 2024
037eb1a
probe name alignment, updated evaluation tests
ashearin Apr 4, 2024
1a1cbc0
consolidate probes, reuse available data sources
ashearin May 2, 2024
82b929d
add autogen doc update
ashearin May 2, 2024
f3b2f31
pull in changes from main
ashearin May 2, 2024
169f53d
address PR comments, remove CI/CD check code
ashearin May 14, 2024
9c53382
update unit tests
ashearin May 14, 2024
3c99ee6
fix linting errors
ashearin May 14, 2024
ba44f55
revert unnecessary changes, correct check documentation
ashearin May 14, 2024
413735e
Merge branch 'main' of https://github.com/ashearin/scorecard-sandbox …
ashearin May 14, 2024
1dafed9
address PR comments
ashearin May 15, 2024
4531221
move release lookback to data collection side
ashearin May 16, 2024
a48f099
Merge branch 'main' into check-for-published-sbom
spencerschrock May 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions checker/raw_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type RawResults struct {
DependencyUpdateToolResults DependencyUpdateToolData
FuzzingResults FuzzingData
LicenseResults LicenseData
SBOMResults SBOMData
MaintainedResults MaintainedData
Metadata MetadataData
PackagingResults PackagingData
Expand Down Expand Up @@ -168,6 +169,18 @@ type LicenseData struct {
LicenseFiles []LicenseFile
}

// SBOM details.
type SBOM struct {
Name string // SBOM Filename
File File // SBOM File Object
}

// SBOMData contains the raw results for the SBOM check.
// Some repos may have more than one SBOM.
type SBOMData struct {
SBOMFiles []SBOM
}

// CodeReviewData contains the raw results
// for the Code-Review check.
type CodeReviewData struct {
Expand Down
1 change: 1 addition & 0 deletions checks/all_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func getAll(overrideExperimental bool) checker.CheckNameToFnMap {
if _, experimental := os.LookupEnv("SCORECARD_EXPERIMENTAL"); !experimental {
// TODO: remove this check when v6 is released
delete(possibleChecks, CheckWebHooks)
delete(possibleChecks, CheckSBOM)
}

return possibleChecks
Expand Down
75 changes: 75 additions & 0 deletions checks/evaluation/sbom.go
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")
}
spencerschrock marked this conversation as resolved.
Show resolved Hide resolved

_, 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)
}
95 changes: 95 additions & 0 deletions checks/evaluation/sbom_test.go
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)
})
}
}
106 changes: 106 additions & 0 deletions checks/raw/sbom.go
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
}
Loading
Loading