Debian Codegraph @

gopls/internal/vulncheck/vulntest/report.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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 // The following was selectively copied from golang.org/x/vulndb/internal/report
21 //
22
23 // readReport reads a Report in YAML format.
24 func readReport(in io.Reader) (*Report, error) {
25 d := yaml.NewDecoder(in)
26 // Require that all fields in the file are in the struct.
27 // This corresponds to v2's UnmarshalStrict.
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 // Report represents a vulnerability report in the vulndb.
37 // See https://go.googlesource.com/vulndb/+/refs/heads/master/doc/format.md
38 type Report struct {
39 ID string `yaml:",omitempty"`
40
41 Modules []*Module `yaml:",omitempty"`
42
43 // Summary is a short phrase describing the vulnerability.
44 Summary string `yaml:",omitempty"`
45
46 // Description is the CVE description from an existing CVE. If we are
47 // assigning a CVE ID ourselves, use CVEMetadata.Description instead.
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 // Write writes r to filename in YAML format.
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 // ToString encodes r to a YAML string.
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 // Symbols originally identified as vulnerable.
101 Symbols []string `yaml:",omitempty"`
102 // Additional vulnerable symbols, computed from Symbols via static analysis
103 // or other technique.
104 DerivedSymbols []string `yaml:"derived_symbols,omitempty"`
105 }
106
107 // Version is a SemVer 2.0.0 semantic version with no leading "v" prefix,
108 // as used by OSV.
109 type Version string
110
111 // V returns the version with a "v" prefix.
112 func (v Version) V() string {
113 return "v" + string(v)
114 }
115
116 // IsValid reports whether v is a valid semantic version string.
117 func (v Version) IsValid() bool {
118 return semver.IsValid(v.V())
119 }
120
121 // Before reports whether v < v2.
122 func (v Version) Before(v2 Version) bool {
123 return semver.Compare(v.V(), v2.V()) < 0
124 }
125
126 // Canonical returns the canonical formatting of the version.
127 func (v Version) Canonical() string {
128 return strings.TrimPrefix(semver.Canonical(v.V()), "v")
129 }
130
131 // A Reference is a link to some external resource.
132 //
133 // For ease of typing, References are represented in the YAML as a
134 // single-element mapping of type to URL.
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 }