Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion utils/catalog/contextualpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ 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"
purlTypeGolang = "golang"
purlTypeMaven = "maven"
defaultPackageNameSeparator = "/"
)

// 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) {
Expand All @@ -21,5 +30,49 @@ 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)
}

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
}

// 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.
func toCatalogPackageVersionKey(pkg xrayutils.PackageVersionKey) xrayutils.PackageVersionKey {
separator := defaultPackageNameSeparator
if pkg.Type == purlTypeMaven {
separator = ":"
}
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-...).
if version != "" && !strings.HasPrefix(version, "v") {
version = "v" + version
}
}
return xrayutils.PackageVersionKey{
Type: pkg.Type,
Name: joinPackageName(pkg.Namespace, pkg.Name, separator),
Namespace: catalogNamespacePublic,
Version: version,
Ecosystem: xrayutils.GenericEcosystem,
}
}

func joinPackageName(namespace, name, separator string) string {
Comment thread
VidrevichMichael marked this conversation as resolved.
Resolved
if namespace == "" {
return name
}
return namespace + separator + name
}
88 changes: 87 additions & 1 deletion utils/catalog/contextualpaths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -80,6 +81,91 @@ func TestGetIndirectCvePaths_ServerReturnsMalformedJson_ReturnsError(t *testing.
assert.Nil(t, result)
}

// 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) {
var gotBody struct {
Cves []string `json:"cves"`
Expand All @@ -104,7 +190,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)
Expand Down
Loading