|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | +import { cwecMap } from '../../lib/cwec.js' |
| 3 | + |
| 4 | +const ajv = new Ajv() |
| 5 | + |
| 6 | +/* |
| 7 | + This is the jtd schema that needs to match the input document so that the |
| 8 | + test is activated. If this schema doesn't match it normally means that the input |
| 9 | + document does not validate against the csaf json schema or optional fields that |
| 10 | + the test checks are not present. |
| 11 | + */ |
| 12 | +const inputSchema = /** @type {const} */ ({ |
| 13 | + additionalProperties: true, |
| 14 | + properties: { |
| 15 | + vulnerabilities: { |
| 16 | + elements: { |
| 17 | + additionalProperties: true, |
| 18 | + properties: { |
| 19 | + cwes: { |
| 20 | + elements: { |
| 21 | + additionalProperties: true, |
| 22 | + properties: {}, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | +}) |
| 30 | + |
| 31 | +const validateInput = ajv.compile(inputSchema) |
| 32 | + |
| 33 | +const cweSchema = /** @type {const} */ ({ |
| 34 | + additionalProperties: true, |
| 35 | + properties: { |
| 36 | + id: { type: 'string' }, |
| 37 | + version: { type: 'string' }, |
| 38 | + name: { type: 'string' }, |
| 39 | + }, |
| 40 | +}) |
| 41 | + |
| 42 | +const validateCWE = ajv.compile(cweSchema) |
| 43 | + |
| 44 | +/** |
| 45 | + * This implements the optional test 6.2.23 of the CSAF 2.1 standard. |
| 46 | + * |
| 47 | + * @param {any} doc |
| 48 | + */ |
| 49 | +export async function optionalTest_6_2_23(doc) { |
| 50 | + /** @type {Array<{ message: string; instancePath: string }>} */ |
| 51 | + const warnings = [] |
| 52 | + const context = { warnings } |
| 53 | + |
| 54 | + if (!validateInput(doc)) { |
| 55 | + return context |
| 56 | + } |
| 57 | + |
| 58 | + for (let i = 0; i < doc.vulnerabilities.length; ++i) { |
| 59 | + const vulnerability = doc.vulnerabilities[i] |
| 60 | + for (let j = 0; j < vulnerability.cwes.length; ++j) { |
| 61 | + const cwe = vulnerability.cwes.at(j) |
| 62 | + if (validateCWE(cwe)) { |
| 63 | + const cwec = cwecMap.get(cwe.version) |
| 64 | + if (!cwec) { |
| 65 | + context.warnings.push({ |
| 66 | + instancePath: `/vulnerabilities/${i}/cwes/${j}/version`, |
| 67 | + message: 'no such cwe version is recognized', |
| 68 | + }) |
| 69 | + continue |
| 70 | + } |
| 71 | + const entry = (await cwec()).default.weaknesses.find( |
| 72 | + (w) => w.id === cwe.id |
| 73 | + ) |
| 74 | + if (!entry) { |
| 75 | + context.warnings.push({ |
| 76 | + instancePath: `/vulnerabilities/${i}/cwes/${j}/id`, |
| 77 | + message: `no weakness with this id is recognized in CWE ${cwe.version}`, |
| 78 | + }) |
| 79 | + continue |
| 80 | + } |
| 81 | + if (entry.status === 'Deprecated') { |
| 82 | + context.warnings.push({ |
| 83 | + instancePath: `/vulnerabilities/${i}/cwes/${j}/id`, |
| 84 | + message: |
| 85 | + 'the status of the weakness with the given id is deprecated', |
| 86 | + }) |
| 87 | + continue |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return context |
| 94 | +} |
0 commit comments