Skip to content

Commit a90fa8a

Browse files
committed
vulncheck: remove isLocal check from fetchVulnerabilities
isLocal check was added to improve efficiency by avoiding fetch of data that's not going to be used. Version info is inaccurate or unavailable for modules that are in writable local directories so vuln check for those modules are skipped anyway. With the check, fetchVulnerabilities excludes vulnerabilities for modules if their source files are outside the module cache. The location of the module cache was determined by querying GOMODCACHE and GOPATH environment variables of the govulncheck process. That worked well for govulncheck when it is used for source scanning. The logic was copied to vulncheck API internal. However, relying on process's GOMODCACHE/GOPATH environment variables limit the API's utility. For example, Gopls may use different GOMODCACHE/GOPATH for each workspace it's processing and they can be different from the Gopls's own GOMODCACHE/GOPATH env vars. Test data can be loaded with a fake GOMODCACHE that's different from the GOMODCACHE env var of the test process. There was an escape flag to skip this check to work with the test environment where the module cache and GOPATH are different from the test process's. But that is unexported; external packages cannot utilize it and that prevents writing tests from external packages. This CL proposes to remove the isLocal check. There is already a cache that reduces volume of data fetch over network, and vulncheck can potentially address the efficiency issue in different ways. Users and applications that need to exclude vulnerabilities of local modules, may utilize golang.org/x/vuln/client.Client and implement filtering from GetByModule. Or, if this problem is common, we may consider an explicit setting in the vulncheck.Config. Change-Id: Iced93351b91a00fdc623a6d1c3076da86fbe2c70 Reviewed-on: https://go-review.googlesource.com/c/exp/+/391914 Trust: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Zvonimir Pavlinovic <[email protected]>
1 parent aaef6db commit a90fa8a

File tree

4 files changed

+7
-54
lines changed

4 files changed

+7
-54
lines changed

vulncheck/binary_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ func TestBinary(t *testing.T) {
8181
})
8282
defer e.Cleanup()
8383

84-
// Make sure local vulns can be loaded.
85-
fetchingInTesting = true
86-
8784
cmd := exec.Command("go", "build")
8885
cmd.Dir = e.Config.Dir
8986
cmd.Env = e.Config.Env

vulncheck/fetch.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ package vulncheck
77
import (
88
"context"
99
"fmt"
10-
"go/build"
11-
"os"
12-
"path/filepath"
13-
"strings"
1410

1511
"golang.org/x/vuln/client"
1612
)
@@ -66,12 +62,6 @@ func fetchVulnerabilities(ctx context.Context, client client.Client, modules []*
6662
modPath = mod.Replace.Path
6763
}
6864

69-
// skip loading vulns for local imports
70-
if isLocal(mod) {
71-
// TODO: what if client has its own db
72-
// with local vulns?
73-
continue
74-
}
7565
vulns, err := client.GetByModule(ctx, modPath)
7666
if err != nil {
7767
return nil, err
@@ -86,29 +76,3 @@ func fetchVulnerabilities(ctx context.Context, client client.Client, modules []*
8676
}
8777
return mv, nil
8878
}
89-
90-
// fetchingInTesting is a flag used to avoid skipping
91-
// loading local vulnerabilities in testing.
92-
var fetchingInTesting bool = false
93-
94-
func isLocal(mod *Module) bool {
95-
if fetchingInTesting {
96-
return false
97-
}
98-
modDir := mod.Dir
99-
if mod.Replace != nil {
100-
modDir = mod.Replace.Dir
101-
}
102-
return modDir != "" && !strings.HasPrefix(modDir, modCacheDirectory())
103-
}
104-
func modCacheDirectory() string {
105-
var modCacheDir string
106-
// TODO: define modCacheDir using something similar to cmd/go/internal/cfg.GOMODCACHE?
107-
if modCacheDir = os.Getenv("GOMODCACHE"); modCacheDir == "" {
108-
if modCacheDir = os.Getenv("GOPATH"); modCacheDir == "" {
109-
modCacheDir = build.Default.GOPATH
110-
}
111-
modCacheDir = filepath.Join(modCacheDir, "pkg", "mod")
112-
}
113-
return modCacheDir
114-
}

vulncheck/fetch_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,30 @@ func TestFetchVulnerabilities(t *testing.T) {
2323
}
2424

