@@ -72,7 +72,7 @@ func (s *service) HandleFirstPartyVulnResult(asset models.Asset, assetVersion *m
7272 AssetVersionName : assetVersion .Name ,
7373 AssetID : asset .ID ,
7474 Message : & result .Message .Text ,
75- ScannerID : scannerID ,
75+ ScannerIDs : scannerID ,
7676 },
7777 RuleID : result .RuleId ,
7878 Uri : result .Locations [0 ].PhysicalLocation .ArtifactLocation .Uri ,
@@ -183,7 +183,7 @@ func (s *service) HandleScanResult(asset models.Asset, assetVersion *models.Asse
183183 Vulnerability : models.Vulnerability {
184184 AssetVersionName : assetVersion .Name ,
185185 AssetID : asset .ID ,
186- ScannerID : scannerID ,
186+ ScannerIDs : scannerID ,
187187 },
188188 CVEID : utils .Ptr (v .CVEID ),
189189 ComponentPurl : utils .Ptr (v .Purl ),
@@ -221,11 +221,12 @@ func (s *service) HandleScanResult(asset models.Asset, assetVersion *models.Asse
221221
222222func (s * service ) handleScanResult (userID string , scannerID string , assetVersion * models.AssetVersion , dependencyVulns []models.DependencyVuln , doRiskManagement bool , asset models.Asset ) (int , int , []models.DependencyVuln , error ) {
223223 // get all existing dependencyVulns from the database - this is the old state
224- existingDependencyVulns , err := s .dependencyVulnRepository .ListByScanner (assetVersion .Name , assetVersion .AssetID , scannerID )
224+ existingDependencyVulns , err := s .dependencyVulnRepository .ListByAssetAndAssetVersion (assetVersion .Name , assetVersion .AssetID )
225225 if err != nil {
226226 slog .Error ("could not get existing dependencyVulns" , "err" , err )
227227 return 0 , 0 , []models.DependencyVuln {}, err
228228 }
229+
229230 // remove all fixed dependencyVulns from the existing dependencyVulns
230231 existingDependencyVulns = utils .Filter (existingDependencyVulns , func (dependencyVuln models.DependencyVuln ) bool {
231232 return dependencyVuln .State != models .VulnStateFixed
@@ -235,26 +236,24 @@ func (s *service) handleScanResult(userID string, scannerID string, assetVersion
235236 return dependencyVuln .CalculateHash ()
236237 })
237238
238- for _ , vuln := range dependencyVulns {
239- for _ , vuln_existing := range existingDependencyVulns {
240- if vuln .CalculateHash () == vuln_existing .CalculateHash () {
241- if ! strings .Contains (vuln_existing .ScannerID , vuln .ScannerID ) {
242- vuln_existing .ScannerID = vuln_existing .ScannerID + " " + vuln .ScannerID
243- }
244- }
245- }
246- }
247-
248- fixedDependencyVulns := comparison .OnlyInA
249- newDependencyVulns := comparison .OnlyInB
239+ foundByScannerAndNotExisting := comparison .OnlyInB //We want to create new vulnerabilities for these
240+ foundByScannerAndExisting := comparison .InBoth //We have to check if it was already found by this scanner or only by other scanners
241+ notFoundByScannerAndExisting := comparison .OnlyInA //We have to update all vulnerabilities which were previously found by this scanner and now aren't
250242
251243 // get a transaction
252244 if err := s .dependencyVulnRepository .Transaction (func (tx core.DB ) error {
253- if err := s .dependencyVulnService .UserDetectedDependencyVulns (tx , userID , newDependencyVulns , * assetVersion , asset , true ); err != nil {
254-
255- // this will cancel the transaction
256- return err
245+ // We can create the newly found one without checking anything
246+ if err := s .dependencyVulnService .UserDetectedDependencyVulns (tx , userID , foundByScannerAndNotExisting , * assetVersion , asset , true ); err != nil {
247+ return err // this will cancel the transaction
257248 }
249+ // Now we work on the vulnerabilities found in both sets -> has the vulnerability this scanner id already in his scanner_ids
250+ for _ , existingVulnerability := range foundByScannerAndExisting {
251+ if ! strings .Contains (existingVulnerability .ScannerID , scannerID ) {
252+ existingVulnerability .ScannerID = existingVulnerability .ScannerID + " " + scannerID
253+ }
254+ }
255+ s .dependencyVulnRepository .ApplyAndSave (tx , & existingDependencyVulns )
256+
258257 return s .dependencyVulnService .UserFixedDependencyVulns (tx , userID , fixedDependencyVulns , * assetVersion , asset , true )
259258 }); err != nil {
260259 slog .Error ("could not save dependencyVulns" , "err" , err )
@@ -265,7 +264,7 @@ func (s *service) handleScanResult(userID string, scannerID string, assetVersion
265264 fixedDependencyVulns = utils .Filter (fixedDependencyVulns , func (dependencyVuln models.DependencyVuln ) bool {
266265 return dependencyVuln .State == models .VulnStateOpen
267266 })
268- return len (newDependencyVulns ), len (fixedDependencyVulns ), append (newDependencyVulns , comparison .InBoth ... ), nil
267+ return len (foundByScannerAndNotExisting ), len (fixedDependencyVulns ), append (foundByScannerAndNotExisting , comparison .InBoth ... ), nil
269268}
270269
271270func recursiveBuildBomRefMap (component cdx.Component ) map [string ]cdx.Component {
0 commit comments