From ec7ecbacab2419b169b967135d7fd3060a71a968 Mon Sep 17 00:00:00 2001 From: Michael Vidrevich Date: Sun, 11 Oct 2026 15:20:22 +0300 Subject: [PATCH 1/4] XRAY-161169 - Normalization --- utils/catalog/contextualpaths.go | 58 ++++++++++++++++++++++++++- utils/catalog/contextualpaths_test.go | 22 +++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/utils/catalog/contextualpaths.go b/utils/catalog/contextualpaths.go index afecc147d..16f83cda3 100644 --- a/utils/catalog/contextualpaths.go +++ b/utils/catalog/contextualpaths.go @@ -2,15 +2,25 @@ package catalog import ( "fmt" + "strings" "github.com/CycloneDX/cyclonedx-go" "github.com/jfrog/jfrog-cli-core/v2/utils/config" catalogServices "github.com/jfrog/jfrog-client-go/catalog/services" + xrayutils "github.com/jfrog/jfrog-client-go/xray/services/utils" "github.com/jfrog/jfrog-cli-security/utils/formats/cdxutils" ) +const ( + catalogNamespacePublic = "public" + catalogTypeGo = "go" + purlTypeGolang = "golang" + purlTypeNPM = "npm" + purlTypeMaven = "maven" +) + // GetIndirectCvePaths asks Catalog for the dependency-chain path(s) that make each of the given indirect CVEs // reachable, given the packages resolved in the BOM. func GetIndirectCvePaths(serverDetails *config.ServerDetails, projectKey string, indirectCves []string, bom *cyclonedx.BOM) (map[string]catalogServices.IndirectContextualResponse, error) { @@ -21,5 +31,51 @@ func GetIndirectCvePaths(serverDetails *config.ServerDetails, projectKey string, if err != nil { return nil, fmt.Errorf("failed to create catalog service manager: %w", err) } - return catalogManager.GetContextualPaths(indirectCves, cdxutils.ExtractPackageVersionKeys(bom)) + packages := toCatalogPackageVersionKeys(cdxutils.ExtractPackageVersionKeys(bom)) + return catalogManager.GetContextualPaths(indirectCves, packages) +} + +// toCatalogPackageVersionKeys rewrites SBOM purl coordinates into the package identity Catalog stores. +// Catalog keeps namespace "public" and folds the purl namespace into the name. +func toCatalogPackageVersionKeys(packages []xrayutils.PackageVersionKey) []xrayutils.PackageVersionKey { + if len(packages) == 0 { + return packages + } + normalized := make([]xrayutils.PackageVersionKey, len(packages)) + for i, pkg := range packages { + normalized[i] = toCatalogPackageVersionKey(pkg) + } + return normalized +} + +func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.PackageVersionKey { + name := pkg.Name + version := pkg.Version + pkgType := pkg.Type + switch pkg.Type { + case purlTypeGolang: + pkgType = catalogTypeGo + name = joinPackageName(pkg.Namespace, pkg.Name, "/") + if version != "" && !strings.HasPrefix(version, "v") { + version = "v" + version + } + case purlTypeNPM: + name = joinPackageName(pkg.Namespace, pkg.Name, "/") + case purlTypeMaven: + name = joinPackageName(pkg.Namespace, pkg.Name, ":") + } + return xrayutils.PackageVersionKey{ + Type: pkgType, + Name: name, + Namespace: catalogNamespacePublic, + Version: version, + Ecosystem: xrayutils.GenericEcosystem, + } +} + +func joinPackageName(namespace, name, separator string) string { + if namespace == "" { + return name + } + return namespace + separator + name } diff --git a/utils/catalog/contextualpaths_test.go b/utils/catalog/contextualpaths_test.go index 046c6546d..7d1590272 100644 --- a/utils/catalog/contextualpaths_test.go +++ b/utils/catalog/contextualpaths_test.go @@ -80,6 +80,26 @@ func TestGetIndirectCvePaths_ServerReturnsMalformedJson_ReturnsError(t *testing. assert.Nil(t, result) } +func TestToCatalogPackageVersionKeys(t *testing.T) { + got := toCatalogPackageVersionKeys([]xrayutils.PackageVersionKey{ + {Type: "golang", Namespace: "golang.org", Name: "x/sys", Version: "0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "golang", Namespace: "github.com/jfrog", Name: "core", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "npm", Namespace: "@protobufjs", Name: "aspromise", Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "npm", Name: "lodash", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "pypi", Name: "requests", Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "maven", Namespace: "org.apache.logging.log4j", Name: "log4j-core", Version: "2.14.1", Ecosystem: xrayutils.GenericEcosystem}, + }) + + assert.Equal(t, []xrayutils.PackageVersionKey{ + {Type: "go", Name: "golang.org/x/sys", Namespace: "public", Version: "v0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "go", Name: "github.com/jfrog/core", Namespace: "public", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "npm", Name: "@protobufjs/aspromise", Namespace: "public", Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "npm", Name: "lodash", Namespace: "public", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "pypi", Name: "requests", Namespace: "public", Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "maven", Name: "org.apache.logging.log4j:log4j-core", Namespace: "public", Version: "2.14.1", Ecosystem: xrayutils.GenericEcosystem}, + }, got) +} + func TestGetIndirectCvePaths_Success_SendsExpectedRequestAndParsesResponse(t *testing.T) { var gotBody struct { Cves []string `json:"cves"` @@ -104,7 +124,7 @@ func TestGetIndirectCvePaths_Success_SendsExpectedRequestAndParsesResponse(t *te require.NoError(t, err) assert.Equal(t, []string{"CVE-2024-1234"}, gotBody.Cves) - assert.Equal(t, []xrayutils.PackageVersionKey{{Type: "npm", Name: "lodash", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}}, gotBody.Packages) + assert.Equal(t, []xrayutils.PackageVersionKey{{Type: "npm", Name: "lodash", Namespace: "public", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}}, gotBody.Packages) assert.Equal(t, "projectKey=myproj", gotQuery) require.Contains(t, result, "CVE-2024-1234") assert.Equal(t, "lodash", result["CVE-2024-1234"].Name) From 4cf16e9041f414234b74732708d42f8456b8d267 Mon Sep 17 00:00:00 2001 From: Michael Vidrevich Date: Mon, 12 Oct 2026 12:25:16 +0300 Subject: [PATCH 2/4] XRAY-161169 - Send golang instead of go --- utils/catalog/contextualpaths.go | 3 +-- utils/catalog/contextualpaths_test.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/utils/catalog/contextualpaths.go b/utils/catalog/contextualpaths.go index 16f83cda3..786b7c01f 100644 --- a/utils/catalog/contextualpaths.go +++ b/utils/catalog/contextualpaths.go @@ -15,7 +15,6 @@ import ( const ( catalogNamespacePublic = "public" - catalogTypeGo = "go" purlTypeGolang = "golang" purlTypeNPM = "npm" purlTypeMaven = "maven" @@ -54,7 +53,7 @@ func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.Packa pkgType := pkg.Type switch pkg.Type { case purlTypeGolang: - pkgType = catalogTypeGo + // Contextual analysis keys Go packages by purl type "golang", not the stored catalog type "go". name = joinPackageName(pkg.Namespace, pkg.Name, "/") if version != "" && !strings.HasPrefix(version, "v") { version = "v" + version diff --git a/utils/catalog/contextualpaths_test.go b/utils/catalog/contextualpaths_test.go index 7d1590272..d84cb4862 100644 --- a/utils/catalog/contextualpaths_test.go +++ b/utils/catalog/contextualpaths_test.go @@ -91,8 +91,8 @@ func TestToCatalogPackageVersionKeys(t *testing.T) { }) assert.Equal(t, []xrayutils.PackageVersionKey{ - {Type: "go", Name: "golang.org/x/sys", Namespace: "public", Version: "v0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "go", Name: "github.com/jfrog/core", Namespace: "public", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "golang", Name: "golang.org/x/sys", Namespace: "public", Version: "v0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, + {Type: "golang", Name: "github.com/jfrog/core", Namespace: "public", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, {Type: "npm", Name: "@protobufjs/aspromise", Namespace: "public", Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem}, {Type: "npm", Name: "lodash", Namespace: "public", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}, {Type: "pypi", Name: "requests", Namespace: "public", Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem}, From 72f5142d101777636b22684e2b972e1c38d2ed4e Mon Sep 17 00:00:00 2001 From: Michael Vidrevich Date: Wed, 14 Oct 2026 15:03:10 +0300 Subject: [PATCH 3/4] XRAY-161169 - Code review --- utils/catalog/contextualpaths.go | 41 +++++++---- utils/catalog/contextualpaths_test.go | 102 +++++++++++++++++++++----- 2 files changed, 109 insertions(+), 34 deletions(-) diff --git a/utils/catalog/contextualpaths.go b/utils/catalog/contextualpaths.go index 786b7c01f..986f7588f 100644 --- a/utils/catalog/contextualpaths.go +++ b/utils/catalog/contextualpaths.go @@ -14,10 +14,10 @@ import ( ) const ( - catalogNamespacePublic = "public" - purlTypeGolang = "golang" - purlTypeNPM = "npm" - purlTypeMaven = "maven" + catalogNamespacePublic = "public" + purlTypeGolang = "golang" + purlTypeMaven = "maven" + defaultPackageNameSeparator = "/" ) // GetIndirectCvePaths asks Catalog for the dependency-chain path(s) that make each of the given indirect CVEs @@ -47,31 +47,40 @@ func toCatalogPackageVersionKeys(packages []xrayutils.PackageVersionKey) []xrayu return normalized } +// toCatalogPackageVersionKey folds a non-empty purl namespace into the name for every package type (Maven uses +// its own "groupId:artifactId" separator, everything else uses "/"), since Catalog forces namespace to "public" +// below and would otherwise silently lose it. Namespace folding isn't limited to the ecosystems Catalog currently +// enables for transitive analysis (npm/maven/golang/pypi/nuget): keeping it unconditional means a newly enabled, +// namespaced ecosystem (e.g. Swift, CocoaPods) won't silently collide same-named packages from different +// namespaces the day Catalog starts processing it. func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.PackageVersionKey { - name := pkg.Name + separator := defaultPackageNameSeparator + if pkg.Type == purlTypeMaven { + separator = ":" + } version := pkg.Version - pkgType := pkg.Type - switch pkg.Type { - case purlTypeGolang: - // Contextual analysis keys Go packages by purl type "golang", not the stored catalog type "go". - name = joinPackageName(pkg.Namespace, pkg.Name, "/") + if pkg.Type == purlTypeGolang { + // Catalog's Go index is built from go.sum/proxy data, which always carries the "v" prefix (v1.2.3, + // pseudo-versions like v0.0.0-...). This repo's own purls strip it (matching Xray's convention), so + // it has to be restored here before querying Catalog. if version != "" && !strings.HasPrefix(version, "v") { version = "v" + version } - case purlTypeNPM: - name = joinPackageName(pkg.Namespace, pkg.Name, "/") - case purlTypeMaven: - name = joinPackageName(pkg.Namespace, pkg.Name, ":") } return xrayutils.PackageVersionKey{ - Type: pkgType, - Name: name, + Type: pkg.Type, + Name: joinPackageName(pkg.Namespace, pkg.Name, separator), Namespace: catalogNamespacePublic, Version: version, Ecosystem: xrayutils.GenericEcosystem, } } +// joinPackageName mirrors the default "/" join techutils.SplitPackageURL applies when turning a purl back into +// a display name, but isn't a drop-in for it: this works on the already-split PackageVersionKey produced by +// cdxutils.ExtractPackageVersionKeys (whose own test expects namespace and name kept apart, not rejoined), and +// the separator is a Catalog request rule, not purl syntax - Maven needs ":" because ToPackageUrl replaces +// Maven's "group:artifact" colon with "/" when building the purl in the first place. func joinPackageName(namespace, name, separator string) string { if namespace == "" { return name diff --git a/utils/catalog/contextualpaths_test.go b/utils/catalog/contextualpaths_test.go index d84cb4862..2965c5013 100644 --- a/utils/catalog/contextualpaths_test.go +++ b/utils/catalog/contextualpaths_test.go @@ -8,6 +8,7 @@ import ( "github.com/CycloneDX/cyclonedx-go" "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/jfrog/jfrog-cli-security/utils/formats/cdxutils" catalogServices "github.com/jfrog/jfrog-client-go/catalog/services" xrayutils "github.com/jfrog/jfrog-client-go/xray/services/utils" "github.com/stretchr/testify/assert" @@ -80,24 +81,89 @@ func TestGetIndirectCvePaths_ServerReturnsMalformedJson_ReturnsError(t *testing. assert.Nil(t, result) } -func TestToCatalogPackageVersionKeys(t *testing.T) { - got := toCatalogPackageVersionKeys([]xrayutils.PackageVersionKey{ - {Type: "golang", Namespace: "golang.org", Name: "x/sys", Version: "0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "golang", Namespace: "github.com/jfrog", Name: "core", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "npm", Namespace: "@protobufjs", Name: "aspromise", Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "npm", Name: "lodash", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "pypi", Name: "requests", Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "maven", Namespace: "org.apache.logging.log4j", Name: "log4j-core", Version: "2.14.1", Ecosystem: xrayutils.GenericEcosystem}, - }) - - assert.Equal(t, []xrayutils.PackageVersionKey{ - {Type: "golang", Name: "golang.org/x/sys", Namespace: "public", Version: "v0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "golang", Name: "github.com/jfrog/core", Namespace: "public", Version: "v1.2.3", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "npm", Name: "@protobufjs/aspromise", Namespace: "public", Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "npm", Name: "lodash", Namespace: "public", Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "pypi", Name: "requests", Namespace: "public", Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem}, - {Type: "maven", Name: "org.apache.logging.log4j:log4j-core", Namespace: "public", Version: "2.14.1", Ecosystem: xrayutils.GenericEcosystem}, - }, got) +// TestToCatalogPackageVersionKeys_FromRealPurls drives each case through a real purl string via +// cdxutils.ExtractPackageVersionKeys, the same path GetIndirectCvePaths uses, rather than hand-building +// PackageVersionKey values. A hand-built Namespace/Name split can't catch a wrong or missing fold: joinPackageName +// is a plain concatenation, so any split of the same full string reassembles to the same result (e.g. splitting +// "golang.org/x/sys" as namespace "golang.org"/name "x/sys", or as namespace "golang.org/x"/name "sys" - which is +// what the real purl library actually returns - both rejoin to the identical string). Only exercising the real +// extractor proves the split/fold pipeline is correct end-to-end. +func TestToCatalogPackageVersionKeys_FromRealPurls(t *testing.T) { + testCases := []struct { + name string + purl string + expected xrayutils.PackageVersionKey + }{ + { + name: "namespaced type outside the switch keeps its namespace folded in", + purl: "pkg:cocoapods/AppAuth/Core@1.7.5", + expected: xrayutils.PackageVersionKey{ + Type: "cocoapods", Name: "AppAuth/Core", Namespace: catalogNamespacePublic, Version: "1.7.5", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "go v2+ module path is reconstructed regardless of where the real purl splits namespace/name", + purl: "pkg:golang/github.com/foo/bar/v2@2.3.4", + expected: xrayutils.PackageVersionKey{ + Type: "golang", Name: "github.com/foo/bar/v2", Namespace: catalogNamespacePublic, Version: "v2.3.4", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "go pseudo-version that already has v is not double-prefixed", + purl: "pkg:golang/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42", + expected: xrayutils.PackageVersionKey{ + Type: "golang", Name: "golang.org/x/sys", Namespace: catalogNamespacePublic, Version: "v0.0.0-20200116001909-b77594299b42", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "go +incompatible version round-trips untouched", + purl: "pkg:golang/github.com/old/pkg@v3.0.0+incompatible", + expected: xrayutils.PackageVersionKey{ + Type: "golang", Name: "github.com/old/pkg", Namespace: catalogNamespacePublic, Version: "v3.0.0+incompatible", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "scoped npm package decodes the percent-encoded @ and keeps its single-segment namespace", + purl: "pkg:npm/%40protobufjs/aspromise@1.1.2", + expected: xrayutils.PackageVersionKey{ + Type: "npm", Name: "@protobufjs/aspromise", Namespace: catalogNamespacePublic, Version: "1.1.2", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "unscoped npm package has no namespace to fold", + purl: "pkg:npm/lodash@4.17.21", + expected: xrayutils.PackageVersionKey{ + Type: "npm", Name: "lodash", Namespace: catalogNamespacePublic, Version: "4.17.21", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "pypi package has no namespace to fold", + purl: "pkg:pypi/requests@2.28.0", + expected: xrayutils.PackageVersionKey{ + Type: "pypi", Name: "requests", Namespace: catalogNamespacePublic, Version: "2.28.0", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + { + name: "maven groupId:artifactId is rejoined with : instead of the purl's /", + purl: "pkg:maven/org.apache.logging.log4j/log4j-core@2.14.1", + expected: xrayutils.PackageVersionKey{ + Type: "maven", Name: "org.apache.logging.log4j:log4j-core", Namespace: catalogNamespacePublic, Version: "2.14.1", Ecosystem: xrayutils.GenericEcosystem, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + bom := &cyclonedx.BOM{Components: &[]cyclonedx.Component{{PackageURL: tc.purl}}} + extracted := cdxutils.ExtractPackageVersionKeys(bom) + require.Len(t, extracted, 1) + + got := toCatalogPackageVersionKeys(extracted) + + require.Len(t, got, 1) + assert.Equal(t, tc.expected, got[0]) + }) + } } func TestGetIndirectCvePaths_Success_SendsExpectedRequestAndParsesResponse(t *testing.T) { From 443e25b79ceeb473b8f9caee25eb2af98c908245 Mon Sep 17 00:00:00 2001 From: Michael Vidrevich Date: Wed, 14 Oct 2026 17:47:31 +0300 Subject: [PATCH 4/4] XRAY-161169 - Code review --- utils/catalog/contextualpaths.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/utils/catalog/contextualpaths.go b/utils/catalog/contextualpaths.go index 986f7588f..b4ed3506b 100644 --- a/utils/catalog/contextualpaths.go +++ b/utils/catalog/contextualpaths.go @@ -34,8 +34,6 @@ func GetIndirectCvePaths(serverDetails *config.ServerDetails, projectKey string, return catalogManager.GetContextualPaths(indirectCves, packages) } -// toCatalogPackageVersionKeys rewrites SBOM purl coordinates into the package identity Catalog stores. -// Catalog keeps namespace "public" and folds the purl namespace into the name. func toCatalogPackageVersionKeys(packages []xrayutils.PackageVersionKey) []xrayutils.PackageVersionKey { if len(packages) == 0 { return packages @@ -49,10 +47,7 @@ func toCatalogPackageVersionKeys(packages []xrayutils.PackageVersionKey) []xrayu // toCatalogPackageVersionKey folds a non-empty purl namespace into the name for every package type (Maven uses // its own "groupId:artifactId" separator, everything else uses "/"), since Catalog forces namespace to "public" -// below and would otherwise silently lose it. Namespace folding isn't limited to the ecosystems Catalog currently -// enables for transitive analysis (npm/maven/golang/pypi/nuget): keeping it unconditional means a newly enabled, -// namespaced ecosystem (e.g. Swift, CocoaPods) won't silently collide same-named packages from different -// namespaces the day Catalog starts processing it. +// below and would otherwise silently lose it. func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.PackageVersionKey { separator := defaultPackageNameSeparator if pkg.Type == purlTypeMaven { @@ -61,8 +56,7 @@ func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.Packa version := pkg.Version if pkg.Type == purlTypeGolang { // Catalog's Go index is built from go.sum/proxy data, which always carries the "v" prefix (v1.2.3, - // pseudo-versions like v0.0.0-...). This repo's own purls strip it (matching Xray's convention), so - // it has to be restored here before querying Catalog. + // pseudo-versions like v0.0.0-...). if version != "" && !strings.HasPrefix(version, "v") { version = "v" + version } @@ -76,11 +70,6 @@ func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.Packa } } -// joinPackageName mirrors the default "/" join techutils.SplitPackageURL applies when turning a purl back into -// a display name, but isn't a drop-in for it: this works on the already-split PackageVersionKey produced by -// cdxutils.ExtractPackageVersionKeys (whose own test expects namespace and name kept apart, not rejoined), and -// the separator is a Catalog request rule, not purl syntax - Maven needs ":" because ToPackageUrl replaces -// Maven's "group:artifact" colon with "/" when building the purl in the first place. func joinPackageName(namespace, name, separator string) string { if namespace == "" { return name