-
Notifications
You must be signed in to change notification settings - Fork 8
/
mandatoryTest_6_1_8.js
98 lines (89 loc) · 2.35 KB
/
mandatoryTest_6_1_8.js
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
import Ajv from 'ajv/dist/jtd.js'
import csafAjv from '../shared/csafAjv.js'
const jtdAjv = new Ajv()
const inputSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
vulnerabilities: {
elements: {
additionalProperties: true,
optionalProperties: {
scores: {
elements: {
additionalProperties: true,
optionalProperties: {
cvss_v2: {
additionalProperties: true,
properties: {},
},
cvss_v3: {
additionalProperties: true,
properties: {},
},
},
},
},
},
},
},
},
})
const validate = jtdAjv.compile(inputSchema)
const validate_2_0 = csafAjv.compile({
$ref: 'https://www.first.org/cvss/cvss-v2.0.json',
})
const validate_3 = csafAjv.compile({
oneOf: [
{
$ref: 'https://www.first.org/cvss/cvss-v3.0.json',
},
{
$ref: 'https://www.first.org/cvss/cvss-v3.1.json',
},
],
})
/**
* @param {any} doc
*/
export default function mandatoryTest_6_1_8(doc) {
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}
if (!validate(doc)) {
return ctx
}
for (const [
vulnerabilityIndex,
vulnerability,
] of doc.vulnerabilities?.entries() ?? []) {
for (const [scoreIndex, score] of vulnerability.scores?.entries() ?? []) {
if (score.cvss_v2) {
const valid = validate_2_0(score.cvss_v2)
if (!valid) {
ctx.isValid = false
for (const err of validate_2_0.errors ?? []) {
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/scores/${scoreIndex}/cvss_v2${err.instancePath}`,
message: err.message ?? '',
})
}
}
}
if (score.cvss_v3) {
const valid = validate_3(score.cvss_v3)
if (!valid) {
ctx.isValid = false
for (const err of validate_3.errors ?? []) {
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/scores/${scoreIndex}/cvss_v3${err.instancePath}`,
message: err.message ?? '',
})
}
}
}
}
}
return ctx
}