forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure SPDXIDs are valid (anchore#955)
- Loading branch information
Showing
6 changed files
with
125 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package spdxhelpers | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var expr = regexp.MustCompile("[^a-zA-Z0-9.-]") | ||
|
||
// SPDX spec says SPDXID must be: | ||
// "SPDXRef-"[idstring] where [idstring] is a unique string containing letters, numbers, ., and/or - | ||
func SanitizeElementID(id string) string { | ||
return expr.ReplaceAllString(id, "-") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package spdxhelpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_SanitizeElementID(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
input: "letters", | ||
expected: "letters", | ||
}, | ||
{ | ||
input: "ssl-client", | ||
expected: "ssl-client", | ||
}, | ||
{ | ||
input: "ssl_client", | ||
expected: "ssl-client", | ||
}, | ||
{ | ||
input: "go-module-sigs.k8s.io/structured-merge-diff/v3", | ||
expected: "go-module-sigs.k8s.io-structured-merge-diff-v3", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.input, func(t *testing.T) { | ||
actual := SanitizeElementID(test.input) | ||
|
||
assert.Equal(t, test.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,13 @@ | ||
package model | ||
|
||
import "github.com/anchore/syft/internal/formats/common/spdxhelpers" | ||
|
||
// ElementID represents the identifier string portion of an SPDX element | ||
// identifier. DocElementID should be used for any attributes which can | ||
// contain identifiers defined in a different SPDX document. | ||
// ElementIDs should NOT contain the mandatory 'SPDXRef-' portion. | ||
type ElementID string | ||
|
||
func (e ElementID) String() string { | ||
return "SPDXRef-" + string(e) | ||
} | ||
|
||
// DocElementID represents an SPDX element identifier that could be defined | ||
// in a different SPDX document, and therefore could have a "DocumentRef-" | ||
// portion, such as Relationship and Annotations. | ||
// ElementID is used for attributes in which a "DocumentRef-" portion cannot | ||
// appear, such as a Package or File definition (since it is necessarily | ||
// being defined in the present document). | ||
// DocumentRefID will be the empty string for elements defined in the | ||
// present document. | ||
// DocElementIDs should NOT contain the mandatory 'DocumentRef-' or | ||
// 'SPDXRef-' portions. | ||
type DocElementID struct { | ||
DocumentRefID string | ||
ElementRefID ElementID | ||
} | ||
|
||
// RenderDocElementID takes a DocElementID and returns the string equivalent, | ||
// with the SPDXRef- prefix (and, if applicable, the DocumentRef- prefix) | ||
// reinserted. | ||
func (d DocElementID) String() string { | ||
prefix := "" | ||
if d.DocumentRefID != "" { | ||
prefix = "DocumentRef-" + d.DocumentRefID + ":" | ||
} | ||
return prefix + d.ElementRefID.String() | ||
return "SPDXRef-" + spdxhelpers.SanitizeElementID(string(e)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
internal/formats/spdx22tagvalue/test-fixtures/snapshot/TestSPDXJSONSPDXIDs.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
SPDXVersion: SPDX-2.2 | ||
DataLicense: CC0-1.0 | ||
SPDXID: SPDXRef-DOCUMENT | ||
DocumentName: . | ||
DocumentNamespace: https://anchore.com/syft/dir/e69056a9-935e-4f00-b85f-9467f5d99a92 | ||
LicenseListVersion: 3.16 | ||
Creator: Organization: Anchore, Inc | ||
Creator: Tool: syft-[not provided] | ||
Created: 2022-04-13T16:38:03Z | ||
|
||
##### Package: @at-sign | ||
|
||
PackageName: @at-sign | ||
SPDXID: SPDXRef-Package---at-sign-739e4f0d93fb8298 | ||
PackageDownloadLocation: NOASSERTION | ||
FilesAnalyzed: false | ||
PackageLicenseConcluded: NONE | ||
PackageLicenseDeclared: NONE | ||
PackageCopyrightText: NOASSERTION | ||
|
||
##### Package: some/slashes | ||
|
||
PackageName: some/slashes | ||
SPDXID: SPDXRef-Package--some-slashes-26db06648b24bff9 | ||
PackageDownloadLocation: NOASSERTION | ||
FilesAnalyzed: false | ||
PackageLicenseConcluded: NONE | ||
PackageLicenseDeclared: NONE | ||
PackageCopyrightText: NOASSERTION | ||
|
||
##### Package: under_scores | ||
|
||
PackageName: under_scores | ||
SPDXID: SPDXRef-Package--under-scores-250cbfefcdea318b | ||
PackageDownloadLocation: NOASSERTION | ||
FilesAnalyzed: false | ||
PackageLicenseConcluded: NONE | ||
PackageLicenseDeclared: NONE | ||
PackageCopyrightText: NOASSERTION | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters