Skip to content

Commit f69c094

Browse files
authored
Merge pull request #1663 from l3montree-dev/fix-csaf-for-v1
deduplicate csaf entries and fix dependency search
2 parents badf0ba + 88b173b commit f69c094

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

controllers/csaf_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ func (controller *CSAFController) GetReportsByYearHTML(ctx shared.Context) error
245245
// extract the requested year and build the revision history first
246246
year := strings.TrimRight(ctx.Param("year"), "/")
247247
allVulns, err := controller.dependencyVulnRepository.GetAllVulnsByAssetID(nil, asset.ID)
248-
249248
if err != nil {
250249
return err
251250
}
@@ -257,6 +256,12 @@ func (controller *CSAFController) GetReportsByYearHTML(ctx shared.Context) error
257256
vulnsOfThatYear := utils.Filter(allVulns, func(vuln models.DependencyVuln) bool {
258257
return len(vuln.Events) > 0 && vuln.Events[0].CreatedAt.Year() == yearNumber
259258
})
259+
260+
// deduplicate Slice to avoid listing the same CVEs
261+
vulnsOfThatYear = utils.DeduplicateSlice(vulnsOfThatYear, func(vuln models.DependencyVuln) string {
262+
return vuln.CVEID
263+
})
264+
260265
type pageData struct {
261266
Year int
262267
Filenames []string

database/repositories/dependency_vuln_repository.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ func (repository *dependencyVulnRepository) GetByAssetVersionPaged(tx *gorm.DB,
195195
packageNameQuery = packageNameQuery.Where(f.SQL(), f.Value())
196196
}
197197

198+
if search != "" && len(search) > 2 {
199+
packageNameQuery.Where("(\"CVE\".description ILIKE ? OR dependency_vulns.cve_id ILIKE ? OR component_purl ILIKE ?)", "%"+search+"%", "%"+search+"%", "%"+search+"%")
200+
}
201+
198202
// apply sorting
199203
if len(sort) > 0 {
200204
for _, s := range sort {

utils/common.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,18 @@ func GetDirFromPath(path string) string {
158158
}
159159
return path
160160
}
161+
162+
// DeduplicateSlice deduplicates a slice in O(n) out of place.
163+
func DeduplicateSlice[T any](slice []T, idFunc func(t T) string) []T {
164+
deduplicatedSlice := make([]T, 0, len(slice))
165+
seen := make(map[string]struct{}, len(slice))
166+
for i := range slice {
167+
id := idFunc(slice[i])
168+
if _, ok := seen[id]; ok {
169+
continue
170+
}
171+
seen[id] = struct{}{}
172+
deduplicatedSlice = append(deduplicatedSlice, slice[i])
173+
}
174+
return deduplicatedSlice
175+
}

0 commit comments

Comments
 (0)