forked from zhanglimao/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbom.go
229 lines (190 loc) · 5.51 KB
/
sbom.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
package sbom
import (
"bufio"
"encoding/json"
"encoding/xml"
"io"
"strings"
"github.com/in-toto/in-toto-golang/in_toto"
"golang.org/x/xerrors"
"github.com/zhyocean/trivy/pkg/attestation"
"github.com/zhyocean/trivy/pkg/sbom/cyclonedx"
"github.com/zhyocean/trivy/pkg/sbom/spdx"
"github.com/zhyocean/trivy/pkg/types"
)
type Format string
const (
FormatCycloneDXJSON Format = "cyclonedx-json"
FormatCycloneDXXML Format = "cyclonedx-xml"
FormatSPDXJSON Format = "spdx-json"
FormatSPDXTV Format = "spdx-tv"
FormatSPDXXML Format = "spdx-xml"
FormatAttestCycloneDXJSON Format = "attest-cyclonedx-json"
FormatUnknown Format = "unknown"
// FormatLegacyCosignAttestCycloneDXJSON is used to support the older format of CycloneDX JSON Attestation
// produced by the Cosign V1.
// ref. https://github.com/sigstore/cosign/pull/2718
FormatLegacyCosignAttestCycloneDXJSON Format = "legacy-cosign-attest-cyclonedx-json"
// PredicateCycloneDXBeforeV05 is the PredicateCycloneDX value defined in in-toto-golang before v0.5.0.
// This is necessary for backward-compatible SBOM detection.
// ref. https://github.com/in-toto/in-toto-golang/pull/188
PredicateCycloneDXBeforeV05 = "https://cyclonedx.org/schema"
)
var ErrUnknownFormat = xerrors.New("Unknown SBOM format")
type cdxHeader struct {
// XML specific field
XMLNS string `json:"-" xml:"xmlns,attr"`
// JSON specific field
BOMFormat string `json:"bomFormat" xml:"-"`
}
type spdxHeader struct {
SpdxID string `json:"SPDXID"`
}
func IsCycloneDXJSON(r io.ReadSeeker) (bool, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return false, xerrors.Errorf("seek error: %w", err)
}
var cdxBom cdxHeader
if err := json.NewDecoder(r).Decode(&cdxBom); err == nil {
if cdxBom.BOMFormat == "CycloneDX" {
return true, nil
}
}
return false, nil
}
func IsCycloneDXXML(r io.ReadSeeker) (bool, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return false, xerrors.Errorf("seek error: %w", err)
}
var cdxBom cdxHeader
if err := xml.NewDecoder(r).Decode(&cdxBom); err == nil {
if strings.HasPrefix(cdxBom.XMLNS, "http://cyclonedx.org") {
return true, nil
}
}
return false, nil
}
func IsSPDXJSON(r io.ReadSeeker) (bool, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return false, xerrors.Errorf("seek error: %w", err)
}
var spdxBom spdxHeader
if err := json.NewDecoder(r).Decode(&spdxBom); err == nil {
if strings.HasPrefix(spdxBom.SpdxID, "SPDX") {
return true, nil
}
}
return false, nil
}
func IsSPDXTV(r io.ReadSeeker) (bool, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return false, xerrors.Errorf("seek error: %w", err)
}
if scanner := bufio.NewScanner(r); scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "SPDX") {
return true, nil
}
}
return false, nil
}
func DetectFormat(r io.ReadSeeker) (Format, error) {
// Rewind the SBOM file at the end
defer r.Seek(0, io.SeekStart)
// Try CycloneDX JSON
if ok, err := IsCycloneDXJSON(r); err != nil {
return FormatUnknown, err
} else if ok {
return FormatCycloneDXJSON, nil
}
// Try CycloneDX XML
if ok, err := IsCycloneDXXML(r); err != nil {
return FormatUnknown, err
} else if ok {
return FormatCycloneDXXML, nil
}
// Try SPDX json
if ok, err := IsSPDXJSON(r); err != nil {
return FormatUnknown, err
} else if ok {
return FormatSPDXJSON, nil
}
// Try SPDX tag-value
if ok, err := IsSPDXTV(r); err != nil {
return FormatUnknown, err
} else if ok {
return FormatSPDXTV, nil
}
if _, err := r.Seek(0, io.SeekStart); err != nil {
return FormatUnknown, xerrors.Errorf("seek error: %w", err)
}
// Try in-toto attestation
format, ok := decodeAttestCycloneDXJSONFormat(r)
if ok {
return format, nil
}
return FormatUnknown, nil
}
func decodeAttestCycloneDXJSONFormat(r io.ReadSeeker) (Format, bool) {
var s attestation.Statement
if err := json.NewDecoder(r).Decode(&s); err != nil {
return "", false
}
if s.PredicateType != in_toto.PredicateCycloneDX && s.PredicateType != PredicateCycloneDXBeforeV05 {
return "", false
}
if s.Predicate == nil {
return "", false
}
m, ok := s.Predicate.(map[string]interface{})
if !ok {
return "", false
}
if _, ok := m["Data"]; ok {
return FormatLegacyCosignAttestCycloneDXJSON, true
}
return FormatAttestCycloneDXJSON, true
}
func Decode(f io.Reader, format Format) (types.SBOM, error) {
var (
v interface{}
bom types.SBOM
decoder interface{ Decode(any) error }
)
switch format {
case FormatCycloneDXJSON:
v = &cyclonedx.BOM{SBOM: &bom}
decoder = json.NewDecoder(f)
case FormatAttestCycloneDXJSON:
// dsse envelope
// => in-toto attestation
// => CycloneDX JSON
v = &attestation.Statement{
Predicate: &cyclonedx.BOM{SBOM: &bom},
}
decoder = json.NewDecoder(f)
case FormatLegacyCosignAttestCycloneDXJSON:
// dsse envelope
// => in-toto attestation
// => cosign predicate
// => CycloneDX JSON
v = &attestation.Statement{
Predicate: &attestation.CosignPredicate{
Data: &cyclonedx.BOM{SBOM: &bom},
},
}
decoder = json.NewDecoder(f)
case FormatSPDXJSON:
v = &spdx.SPDX{SBOM: &bom}
decoder = json.NewDecoder(f)
case FormatSPDXTV:
v = &spdx.SPDX{SBOM: &bom}
decoder = spdx.NewTVDecoder(f)
default:
return types.SBOM{}, xerrors.Errorf("%s scanning is not yet supported", format)
}
// Decode a file content into sbom.SBOM
if err := decoder.Decode(v); err != nil {
return types.SBOM{}, xerrors.Errorf("failed to decode: %w", err)
}
return bom, nil
}