-
Notifications
You must be signed in to change notification settings - Fork 15
/
csaf.go
349 lines (302 loc) · 11.5 KB
/
csaf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package csaf
import (
"encoding/json"
"fmt"
"os"
"time"
)
// CSAF is a Common Security Advisory Framework Version 2.0 document.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html
type CSAF struct {
// Document contains metadata about the CSAF document itself.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#321-document-property
Document DocumentMetadata `json:"document"`
// ProductTree contains information about the product tree (branches only).
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#322-product-tree-property
ProductTree ProductBranch `json:"product_tree"`
// Vulnerabilities contains information about the vulnerabilities,
// (i.e. CVEs), associated threats, and product status.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#323-vulnerabilities-property
Vulnerabilities []Vulnerability `json:"vulnerabilities"`
// Notes holds notes associated with the whole document.
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3217-document-property---notes
Notes []Note `json:"notes"`
}
// DocumentMetadata contains metadata about the CSAF document itself.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#321-document-property
type DocumentMetadata struct {
// Aggregate severity is a vehicle that is provided by the document producer to convey the urgency and
// criticality with which the one or more vulnerabilities reported should be addressed.
//
Title string `json:"title"`
Tracking Tracking `json:"tracking"`
References []Reference `json:"references"`
Publisher Publisher `json:"publisher"`
}
// Document references holds a list of references associated with the whole document.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3219-document-property---references
type Reference struct {
Category string `json:"category"`
Summary string `json:"summary"`
URL string `json:"url"`
}
// Tracking contains information used to track the CSAF document through its lifecycle.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32112-document-property---tracking
type Tracking struct {
ID string `json:"id"`
CurrentReleaseDate time.Time `json:"current_release_date"`
InitialReleaseDate time.Time `json:"initial_release_date"`
}
// Publisher provides information on the publishing entity.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3218-document-property---publisher
type Publisher struct {
Category string `json:"category"`
ContactDetails string `json:"contact_details"`
IssuingAuthority string `json:"issuing_authority"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
// Vulnerability contains information about a CVE and its associated threats.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#323-vulnerabilities-property
type Vulnerability struct {
// MITRE standard Common Vulnerabilities and Exposures (CVE) tracking number for the vulnerability.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3232-vulnerabilities-property---cve
CVE string `json:"cve"`
// List of IDs represents a list of unique labels or tracking IDs for the vulnerability (if such information exists).
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3236-vulnerabilities-property---ids
IDs []TrackingID `json:"ids"`
// Provide details on the status of the referenced product related to the vulnerability.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3239-vulnerabilities-property---product-status
ProductStatus map[string][]string `json:"product_status"`
// Provide details of threats associated with a vulnerability.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32314-vulnerabilities-property---threats
Threats []ThreatData `json:"threats"`
// Provide details of remediations associated with a Vulnerability
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32312-vulnerabilities-property---remediations
Remediations []RemediationData `json:"remediations"`
// Machine readable flags for products related to vulnerability
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3235-vulnerabilities-property---flags
Flags []Flag `json:"flags"`
// Vulnerability references holds a list of references associated with this vulnerability item.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32310-vulnerabilities-property---references
References []Reference `json:"references"`
ReleaseDate time.Time `json:"release_date"`
}
type Note struct {
Category string `json:"category"`
Text string `json:"text"`
Title string `json:"title"`
Audience string `json:"audience"`
}
// Every ID item with the two mandatory properties System Name (system_name) and Text (text) contains a single unique label or tracking ID for the vulnerability.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3236-vulnerabilities-property---ids
type TrackingID struct {
SystemName string `json:"system_name"`
Text string `json:"text"`
}
// ThreatData contains information about a threat to a product.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32314-vulnerabilities-property---threats
type ThreatData struct {
Category string `json:"category"`
Details string `json:"details"`
ProductIDs []string `json:"product_ids"`
}
// RemediationData contains information about how to remediate a vulnerability for a set of products.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#32312-vulnerabilities-property---remediations
type RemediationData struct {
Category string `json:"category"`
Date time.Time `json:"date"`
Details string `json:"details"`
Entitlements []string `json:"entitlements"`
GroupIDs []string `json:"group_ids"`
ProductIDs []string `json:"product_ids"`
Restart RestartData `json:"restart_required"`
}
// Remediation instructions for restart of affected software.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#323127-vulnerabilities-property---remediations---restart-required
type RestartData struct {
Category string `json:"category"`
Details string `json:"details"`
}
// Machine readable flags for products related to the Vulnerability
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3235-vulnerabilities-property---flags
type Flag struct {
Label string `json:"label"`
Date time.Time `json:"date"`
GroupIDs []string `json:"group_ids"`
ProductIDs []string `json:"product_ids"`
}
// ProductBranch is a recursive struct that contains information about a product and
// its nested products.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3221-product-tree-property---branches
type ProductBranch struct {
Category string `json:"category"`
Name string `json:"name"`
Branches []ProductBranch `json:"branches"`
Product Product `json:"product,omitempty"`
Relationships []Relationship `json:"relationships"`
}
// Relationship establishes a link between two existing full_product_name_t elements, allowing
// the document producer to define a combination of two products that form a new full_product_name entry.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3224-product-tree-property---relationships
type Relationship struct {
Category string `json:"category"`
FullProductName Product `json:"full_product_name"`
ProductRef string `json:"product_reference"`
RelatesToProductRef string `json:"relates_to_product_reference"`
}
// Product contains information used to identify a product.
//
// https://docs.oasis-open.org/csaf/csaf/v2.0/os/csaf-v2.0-os.html#3124-branches-type---product
type Product struct {
Name string `json:"name"`
ID string `json:"product_id"`
IdentificationHelper map[string]string `json:"product_identification_helper"`
}
// Open reads and parses a given file path and returns a CSAF document
// or an error if the file could not be opened or parsed.
func Open(path string) (*CSAF, error) {
fh, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("csaf: failed to open document: %w", err)
}
defer fh.Close()
csafDoc := &CSAF{}
err = json.NewDecoder(fh).Decode(csafDoc)
if err != nil {
return nil, fmt.Errorf("csaf: failed to decode document: %w", err)
}
return csafDoc, nil
}
// FirstProductName returns the first product name in the product tree
// or an empty string if no product name is found.
func (csafDoc *CSAF) FirstProductName() string {
return csafDoc.ProductTree.FindFirstProduct()
}
// FindFirstProduct recursively searches for the first product identifier in the tree
// and returns it or an empty string if no product identifier is found.
func (branch *ProductBranch) FindFirstProduct() string {
if branch.Product.ID != "" {
return branch.Product.ID
}
// No nested branches
if branch.Branches == nil {
return ""
}
// Recursively search for the first product identifier
for _, b := range branch.Branches {
if p := b.FindFirstProduct(); p != "" {
return p
}
}
return ""
}
// FindFirstProductName recursively searches for the first product name in the tree
// and returns it or an empty string if no product name is found.
func (branch *ProductBranch) FindFirstProductName() string {
if branch.Product.Name != "" {
return branch.Product.Name
}
// No nested branches
if branch.Branches == nil {
return ""
}
// Recursively search for the first product identifier
for _, b := range branch.Branches {
if p := b.FindFirstProductName(); p != "" {
return p
}
}
return ""
}
// FindProductIdentifier recursively searches for the first product identifier in the tree
func (branch *ProductBranch) FindProductIdentifier(helperType, helperValue string) *Product {
if len(branch.Product.IdentificationHelper) != 0 {
for k := range branch.Product.IdentificationHelper {
if k != helperType {
continue
}
if branch.Product.IdentificationHelper[k] == helperValue {
return &branch.Product
}
}
}
// No nested branches
if branch.Branches == nil {
return nil
}
// Recursively search for the first identifier
for _, b := range branch.Branches {
if p := b.FindProductIdentifier(helperType, helperValue); p != nil {
return p
}
}
return nil
}
type ProductList []Product
// Add adds a prodocut to the product list if its not there, matching id and
// software identifiers.
func (pl *ProductList) Add(p Product) {
if p.ID == "" && len(p.IdentificationHelper) == 0 {
return
}
helpers := map[string]struct{}{}
for _, ih := range p.IdentificationHelper {
helpers[ih] = struct{}{}
}
for _, tp := range *pl {
if tp.ID == p.ID {
return
}
for _, idhelper := range tp.IdentificationHelper {
if _, ok := helpers[idhelper]; ok {
return
}
}
}
*pl = append(ProductList{p}, *pl...)
}
// ListProducts returns a flat list of all products in the branch
func (branch *ProductBranch) ListProducts() ProductList {
list := ProductList{}
list.Add(branch.Product)
for _, b := range branch.Branches {
for _, p := range b.ListProducts() {
list.Add(p)
}
}
return list
}
func (csafDoc *CSAF) ListProducts() ProductList {
prods := ProductList{}
for _, b := range csafDoc.ProductTree.Branches {
brachProds := b.ListProducts()
for _, sp := range brachProds {
prods.Add(sp)
}
}
return prods
}