Skip to content

Commit 94de854

Browse files
committed
fix Ref and PURL calc
1 parent 47b85d9 commit 94de854

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

policy/enforcer/policyenforcer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ func getScaViolationType(violation services.XrayViolation) violationutils.Violat
202202
}
203203

204204
func locateBomComponentInfo(cmdResults *results.SecurityCommandResults, impactedComponentXrayId string, violation services.XrayViolation) (impactedComponent *cyclonedx.Component, directComponents []formats.ComponentRow, impactPaths [][]formats.ComponentRow) {
205+
ref := techutils.XrayComponentIdToCdxComponentRef(impactedComponentXrayId)
205206
for _, target := range cmdResults.Targets {
206207
if target.ScaResults == nil || target.ScaResults.Sbom == nil || target.ScaResults.Sbom.Components == nil {
207208
continue
208209
}
209-
ref := techutils.XrayComponentIdToCdxComponentRef(impactedComponentXrayId)
210210
for _, component := range *target.ScaResults.Sbom.Components {
211211
if strings.HasPrefix(component.BOMRef, ref) {
212212
// Found the relevant component

utils/techutils/techutils.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package techutils
33
import (
44
"errors"
55
"fmt"
6-
"net/url"
76
"os"
87
"path/filepath"
98
"regexp"
@@ -899,26 +898,20 @@ func ToPackageUrl(compName, version, packageType string, properties ...packageur
899898
if packageType == "" {
900899
packageType = "generic"
901900
}
902-
purl := packageurl.NewPackageURL(packageType, "", compName, version, properties, "").String()
903-
// Unescape the output
904-
output, err := url.QueryUnescape(purl)
905-
if err != nil {
906-
log.Debug(fmt.Sprintf("Failed to unescape package URL: %s", err))
907-
// Return the original output
908-
return purl
901+
// Check if compName contains a namespace
902+
namespace := ""
903+
if lastIndex := strings.LastIndex(compName, "/"); lastIndex != -1 {
904+
namespace = compName[:lastIndex]
905+
compName = compName[lastIndex+1:]
909906
}
910-
return
907+
return packageurl.NewPackageURL(packageType, namespace, compName, version, properties, "").String()
911908
}
912909

913910
func ToPackageRef(compName, version, packageType string) (output string) {
914911
if packageType == "" {
915912
packageType = "generic"
916913
}
917-
output = fmt.Sprintf("pkg:%s/%s", packageType, strings.ReplaceAll(compName, ":", "/"))
918-
if version != "" {
919-
output += fmt.Sprintf("@%s", version)
920-
}
921-
return output
914+
return ToPackageUrl(compName, version, packageType)
922915
}
923916

924917
// Extract the component name, version and type from PackageUrl and translate it to an Xray component id

0 commit comments

Comments
 (0)