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
13 changes: 13 additions & 0 deletions commands/scan/buildscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions commands/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
11 changes: 8 additions & 3 deletions utils/xray/scangraph/scangraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}
Expand Down
75 changes: 69 additions & 6 deletions utils/xray/scangraph/scangraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand Down
Loading