mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 22:12:28 +01:00
* fix(spdx): write a name for the root package when the source has none
A source read from another SBOM (a purl list, a CycloneDX document
without metadata.component, an SPDX document without a DESCRIBES
relationship) can have no name. The SPDX encoder then wrote the document
root package with an empty PackageName, which SPDX requires. In
tag-value output the PackageName line is omitted, so the root package's
fields land in the previous section and syft cannot read its own output
back ("received unknown tag SPDXID in CreationInfo section").
Fall back to the document name for the root package, and make the
document name fall back to "unknown" for every source type rather than
only for unrecognized ones.
Signed-off-by: Tony <tjagodka@gmail.com>
* fix(spdx): drop syft's root package on decode for every SPDX version
- the root package name now comes from one fallback chain (source name, image repo name, source ID, document name), so the root purl no longer gets an empty name for a source decoded from CycloneDX
- decoding strips the root syft writes for an unknown source, so `syft convert` round trips stop adding an `unknown` package each pass
- for SPDX 2.1 and 2.2, which have no `primaryPackagePurpose`, the purpose is taken from the source type in syft's `DocumentRoot-<type>-` ID
**behavior change:** decoding a syft-written SPDX 2.1/2.2 document no longer returns the root as a package or its `CONTAINS` relationships, matching 2.3.
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
---------
Signed-off-by: Tony <tjagodka@gmail.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
30 lines
593 B
Go
30 lines
593 B
Go
package helpers
|
|
|
|
import (
|
|
"github.com/anchore/syft/syft/source"
|
|
)
|
|
|
|
func DocumentName(src source.Description) string {
|
|
if src.Name != "" {
|
|
return src.Name
|
|
}
|
|
|
|
var name string
|
|
switch metadata := src.Metadata.(type) {
|
|
case source.ImageMetadata:
|
|
name = metadata.UserInput
|
|
case source.OCIModelMetadata:
|
|
name = metadata.UserInput
|
|
case source.DirectoryMetadata:
|
|
name = metadata.Path
|
|
case source.FileMetadata:
|
|
name = metadata.Path
|
|
}
|
|
|
|
// the SPDX document name is mandatory, and a source read from another SBOM may not have one
|
|
if name == "" {
|
|
return "unknown"
|
|
}
|
|
return name
|
|
}
|