Skip to content

Commit 461c88c

Browse files
patrick.rissmann@l3montree.compatrick.rissmann@l3montree.com
authored andcommitted
Basis functionality is now implemented
1 parent 3abc36b commit 461c88c

64 files changed

Lines changed: 175 additions & 76 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/core/assetversion/asset_version_service.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log/slog"
66
"math"
7+
"math/rand/v2"
78
"net/http"
89
"strings"
910
"time"
@@ -72,7 +73,7 @@ func (s *service) HandleFirstPartyVulnResult(asset models.Asset, assetVersion *m
7273
AssetVersionName: assetVersion.Name,
7374
AssetID: asset.ID,
7475
Message: &result.Message.Text,
75-
ScannerIDs: scannerID,
76+
ScannerID: scannerID,
7677
},
7778
RuleID: result.RuleId,
7879
Uri: result.Locations[0].PhysicalLocation.ArtifactLocation.Uri,
@@ -183,7 +184,7 @@ func (s *service) HandleScanResult(asset models.Asset, assetVersion *models.Asse
183184
Vulnerability: models.Vulnerability{
184185
AssetVersionName: assetVersion.Name,
185186
AssetID: asset.ID,
186-
ScannerIDs: scannerID,
187+
ScannerID: scannerID + " ",
187188
},
188189
CVEID: utils.Ptr(v.CVEID),
189190
ComponentPurl: utils.Ptr(v.Purl),
@@ -221,6 +222,10 @@ func (s *service) HandleScanResult(asset models.Asset, assetVersion *models.Asse
221222

222223
func (s *service) handleScanResult(userID string, scannerID string, assetVersion *models.AssetVersion, dependencyVulns []models.DependencyVuln, doRiskManagement bool, asset models.Asset) (int, int, []models.DependencyVuln, error) {
223224
// get all existing dependencyVulns from the database - this is the old state
225+
226+
number := rand.IntN(len(dependencyVulns))
227+
dependencyVulns = dependencyVulns[:number]
228+
scannerID = scannerID + " "
224229
existingDependencyVulns, err := s.dependencyVulnRepository.ListByAssetAndAssetVersion(assetVersion.Name, assetVersion.AssetID)
225230
if err != nil {
226231
slog.Error("could not get existing dependencyVulns", "err", err)
@@ -240,31 +245,64 @@ func (s *service) handleScanResult(userID string, scannerID string, assetVersion
240245
foundByScannerAndExisting := comparison.InBoth //We have to check if it was already found by this scanner or only by other scanners
241246
notFoundByScannerAndExisting := comparison.OnlyInA //We have to update all vulnerabilities which were previously found by this scanner and now aren't
242247

248+
var vulnerabilitiesToFix []models.DependencyVuln //We should collect all vulnerabilities we want to fix so we can do it all at once
249+
var vulnerabilitiesToUpdate []models.DependencyVuln
243250
// get a transaction
244251
if err := s.dependencyVulnRepository.Transaction(func(tx core.DB) error {
245252
// We can create the newly found one without checking anything
246253
if err := s.dependencyVulnService.UserDetectedDependencyVulns(tx, userID, foundByScannerAndNotExisting, *assetVersion, asset, true); err != nil {
247254
return err // this will cancel the transaction
248255
}
256+
249257
// 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
258+
for i := range foundByScannerAndExisting {
259+
if !strings.Contains(foundByScannerAndExisting[i].ScannerID, scannerID) {
260+
fmt.Printf("\nThe Scanner ID before : %s\n", foundByScannerAndExisting[i].ScannerID)
261+
foundByScannerAndExisting[i].ScannerID = foundByScannerAndExisting[i].ScannerID + scannerID
262+
fmt.Printf("\nThe Scanner ID after : %s\n", foundByScannerAndExisting[i].ScannerID)
263+
}
264+
}
265+
err := s.dependencyVulnRepository.SaveBatch(tx, foundByScannerAndExisting)
266+
if err != nil {
267+
slog.Error("error when trying to update vulnerabilities")
268+
return err
269+
}
270+
271+
//Last we have to change the already existing vulnerabilities which were not found this time
272+
273+
for i := range notFoundByScannerAndExisting {
274+
if notFoundByScannerAndExisting[i].ScannerID == scannerID {
275+
notFoundByScannerAndExisting[i].ScannerID = ""
276+
vulnerabilitiesToFix = append(vulnerabilitiesToFix, notFoundByScannerAndExisting[i])
277+
} else if strings.Contains(notFoundByScannerAndExisting[i].ScannerID, scannerID) {
278+
removeScannerFromVulnerability(&notFoundByScannerAndExisting[i], scannerID)
279+
vulnerabilitiesToUpdate = append(vulnerabilitiesToUpdate, notFoundByScannerAndExisting[i])
253280
}
254281
}
255-
s.dependencyVulnRepository.ApplyAndSave(tx, &existingDependencyVulns)
256282

257-
return s.dependencyVulnService.UserFixedDependencyVulns(tx, userID, fixedDependencyVulns, *assetVersion, asset, true)
283+
err = s.dependencyVulnRepository.SaveBatch(tx, vulnerabilitiesToUpdate)
284+
if err != nil {
285+
slog.Error("error when trying to update vulnerabilities")
286+
return err
287+
}
288+
289+
return s.dependencyVulnService.UserFixedDependencyVulns(tx, userID, vulnerabilitiesToFix, *assetVersion, asset, true)
258290
}); err != nil {
259291
slog.Error("could not save dependencyVulns", "err", err)
260292
return 0, 0, []models.DependencyVuln{}, err
261293
}
262294

263295
// the amount we actually fixed, is the amount that was open before
264-
fixedDependencyVulns = utils.Filter(fixedDependencyVulns, func(dependencyVuln models.DependencyVuln) bool {
296+
vulnerabilitiesToFix = utils.Filter(vulnerabilitiesToFix, func(dependencyVuln models.DependencyVuln) bool {
265297
return dependencyVuln.State == models.VulnStateOpen
266298
})
267-
return len(foundByScannerAndNotExisting), len(fixedDependencyVulns), append(foundByScannerAndNotExisting, comparison.InBoth...), nil
299+
return len(foundByScannerAndNotExisting /* maybe also return vulns newly found by this scanner*/), len(vulnerabilitiesToFix), append(foundByScannerAndNotExisting, comparison.InBoth...), nil
300+
}
301+
302+
// pass by reference to edit the actual vulnerability and not a copy
303+
func removeScannerFromVulnerability(vulnerability *models.DependencyVuln, scannerID string) {
304+
305+
vulnerability.ScannerID = strings.Replace(vulnerability.ScannerID, scannerID, "", 1)
268306
}
269307

270308
func recursiveBuildBomRefMap(component cdx.Component) map[string]cdx.Component {

internal/core/daemon/flaw_daemon.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ func UpdateComponentProperties(db core.DB) error {
7474
// group by scanner id
7575
groups := make(map[string]map[string][]models.DependencyVuln)
7676
for _, f := range dependencyVulns {
77-
if _, ok := groups[f.ScannerIDs]; !ok {
78-
groups[f.ScannerIDs] = make(map[string][]models.DependencyVuln)
77+
if _, ok := groups[f.ScannerID]; !ok {
78+
groups[f.ScannerID] = make(map[string][]models.DependencyVuln)
7979
}
8080

81-
if _, ok := groups[f.ScannerIDs][f.AssetVersionName]; !ok {
82-
groups[f.ScannerIDs][f.AssetVersionName] = make([]models.DependencyVuln, 0)
81+
if _, ok := groups[f.ScannerID][f.AssetVersionName]; !ok {
82+
groups[f.ScannerID][f.AssetVersionName] = make([]models.DependencyVuln, 0)
8383
}
8484

85-
groups[f.ScannerIDs][f.AssetVersionName] = append(groups[f.ScannerIDs][f.AssetVersionName], f)
85+
groups[f.ScannerID][f.AssetVersionName] = append(groups[f.ScannerID][f.AssetVersionName], f)
8686
}
8787

8888
// group the dependencyVulns by scanner id

internal/core/dependency_vuln/dependency_vuln_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c dependencyVulnHttpController) ListPaged(ctx core.Context) error {
136136
// append the dependencyVuln to the package
137137
dependencyVulnsByPackage.DependencyVulns = append(res[*dependencyVuln.ComponentPurl].DependencyVulns, DependencyVulnDTO{
138138
ID: dependencyVuln.ID,
139-
ScannerID: dependencyVuln.ScannerIDs,
139+
ScannerID: dependencyVuln.ScannerID,
140140
Message: dependencyVuln.Message,
141141
AssetVersionName: dependencyVuln.AssetVersionName,
142142
AssetID: dependencyVuln.AssetID.String(),
@@ -297,7 +297,7 @@ func convertToDetailedDTO(dependencyVuln models.DependencyVuln) detailedDependen
297297
Priority: dependencyVuln.Priority,
298298
LastDetected: dependencyVuln.LastDetected,
299299
CreatedAt: dependencyVuln.CreatedAt,
300-
ScannerID: dependencyVuln.ScannerIDs,
300+
ScannerID: dependencyVuln.ScannerID,
301301
TicketID: dependencyVuln.TicketID,
302302
TicketURL: dependencyVuln.TicketURL,
303303
RiskRecalculatedAt: dependencyVuln.RiskRecalculatedAt,

internal/core/dependency_vuln/dependency_vuln_dto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func DependencyVulnToDto(f models.DependencyVuln) DependencyVulnDTO {
5555

5656
return DependencyVulnDTO{
5757
ID: f.ID,
58-
ScannerID: f.ScannerIDs,
58+
ScannerID: f.ScannerID,
5959
Message: f.Message,
6060
AssetVersionName: f.AssetVersionName,
6161
AssetID: f.AssetID.String(),

internal/core/dependency_vuln/first_party_vuln_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func convertFirstPartyVulnToDetailedDTO(firstPartyVuln models.FirstPartyVulnerab
204204
return detailedFirstPartyVulnDTO{
205205
FirstPartyVulnDTO: FirstPartyVulnDTO{
206206
ID: firstPartyVuln.ID,
207-
ScannerID: firstPartyVuln.ScannerIDs,
207+
ScannerID: firstPartyVuln.ScannerID,
208208
Message: firstPartyVuln.Message,
209209
AssetID: firstPartyVuln.AssetID.String(),
210210
State: firstPartyVuln.State,

internal/core/dependency_vuln/first_party_vuln_dto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func FirstPartyVulnToDto(f models.FirstPartyVulnerability) FirstPartyVulnDTO {
3838

3939
return FirstPartyVulnDTO{
4040
ID: f.ID,
41-
ScannerID: f.ScannerIDs,
41+
ScannerID: f.ScannerID,
4242
Message: f.Message,
4343
AssetID: f.AssetID.String(),
4444
State: f.State,

internal/core/risk/risk_explaination.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func Explain(dependencyVuln models.DependencyVuln, asset models.Asset, vector st
300300
cveDescription: dependencyVuln.CVE.Description,
301301

302302
affectedComponentName: utils.SafeDereference(dependencyVuln.ComponentPurl),
303-
scanner: dependencyVuln.ScannerIDs,
303+
scanner: dependencyVuln.ScannerID,
304304
fixedVersion: dependencyVuln.ComponentFixedVersion,
305305
}
306306
}

internal/core/vulndb/scan/scan_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func DependencyVulnScan(c core.Context, bom normalize.SBOM, s *httpController) (
125125
slog.Error("no scanner id provided")
126126
return scanResults, err
127127
}
128-
128+
//scannerID = ""
129129
// handle the scan result
130130
amountOpened, amountClose, newState, err := s.assetVersionService.HandleScanResult(asset, &assetVersion, vulns, scannerID, scannerID, userID, doRiskManagement)
131131
if err != nil {

internal/database/models/first_party_vuln_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (m *FirstPartyVulnerability) CalculateHash() string {
4848
startColumnStr := strconv.Itoa(m.StartColumn)
4949
endColumnStr := strconv.Itoa(m.EndColumn)
5050

51-
hash := utils.HashString(startLineStr + endLineStr + startColumnStr + endColumnStr + m.RuleID + m.Uri + m.ScannerIDs + m.AssetID.String() + m.AssetVersionName)
51+
hash := utils.HashString(startLineStr + endLineStr + startColumnStr + endColumnStr + m.RuleID + m.Uri + m.ScannerID + m.AssetID.String() + m.AssetVersionName)
5252
m.ID = hash
5353
return hash
5454
}

internal/database/models/vulnevent_model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func (e VulnEvent) Apply(vuln Vuln) {
9494
vuln.SetState(VulnStateAccepted)
9595
case EventTypeFalsePositive:
9696
vuln.SetState(VulnStateFalsePositive)
97+
case EventTypeDetectedByOtherScanner:
98+
9799
case EventTypeMarkedForTransfer:
98100
vuln.SetState(VulnStateMarkedForTransfer)
99101
case EventTypeRawRiskAssessmentUpdated:

0 commit comments

Comments
 (0)