-
Notifications
You must be signed in to change notification settings - Fork 31
Vulndb endpoint improvements #1689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/url" | ||
| "strconv" | ||
|
|
@@ -216,35 +215,36 @@ type ecosystemRow struct { | |
| Count int `gorm:"count" json:"count"` | ||
| } | ||
|
|
||
| // return the number of affected packages by ecosystem | ||
| func (c VulnDBController) GetEcosystemDistribution(ctx shared.Context) error { | ||
| results := make([]ecosystemRow, 1024) | ||
| // return the number of vulnerabilities in affected packages per ecosystem | ||
| func (c VulnDBController) GetCVEEcosystemDistribution(ctx shared.Context) error { | ||
| cveResults := make([]ecosystemRow, 0, 1024) | ||
| maliciousPackageResults := make([]ecosystemRow, 0, 64) | ||
|
|
||
| // static sql to get amount of packages by ecosystem | ||
| sql := `SELECT ecosystem, COUNT(*) FROM affected_components GROUP BY ecosystem;` | ||
| err := c.affectedComponentRepository.GetDB(nil).Raw(sql).Find(&results).Error | ||
| // get the amount of CVEs in affected packages per ecosystem | ||
| cveSQL := `SELECT LOWER(b.ecosystem) as ecosystem, COUNT(*) FROM cve_affected_component a | ||
| LEFT JOIN affected_components b ON b.id = a.affected_component_id | ||
| GROUP BY LOWER(b.ecosystem);` | ||
| err := c.affectedComponentRepository.GetDB(nil).Raw(cveSQL).Find(&cveResults).Error | ||
| if err != nil { | ||
| return echo.NewHTTPError(500, "could not fetch data from database").WithInternal(err) | ||
| } | ||
|
|
||
| // since ecosystem have tags behind the : character we want to group them by their prefix | ||
| jsonResults := buildResultsJSON(results) | ||
|
|
||
| return ctx.String(200, jsonResults) | ||
| } | ||
|
|
||
| // group ecosystem by prefix ecosystem string and return the equivalent json encoding | ||
| func buildResultsJSON(rows []ecosystemRow) string { | ||
| // map to deduplicate ecosystem with different tags | ||
| aggregatedResults := make(map[string]int) | ||
| // do the same thing for malicious packages | ||
| maliciousPackagesSQL := `SELECT LOWER(b.ecosystem) as ecosystem, COUNT(*) FROM malicious_packages a | ||
|
timbastin marked this conversation as resolved.
|
||
| LEFT JOIN malicious_affected_components b ON a.id = b.malicious_package_id | ||
| GROUP BY LOWER(b.ecosystem);` | ||
| err = c.affectedComponentRepository.GetDB(nil).Raw(maliciousPackagesSQL).Find(&maliciousPackageResults).Error | ||
|
Comment on lines
+224
to
+236
|
||
| if err != nil { | ||
| return echo.NewHTTPError(500, "could not fetch data from database").WithInternal(err) | ||
| } | ||
|
|
||
| // fill the map with the value of the rows | ||
| for _, row := range rows { | ||
| before, _, _ := strings.Cut(row.Ecosystem, ":") | ||
| aggregatedResults[before] += row.Count | ||
| // group the results in a map by cutting the ecosystem identifier before the ':' | ||
| ecosystemToAmount := make(map[string]int, len(cveResults)) | ||
| for _, row := range append(cveResults, maliciousPackageResults...) { | ||
| key, _, _ := strings.Cut(row.Ecosystem, ":") | ||
| ecosystemToAmount[key] += row.Count | ||
| } | ||
|
|
||
| // marshal to JSON with proper indentation | ||
| data, _ := json.MarshalIndent(aggregatedResults, "", config.PrettyJSONIndent) | ||
| return string(data) | ||
| // convert the result in a map and return it | ||
| return ctx.JSONPretty(200, ecosystemToAmount, config.PrettyJSONIndent) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.