|
1 | 1 | // Copyright 2022 The Go Authors. All rights reserved.
|
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
| 4 | + |
| 5 | +// Package govulncheck provides functionality to support the govulncheck command. |
4 | 6 | package govulncheck
|
5 | 7 |
|
6 |
| -// Result is contains output information for govulncheck. |
7 |
| -// |
8 |
| -// TODO(https://go.dev/issue/56042): this API is a work in progress. |
| 8 | +import ( |
| 9 | + "go/token" |
| 10 | + |
| 11 | + "golang.org/x/tools/go/packages" |
| 12 | + "golang.org/x/vuln/client" |
| 13 | + "golang.org/x/vuln/osv" |
| 14 | +) |
| 15 | + |
| 16 | +// LoadMode is the level of information needed for each package |
| 17 | +// for running golang.org/x/tools/go/packages.Load. |
| 18 | +var LoadMode = packages.NeedName | packages.NeedImports | packages.NeedTypes | |
| 19 | + packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps | |
| 20 | + packages.NeedModule |
| 21 | + |
| 22 | +// Config is used for configuring the output of govulncheck. |
| 23 | +type Config struct { |
| 24 | + // Client is the client used to make requests to a vulnerability |
| 25 | + // database(s). If nil, a default client is constructed that makes requests |
| 26 | + // to vuln.go.dev. |
| 27 | + Client client.Client |
| 28 | + |
| 29 | + // GoVersion specifies the Go version used when analyzing source code. |
| 30 | + // |
| 31 | + // By default, GoVersion is the go command version found from the PATH. |
| 32 | + GoVersion string |
| 33 | + |
| 34 | + // Verbosity controls the stdout and stderr output when running Source. |
| 35 | + // |
| 36 | + // TODO(https://go.dev/issue/56042): make this an enum. |
| 37 | + Verbosity string |
| 38 | +} |
| 39 | + |
| 40 | +// Result is the result of executing Source or Binary. |
9 | 41 | type Result struct {
|
| 42 | + // Vulns contains all vulnerabilities that are called or imported by |
| 43 | + // the analyzed module. |
| 44 | + Vulns []*Vuln |
| 45 | +} |
| 46 | + |
| 47 | +// Vuln represents a single OSV entry. |
| 48 | +type Vuln struct { |
| 49 | + // OSV contains all data from the OSV entry for this vulnerability. |
| 50 | + OSV *osv.Entry |
| 51 | + |
| 52 | + // Modules contains all of the modules in the OSV entry where a |
| 53 | + // vulnerable package is imported by the target source code or binary. |
| 54 | + // |
| 55 | + // For example, a module M with two packages M/p1 and M/p2, where only p1 |
| 56 | + // is vulnerable, will appear in this list if and only if p1 is imported by |
| 57 | + // the target source code or binary. |
| 58 | + Modules []*Module |
| 59 | +} |
| 60 | + |
| 61 | +// IsCalled reports whether the vulnerability is called, therefore |
| 62 | +// affecting the target source code or binary. |
| 63 | +// |
| 64 | +// TODO(https://go.dev/issue/56042): implement |
| 65 | +func (v *Vuln) IsCalled() bool { |
| 66 | + return false |
| 67 | +} |
| 68 | + |
| 69 | +// Module represents a specific vulnerability relevant to a single module. |
| 70 | +type Module struct { |
| 71 | + // Path is the module path of the module containing the vulnerability. |
| 72 | + // |
| 73 | + // Importable packages in the standard library will have the path "stdlib". |
| 74 | + Path string |
| 75 | + |
| 76 | + // FoundVersion is the module version where the vulnerability was found. |
| 77 | + FoundVersion string |
| 78 | + |
| 79 | + // FixedVersion is the module version where the vulnerability was |
| 80 | + // fixed. If there are multiple fixed versions in the OSV report, this will |
| 81 | + // be the latest fixed version. |
| 82 | + // |
| 83 | + // This is empty if a fix is not available. |
| 84 | + FixedVersion string |
| 85 | + |
| 86 | + // Packages contains all the vulnerable packages in OSV entry that are |
| 87 | + // imported by the target source code or binary. |
| 88 | + // |
| 89 | + // For example, given a module M with two packages M/p1 and M/p2, where |
| 90 | + // both p1 and p2 are vulnerable, p1 and p2 will each only appear in this |
| 91 | + // list they are individually imported by the target source code or binary. |
| 92 | + Packages []*Package |
| 93 | +} |
| 94 | + |
| 95 | +// Package is a Go package with known vulnerable symbols. |
| 96 | +type Package struct { |
| 97 | + // Path is the import path of the package containing the vulnerability. |
| 98 | + Path string |
| 99 | + |
| 100 | + // CallStacks contains a representative call stack for each |
| 101 | + // vulnerable symbol that is called. |
| 102 | + // |
| 103 | + // For vulnerabilities found from binary analysis, only CallStack.Symbol |
| 104 | + // will be provided. |
| 105 | + // |
| 106 | + // For non-affecting vulnerabilities reported from the source mode |
| 107 | + // analysis, this will be empty. |
| 108 | + CallStacks []CallStack |
| 109 | +} |
| 110 | + |
| 111 | +// CallStacks contains a representative call stack for a vulnerable |
| 112 | +// symbol. |
| 113 | +type CallStack struct { |
| 114 | + // Symbol is the name of the detected vulnerable function |
| 115 | + // or method. |
| 116 | + // |
| 117 | + // This follows the naming convention in the OSV report. |
| 118 | + Symbol string |
| 119 | + |
| 120 | + // Summary is a one-line description of the callstack, used by the |
| 121 | + // default govulncheck mode. |
| 122 | + // |
| 123 | + // Example: module3.main calls github.com/shiyanhui/dht.DHT.Run |
| 124 | + Summary string |
| 125 | + |
| 126 | + // Frames contains an entry for each stack in the call stack. |
| 127 | + // |
| 128 | + // Frames are sorted starting from the entry point to the |
| 129 | + // imported vulnerable symbol. The last frame in Frames should match |
| 130 | + // Symbol. |
| 131 | + Frames []*StackFrame |
| 132 | +} |
| 133 | + |
| 134 | +// StackFrame represents a call stack entry. |
| 135 | +type StackFrame struct { |
| 136 | + // PackagePath is the import path. |
| 137 | + PkgPath string |
| 138 | + |
| 139 | + // FuncName is the function name. |
| 140 | + FuncName string |
| 141 | + |
| 142 | + // RecvName is the receiver name, if the symbol is a |
| 143 | + // method. |
| 144 | + // |
| 145 | + // The client can create the final symbol name by |
| 146 | + // prepending RecvName to FuncName. |
| 147 | + RecvName string |
| 148 | + |
| 149 | + // Position describes an arbitrary source position |
| 150 | + // including the file, line, and column location. |
| 151 | + // A Position is valid if the line number is > 0. |
| 152 | + Position token.Position |
10 | 153 | }
|
0 commit comments