Skip to content

Commit db8be72

Browse files
committed
init vuncheck linter
1 parent 66ac4b5 commit db8be72

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ require (
113113
github.com/ykadowak/zerologlint v0.1.1
114114
gitlab.com/bosi/decorder v0.2.3
115115
go.tmz.dev/musttag v0.6.0
116+
golang.org/x/net v0.9.0
116117
golang.org/x/tools v0.8.0
118+
golang.org/x/vuln v0.0.0-20220902211423-27dd78d2ca39
117119
gopkg.in/yaml.v3 v3.0.1
118120
honnef.co/go/tools v0.4.3
119121
mvdan.cc/gofumpt v0.5.0
@@ -187,7 +189,7 @@ require (
187189
golang.org/x/mod v0.10.0 // indirect
188190
golang.org/x/sync v0.1.0 // indirect
189191
golang.org/x/sys v0.7.0 // indirect
190-
golang.org/x/text v0.7.0 // indirect
192+
golang.org/x/text v0.9.0 // indirect
191193
google.golang.org/protobuf v1.28.0 // indirect
192194
gopkg.in/ini.v1 v1.67.0 // indirect
193195
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ type LintersSettings struct {
224224
Whitespace WhitespaceSettings
225225
Wrapcheck WrapcheckSettings
226226
WSL WSLSettings
227+
Vulncheck VulncheckSettings
227228

228229
Custom map[string]CustomLinterSettings
229230
}
@@ -744,6 +745,10 @@ type VarnamelenSettings struct {
744745
IgnoreDecls []string `mapstructure:"ignore-decls"`
745746
}
746747

748+
type VulncheckSettings struct {
749+
VulnDatabase []string `mapstructure:"vuln-database"`
750+
}
751+
747752
type WhitespaceSettings struct {
748753
MultiIf bool `mapstructure:"multi-if"`
749754
MultiFunc bool `mapstructure:"multi-func"`

pkg/golinters/vulncheck.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package golinters
2+
3+
import (
4+
"sync"
5+
6+
"golang.org/x/net/context"
7+
"golang.org/x/tools/go/analysis"
8+
"golang.org/x/vuln/client"
9+
"golang.org/x/vuln/vulncheck"
10+
11+
"github.com/golangci/golangci-lint/pkg/config"
12+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
13+
"github.com/golangci/golangci-lint/pkg/lint/linter"
14+
"github.com/golangci/golangci-lint/pkg/result"
15+
)
16+
17+
const (
18+
vulncheckName = "vulncheck"
19+
vulncheckDoc = "Package vulncheck detects uses of known vulnerabilities in Go programs."
20+
)
21+
22+
func NewVulncheck(settings *config.VulncheckSettings) *goanalysis.Linter {
23+
var mu sync.Mutex
24+
var resIssues []goanalysis.Issue
25+
26+
var analyzer = &analysis.Analyzer{
27+
Name: vulncheckName,
28+
Doc: vulncheckDoc,
29+
Run: goanalysis.DummyRun,
30+
}
31+
32+
return goanalysis.NewLinter(
33+
"vulncheck",
34+
"Package vulncheck detects uses of known vulnerabilities in Go programs.",
35+
[]*analysis.Analyzer{analyzer},
36+
nil,
37+
).WithContextSetter(func(lintCtx *linter.Context) {
38+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
39+
issues, err := vulncheckRun(lintCtx, pass, settings)
40+
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
mu.Lock()
46+
resIssues = append(resIssues, issues...)
47+
mu.Unlock()
48+
49+
return nil, nil
50+
}
51+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
52+
return resIssues
53+
})
54+
}
55+
56+
func vulncheckRun(lintCtx *linter.Context, pass *analysis.Pass, settings *config.VulncheckSettings) ([]goanalysis.Issue, error) {
57+
dbs := []string{"https://vuln.go.dev"}
58+
if len(settings.VulnDatabase) > 0 {
59+
dbs = settings.VulnDatabase
60+
}
61+
dbClient, err := client.NewClient(dbs, client.Options{})
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
vcfg := &vulncheck.Config{Client: dbClient, SourceGoVersion: lintCtx.Cfg.Run.Go}
67+
vpkgs := vulncheck.Convert(lintCtx.Packages)
68+
ctx := context.Background()
69+
70+
r, err := vulncheck.Source(ctx, vpkgs, vcfg)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
issues := make([]goanalysis.Issue, len(r.Vulns))
76+
77+
for _, vuln := range r.Vulns {
78+
issues = append(issues, goanalysis.NewIssue(&result.Issue{
79+
Text: vuln.OSV.ID,
80+
}, pass))
81+
}
82+
83+
return issues, nil
84+
}

pkg/lint/lintersdb/manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
178178
whitespaceCfg *config.WhitespaceSettings
179179
wrapcheckCfg *config.WrapcheckSettings
180180
wslCfg *config.WSLSettings
181+
vulncheckCfg *config.VulncheckSettings
181182
)
182183

183184
if m.cfg != nil {
@@ -258,6 +259,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
258259
whitespaceCfg = &m.cfg.LintersSettings.Whitespace
259260
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
260261
wslCfg = &m.cfg.LintersSettings.WSL
262+
vulncheckCfg = &m.cfg.LintersSettings.Vulncheck
261263

262264
if govetCfg != nil {
263265
govetCfg.Go = m.cfg.Run.Go
@@ -897,6 +899,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
897899
WithPresets(linter.PresetBugs).
898900
WithLoadForGoAnalysis().
899901
WithURL("https://github.com/ykadowak/zerologlint"),
902+
903+
linter.NewConfig(golinters.NewVulncheck(vulncheckCfg)).
904+
WithSince("v1.53.0").
905+
WithPresets(linter.PresetModule).
906+
WithURL("https://vuln.go.dev/"),
900907
}
901908

902909
enabledByDefault := map[string]bool{

0 commit comments

Comments
 (0)