Skip to content

Commit

Permalink
chore: pick more detailed PE product or file versions
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow committed Nov 29, 2023
1 parent 495575d commit fe83ea8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 12 additions & 7 deletions syft/pkg/cataloger/dotnet/parse_dotnet_portable_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ func findVersion(versionResources map[string]string) string {
return fileVersion
}

if containsNumber(productVersion) && containsDot(productVersion) {
productVersionDetail := punctuationCount(productVersion)
fileVersionDetail := punctuationCount(fileVersion)

if containsNumber(productVersion) && productVersionDetail >= fileVersionDetail {
return productVersion
}

if containsNumber(fileVersion) && containsDot(fileVersion) {
if containsNumber(fileVersion) && fileVersionDetail > 0 {
return fileVersion
}

Expand All @@ -154,17 +157,19 @@ func findVersion(versionResources map[string]string) string {
return productVersion
}

func containsNumber(out string) bool {
return strings.ContainsAny(out, "1234567890")
func containsNumber(s string) bool {
return numberRegex.MatchString(s)
}

func containsDot(out string) bool {
return strings.ContainsRune(out, '.')
func punctuationCount(s string) int {
return len(versionPunctuationRegex.FindAllString(s, -1))
}

var (
// spaceRegex includes nbsp (#160) considered to be a space character
spaceRegex = regexp.MustCompile(`[\s\xa0]+`)
spaceRegex = regexp.MustCompile(`[\s\xa0]+`)
numberRegex = regexp.MustCompile(`\d`)
versionPunctuationRegex = regexp.MustCompile(`[.,]+`)
)

func findName(versionResources map[string]string) string {
Expand Down
24 changes: 24 additions & 0 deletions syft/pkg/cataloger/dotnet/parse_dotnet_portable_executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,30 @@ func TestParseDotnetPortableExecutable(t *testing.T) {
Version: "80.1.7.92",
},
},
{
name: "Better product version",
versionResources: map[string]string{
"FileDescription": "Better version",
"FileVersion": "80.1.7",
"ProductVersion": "80.1.7.92",
},
expectedPackage: pkg.Package{
Name: "Better version",
Version: "80.1.7.92",
},
},
{
name: "Better file version",
versionResources: map[string]string{
"FileDescription": "Better version",
"FileVersion": "80.1.7.92",
"ProductVersion": "80.1.7",
},
expectedPackage: pkg.Package{
Name: "Better version",
Version: "80.1.7.92",
},
},
}

for _, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/catalog_packages_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var imageOnlyTestCases = []testCase{
pkgType: pkg.DotnetPkg,
pkgLanguage: pkg.Dotnet,
pkgInfo: map[string]string{
"DocuSign.eSign": "6.8.0",
"DocuSign.eSign": "6.8.0.0",
},
},
}
Expand Down

0 comments on commit fe83ea8

Please sign in to comment.