|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | + |
| 3 | +const ajv = new Ajv() |
| 4 | + |
| 5 | +/* |
| 6 | + This is the jtd schema that needs to match the input document so that the |
| 7 | + test is activated. If this schema doesn't match it normally means that the input |
| 8 | + document does not validate against the csaf json schema or optional fields that |
| 9 | + the test checks are not present. |
| 10 | + */ |
| 11 | +const inputSchema = /** @type {const} */ ({ |
| 12 | + additionalProperties: true, |
| 13 | + properties: { |
| 14 | + document: { |
| 15 | + additionalProperties: true, |
| 16 | + properties: { |
| 17 | + tracking: { |
| 18 | + additionalProperties: true, |
| 19 | + properties: { |
| 20 | + id: { type: 'string' }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + vulnerabilities: { |
| 26 | + elements: { |
| 27 | + additionalProperties: true, |
| 28 | + optionalProperties: { |
| 29 | + cve: { type: 'string' }, |
| 30 | + ids: { |
| 31 | + elements: { |
| 32 | + additionalProperties: true, |
| 33 | + optionalProperties: { |
| 34 | + text: { type: 'string' }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + metrics: { |
| 39 | + elements: { |
| 40 | + additionalProperties: true, |
| 41 | + optionalProperties: { |
| 42 | + content: { |
| 43 | + additionalProperties: true, |
| 44 | + properties: { |
| 45 | + ssvc_v1: { |
| 46 | + additionalProperties: true, |
| 47 | + properties: { |
| 48 | + id: { type: 'string' }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | +}) |
| 61 | + |
| 62 | +const validateInput = ajv.compile(inputSchema) |
| 63 | + |
| 64 | +/** |
| 65 | + * This implements the mandatory test 6.1.47 of the CSAF 2.1 standard. |
| 66 | + * |
| 67 | + * @param {any} doc |
| 68 | + */ |
| 69 | +export function mandatoryTest_6_1_47(doc) { |
| 70 | + const ctx = { |
| 71 | + errors: |
| 72 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 73 | + isValid: true, |
| 74 | + } |
| 75 | + |
| 76 | + if (!validateInput(doc)) { |
| 77 | + return ctx |
| 78 | + } |
| 79 | + |
| 80 | + doc.vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => { |
| 81 | + vulnerability.metrics?.forEach((metric, metricIndex) => { |
| 82 | + const ssvcId = metric.content?.ssvc_v1.id |
| 83 | + if (ssvcId === doc.document.tracking.id) { |
| 84 | + if (doc.vulnerabilities.length > 1) { |
| 85 | + ctx.isValid = false |
| 86 | + ctx.errors.push({ |
| 87 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/id`, |
| 88 | + message: |
| 89 | + 'the ssvc id equals the `document/tracking/id` even the csaf document has multiple vulnerabilities', |
| 90 | + }) |
| 91 | + } |
| 92 | + } else { |
| 93 | + const idTexts = vulnerability.ids?.map((id) => id.text) |
| 94 | + if (ssvcId !== vulnerability.cve && !idTexts?.includes(ssvcId)) { |
| 95 | + ctx.isValid = false |
| 96 | + ctx.errors.push({ |
| 97 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/id`, |
| 98 | + message: |
| 99 | + 'the ssvc id does neither match the `cve` nor it matches the `text` of any item in the `ids` array', |
| 100 | + }) |
| 101 | + } |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + return ctx |
| 107 | +} |
0 commit comments