Skip to content

Commit 3abc36b

Browse files
patrick.rissmann@l3montree.compatrick.rissmann@l3montree.com
authored andcommitted
Stashing changes for now
1 parent 504c98e commit 3abc36b

12 files changed

Lines changed: 46 additions & 37 deletions

internal/core/assetversion/asset_version_service.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

222222
func (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

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

internal/core/common_interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type DependencyVulnRepository interface {
108108
GetDefaultDependencyVulnsByProjectIdPaged(tx DB, projectID uuid.UUID, pageInfo PageInfo, search string, filter []FilterQuery, sort []SortQuery) (Paged[models.DependencyVuln], error)
109109
GetDependencyVulnsByAssetVersionPagedAndFlat(tx DB, assetVersionName string, assetVersionID uuid.UUID, pageInfo PageInfo, search string, filter []FilterQuery, sort []SortQuery) (Paged[models.DependencyVuln], error)
110110
ListByScanner(assetVersionName string, assetID uuid.UUID, scannerID string) ([]models.DependencyVuln, error)
111+
ListByAssetAndAssetVersion(assetVersionName string, assetID uuid.UUID) ([]models.DependencyVuln, error)
111112
GetDependencyVulnsByPurl(tx DB, purls []string) ([]models.DependencyVuln, error)
112113
ApplyAndSave(tx DB, dependencyVuln *models.DependencyVuln, vulnEvent *models.VulnEvent) error
113114
}

internal/core/component/component_dto.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
)
77

88
type componentDTO struct {
9-
ID uuid.UUID `gorm:"primarykey;type:uuid;default:gen_random_uuid()" json:"id"`
9+
ID uuid.UUID `json:"id"`
1010

1111
// the provided sbom from cyclondx only contains the transitive dependencies, which do really get used
1212
// this means, that the dependency graph between people using the same library might differ, since they use it differently
1313
// we use edges, which provide the information, that a component is used by another component in one asset
1414
Dependency models.Component `json:"dependency"`
1515
DependencyPurl string `json:"dependencyPurl"` // will be nil, for direct dependencies
1616
AssetID uuid.UUID `json:"assetVersionId"`
17-
ScannerID string `json:"scannerId" gorm:"column:scanner_id"` // the id of the scanner
17+
ScannerID string `json:"scannerId"` // the id of the scanner
1818
}
1919

2020
func toDTO(m models.ComponentDependency) componentDTO {

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.ScannerID]; !ok {
78-
groups[f.ScannerID] = make(map[string][]models.DependencyVuln)
77+
if _, ok := groups[f.ScannerIDs]; !ok {
78+
groups[f.ScannerIDs] = make(map[string][]models.DependencyVuln)
7979
}
8080

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

85-
groups[f.ScannerID][f.AssetVersionName] = append(groups[f.ScannerID][f.AssetVersionName], f)
85+
groups[f.ScannerIDs][f.AssetVersionName] = append(groups[f.ScannerIDs][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.ScannerID,
139+
ScannerID: dependencyVuln.ScannerIDs,
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.ScannerID,
300+
ScannerID: dependencyVuln.ScannerIDs,
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.ScannerID,
58+
ScannerID: f.ScannerIDs,
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.ScannerID,
207+
ScannerID: firstPartyVuln.ScannerIDs,
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.ScannerID,
41+
ScannerID: f.ScannerIDs,
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.ScannerID,
303+
scanner: dependencyVuln.ScannerIDs,
304304
fixedVersion: dependencyVuln.ComponentFixedVersion,
305305
}
306306
}

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

0 commit comments

Comments
 (0)