-
Notifications
You must be signed in to change notification settings - Fork 887
Relativize CycloneDX-JSON file component paths against --base-path #4966
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ import ( | |
| "github.com/anchore/syft/internal/spdxlicense" | ||
| "github.com/anchore/syft/syft/artifact" | ||
| "github.com/anchore/syft/syft/file" | ||
| formatInternal "github.com/anchore/syft/syft/format/internal" | ||
| formatinternal "github.com/anchore/syft/syft/format/internal" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we avoid these import renames? |
||
| "github.com/anchore/syft/syft/format/internal/spdxutil/helpers" | ||
| "github.com/anchore/syft/syft/pkg" | ||
| "github.com/anchore/syft/syft/sbom" | ||
|
|
@@ -680,7 +680,7 @@ func lookupRelationship(ty artifact.RelationshipType) (bool, helpers.Relationshi | |
| func toFiles(s sbom.SBOM) (results []*spdx.File) { | ||
| artifacts := s.Artifacts | ||
|
|
||
| _, coordinateSorter := formatInternal.GetLocationSorters(s) | ||
| _, coordinateSorter := formatinternal.GetLocationSorters(s) | ||
|
|
||
| coordinates := s.AllCoordinates() | ||
| slices.SortFunc(coordinates, coordinateSorter) | ||
|
|
@@ -871,20 +871,7 @@ func trimPatchVersion(semver string) string { | |
| // spdx requires that the file name field is a relative filename | ||
| // with the root of the package archive or directory | ||
| func convertAbsoluteToRelative(absPath string) (string, error) { | ||
| // Ensure the absolute path is absolute (although it should already be) | ||
| if !path.IsAbs(absPath) { | ||
| // already relative | ||
| log.Debugf("%s is already relative", absPath) | ||
| return absPath, nil | ||
| } | ||
|
|
||
| // we use "/" here given that we're converting absolute paths from root to relative | ||
| relPath, found := strings.CutPrefix(absPath, "/") | ||
| if !found { | ||
| return "", fmt.Errorf("error calculating relative path: %s", absPath) | ||
| } | ||
|
|
||
| return relPath, nil | ||
| return formatinternal.ConvertAbsoluteToRelative(absPath) | ||
| } | ||
|
|
||
| func convertOtherLicense(otherLicenses []spdx.OtherLicense) []*spdx.OtherLicense { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path" | ||
| "strings" | ||
| ) | ||
|
|
||
| func ConvertAbsoluteToRelative(absPath string) (string, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be something like what filepath.Rel is doing, though I don't think we can use that function because the paths here are normalized to forwards slashes |
||
| if !path.IsAbs(absPath) { | ||
| return absPath, nil | ||
| } | ||
|
|
||
| relPath, found := strings.CutPrefix(absPath, "/") | ||
| if !found { | ||
| return "", fmt.Errorf("error calculating relative path: %s", absPath) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function returns an error converting a relative path to a relative path? |
||
| } | ||
|
|
||
| return relPath, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConvertAbsoluteToRelative(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| absPath string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "absolute path", | ||
| absPath: "/usr/bin/foo", | ||
| want: "usr/bin/foo", | ||
| }, | ||
| { | ||
| name: "relative path", | ||
| absPath: "relative/path/bar", | ||
| want: "relative/path/bar", | ||
| }, | ||
| { | ||
| name: "root path", | ||
| absPath: "/", | ||
| want: "", | ||
| }, | ||
| { | ||
| name: "dot relative path", | ||
| absPath: "./foo", | ||
| want: "./foo", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := ConvertAbsoluteToRelative(tt.absPath) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As was noted on the issue, converting the
RealPathto a relative path in this manner seems like the wrong thing to do: if I scanned/subdirand there's a symlink to../foo, this would be resolved to the absolute path of/foo, which is aRealPath, which then would be converted to the relative pathfoo, but there is no/subdir/foo. As noted in another comment, this should probably be../foo