Skip to content

Commit ea16ff1

Browse files
ccojocargcmurphy
authored andcommitted
Remove GOPATH check to allow running gosec outside of GOPATH
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 6c174a6 commit ea16ff1

File tree

1 file changed

+3
-52
lines changed

1 file changed

+3
-52
lines changed

cmd/gosec/main.go

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"io/ioutil"
2121
"log"
2222
"os"
23-
"path/filepath"
2423
"regexp"
2524
"sort"
2625
"strings"
@@ -185,52 +184,6 @@ func saveOutput(filename, format, rootPath string, issues []*gosec.Issue, metric
185184
return nil
186185
}
187186

188-
func cleanPath(path string) (string, error) {
189-
cleanFailed := fmt.Errorf("%s is not within the $GOPATH and cannot be processed", path)
190-
nonRecursivePath := strings.TrimSuffix(path, "/...")
191-
// do not attempt to clean directs that are resolvable on gopath
192-
if _, err := os.Stat(nonRecursivePath); err != nil && os.IsNotExist(err) {
193-
log.Printf("directory %s doesn't exist, checking if is a package on $GOPATH", path)
194-
for _, basedir := range gosec.Gopath() {
195-
dir := filepath.Join(basedir, "src", nonRecursivePath)
196-
if st, err := os.Stat(dir); err == nil && st.IsDir() {
197-
log.Printf("located %s in %s", path, dir)
198-
return path, nil
199-
}
200-
}
201-
return "", cleanFailed
202-
}
203-
204-
// ensure we resolve package directory correctly based on $GOPATH
205-
pkgPath, err := gosec.GetPkgRelativePath(path)
206-
if err != nil {
207-
return "", cleanFailed
208-
}
209-
return pkgPath, nil
210-
}
211-
212-
func cleanPaths(paths []string) []string {
213-
var clean []string
214-
for _, path := range paths {
215-
cleaned, err := cleanPath(path)
216-
if err != nil {
217-
log.Fatal(err)
218-
}
219-
clean = append(clean, cleaned)
220-
}
221-
return clean
222-
}
223-
224-
func resolvePackage(pkg string, searchPaths []string) string {
225-
for _, basedir := range searchPaths {
226-
dir := filepath.Join(basedir, "src", pkg)
227-
if st, err := os.Stat(dir); err == nil && st.IsDir() {
228-
return dir
229-
}
230-
}
231-
return pkg
232-
}
233-
234187
func convertToScore(severity string) (gosec.Score, error) {
235188
severity = strings.ToLower(severity)
236189
switch severity {
@@ -299,19 +252,16 @@ func main() {
299252
analyzer.LoadRules(ruleDefinitions.Builders())
300253

301254
vendor := regexp.MustCompile(`[\\/]vendor([\\/]|$)`)
302-
303255
var packages []string
304256
// Iterate over packages on the import paths
305-
gopaths := gosec.Gopath()
306-
for _, pkg := range gotool.ImportPaths(cleanPaths(flag.Args())) {
307-
257+
for _, pkg := range gotool.ImportPaths(flag.Args()) {
308258
// Skip vendor directory
309259
if !*flagScanVendor {
310260
if vendor.MatchString(pkg) {
311261
continue
312262
}
313263
}
314-
packages = append(packages, resolvePackage(pkg, gopaths))
264+
packages = append(packages, pkg)
315265
}
316266

317267
var buildTags []string
@@ -343,6 +293,7 @@ func main() {
343293
if !issuesFound && *flagQuiet {
344294
os.Exit(0)
345295
}
296+
346297
rootPath := packages[0]
347298
// Create output report
348299
if err := saveOutput(*flagOutput, *flagFormat, rootPath, issues, metrics, errors); err != nil {

0 commit comments

Comments
 (0)