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
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions internal/services/formatters/generic/trivy/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package trivy

import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/ZupIT/horusec-devkit/pkg/entities/vulnerability"
Expand Down Expand Up @@ -161,9 +165,32 @@ func (f *Formatter) setVulnerabilities(cmd string, result *entities.Result, path
}
}

func findLineByFilename(target string, pkgName string, installedVersion string) string {
line := 0
file, err := os.Open(target)
if err != nil {
return strconv.Itoa(line)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), pkgName+" v"+installedVersion) {
return strconv.Itoa(line)
}
line++
}

if err := scanner.Err(); err != nil {
line = 0
}
return strconv.Itoa(line)
}

matheusalcantarazup marked this conversation as resolved.
Show resolved Hide resolved
func (f *Formatter) setVulnerabilitiesOutput(vulnerabilities []*entities.Vulnerability, target string) {
for _, vuln := range vulnerabilities {
addVuln := f.getVulnBase()
addVuln.Line = findLineByFilename(target, vuln.PkgName, vuln.InstalledVersion)
addVuln.File = target
addVuln.Code = vuln.PkgName
addVuln.Details = vuln.GetDetails()
Expand Down