diff --git a/commands/scan/buildscan_test.go b/commands/scan/buildscan_test.go index 0be13e94c..31aece15f 100644 --- a/commands/scan/buildscan_test.go +++ b/commands/scan/buildscan_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jfrog/jfrog-cli-core/v2/utils/config" + "github.com/jfrog/jfrog-client-go/xray/services" "github.com/stretchr/testify/assert" ) @@ -59,3 +60,15 @@ func TestGetActualUrl(t *testing.T) { }) } } + +func TestBinaryGraphParamsKeepXscIds(t *testing.T) { + cmd := &ScanCommand{ + xrayVersion: "3.120.0", + xscVersion: "1.16.0", + multiScanId: "msi-from-analytics", + } + params := cmd.getXrayScanGraphParams().XrayGraphScanParams() + assert.Equal(t, services.Binary, params.ScanType) + assert.Equal(t, "1.16.0", params.XscVersion) + assert.Equal(t, "msi-from-analytics", params.MultiScanId) +} diff --git a/commands/scan/scan.go b/commands/scan/scan.go index 04899e1f9..01c63fb9f 100644 --- a/commands/scan/scan.go +++ b/commands/scan/scan.go @@ -586,6 +586,8 @@ func (scanCmd *ScanCommand) getXrayScanGraphParams() *scangraph.ScanGraphParams IncludeVulnerabilities: scanCmd.resultsContext.IncludeVulnerabilities, ProjectKey: scanCmd.resultsContext.ProjectKey, ScanType: services.Binary, + MultiScanId: scanCmd.multiScanId, + XscVersion: scanCmd.xscVersion, XrayVersion: scanCmd.xrayVersion, } return scangraph.NewScanGraphParams(). diff --git a/utils/xray/scangraph/scangraph.go b/utils/xray/scangraph/scangraph.go index 49aae8543..8b962535d 100644 --- a/utils/xray/scangraph/scangraph.go +++ b/utils/xray/scangraph/scangraph.go @@ -15,19 +15,24 @@ const ( CocoapodsScanMinXrayVersion = "3.103.3" ) -// Binary scans stay on Xray api/v1/scan/graph; XSC is only used for dependency scans with analytics ids. +// XSC sca/scan/graph is used when analytics ids are present. +// Binary leaf graphs drop those ids first, so they stay on Xray api/v1/scan/graph. func useXscGraphScan(params *services.XrayGraphScanParams) bool { - if params == nil || params.ScanType == services.Binary { + if params == nil { return false } return params.XscVersion != "" && params.MultiScanId != "" } -// XSC sca/scan/graph requires "nodes"; binary leaf graphs omit it, so drop XSC ids and stay on Xray. +// XSC sca/scan/graph requires "nodes". Binary graphs that have child nodes keep the analytics ids +// so Xray can link the scan to its meta scan. Leaf graphs omit "nodes", so drop the ids and stay on Xray. func disableXscForBinaryScan(params *services.XrayGraphScanParams) { if params == nil || params.ScanType != services.Binary { return } + if params.BinaryGraph != nil && len(params.BinaryGraph.Nodes) > 0 { + return + } params.XscVersion = "" params.MultiScanId = "" } diff --git a/utils/xray/scangraph/scangraph_test.go b/utils/xray/scangraph/scangraph_test.go index 678eee5eb..df6324aa5 100644 --- a/utils/xray/scangraph/scangraph_test.go +++ b/utils/xray/scangraph/scangraph_test.go @@ -32,13 +32,13 @@ func TestUseXscGraphScan(t *testing.T) { want: true, }, { - name: "binary never uses XSC even with analytics ids", + name: "binary with analytics ids uses XSC", params: &services.XrayGraphScanParams{ ScanType: services.Binary, XscVersion: "1.16.0", MultiScanId: "msi", }, - want: false, + want: true, }, { name: "dependency without multi scan id stays on Xray", @@ -57,10 +57,28 @@ func TestUseXscGraphScan(t *testing.T) { } func TestDisableXscForBinaryScan(t *testing.T) { - binary := &services.XrayGraphScanParams{ScanType: services.Binary, XscVersion: "1.16.0", MultiScanId: "msi"} - disableXscForBinaryScan(binary) - assert.Empty(t, binary.XscVersion) - assert.Empty(t, binary.MultiScanId) + leaf := &services.XrayGraphScanParams{ + ScanType: services.Binary, + XscVersion: "1.16.0", + MultiScanId: "msi", + BinaryGraph: &xrayUtils.BinaryGraphNode{Id: "npm://left-pad:1.3.0"}, + } + disableXscForBinaryScan(leaf) + assert.Empty(t, leaf.XscVersion) + assert.Empty(t, leaf.MultiScanId) + + withNodes := &services.XrayGraphScanParams{ + ScanType: services.Binary, + XscVersion: "1.16.0", + MultiScanId: "msi", + BinaryGraph: &xrayUtils.BinaryGraphNode{ + Id: "docker://xmas:secrets", + Nodes: []*xrayUtils.BinaryGraphNode{{Id: "deb://ubuntu:openssl:1.1.1"}}, + }, + } + disableXscForBinaryScan(withNodes) + assert.Equal(t, "1.16.0", withNodes.XscVersion) + assert.Equal(t, "msi", withNodes.MultiScanId) dep := &services.XrayGraphScanParams{ScanType: services.Dependency, XscVersion: "1.16.0", MultiScanId: "msi"} disableXscForBinaryScan(dep) @@ -101,9 +119,54 @@ func TestBinaryScanWithEmptyGraphTargetsXrayEndpoint(t *testing.T) { assert.NoError(t, err) assert.Contains(t, postPath, "api/v1/scan/graph") assert.NotContains(t, postPath, "sca/scan/graph") + assert.NotContains(t, postPath, "multi_scan_id") assert.Contains(t, postBody, "npm://left-pad:1.3.0") } +func TestBinaryScanWithChildNodesTargetsXscEndpoint(t *testing.T) { + var postPath, postBody, getPath string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if r.Method == http.MethodPost { + postPath = r.URL.RequestURI() + body, err := io.ReadAll(r.Body) + assert.NoError(t, err) + postBody = string(body) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"scan_id":"scan-1"}`)) + return + } + getPath = r.URL.Path + _, _ = w.Write([]byte(`{}`)) + })) + defer server.Close() + + params := NewScanGraphParams(). + SetServerDetails(&config.ServerDetails{XrayUrl: server.URL + "/"}). + SetXrayGraphScanParams(&services.XrayGraphScanParams{ + ScanType: services.Binary, + XrayVersion: "3.120.0", + XscVersion: "1.16.0", + MultiScanId: "msi-from-analytics", + BinaryGraph: &xrayUtils.BinaryGraphNode{ + Id: "docker://xmas:secrets", + Nodes: []*xrayUtils.BinaryGraphNode{{Id: "deb://ubuntu:openssl:1.1.1"}}, + }, + }) + xrayManager, err := coreXray.CreateXrayServiceManager(params.ServerDetails()) + assert.NoError(t, err) + + _, err = RunScanGraphAndGetResults(params, xrayManager) + assert.NoError(t, err) + assert.Contains(t, postPath, "sca/scan/graph") + assert.Contains(t, postPath, "multi_scan_id=msi-from-analytics") + assert.Contains(t, postPath, "scan_type=binary") + assert.Contains(t, postBody, "docker://xmas:secrets") + assert.Contains(t, postBody, "deb://ubuntu:openssl:1.1.1") + assert.Contains(t, getPath, "sca/scan/graph") + assert.Equal(t, "msi-from-analytics", params.XrayGraphScanParams().MultiScanId) +} + func TestScanGraphParamsCloneDoesNotShareMutableGraph(t *testing.T) { original := NewScanGraphParams(). SetXrayGraphScanParams(&services.XrayGraphScanParams{