|
| 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 | + category: { |
| 18 | + type: 'string', |
| 19 | + }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + vulnerabilities: { |
| 23 | + elements: { |
| 24 | + additionalProperties: true, |
| 25 | + optionalProperties: { |
| 26 | + notes: { |
| 27 | + elements: { |
| 28 | + additionalProperties: true, |
| 29 | + properties: {}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | +}) |
| 37 | + |
| 38 | +const validate = ajv.compile(inputSchema) |
| 39 | +/** |
| 40 | + * @param {any} doc |
| 41 | + */ |
| 42 | +export function mandatoryTest_6_1_27_5(doc) { |
| 43 | + /** @type {Array<{ message: string; instancePath: string }>} */ |
| 44 | + const errors = [] |
| 45 | + let isValid = true |
| 46 | + |
| 47 | + if (!validate(doc)) return { errors, isValid } |
| 48 | + |
| 49 | + const checkedDocumentCategories = new Set([ |
| 50 | + 'csaf_security_advisory', |
| 51 | + 'csaf_vex', |
| 52 | + 'csaf_deprecated_security_advisory', |
| 53 | + ]) |
| 54 | + |
| 55 | + if (!checkedDocumentCategories.has(doc.document?.category)) { |
| 56 | + return { errors, isValid } |
| 57 | + } |
| 58 | + |
| 59 | + const vulnerabilities = doc.vulnerabilities |
| 60 | + if (Array.isArray(vulnerabilities)) { |
| 61 | + vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => { |
| 62 | + if (!vulnerability.notes || vulnerability.notes.length === 0) { |
| 63 | + isValid = false |
| 64 | + errors.push({ |
| 65 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}`, |
| 66 | + message: 'needs a `notes` attribute', |
| 67 | + }) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + return { errors, isValid } |
| 73 | +} |
0 commit comments