Skip to content

Commit

Permalink
Merge pull request #429 from another-rex:fix-sbom-extractors
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 723433104
  • Loading branch information
copybara-github committed Feb 5, 2025
2 parents e420074 + 6b464d5 commit 6f1cd07
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
16 changes: 14 additions & 2 deletions extractor/filesystem/sbom/cdx/cdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ type extractFunc = func(io.Reader) (cyclonedx.BOM, error)
// https://cyclonedx.org/specification/overview/#recognized-file-patterns
var cdxExtensions = map[string]cyclonedx.BOMFileFormat{
".cdx.json": cyclonedx.BOMFileFormatJSON,
".bom.json": cyclonedx.BOMFileFormatJSON,
".cdx.xml": cyclonedx.BOMFileFormatXML,
".bom.xml": cyclonedx.BOMFileFormatXML,
}

var cdxNames = map[string]cyclonedx.BOMFileFormat{
"bom.json": cyclonedx.BOMFileFormatJSON,
"bom.xml": cyclonedx.BOMFileFormatXML,
}

// FileRequired returns true if the specified file is a supported cdx file.
Expand Down Expand Up @@ -86,6 +89,15 @@ func findExtractor(path string) extractFunc {
}
}

for name, format := range cdxNames {
if strings.ToLower(filepath.Base(path)) == name {
return func(rdr io.Reader) (cyclonedx.BOM, error) {
var cdxBOM cyclonedx.BOM
return cdxBOM, cyclonedx.NewBOMDecoder(rdr, format).Decode(&cdxBOM)
}
}
}

return nil
}

Expand Down
12 changes: 11 additions & 1 deletion extractor/filesystem/sbom/cdx/cdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ func TestFileRequired(t *testing.T) {
{
name: "sbom.bom.json",
path: "testdata/sbom.bom.json",
wantIsRequired: true,
wantIsRequired: false,
},
{
name: "sbom.bom.xml",
path: "testdata/sbom.bom.xml",
wantIsRequired: false,
},
{
name: "bom.json",
path: "testdata/bom.json",
wantIsRequired: true,
},
{
name: "bom.xml",
path: "testdata/bom.xml",
wantIsRequired: true,
},
{
Expand Down
9 changes: 5 additions & 4 deletions extractor/filesystem/sbom/spdx/spdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ type extractFunc = func(io.Reader) (*spdx.Document, error)

// Format support based on https://spdx.dev/resources/use/#documents
var extensionHandlers = map[string]extractFunc{
".spdx.json": json.Read,
".spdx": tagvalue.Read,
".spdx.yml": yaml.Read,
".spdx.rdf": rdf.Read,
".spdx.json": json.Read,
".spdx": tagvalue.Read,
".spdx.yml": yaml.Read,
".spdx.rdf": rdf.Read,
".spdx.rdf.xml": rdf.Read,
// No support for .xsl files because those are too ambiguous and could be many other things.
}

Expand Down
5 changes: 5 additions & 0 deletions extractor/filesystem/sbom/spdx/spdx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func TestFileRequired(t *testing.T) {
path: "testdata/sbom.spdx.rdf",
wantIsRequired: true,
},
{
name: "sbom.spdx.rdf.xml",
path: "testdata/sbom.spdx.rdf.xml",
wantIsRequired: true,
},
{
name: "random_file.ext",
path: "testdata/random_file.ext",
Expand Down

0 comments on commit 6f1cd07

Please sign in to comment.