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

govulncheck integration #198

Merged
merged 54 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3abcfbd
Go vulncheck offline integration
another-rex Jan 25, 2023
a60c9f4
Use non experimental govulncheck
another-rex Jan 29, 2023
5935bad
Refactor shim client to another file
another-rex Jan 30, 2023
38d1393
Merge branch 'main' into govulncheck-integration
another-rex Jan 30, 2023
ce51724
Integration into json output
another-rex Jan 30, 2023
9539525
Merge branch 'main' into govulncheck-integration
another-rex Feb 7, 2023
652688d
Return back to using go vuln check
another-rex Feb 7, 2023
35e12a4
Fix linting issues
another-rex Feb 8, 2023
324e548
Add client tests
another-rex Feb 8, 2023
43d7d5f
Fix tests
another-rex Feb 8, 2023
a00a5da
Add notes about tags
another-rex Feb 8, 2023
5936116
Merge remote-tracking branch 'upstream/main' into govulncheck-integra…
another-rex Feb 8, 2023
5d1ff68
Fix go mod
another-rex Feb 8, 2023
060de78
Default to false, add cli flag to enable this
another-rex Feb 9, 2023
e60dbd4
Remove underscore
another-rex Feb 10, 2023
0f65e30
Address PR comments
another-rex Feb 13, 2023
e1a0eff
Return directly
another-rex Feb 13, 2023
710ad36
Merge remote-tracking branch 'upstream/main' into govulncheck-integra…
another-rex Feb 13, 2023
2f3798d
table output for code analysis
another-rex Feb 14, 2023
f1d3cb4
Merge branch 'main' into govulncheck-integration
another-rex Feb 14, 2023
747ac38
Add go as a docker dependency
another-rex Feb 14, 2023
3c43515
Add grouping and analysis info to flattened output, change exit code …
another-rex Feb 15, 2023
ede1750
Show error message rather than panicing if go is not installed.
another-rex Feb 15, 2023
3e69319
Update documentation
another-rex Feb 15, 2023
78e4269
Fix naming nit
another-rex Feb 16, 2023
637cb3b
Propagate error
another-rex Feb 16, 2023
8fca4ca
Merge branch 'main' into govulncheck-integration
another-rex Feb 16, 2023
b2833ae
Improve docs, add experimental everywhere, add readme explanation of …
another-rex Feb 16, 2023
23a98b8
Merge remote-tracking branch 'origin/govulncheck-integration' into go…
another-rex Feb 16, 2023
3a0bc75
Refactor to remove inter dependencies and make code easier to read.
another-rex Feb 17, 2023
1888e84
add more comments
another-rex Feb 17, 2023
9d01518
Merge branch 'main' into govulncheck-integration
another-rex Feb 17, 2023
fc7e90d
rename govulncheck file
another-rex Feb 19, 2023
f283683
Adds go snaps for easier testing, adds test for RunGoVulnCheck
another-rex Feb 20, 2023
38e56ce
Clean up test
another-rex Feb 20, 2023
13d577e
Add t.Parallel
another-rex Feb 20, 2023
e58a73e
Fix tests
another-rex Feb 20, 2023
4b90d30
Move to help tests work
another-rex Feb 20, 2023
0964570
Update to resolve multiple packages issue
another-rex Feb 22, 2023
077b3a8
Merge branch 'main' into govulncheck-integration
another-rex Feb 22, 2023
283720e
Fix linter warning
another-rex Feb 22, 2023
d9325eb
Add error printing and todo
another-rex Feb 22, 2023
e7741d1
Rewrote source analysis to order by packages and avoid missing modules
another-rex Feb 22, 2023
eec8bc2
Minor documentation comment
another-rex Feb 22, 2023
20fac09
Fix nits
another-rex Feb 23, 2023
c8c30f9
Check if source level info exists
another-rex Feb 23, 2023
c10dc4b
Minor comment update
another-rex Feb 23, 2023
e9b3b10
Refactor and add tests
another-rex Feb 24, 2023
5eb67d4
Merge branch 'main' into govulncheck-integration
another-rex Feb 24, 2023
7466e30
Add more tests, remove snaps dependency, add pretty dependency
another-rex Feb 26, 2023
dcd1bd9
Fix linter issues
another-rex Feb 26, 2023
d713e89
Fix nits
another-rex Feb 27, 2023
1befcfa
Fix nits again
another-rex Feb 27, 2023
403251a
Remove unused snapshot folder
another-rex Feb 27, 2023
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
22 changes: 14 additions & 8 deletions cmd/osv-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func run(args []string, stdout, stderr io.Writer) int {
Usage: "check subdirectories",
Value: false,
},
&cli.BoolFlag{
Name: "call-analysis",
Usage: "attempt call analysis on code to detect only active vulnerabilities",
Value: false,
},
&cli.BoolFlag{
Name: "no-ignore",
Usage: "also scan files that would be ignored by .gitignore",
Expand All @@ -106,14 +111,15 @@ func run(args []string, stdout, stderr io.Writer) int {
r = output.NewReporter(stdout, stderr, format)

vulnResult, err := osvscanner.DoScan(osvscanner.ScannerActions{
LockfilePaths: context.StringSlice("lockfile"),
SBOMPaths: context.StringSlice("sbom"),
DockerContainerNames: context.StringSlice("docker"),
Recursive: context.Bool("recursive"),
SkipGit: context.Bool("skip-git"),
NoIgnore: context.Bool("no-ignore"),
ConfigOverridePath: context.String("config"),
DirectoryPaths: context.Args().Slice(),
LockfilePaths: context.StringSlice("lockfile"),
SBOMPaths: context.StringSlice("sbom"),
DockerContainerNames: context.StringSlice("docker"),
Recursive: context.Bool("recursive"),
SkipGit: context.Bool("skip-git"),
NoIgnore: context.Bool("no-ignore"),
ConfigOverridePath: context.String("config"),
DirectoryPaths: context.Args().Slice(),
ExperimentalCallAnalysis: context.Bool("experimental-call-analysis"),
}, r)

if errPrint := r.PrintResult(&vulnResult); errPrint != nil {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require (
golang.org/x/exp v0.0.0-20230212135524-a684f29349b6
golang.org/x/mod v0.8.0
golang.org/x/term v0.5.0
golang.org/x/tools v0.5.1-0.20230117180257-8aba49bb5ea2
golang.org/x/vuln v0.0.0-20230118164824-4ec8867cc0e6
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -39,7 +41,7 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
13 changes: 11 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand Down Expand Up @@ -115,10 +116,12 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -148,11 +151,15 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.5.1-0.20230117180257-8aba49bb5ea2 h1:v0FhRDmSCNH/0EurAT6T8KRY4aNuUhz6/WwBMxG+gvQ=
golang.org/x/tools v0.5.1-0.20230117180257-8aba49bb5ea2/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
golang.org/x/vuln v0.0.0-20230118164824-4ec8867cc0e6 h1:XZD8apnMaMVuqE3ZEzf5JJncKMlOsMnnov7U+JRT/d4=
golang.org/x/vuln v0.0.0-20230118164824-4ec8867cc0e6/go.mod h1:cBP4HMKv0X+x96j8IJWCKk0eqpakBmmHjKGSSC0NaYE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand All @@ -170,4 +177,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk=
mvdan.cc/unparam v0.0.0-20211214103731-d0ef000c54e5 h1:Jh3LAeMt1eGpxomyu3jVkmVZWW2MxZ1qIIV2TZ/nRio=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
110 changes: 110 additions & 0 deletions internal/govulncheckshim/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package govulncheckshim

import (
"context"
"encoding/json"
"time"

"github.com/google/osv-scanner/pkg/models"
"golang.org/x/vuln/client"
gvcOSV "golang.org/x/vuln/osv"
)

type localSource struct {
vulnList []models.Vulnerability
vulnsByID map[string]*models.Vulnerability
vulnsByAlias map[string][]*models.Vulnerability
vulnsByModule map[string][]*models.Vulnerability
lastModifiedTime time.Time
client.Client
}

func newClient(vulns []models.Vulnerability) *localSource {
client := localSource{
vulnList: vulns,
vulnsByID: make(map[string]*models.Vulnerability),
vulnsByAlias: make(map[string][]*models.Vulnerability),
vulnsByModule: make(map[string][]*models.Vulnerability),
lastModifiedTime: time.Unix(0, 0),
}
for idx := range vulns {
// Iterate on reference to avoid copying entire data structure
v := &client.vulnList[idx]
client.vulnsByID[v.ID] = v
for _, alias := range v.Aliases {
client.vulnsByAlias[alias] = append(client.vulnsByAlias[alias], v)
}
for _, affected := range v.Affected {
client.vulnsByModule[affected.Package.Name] = append(client.vulnsByModule[affected.Package.Name], v)
}
if client.lastModifiedTime.Before(v.Modified) {
client.lastModifiedTime = v.Modified
}
}

return &client
}

func convertToGvcOSV(osv models.Vulnerability) gvcOSV.Entry {
val, err := json.Marshal(osv)
if err != nil {
panic("failed to convert vulnerability")
}
response := gvcOSV.Entry{}
err = json.Unmarshal(val, &response)
if err != nil {
panic("gvc format is no longer compatible with osv format")
}

return response
}

func (ls *localSource) GetByModule(ctx context.Context, modulePath string) ([]*gvcOSV.Entry, error) {
//nolint:prealloc // Need to be nil if none exists
var entries []*gvcOSV.Entry
for _, v := range ls.vulnsByModule[modulePath] {
res := convertToGvcOSV(*v)
entries = append(entries, &res)
}

return entries, nil
}

func (ls *localSource) GetByID(ctx context.Context, id string) (*gvcOSV.Entry, error) {
entry, ok := ls.vulnsByID[id]
if !ok {
//nolint:nilnil // This follows govulncheck's client implementation
// See: https://github.com/golang/vuln/blob/master/client/client.go
return nil, nil
}
response := convertToGvcOSV(*entry)

return &response, nil
}

func (ls *localSource) GetByAlias(ctx context.Context, alias string) ([]*gvcOSV.Entry, error) {
//nolint:prealloc // Need to be nil if none exists
var entries []*gvcOSV.Entry

for _, v := range ls.vulnsByAlias[alias] {
res := convertToGvcOSV(*v)
entries = append(entries, &res)
}

return entries, nil
}

func (ls *localSource) ListIDs(ctx context.Context) ([]string, error) {
//nolint:prealloc // Need to be nil if none exists
var ids []string
for i := range ls.vulnList {
ids = append(ids, ls.vulnList[i].ID)
}

return ids, nil
}

func (ls *localSource) LastModifiedTime(context.Context) (time.Time, error) {
// Assume that if anything changes, the index does.
return ls.lastModifiedTime, nil
}
Loading