-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for loading VEX files from External References de…
…fined in CycloneDX SBOMs
- Loading branch information
Showing
6 changed files
with
272 additions
and
14 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
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 @@ | ||
package vex | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/aquasecurity/trivy/pkg/fanal/artifact" | ||
"github.com/aquasecurity/trivy/pkg/log" | ||
"github.com/aquasecurity/trivy/pkg/sbom/core" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
"golang.org/x/xerrors" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type SBOMReferenceSet struct { | ||
Vexes []VEX | ||
} | ||
|
||
func NewSBOMReferenceSet(report *types.Report) (*SBOMReferenceSet, error) { | ||
fmt.Println("hello there") | ||
|
||
if report.ArtifactType != artifact.TypeCycloneDX { | ||
return nil, xerrors.Errorf("externalReferences can only be used when scanning CycloneDX SBOMs: %w", report.ArtifactType) | ||
} | ||
|
||
var externalRefs = report.BOM.ExternalReferences() | ||
urls := ParseToURLs(externalRefs) | ||
|
||
v, err := RetrieveExternalVEXDocuments(urls, report) | ||
if err != nil { | ||
return nil, xerrors.Errorf("failed to fetch external VEX document: %w", err) | ||
} | ||
|
||
return &SBOMReferenceSet{Vexes: v}, nil | ||
} | ||
|
||
func ParseToURLs(refs []core.ExternalReference) []url.URL { | ||
var urls []url.URL | ||
for _, ref := range refs { | ||
if ref.Type == core.ExternalReferenceVex { | ||
val, err := url.Parse(ref.URL) | ||
// do not concern ourselves with relative URLs at this point | ||
if err != nil || (val.Scheme != "https" && val.Scheme != "http") { | ||
continue | ||
} | ||
urls = append(urls, *val) | ||
} | ||
} | ||
return urls | ||
} | ||
|
||
func RetrieveExternalVEXDocuments(refs []url.URL, report *types.Report) ([]VEX, error) { | ||
|
||
logger := log.WithPrefix("vex").With(log.String("type", "externalReference")) | ||
|
||
var docs []VEX | ||
for _, ref := range refs { | ||
doc, err := RetrieveExternalVEXDocument(ref, report) | ||
if err != nil && doc != nil { | ||
docs = append(docs, doc) | ||
} | ||
} | ||
logger.Debug("Retrieved external VEX documents", "count", len(docs)) | ||
|
||
if len(docs) == 0 { | ||
logger.Info("No external VEX documents found") | ||
return nil, nil | ||
} | ||
return docs, nil | ||
|
||
} | ||
|
||
func RetrieveExternalVEXDocument(VEXUrl url.URL, report *types.Report) (VEX, error) { | ||
|
||
logger := log.WithPrefix("vex").With(log.String("type", "externalReference")) | ||
|
||
logger.Info(fmt.Sprintf("Retrieving external VEX document from host %s", VEXUrl.Host)) | ||
|
||
res, err := http.Get(VEXUrl.String()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to fetch file via HTTP: %w", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
val, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to read response into memory: %w", err) | ||
} | ||
|
||
if v, err := decodeVEX(bytes.NewReader(val), VEXUrl.String(), report); err != nil { | ||
return nil, xerrors.Errorf("unable to load VEX: %w", err) | ||
} else { | ||
return v, nil | ||
} | ||
} | ||
|
||
func (set *SBOMReferenceSet) NotAffected(vuln types.DetectedVulnerability, product, subComponent *core.Component) (types.ModifiedFinding, bool) { | ||
|
||
for _, vex := range set.Vexes { | ||
if m, notAffected := vex.NotAffected(vuln, product, subComponent); notAffected { | ||
return m, notAffected | ||
} | ||
} | ||
return types.ModifiedFinding{}, false | ||
} |
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,73 @@ | ||
package vex_test | ||
|
||
import ( | ||
"github.com/aquasecurity/trivy/pkg/fanal/artifact" | ||
"github.com/aquasecurity/trivy/pkg/sbom/core" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
"github.com/aquasecurity/trivy/pkg/vex" | ||
"github.com/stretchr/testify/require" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
const ( | ||
vexExternalRef = "/openvex" | ||
vexUnknown = "/unknown" | ||
) | ||
|
||
func setUpServer(t *testing.T) *httptest.Server { | ||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == vexExternalRef { | ||
f, err := os.Open("testdata/" + vexExternalRef + ".json") | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
_, err = io.Copy(w, f) | ||
require.NoError(t, err) | ||
} else if r.URL.Path == vexUnknown { | ||
f, err := os.Open("testdata/" + vexUnknown + ".json") | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
_, err = io.Copy(w, f) | ||
require.NoError(t, err) | ||
} | ||
|
||
http.NotFound(w, r) | ||
return | ||
})) | ||
return s | ||
} | ||
|
||
func setupTestReport(s *httptest.Server, path string) *types.Report { | ||
r := types.Report{ | ||
ArtifactType: artifact.TypeCycloneDX, | ||
BOM: &core.BOM{}, | ||
} | ||
r.BOM.AddExternalReferences([]core.ExternalReference{{ | ||
URL: s.URL + path, | ||
Type: core.ExternalReferenceVex, | ||
}}) | ||
|
||
return &r | ||
} | ||
|
||
func TestRetrieveExternalVEXDocuments(t *testing.T) { | ||
s := setUpServer(t) | ||
t.Cleanup(s.Close) | ||
|
||
t.Run("external vex retrieval", func(t *testing.T) { | ||
set, err := vex.NewSBOMReferenceSet(setupTestReport(s, vexExternalRef)) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, len(set.Vexes)) | ||
}) | ||
|
||
t.Run("incompatible external vex", func(t *testing.T) { | ||
set, err := vex.NewSBOMReferenceSet(setupTestReport(s, vexUnknown)) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, len(set.Vexes)) | ||
}) | ||
} |
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