2525
mv, err := fetchVulnerabilities(context.Background(), mc, []*Module{
26-
{Path: "example.mod/a", Dir: modCacheDirectory(), Version: "v1.0.0"},
27-
{Path: "example.mod/b", Dir: modCacheDirectory(), Version: "v1.0.4"},
28-
{Path: "example.mod/c", Replace: &Module{Path: "example.mod/d", Dir: modCacheDirectory(), Version: "v1.0.0"}, Version: "v2.0.0"},
29-
{Path: "example.mod/e", Replace: &Module{Path: "../local/example.mod/d", Dir: modCacheDirectory(), Version: "v1.0.1"}, Version: "v2.1.0"},
26+
{Path: "example.mod/a", Version: "v1.0.0"},
27+
{Path: "example.mod/b", Version: "v1.0.4"},
28+
{Path: "example.mod/c", Replace: &Module{Path: "example.mod/d", Version: "v1.0.0"}, Version: "v2.0.0"},
29+
{Path: "example.mod/e", Replace: &Module{Path: "../local/example.mod/d", Version: "v1.0.1"}, Version: "v2.1.0"},
3030
})
3131
if err != nil {
3232
t.Fatalf("FetchVulnerabilities failed: %s", err)
3333
}
3434

3535
expected := moduleVulnerabilities{
3636
{
37-
mod: &Module{Path: "example.mod/a", Dir: modCacheDirectory(), Version: "v1.0.0"},
37+
mod: &Module{Path: "example.mod/a", Version: "v1.0.0"},
3838
vulns: []*osv.Entry{
3939
{ID: "a", Affected: []osv.Affected{{Package: osv.Package{Name: "example.mod/a"}, Ranges: osv.Affects{{Type: osv.TypeSemver, Events: []osv.RangeEvent{{Fixed: "2.0.0"}}}}}}},
4040
},
4141
},
4242
{
43-
mod: &Module{Path: "example.mod/b", Dir: modCacheDirectory(), Version: "v1.0.4"},
43+
mod: &Module{Path: "example.mod/b", Version: "v1.0.4"},
4444
vulns: []*osv.Entry{
4545
{ID: "b", Affected: []osv.Affected{{Package: osv.Package{Name: "example.mod/b"}, Ranges: osv.Affects{{Type: osv.TypeSemver, Events: []osv.RangeEvent{{Fixed: "1.1.1"}}}}}}},
4646
},
4747
},
4848
{
49-
mod: &Module{Path: "example.mod/c", Replace: &Module{Path: "example.mod/d", Dir: modCacheDirectory(), Version: "v1.0.0"}, Version: "v2.0.0"},
49+
mod: &Module{Path: "example.mod/c", Replace: &Module{Path: "example.mod/d", Version: "v1.0.0"}, Version: "v2.0.0"},
5050
vulns: []*osv.Entry{
5151
{ID: "c", Affected: []osv.Affected{{Package: osv.Package{Name: "example.mod/d"}, Ranges: osv.Affects{{Type: osv.TypeSemver, Events: []osv.RangeEvent{{Fixed: "2.0.0"}}}}}}},
5252
},

vulncheck/source_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ func TestImportsOnly(t *testing.T) {
9696
})
9797
defer e.Cleanup()
9898

99-
// Make sure local vulns can be loaded.
100-
fetchingInTesting = true
10199
// Load x and y as entry packages.
102100
pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"), path.Join(e.Temp(), "entry/y"))
103101
if err != nil {
@@ -341,8 +339,6 @@ func TestCallGraph(t *testing.T) {
341339
})
342340
defer e.Cleanup()
343341

344-
// Make sure local vulns can be loaded.
345-
fetchingInTesting = true
346342
// Load x and y as entry packages.
347343
pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"), path.Join(e.Temp(), "entry/y"))
348344
if err != nil {
@@ -440,8 +436,6 @@ func TestFiltering(t *testing.T) {
440436
},
441437
}
442438

443-
// Make sure local vulns can be loaded.
444-
fetchingInTesting = true
445439
// Load x as entry package.
446440
pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"))
447441
if err != nil {
@@ -527,8 +521,6 @@ func TestAllSymbolsVulnerable(t *testing.T) {
527521
},
528522
}
529523

530-
// Make sure local vulns can be loaded.
531-
fetchingInTesting = true
532524
// Load x as entry package.
533525
pkgs, err := loadPackages(e, path.Join(e.Temp(), "entry/x"))
534526
if err != nil {

0 commit comments

Comments
 (0)