Skip to content

Commit ce9ce57

Browse files
committed
Malicious code scanner
1 parent 7e70c6b commit ce9ce57

File tree

8 files changed

+183
-135
lines changed

8 files changed

+183
-135
lines changed

commands/maliciousscan/maliciousscan.go

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ package maliciousscan
33
import (
44
"errors"
55
"fmt"
6-
"os/exec"
76
"path/filepath"
87
"strings"
98

10-
clientutils "github.com/jfrog/jfrog-client-go/utils"
11-
129
"github.com/jfrog/jfrog-cli-core/v2/common/format"
1310
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
1411
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
1512
"github.com/jfrog/jfrog-cli-security/jas"
16-
"github.com/jfrog/jfrog-cli-security/jas/runner"
13+
"github.com/jfrog/jfrog-cli-security/jas/maliciouscode"
1714
"github.com/jfrog/jfrog-cli-security/utils"
15+
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
1816
"github.com/jfrog/jfrog-cli-security/utils/results"
1917
"github.com/jfrog/jfrog-cli-security/utils/results/output"
2018
"github.com/jfrog/jfrog-cli-security/utils/severityutils"
@@ -81,17 +79,6 @@ func NewMaliciousScanCommand() *MaliciousScanCommand {
8179
}
8280

8381
func (cmd *MaliciousScanCommand) Run() (err error) {
84-
defer func() {
85-
if err != nil {
86-
var e *exec.ExitError
87-
if errors.As(err, &e) {
88-
if e.ExitCode() != coreutils.ExitCodeVulnerableBuild.Code {
89-
err = errors.New("Malicious scan command failed. " + err.Error())
90-
}
91-
}
92-
}
93-
}()
94-
9582
xrayVersion, entitledForJas, workingDirs, err := cmd.validateAndPrepare()
9683
if err != nil {
9784
return err
@@ -109,12 +96,6 @@ func (cmd *MaliciousScanCommand) Run() (err error) {
10996
return err
11097
}
11198

112-
if cmd.progress != nil {
113-
if err = cmd.progress.Quit(); err != nil {
114-
return err
115-
}
116-
}
117-
11899
return cmd.outputResults(cmdResults)
119100
}
120101

@@ -199,52 +180,41 @@ func (cmd *MaliciousScanCommand) setAnalyzerManagerPath(scanner *jas.JasScanner)
199180
}
200181
} else {
201182
scanner.AnalyzerManager.AnalyzerManagerFullPath = cmd.customAnalyzerManagerPath
202-
log.Debug(clientutils.GetLogMsgPrefix(0, false) + "using custom analyzer manager binary path")
183+
log.Debug("using custom analyzer manager binary path")
203184
}
204185
return nil
205186
}
206187

