Skip to content

Commit 955211d

Browse files
author
Florent Viel
committed
init vuncheck linter
1 parent 281e184 commit 955211d

File tree

5 files changed

+105
-6
lines changed

5 files changed

+105
-6
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ require (
106106
github.com/yagipy/maintidx v1.0.0
107107
github.com/yeya24/promlinter v0.2.0
108108
gitlab.com/bosi/decorder v0.2.3
109-
golang.org/x/tools v0.1.12
109+
golang.org/x/tools v0.1.13-0.20220803210227-8b9a1fbdf5c3
110+
golang.org/x/vuln v0.0.0-20220902211423-27dd78d2ca39
110111
gopkg.in/yaml.v3 v3.0.1
111112
honnef.co/go/tools v0.3.3
112113
mvdan.cc/gofumpt v0.3.1
@@ -185,3 +186,5 @@ require (
185186
gopkg.in/yaml.v2 v2.4.0 // indirect
186187
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
187188
)
189+
190+
require golang.org/x/net v0.0.0-20220722155237-a158d28d115b

go.sum

Lines changed: 5 additions & 5 deletions
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
@@ -199,6 +199,7 @@ type LintersSettings struct {
199199
Whitespace WhitespaceSettings
200200
Wrapcheck WrapcheckSettings
201201
WSL WSLSettings
202+
Vulncheck VulncheckSettings
202203

203204
Custom map[string]CustomLinterSettings
204205
}
@@ -672,6 +673,10 @@ type VarnamelenSettings struct {
672673
IgnoreDecls []string `mapstructure:"ignore-decls"`
673674
}
674675

676+
type VulncheckSettings struct {
677+
VulnDatabase []string `mapstructure:"vuln-database"`
678+
}
679+
675680
type WhitespaceSettings struct {
676681
MultiIf bool `mapstructure:"multi-if"`
677682
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
@@ -173,6 +173,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
173173
whitespaceCfg *config.WhitespaceSettings
174174
wrapcheckCfg *config.WrapcheckSettings
175175
wslCfg *config.WSLSettings
176+
vulncheckCfg *config.VulncheckSettings
176177
)
177178

178179
if m.cfg != nil {
@@ -247,6 +248,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
247248
whitespaceCfg = &m.cfg.LintersSettings.Whitespace
248249
wrapcheckCfg = &m.cfg.LintersSettings.Wrapcheck
249250
wslCfg = &m.cfg.LintersSettings.WSL
251+
vulncheckCfg = &m.cfg.LintersSettings.Vulncheck
250252

251253
if govetCfg != nil {
252254
govetCfg.Go = m.cfg.Run.Go
@@ -846,6 +848,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
846848
WithSince("v1.26.0").
847849
WithPresets(linter.PresetStyle).
848850
WithURL("https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/nolintlint/README.md"),
851+
852+
linter.NewConfig(golinters.NewVulncheck(vulncheckCfg)).
853+
WithSince("v1.49.0").
854+
WithPresets(linter.PresetModule).
855+
WithURL("https://vuln.go.dev/"),
849856
}
850857

851858
enabledByDefault := map[string]bool{

0 commit comments

Comments
 (0)