@@ -8,12 +8,14 @@ import (
88 "slices"
99 "strings"
1010
11+ ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
1112 ptypes "github.com/aquasecurity/trivy/pkg/types"
1213 codacy "github.com/codacy/codacy-engine-golang-seed/v6"
1314 "github.com/samber/lo"
1415 "golang.org/x/mod/semver"
1516)
1617
18+ // MaliciousPackagesIndexPath is the default path to the malicious package index.
1719const MaliciousPackagesIndexPath = "/dist/cache/codacy-trivy/openssf-malicious-packages-index.json.gz"
1820
1921// maliciousPackage represents a shallow representation of an Open Source Vulnerability (OSV).
@@ -120,7 +122,7 @@ func NewMaliciousPackagesScanner(indexPath string) (*MaliciousPackagesScanner, e
120122 return & MaliciousPackagesScanner {index : index }, nil
121123}
122124
123- // Scans the given Trivy report for malicious packages.
125+ // Scan scans the given Trivy report for malicious packages.
124126func (s MaliciousPackagesScanner ) Scan (report ptypes.Report , toolExecution codacy.ToolExecution ) []codacy.Result {
125127 maliciousPackagesEnabled := lo .SomeBy (* toolExecution .Patterns , func (p codacy.Pattern ) bool {
126128 return p .ID == ruleIDMaliciousPackages
@@ -148,23 +150,8 @@ func (s MaliciousPackagesScanner) Scan(report ptypes.Report, toolExecution codac
148150 }
149151
150152 for _ , candidate := range maliciousPkg {
151- if pkg .Version != "" && candidate .matchesVersion (pkg .Version ) {
152-
153- var lineNumber int
154- if len (pkg .Locations ) > 0 {
155- lineNumber = pkg .Locations [0 ].StartLine
156- } else {
157- lineNumber = fallbackSearchForLineNumber (toolExecution .SourceDir , result .Target , pkg .Name )
158- }
159-
160- issue := codacy.Issue {
161- File : result .Target ,
162- Line : lineNumber ,
163- Message : fmt .Sprintf ("%s - %s@%s" , candidate .Summary , pkg .Name , pkg .Version ),
164- PatternID : ruleIDMaliciousPackages ,
165- SourceID : candidate .ID ,
166- }
167- issues = append (issues , issue )
153+ if issue := issueForMaliciousPackage (pkg , candidate , result .Target , toolExecution .SourceDir ); issue != nil {
154+ issues = append (issues , * issue )
168155 }
169156 }
170157
@@ -174,6 +161,26 @@ func (s MaliciousPackagesScanner) Scan(report ptypes.Report, toolExecution codac
174161 return mapIssuesWithoutLineNumber (filterIssuesFromKnownFiles (issues , * toolExecution .Files ))
175162}
176163
164+ func issueForMaliciousPackage (pkg ftypes.Package , maliciousPkg maliciousPackage , srcFile , srcDir string ) * codacy.Issue {
165+ if pkg .Version != "" && maliciousPkg .matchesVersion (pkg .Version ) {
166+ var lineNumber int
167+ if len (pkg .Locations ) > 0 {
168+ lineNumber = pkg .Locations [0 ].StartLine
169+ } else {
170+ lineNumber = fallbackSearchForLineNumber (srcDir , srcFile , pkg .Name )
171+ }
172+
173+ return & codacy.Issue {
174+ File : srcFile ,
175+ Line : lineNumber ,
176+ Message : fmt .Sprintf ("%s - %s@%s" , maliciousPkg .Summary , pkg .Name , pkg .Version ),
177+ PatternID : ruleIDMaliciousPackages ,
178+ SourceID : maliciousPkg .ID ,
179+ }
180+ }
181+ return nil
182+ }
183+
177184// loadIndex attempts to load into memory the gzipped prebuilt index.
178185func loadIndex (indexPath string ) (maliciousPackagesByEcosystemAndName , error ) {
179186 f , err := os .Open (indexPath )
0 commit comments