207188
func (cmd *MaliciousScanCommand) runMaliciousScans(cmdResults *results.SecurityCommandResults, scanner *jas.JasScanner) error {
208189
jasScanProducerConsumer := utils.NewSecurityParallelRunner(cmd.threads)
209-
210-
serverDetails, err := cmd.ServerDetails()
211-
if err != nil {
212-
return err
213-
}
214-
215190
jasScanProducerConsumer.JasWg.Add(1)
216191
createMaliciousScansTask := func(threadId int) (generalError error) {
217192
defer func() {
218193
jasScanProducerConsumer.JasWg.Done()
219194
}()
220195
for _, targetResult := range cmdResults.Targets {
221-
if targetResult.AppsConfigModule == nil {
222-
_ = targetResult.AddTargetError(fmt.Errorf("can't find module for path %s", targetResult.Target), false)
223-
continue
224-
}
225-
appsConfigModule := *targetResult.AppsConfigModule
226-
jasParams := runner.JasRunnerParams{
227-
Runner: &jasScanProducerConsumer,
228-
ServerDetails: serverDetails,
229-
Scanner: scanner,
230-
Module: appsConfigModule,
231-
ScansToPerform: []utils.SubScanType{utils.MaliciousCodeScan},
232-
ScanResults: targetResult,
233-
TargetCount: len(cmdResults.Targets),
234-
}
235-
236-
if generalError = runner.AddJasScannersTasks(jasParams); generalError != nil {
237-
_ = targetResult.AddTargetError(fmt.Errorf("failed to add malicious scan task: %w", generalError), false)
238-
generalError = nil
196+
vulnerabilitiesResults, err := maliciouscode.RunMaliciousScan(
197+
scanner,
198+
maliciouscode.MaliciousScannerType,
199+
targetResult.Target,
200+
len(cmdResults.Targets),
201+
threadId,
202+
)
203+
jasScanProducerConsumer.ResultsMu.Lock()
204+
// Malicious code scans only return vulnerabilities, not violations
205+
targetResult.AddJasScanResults(jasutils.MaliciousCode, vulnerabilitiesResults, nil, jas.GetAnalyzerManagerExitCode(err))
206+
jasScanProducerConsumer.ResultsMu.Unlock()
207+
if err = jas.ParseAnalyzerManagerError(jasutils.MaliciousCode, err); err != nil {
208+
_ = targetResult.AddTargetError(fmt.Errorf("failed to run malicious scan: %w", err), false)
239209
}
240210
}
241211
return
242212
}
243213

244214
if _, addTaskErr := jasScanProducerConsumer.Runner.AddTaskWithError(createMaliciousScansTask, func(taskErr error) {
245-
cmdResults.AddGeneralError(fmt.Errorf("failed while adding JAS scan tasks: %s", taskErr.Error()), false)
215+
cmdResults.AddGeneralError(fmt.Errorf("failed while adding malicious scan tasks: %s", taskErr.Error()), false)
246216
}); addTaskErr != nil {
247-
return fmt.Errorf("failed to create JAS task: %w", addTaskErr)
217+
return fmt.Errorf("failed to create malicious scan task: %w", addTaskErr)
248218
}
249219

250220
jasScanProducerConsumer.Start()
@@ -295,16 +265,6 @@ func populateScanTargets(cmdResults *results.SecurityCommandResults, workingDirs
295265
return
296266
}
297267

298-
jfrogAppsConfig, err := jas.CreateJFrogAppsConfig(cmdResults.GetTargetsPaths())
299-
if err != nil {
300-
cmdResults.AddGeneralError(fmt.Errorf("failed to create JFrogAppsConfig: %w", err), false)
301-
return
302-
}
303-
304-
for _, targetResult := range cmdResults.Targets {
305-
targetResult.AppsConfigModule = jas.GetModule(targetResult.Target, jfrogAppsConfig)
306-
}
307-
308268
logScanTargetsInfo(cmdResults)
309269
}
310270

go.mod

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ require (
1111
github.com/gookit/color v1.6.0
1212
github.com/hashicorp/go-hclog v1.6.3
1313
github.com/hashicorp/go-plugin v1.6.3
14-
github.com/jfrog/build-info-go v1.12.5-0.20251209031413-f5f0e93dc8db
14+
github.com/jfrog/build-info-go v1.12.5-0.20251209171349-eb030db986f9
1515
github.com/jfrog/froggit-go v1.20.4
1616
github.com/jfrog/gofrog v1.7.6
1717
github.com/jfrog/jfrog-apps-config v1.0.1
18-
github.com/jfrog/jfrog-cli-artifactory v0.7.3-0.20251021143342-49bab7f38cec
18+
github.com/jfrog/jfrog-cli-artifactory v0.8.1-0.20251218044417-5113b260e416
1919
github.com/jfrog/jfrog-cli-core/v2 v2.60.1-0.20251125083543-e689762c4ff0
20-
github.com/jfrog/jfrog-client-go v1.55.1-0.20251216111654-54d67e0c03ac
20+
github.com/jfrog/jfrog-client-go v1.55.1-0.20251217134720-582451986897
2121
github.com/magiconair/properties v1.8.10
2222
github.com/owenrumney/go-sarif/v3 v3.2.3
2323
github.com/package-url/packageurl-go v0.1.3
2424
github.com/stretchr/testify v1.11.1
2525
github.com/urfave/cli v1.22.17
2626
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74
27-
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546
27+
golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39
2828
golang.org/x/sync v0.18.0
2929
golang.org/x/text v0.31.0
3030
gopkg.in/yaml.v3 v3.0.1
@@ -41,10 +41,16 @@ require (
4141
github.com/buger/jsonparser v1.1.1 // indirect
4242
github.com/c-bata/go-prompt v0.2.6 // indirect
4343
github.com/chzyer/readline v1.5.1 // indirect
44+
github.com/clipperhouse/stringish v0.1.1 // indirect
45+
github.com/clipperhouse/uax29/v2 v2.3.0 // indirect
4446
github.com/cloudflare/circl v1.6.1 // indirect
47+
github.com/containerd/stargz-snapshotter/estargz v0.18.1 // indirect
4548
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
46-
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
49+
github.com/cyphar/filepath-securejoin v0.6.0 // indirect
4750
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
51+
github.com/docker/cli v29.0.3+incompatible // indirect
52+
github.com/docker/distribution v2.8.3+incompatible // indirect
53+
github.com/docker/docker-credential-helpers v0.9.3 // indirect
4854
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
4955
github.com/emirpasic/gods v1.18.1 // indirect
5056
github.com/fatih/color v1.16.0 // indirect
@@ -59,30 +65,34 @@ require (
5965
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
6066
github.com/golang/protobuf v1.5.4 // indirect
6167
github.com/golang/snappy v1.0.0 // indirect
68+
github.com/google/go-containerregistry v0.20.7 // indirect
6269
github.com/google/go-github/v74 v74.0.0 // indirect
6370
github.com/google/go-querystring v1.1.0 // indirect
6471
github.com/grokify/mogo v0.64.12 // indirect
6572
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
6673
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
6774
github.com/hashicorp/yamux v0.1.1 // indirect
6875
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
69-
github.com/jedib0t/go-pretty/v6 v6.6.8 // indirect
76+
github.com/jedib0t/go-pretty/v6 v6.7.5 // indirect
7077
github.com/jfrog/archiver/v3 v3.6.1 // indirect
7178
github.com/kevinburke/ssh_config v1.2.0 // indirect
72-
github.com/klauspost/compress v1.18.0 // indirect
79+
github.com/klauspost/compress v1.18.1 // indirect
7380
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
7481
github.com/klauspost/pgzip v1.2.6 // indirect
7582
github.com/ktrysmt/go-bitbucket v0.9.80 // indirect
7683
github.com/manifoldco/promptui v0.9.0 // indirect
7784
github.com/mattn/go-colorable v0.1.14 // indirect
7885
github.com/mattn/go-isatty v0.0.20 // indirect
79-
github.com/mattn/go-runewidth v0.0.16 // indirect
80-
github.com/mattn/go-tty v0.0.3 // indirect
86+
github.com/mattn/go-runewidth v0.0.19 // indirect
87+
github.com/mattn/go-tty v0.0.7 // indirect
8188
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0 // indirect
8289
github.com/minio/sha256-simd v1.0.1 // indirect
90+
github.com/mitchellh/go-homedir v1.1.0 // indirect
8391
github.com/mitchellh/mapstructure v1.5.0 // indirect
8492
github.com/nwaples/rardecode v1.1.3 // indirect
8593
github.com/oklog/run v1.0.0 // indirect
94+
github.com/opencontainers/go-digest v1.0.0 // indirect
95+
github.com/opencontainers/image-spec v1.1.1 // indirect
8696
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
8797
github.com/pierrec/lz4/v4 v4.1.22 // indirect
8898
github.com/pjbgf/sha1cd v0.3.2 // indirect
@@ -91,16 +101,17 @@ require (
91101
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
92102
github.com/rivo/uniseg v0.4.7 // indirect
93103
github.com/russross/blackfriday/v2 v2.1.0 // indirect
94-
github.com/sagikazarmark/locafero v0.11.0 // indirect
104+
github.com/sagikazarmark/locafero v0.12.0 // indirect
95105
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
106+
github.com/sirupsen/logrus v1.9.3 // indirect
96107
github.com/skeema/knownhosts v1.3.1 // indirect
97-
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
98108
github.com/spf13/afero v1.15.0 // indirect
99109
github.com/spf13/cast v1.10.0 // indirect
100110
github.com/spf13/pflag v1.0.10 // indirect
101111
github.com/spf13/viper v1.21.0 // indirect
102112
github.com/subosito/gotenv v1.6.0 // indirect
103113
github.com/ulikunitz/xz v0.5.15 // indirect
114+
github.com/vbatts/tar-split v0.12.2 // indirect
104115
github.com/vbauerster/mpb/v8 v8.10.2 // indirect
105116
github.com/xanzy/go-gitlab v0.110.0 // indirect
106117
github.com/xanzy/ssh-agent v0.3.3 // indirect
@@ -111,20 +122,20 @@ require (
111122
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
112123
go.yaml.in/yaml/v3 v3.0.4 // indirect
113124
golang.org/x/crypto v0.45.0 // indirect
114-
golang.org/x/mod v0.29.0 // indirect
125+
golang.org/x/mod v0.30.0 // indirect
115126
golang.org/x/net v0.47.0 // indirect
116-
golang.org/x/oauth2 v0.31.0 // indirect
127+
golang.org/x/oauth2 v0.33.0 // indirect
117128
golang.org/x/sys v0.38.0 // indirect
118129
golang.org/x/term v0.37.0 // indirect
119130
golang.org/x/time v0.12.0 // indirect
120-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
121-
google.golang.org/grpc v1.67.3 // indirect
131+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
132+
google.golang.org/grpc v1.72.1 // indirect
122133
google.golang.org/protobuf v1.36.8 // indirect
123134
gopkg.in/ini.v1 v1.67.0 // indirect
124135
gopkg.in/warnings.v0 v0.1.2 // indirect
125136
)
126137

127-
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.55.1-0.20251216111654-54d67e0c03ac
138+
// replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go master
128139

129140
// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 master
130141

@@ -133,5 +144,3 @@ replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.
133144
// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go dev
134145

135146
// replace github.com/jfrog/froggit-go => github.com/jfrog/froggit-go master
136-
137-
replace github.com/jfrog/jfrog-apps-config => github.com/barv-jfrog/jfrog-apps-config v0.0.0-20250128142442-6fd49006bb85

0 commit comments

Comments
 (0)