| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package vulntest |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"time" |
| 13 |
|
| 14 |
"golang.org/x/mod/semver" |
| 15 |
"golang.org/x/tools/gopls/internal/vulncheck/osv" |
| 16 |
"gopkg.in/yaml.v3" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
func readReport(in io.Reader) (*Report, error) { |
| 25 |
d := yaml.NewDecoder(in) |
| 26 |
|
| 27 |
|
| 28 |
d.KnownFields(true) |
| 29 |
var r Report |
| 30 |
if err := d.Decode(&r); err != nil { |
| 31 |
return nil, fmt.Errorf("yaml.Decode: %v", err) |
| 32 |
} |
| 33 |
return &r, nil |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type Report struct { |
| 39 |
ID string `yaml:",omitempty"` |
| 40 |
|
| 41 |
Modules []*Module `yaml:",omitempty"` |
| 42 |
|
| 43 |
|
| 44 |
Summary string `yaml:",omitempty"` |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Description string `yaml:",omitempty"` |
| 49 |
Published time.Time `yaml:",omitempty"` |
| 50 |
Withdrawn *time.Time `yaml:",omitempty"` |
| 51 |
|
| 52 |
References []*Reference `yaml:",omitempty"` |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func (r *Report) Write(filename string) (err error) { |
| 57 |
f, err := os.Create(filename) |
| 58 |
if err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
err = r.encode(f) |
| 62 |
err2 := f.Close() |
| 63 |
if err == nil { |
| 64 |
err = err2 |
| 65 |
} |
| 66 |
return err |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func (r *Report) ToString() (string, error) { |
| 71 |
var b strings.Builder |
| 72 |
if err := r.encode(&b); err != nil { |
| 73 |
return "", err |
| 74 |
} |
| 75 |
return b.String(), nil |
| 76 |
} |
| 77 |
|
| 78 |
func (r *Report) encode(w io.Writer) error { |
| 79 |
e := yaml.NewEncoder(w) |
| 80 |
defer e.Close() |
| 81 |
e.SetIndent(4) |
| 82 |
return e.Encode(r) |
| 83 |
} |
| 84 |
|
| 85 |
type VersionRange struct { |
| 86 |
Introduced Version `yaml:"introduced,omitempty"` |
| 87 |
Fixed Version `yaml:"fixed,omitempty"` |
| 88 |
} |
| 89 |
|
| 90 |
type Module struct { |
| 91 |
Module string `yaml:",omitempty"` |
| 92 |
Versions []VersionRange `yaml:",omitempty"` |
| 93 |
Packages []*Package `yaml:",omitempty"` |
| 94 |
} |
| 95 |
|
| 96 |
type Package struct { |
| 97 |
Package string `yaml:",omitempty"` |
| 98 |
GOOS []string `yaml:"goos,omitempty"` |
| 99 |
GOARCH []string `yaml:"goarch,omitempty"` |
| 100 |
|
| 101 |
Symbols []string `yaml:",omitempty"` |
| 102 |
|
| 103 |
|
| 104 |
DerivedSymbols []string `yaml:"derived_symbols,omitempty"` |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
type Version string |
| 110 |
|
| 111 |
|
| 112 |
func (v Version) V() string { |
| 113 |
return "v" + string(v) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func (v Version) IsValid() bool { |
| 118 |
return semver.IsValid(v.V()) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
func (v Version) Before(v2 Version) bool { |
| 123 |
return semver.Compare(v.V(), v2.V()) < 0 |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func (v Version) Canonical() string { |
| 128 |
return strings.TrimPrefix(semver.Canonical(v.V()), "v") |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
type Reference osv.Reference |
| 136 |
|
| 137 |
func (r *Reference) MarshalYAML() (any, error) { |
| 138 |
return map[string]string{ |
| 139 |
strings.ToLower(string(r.Type)): r.URL, |
| 140 |
}, nil |
| 141 |
} |
| 142 |
|
| 143 |
func (r *Reference) UnmarshalYAML(n *yaml.Node) (err error) { |
| 144 |
if n.Kind != yaml.MappingNode || len(n.Content) != 2 || n.Content[0].Kind != yaml.ScalarNode || n.Content[1].Kind != yaml.ScalarNode { |
| 145 |
return &yaml.TypeError{Errors: []string{ |
| 146 |
fmt.Sprintf("line %d: report.Reference must contain a mapping with one value", n.Line), |
| 147 |
}} |
| 148 |
} |
| 149 |
r.Type = osv.ReferenceType(strings.ToUpper(n.Content[0].Value)) |
| 150 |
r.URL = n.Content[1].Value |
| 151 |
return nil |
| 152 |
} |