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

Better SBOM documentation and error message #349

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ The `--no-ignore` flag can be used to force the scanner to scan ignored files.
If you want to check for known vulnerabilities only in dependencies in your SBOM, you can use the following command:

```bash
osv-scanner --sbom=/path/to/your/sbom.json
osv-scanner --sbom=/path/to/your/sbom.spdx.json
```

[SPDX] and [CycloneDX] SBOMs using [Package URLs] are supported. The format is
auto-detected based on the input file contents.
auto-detected based on the input file contents and the file name.

When scanning a directory, only SBOMs following the specification filename will be scanned. See the specs for [SPDX Filenames] and [CycloneDX Filenames].

[SPDX]: https://spdx.dev/
[SPDX Filenames]: https://spdx.github.io/spdx-spec/v2.3/conformance/
[CycloneDX Filenames]: https://cyclonedx.org/specification/overview/#recognized-file-patterns
[CycloneDX]: https://cyclonedx.org/
[Package URLs]: https://github.com/package-url/purl-spec

Expand Down
4 changes: 2 additions & 2 deletions internal/sbom/cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *CycloneDX) GetPackages(r io.ReadSeeker, callback func(Identifier) error
}

return InvalidFormatError{
msg: "failed to parse CycloneDX",
errs: errs,
Msg: "failed to parse CycloneDX",
Errs: errs,
}
}
10 changes: 5 additions & 5 deletions internal/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ var (
)

type InvalidFormatError struct {
msg string
errs []error
Msg string
Errs []error
}

func (e InvalidFormatError) Error() string {
errStrings := make([]string, 0, len(e.errs))
for _, e := range e.errs {
errStrings := make([]string, 0, len(e.Errs))
for _, e := range e.Errs {
errStrings = append(errStrings, "\t"+e.Error())
}

return fmt.Sprintf("%s:\n%s", e.msg, strings.Join(errStrings, "\n"))
return fmt.Sprintf("%s:\n%s", e.Msg, strings.Join(errStrings, "\n"))
}
4 changes: 2 additions & 2 deletions internal/sbom/spdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *SPDX) GetPackages(r io.ReadSeeker, callback func(Identifier) error) err
}

return InvalidFormatError{
msg: "failed to parse SPDX",
errs: errs,
Msg: "failed to parse SPDX",
Errs: errs,
}
}
8 changes: 8 additions & 0 deletions pkg/osvscanner/osvscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ func scanSBOMFile(r *reporter.Reporter, query *osv.BatchedQuery, path string, fr
})
if err == nil {
// Found the right format.
if count == 0 {
return sbom.InvalidFormatError{
Msg: "no Package URLs found",
Errs: []error{
fmt.Errorf("scanned %s as %s SBOM, but failed to find any package URLs, this is required to scan SBOMs", path, provider.Name()),
},
}
}
r.PrintText(fmt.Sprintf("Scanned %s as %s SBOM and found %d packages\n", path, provider.Name(), count))
if ignoredCount > 0 {
r.PrintText(fmt.Sprintf("Ignored %d packages with invalid PURLs\n", ignoredCount))
Expand Down