|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | +import { compareZonedDateTimes } from '../../lib/shared/dateHelper.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 | + document: { |
| 16 | + additionalProperties: true, |
| 17 | + properties: { |
| 18 | + distribution: { |
| 19 | + additionalProperties: true, |
| 20 | + properties: { |
| 21 | + tlp: { |
| 22 | + additionalProperties: true, |
| 23 | + properties: { |
| 24 | + label: { type: 'string' }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + tracking: { |
| 30 | + additionalProperties: true, |
| 31 | + properties: { |
| 32 | + revision_history: { |
| 33 | + elements: { |
| 34 | + additionalProperties: true, |
| 35 | + optionalProperties: { |
| 36 | + date: { type: 'string' }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + vulnerabilities: { |
| 45 | + elements: { |
| 46 | + additionalProperties: true, |
| 47 | + optionalProperties: { |
| 48 | + disclosure_date: { type: 'string' }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | +}) |
| 54 | + |
| 55 | +const validate = ajv.compile(inputSchema) |
| 56 | + |
| 57 | +/** |
| 58 | + * This implements the mandatory test 6.1.45 of the CSAF 2.1 standard. |
| 59 | + * |
| 60 | + * @param {any} doc |
| 61 | + */ |
| 62 | +export function mandatoryTest_6_1_45(doc) { |
| 63 | + const ctx = { |
| 64 | + errors: |
| 65 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 66 | + isValid: true, |
| 67 | + } |
| 68 | + if (!validate(doc)) { |
| 69 | + return ctx |
| 70 | + } |
| 71 | + const status = doc.document.tracking.status |
| 72 | + const tlpLabel = doc.document.distribution.tlp.label |
| 73 | + if (!(tlpLabel === 'CLEAR' && (status === 'final' || status === 'interim'))) { |
| 74 | + return ctx |
| 75 | + } |
| 76 | + |
| 77 | + const revisionHistory = doc.document.tracking?.revision_history |
| 78 | + // sort the revision history (descending) and save the newest entry |
| 79 | + const newestRevisionHistoryItem = revisionHistory |
| 80 | + .filter((item) => item.date !== undefined) |
| 81 | + .sort((a, b) => |
| 82 | + compareZonedDateTimes( |
| 83 | + /** @type {string} */ (b.date), |
| 84 | + /** @type {string} */ (a.date) |
| 85 | + ) |
| 86 | + )[0] |
| 87 | + |
| 88 | + doc.vulnerabilities?.forEach((vulnerability, vulnerabilityIndex) => { |
| 89 | + const disclosureDate = vulnerability.disclosure_date |
| 90 | + // compare the disclosure date with the date of the newest item in the revision history |
| 91 | + if ( |
| 92 | + disclosureDate && |
| 93 | + compareZonedDateTimes( |
| 94 | + disclosureDate, |
| 95 | + /** @type {string} */ (newestRevisionHistoryItem.date) |
| 96 | + ) > 0 |
| 97 | + ) { |
| 98 | + ctx.isValid = false |
| 99 | + ctx.errors.push({ |
| 100 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}/disclosure_date`, |
| 101 | + message: `the "status" is ${status}, but the "disclosure date" is newer than the date of the newest item of the revision_history`, |
| 102 | + }) |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + return ctx |
| 107 | +} |
0 commit comments