Skip to content

Commit 878d84e

Browse files
authored
Merge pull request #1689 from l3montree-dev/fix-vulndb-frontend-endpoints
2 parents fb57762 + a6b9eb2 commit 878d84e

3 files changed

Lines changed: 25 additions & 27 deletions

File tree

controllers/vulndb_controller.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package controllers
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"net/url"
76
"strconv"
@@ -241,35 +240,36 @@ type ecosystemRow struct {
241240
Count int `gorm:"count" json:"count"`
242241
}
243242

244-
// return the number of affected packages by ecosystem
245-
func (c VulnDBController) GetEcosystemDistribution(ctx shared.Context) error {
246-
results := make([]ecosystemRow, 1024)
243+
// return the number of vulnerabilities in affected packages per ecosystem
244+
func (c VulnDBController) GetCVEEcosystemDistribution(ctx shared.Context) error {
245+
cveResults := make([]ecosystemRow, 0, 1024)
246+
maliciousPackageResults := make([]ecosystemRow, 0, 64)
247247

248-
// static sql to get amount of packages by ecosystem
249-
sql := `SELECT ecosystem, COUNT(*) FROM affected_components GROUP BY ecosystem;`
250-
err := c.affectedComponentRepository.GetDB(nil).Raw(sql).Find(&results).Error
248+
// get the amount of CVEs in affected packages per ecosystem
249+
cveSQL := `SELECT LOWER(b.ecosystem) as ecosystem, COUNT(*) FROM cve_affected_component a
250+
LEFT JOIN affected_components b ON b.id = a.affected_component_id
251+
GROUP BY LOWER(b.ecosystem);`
252+
err := c.affectedComponentRepository.GetDB(nil).Raw(cveSQL).Find(&cveResults).Error
251253
if err != nil {
252254
return echo.NewHTTPError(500, "could not fetch data from database").WithInternal(err)
253255
}
254256

255-
// since ecosystem have tags behind the : character we want to group them by their prefix
256-
jsonResults := buildResultsJSON(results)
257-
258-
return ctx.String(200, jsonResults)
259-
}
260-
261-
// group ecosystem by prefix ecosystem string and return the equivalent json encoding
262-
func buildResultsJSON(rows []ecosystemRow) string {
263-
// map to deduplicate ecosystem with different tags
264-
aggregatedResults := make(map[string]int)
257+
// do the same thing for malicious packages
258+
maliciousPackagesSQL := `SELECT LOWER(b.ecosystem) as ecosystem, COUNT(*) FROM malicious_packages a
259+
LEFT JOIN malicious_affected_components b ON a.id = b.malicious_package_id
260+
GROUP BY LOWER(b.ecosystem);`
261+
err = c.affectedComponentRepository.GetDB(nil).Raw(maliciousPackagesSQL).Find(&maliciousPackageResults).Error
262+
if err != nil {
263+
return echo.NewHTTPError(500, "could not fetch data from database").WithInternal(err)
264+
}
265265

266-
// fill the map with the value of the rows
267-
for _, row := range rows {
268-
before, _, _ := strings.Cut(row.Ecosystem, ":")
269-
aggregatedResults[before] += row.Count
266+
// group the results in a map by cutting the ecosystem identifier before the ':'
267+
ecosystemToAmount := make(map[string]int, len(cveResults))
268+
for _, row := range append(cveResults, maliciousPackageResults...) {
269+
key, _, _ := strings.Cut(row.Ecosystem, ":")
270+
ecosystemToAmount[key] += row.Count
270271
}
271272

272-
// marshal to JSON with proper indentation
273-
data, _ := json.MarshalIndent(aggregatedResults, "", config.PrettyJSONIndent)
274-
return string(data)
273+
// convert the result in a map and return it
274+
return ctx.JSONPretty(200, ecosystemToAmount, config.PrettyJSONIndent)
275275
}

database/repositories/cve_repository.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func (g *cveRepository) FindAllListPaged(tx *gorm.DB, pageInfo shared.PageInfo,
137137
for _, f := range filter {
138138
q = q.Where(f.SQL(), f.Value())
139139
}
140-
q = q.Where("cvss > 0")
141140
q.Count(&count)
142141

143142
// get all cves
@@ -147,7 +146,6 @@ func (g *cveRepository) FindAllListPaged(tx *gorm.DB, pageInfo shared.PageInfo,
147146
for _, f := range filter {
148147
q = q.Where(f.SQL(), f.Value())
149148
}
150-
q = q.Where("cvss > 0")
151149

152150
// apply sorting
153151
if len(sort) > 0 {

router/vulndb_router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewVulnDBRouter(apiV1Router APIV1Router, vulndbController *controllers.Vuln
3030
cveRouter.GET("/:cveID/", vulndbController.Read)
3131
cveRouter.GET("/purl-inspect/:purl", vulndbController.PURLInspect)
3232
cveRouter.GET("/list-ids-by-creation-date/", vulndbController.ListIDsByCreationDate)
33-
cveRouter.GET("/affected-package-distribution/", vulndbController.GetEcosystemDistribution)
33+
cveRouter.GET("/cve-ecosystem-distribution/", vulndbController.GetCVEEcosystemDistribution)
3434
return VulnDBRouter{
3535
Group: cveRouter,
3636
}

0 commit comments

Comments
 (0)