|
| 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 | + tracking: { |
| 19 | + additionalProperties: true, |
| 20 | + properties: { |
| 21 | + revision_history: { |
| 22 | + elements: { |
| 23 | + additionalProperties: true, |
| 24 | + optionalProperties: { |
| 25 | + date: {type: 'string'}, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + status: {type: 'string'}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + vulnerabilities: { |
| 35 | + elements: { |
| 36 | + additionalProperties: true, |
| 37 | + optionalProperties: { |
| 38 | + metrics: { |
| 39 | + elements: { |
| 40 | + additionalProperties: true, |
| 41 | + optionalProperties: { |
| 42 | + content: { |
| 43 | + additionalProperties: true, |
| 44 | + optionalProperties: { |
| 45 | + epss: { |
| 46 | + additionalProperties: true, |
| 47 | + optionalProperties: { |
| 48 | + timestamp: {type: 'string'}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | +}) |
| 61 | + |
| 62 | +const validate = ajv.compile(inputSchema) |
| 63 | + |
| 64 | +/** |
| 65 | + * This implements the mandatory test 6.1.51 of the CSAF 2.1 standard. |
| 66 | + * |
| 67 | + * @param {any} doc |
| 68 | + */ |
| 69 | +export function mandatoryTest_6_1_51(doc) { |
| 70 | + /* |
| 71 | + The `ctx` variable holds the state that is accumulated during the test ran and is |
| 72 | + finally returned by the function. |
| 73 | + */ |
| 74 | + const ctx = { |
| 75 | + errors: |
| 76 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 77 | + isValid: true, |
| 78 | + } |
| 79 | + |
| 80 | + if (!validate(doc)) { |
| 81 | + return ctx |
| 82 | + } |
| 83 | + const status = doc.document.tracking.status |
| 84 | + if (status !== 'final' && status !== 'interim') { |
| 85 | + return ctx |
| 86 | + } |
| 87 | + |
| 88 | + const newestRevisionHistoryItem = doc.document.tracking.revision_history |
| 89 | + .filter(item => item.date != null) |
| 90 | + .slice() |
| 91 | + .sort((a, z) => |
| 92 | + compareZonedDateTimes( |
| 93 | + /** @type {string} */ (z.date), |
| 94 | + /** @type {string} */ (a.date) |
| 95 | + ) |
| 96 | + )[0] |
| 97 | + |
| 98 | + doc.vulnerabilities?.forEach((vulnerability, vulnerabilityIndex) => { |
| 99 | + const metrics = vulnerability.metrics || [] |
| 100 | + metrics.forEach((metric, metricIdx) => { |
| 101 | + const content = metric.content || {} |
| 102 | + const epss = content.epss || {} |
| 103 | + if (epss.timestamp) { |
| 104 | + if ( |
| 105 | + newestRevisionHistoryItem && |
| 106 | + compareZonedDateTimes( |
| 107 | + /** @type {string} */ (newestRevisionHistoryItem.date), |
| 108 | + epss.timestamp |
| 109 | + ) < 0 |
| 110 | + ) { |
| 111 | + ctx.isValid = false |
| 112 | + ctx.errors.push({ |
| 113 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIdx}/content/epss/timestamp`, |
| 114 | + message: `the status is ${status}, but the EPSS 'timestamp' is newer than the newest revision history date`, |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + return ctx |
| 122 | +} |
0 commit comments