Skip to content

Commit 0318924

Browse files
committed
feat: convert spdx absolute to relative
Signed-off-by: Christopher Phillips <[email protected]>
1 parent 3508e64 commit 0318924

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

syft/format/common/spdxhelpers/to_format_model.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha1"
66
"fmt"
77
"path"
8+
"path/filepath"
89
"regexp"
910
"slices"
1011
"sort"
@@ -627,13 +628,18 @@ func toFiles(s sbom.SBOM) (results []*spdx.File) {
627628
comment = fmt.Sprintf("layerID: %s", coordinates.FileSystemID)
628629
}
629630

631+
relativePath, err := convertAbsoluteToRelative(coordinates.RealPath)
632+
if err != nil {
633+
// TODO:
634+
}
635+
630636
results = append(results, &spdx.File{
631637
FileSPDXIdentifier: toSPDXID(coordinates),
632638
FileComment: comment,
633639
// required, no attempt made to determine license information
634640
LicenseConcluded: noAssertion,
635641
Checksums: toFileChecksums(digests),
636-
FileName: coordinates.RealPath,
642+
FileName: relativePath,
637643
FileTypes: toFileTypes(metadata),
638644
LicenseInfoInFiles: []string{ // required in SPDX 2.2
639645
helpers.NOASSERTION,
@@ -831,3 +837,21 @@ func trimPatchVersion(semver string) string {
831837
}
832838
return semver
833839
}
840+
841+
// spdx requires that the file name field is a relative filename
842+
// with the root of the package archive or directory
843+
func convertAbsoluteToRelative(absPath string) (string, error) {
844+
// Ensure the absolute path is absolute (although it should already be)
845+
absPath, err := filepath.Abs(absPath)
846+
if err != nil {
847+
return "", fmt.Errorf("error converting absPath to absolute path: %v", err)
848+
}
849+
850+
// Calculate the relative path from the root directory "/"
851+
relPath, err := filepath.Rel("/", absPath)
852+
if err != nil {
853+
return "", fmt.Errorf("error calculating relative path: %v", err)
854+
}
855+
856+
return relPath, nil
857+
}

syft/format/common/spdxhelpers/to_format_model_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,51 @@ func Test_toPackageChecksums(t *testing.T) {
382382
}
383383
}
384384

385+
func Test_toFiles(t *testing.T) {
386+
tests := []struct {
387+
name string
388+
in sbom.SBOM
389+
want spdx.File
390+
}{
391+
{
392+
name: "File paths are converted to relative in final SPDX collection",
393+
in: sbom.SBOM{
394+
Source: source.Description{
395+
Name: "alpine",
396+
Version: "sha256:d34db33f",
397+
Metadata: source.ImageMetadata{
398+
UserInput: "alpine:latest",
399+
ManifestDigest: "sha256:d34db33f",
400+
},
401+
},
402+
Artifacts: sbom.Artifacts{
403+
Packages: pkg.NewCollection(pkg.Package{
404+
Name: "pkg-1",
405+
Version: "version-1",
406+
}),
407+
FileMetadata: map[file.Coordinates]file.Metadata{
408+
file.Coordinates{
409+
RealPath: "/some/path",
410+
FileSystemID: "",
411+
}: file.Metadata{
412+
Path: "/some/path",
413+
},
414+
},
415+
},
416+
},
417+
want: spdx.File{
418+
FileName: "some/path",
419+
},
420+
},
421+
}
422+
423+
for _, test := range tests {
424+
files := toFiles(test.in)
425+
got := files[0]
426+
assert.Equal(t, test.want.FileName, got.FileName)
427+
}
428+
}
429+
385430
func Test_toFileTypes(t *testing.T) {
386431

387432
tests := []struct {

0 commit comments

Comments
 (0)