Skip to content
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

formatters/trivy:fix - find correct line of dependency #882

Merged
merged 3 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/services/formatters/generic/trivy/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/ZupIT/horusec/internal/helpers/messages"
"github.com/ZupIT/horusec/internal/services/formatters"
"github.com/ZupIT/horusec/internal/services/formatters/generic/trivy/entities"
"github.com/ZupIT/horusec/internal/utils/file"
vulnhash "github.com/ZupIT/horusec/internal/utils/vuln_hash"
)

Expand Down Expand Up @@ -164,8 +165,9 @@ func (f *Formatter) setVulnerabilities(cmd string, result *entities.Result, path
func (f *Formatter) setVulnerabilitiesOutput(vulnerabilities []*entities.Vulnerability, target string) {
for _, vuln := range vulnerabilities {
addVuln := f.getVulnBase()
addVuln.Code = fmt.Sprintf("%s v%s", vuln.PkgName, vuln.InstalledVersion)
_, _, addVuln.Line = file.GetDependencyInfo(addVuln.Code, target)
addVuln.File = target
addVuln.Code = vuln.PkgName
addVuln.Details = vuln.GetDetails()
addVuln.Severity = severities.GetSeverityByString(vuln.Severity)
addVuln = vulnhash.Bind(addVuln)
Expand Down
6 changes: 3 additions & 3 deletions internal/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func GetDependencyCodeFilepathAndLine(
return "", "", ""
}

return getDependencyInfo(paths, dependency)
return GetDependencyInfo(dependency, paths...)
}

// nolint: funlen
Expand All @@ -266,12 +266,12 @@ func getPathsByExtension(projectPath, subPath string, extensions ...string) ([]s
})
}

// getDependencyInfo return the path inside paths that match the dependency.
// GetDependencyInfo return the path inside paths that match the dependency.
//
// The line and the dependency trimmed is also returned.
//
//nolint:funlen,gocyclo
func getDependencyInfo(paths []string, dependency string) (string, string, string) {
func GetDependencyInfo(dependency string, paths ...string) (string, string, string) {
var line int

for _, path := range paths {
Expand Down