Skip to content

Commit da610ad

Browse files
authored
feat(sboms): do not fail on duplicated elements (cyclonedx) (#2539)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent 4c6f0e7 commit da610ad

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

internal/schemavalidators/schemavalidators.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,16 @@ func ValidateCycloneDX(data interface{}, version CycloneDXVersion) error {
138138
}
139139
var validationError *jsonschema.ValidationError
140140
if errors.As(err, &validationError) {
141-
if slices.ContainsFunc(validationError.Causes, func(v0 *jsonschema.ValidationError) bool {
142-
return slices.ContainsFunc(v0.Causes, func(v1 *jsonschema.ValidationError) bool {
143-
// workaround: Some scanners like Jfrog Xray might report null `cwes` element ("cwes": null)
141+
if slices.ContainsFunc(validationError.Causes, func(cause *jsonschema.ValidationError) bool {
142+
// Jfrog Xray: Do not fail in case of duplicated components. Policies will take care of validation and deduplication
143+
if cause.KeywordLocation == "/properties/components/uniqueItems" {
144+
return true
145+
}
146+
// Some validation errors are found deeper in the tree
147+
return slices.ContainsFunc(cause.Causes, func(c1 *jsonschema.ValidationError) bool {
148+
// Some scanners like Jfrog Xray might report null `cwes` element ("cwes": null)
144149
// the validator would fail with "expected array, but got null"
145-
return v1.KeywordLocation == "/properties/vulnerabilities/items/$ref/properties/cwes/type"
150+
return c1.KeywordLocation == "/properties/vulnerabilities/items/$ref/properties/cwes/type"
146151
})
147152
}) {
148153
return nil

internal/schemavalidators/schemavalidators_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func TestValidateCycloneDX1_6(t *testing.T) {
102102
name: "1.6 version",
103103
filePath: "./testdata/sbom.cyclonedx-1.6.json",
104104
},
105+
{
106+
name: "1.6 version with duplicated element",
107+
filePath: "./testdata/sbom.cyclonedx-duplicated.json",
108+
},
105109
}
106110

107111
for _, tc := range testCases {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"bomFormat": "CycloneDX",
3+
"specVersion": "1.6",
4+
"serialNumber": "urn:uuid:e8c355aa-2142-4084-a8c7-6d42c8610ba2",
5+
"version": 1,
6+
"metadata": {
7+
"timestamp": "2024-01-09T12:00:00Z",
8+
"component": {
9+
"type": "application",
10+
"name": "my application",
11+
"version": "1.0"
12+
}
13+
},
14+
"components": [
15+
{
16+
"name": "TLSv1.2",
17+
"type": "cryptographic-asset",
18+
"bom-ref": "crypto/protocol/[email protected]",
19+
"cryptoProperties": {
20+
"assetType": "protocol",
21+
"protocolProperties": {
22+
"type": "tls",
23+
"version": "1.2",
24+
"cipherSuites": [
25+
{
26+
"name": "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
27+
"algorithms": [
28+
"crypto/algorithm/[email protected]",
29+
"crypto/algorithm/[email protected]",
30+
"crypto/algorithm/[email protected]",
31+
"crypto/algorithm/[email protected]"
32+
],
33+
"identifiers": [ "0xC0", "0x30" ]
34+
}
35+
],
36+
"cryptoRefArray": [
37+
"crypto/certificate/google.com@sha256:1e15e0fbd3ce95bde5945633ae96add551341b11e5bae7bba12e98ad84a5beb4"
38+
]
39+
},
40+
"oid": "1.3.18.0.2.32.104"
41+
}
42+
},
43+
{
44+
"name": "TLSv1.2",
45+
"type": "cryptographic-asset",
46+
"bom-ref": "crypto/protocol/[email protected]",
47+
"cryptoProperties": {
48+
"assetType": "protocol",
49+
"protocolProperties": {
50+
"type": "tls",
51+
"version": "1.2",
52+
"cipherSuites": [
53+
{
54+
"name": "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
55+
"algorithms": [
56+
"crypto/algorithm/[email protected]",
57+
"crypto/algorithm/[email protected]",
58+
"crypto/algorithm/[email protected]",
59+
"crypto/algorithm/[email protected]"
60+
],
61+
"identifiers": [ "0xC0", "0x30" ]
62+
}
63+
],
64+
"cryptoRefArray": [
65+
"crypto/certificate/google.com@sha256:1e15e0fbd3ce95bde5945633ae96add551341b11e5bae7bba12e98ad84a5beb4"
66+
]
67+
},
68+
"oid": "1.3.18.0.2.32.104"
69+
}
70+
}
71+
]
72+
}

0 commit comments

Comments
 (0)