|
| 1 | +import Ajv from 'ajv/dist/jtd.js' |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {string} Product |
| 5 | + * / |
| 6 | +
|
| 7 | + /** @typedef {import('ajv/dist/jtd.js').JTDDataType<typeof inputSchema>} InputSchema */ |
| 8 | + |
| 9 | +/** @typedef {InputSchema['vulnerabilities'][number]} Vulnerability */ |
| 10 | + |
| 11 | +/** @typedef {NonNullable<Vulnerability['metrics']>[number]} Metric */ |
| 12 | + |
| 13 | +/** @typedef {NonNullable<Metric['content']>} MetricContent */ |
| 14 | + |
| 15 | +const jtdAjv = new Ajv() |
| 16 | + |
| 17 | +const inputSchema = /** @type {const} */ ({ |
| 18 | + additionalProperties: true, |
| 19 | + properties: { |
| 20 | + vulnerabilities: { |
| 21 | + elements: { |
| 22 | + additionalProperties: true, |
| 23 | + optionalProperties: { |
| 24 | + metrics: { |
| 25 | + elements: { |
| 26 | + additionalProperties: true, |
| 27 | + optionalProperties: { |
| 28 | + source: { |
| 29 | + type: 'string', |
| 30 | + }, |
| 31 | + products: { |
| 32 | + elements: { type: 'string' }, |
| 33 | + }, |
| 34 | + content: { |
| 35 | + additionalProperties: true, |
| 36 | + optionalProperties: { |
| 37 | + cvss_v2: { |
| 38 | + additionalProperties: true, |
| 39 | + optionalProperties: { |
| 40 | + version: { type: 'string' }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + cvss_v3: { |
| 44 | + additionalProperties: true, |
| 45 | + optionalProperties: { |
| 46 | + version: { type: 'string' }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + cvss_v4: { |
| 50 | + additionalProperties: true, |
| 51 | + optionalProperties: { |
| 52 | + version: { type: 'string' }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | +}) |
| 65 | + |
| 66 | +const validate = jtdAjv.compile(inputSchema) |
| 67 | + |
| 68 | +/** |
| 69 | + * For each item in /vulnerabilities it MUST be tested that the same Product ID |
| 70 | + * is not a member of more than one CVSS-Vectors with the same version and the same source. |
| 71 | + * @param {unknown} doc |
| 72 | + */ |
| 73 | +export function mandatoryTest_6_1_7(doc) { |
| 74 | + const ctx = { |
| 75 | + errors: |
| 76 | + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), |
| 77 | + isValid: true, |
| 78 | + } |
| 79 | + |
| 80 | + /** @type {Array<{ message: string; instancePath: string }>} */ |
| 81 | + const errors = [] |
| 82 | + let isValid = true |
| 83 | + |
| 84 | + if (!validate(doc)) { |
| 85 | + return ctx |
| 86 | + } |
| 87 | + |
| 88 | + /** @type {Array<Vulnerability>} */ |
| 89 | + const vulnerabilities = doc.vulnerabilities |
| 90 | + |
| 91 | + /** |
| 92 | + * Create a unique string for the tuple of version and source |
| 93 | + * to compare them easily |
| 94 | + * @param {string} version |
| 95 | + * @param {string | undefined} source |
| 96 | + */ |
| 97 | + function createIdForVersionAndSource(version, source) { |
| 98 | + return JSON.stringify({ version: version, source: source ?? '' }) |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * |
| 103 | + * @param {Metric} metric |
| 104 | + * @param {string} versionSourceId |
| 105 | + * @returns {string|null} |
| 106 | + */ |
| 107 | + function findCvssVersionWithSameVersionAndSource(metric, versionSourceId) { |
| 108 | + if ( |
| 109 | + metric.content?.cvss_v2?.version !== undefined && |
| 110 | + versionSourceId === |
| 111 | + createIdForVersionAndSource( |
| 112 | + metric.content?.cvss_v2.version, |
| 113 | + metric.source |
| 114 | + ) |
| 115 | + ) { |
| 116 | + return metric.content?.cvss_v2?.version |
| 117 | + } else if ( |
| 118 | + metric.content?.cvss_v3?.version !== undefined && |
| 119 | + versionSourceId === |
| 120 | + createIdForVersionAndSource( |
| 121 | + metric.content?.cvss_v3.version, |
| 122 | + metric.source |
| 123 | + ) |
| 124 | + ) { |
| 125 | + return metric.content?.cvss_v3?.version |
| 126 | + } else if ( |
| 127 | + metric.content?.cvss_v4?.version !== undefined && |
| 128 | + versionSourceId === |
| 129 | + createIdForVersionAndSource( |
| 130 | + metric.content?.cvss_v4.version, |
| 131 | + metric.source |
| 132 | + ) |
| 133 | + ) { |
| 134 | + return metric.content?.cvss_v4?.version |
| 135 | + } else { |
| 136 | + return null |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @param {Metric} metric |
| 142 | + * @param {Set<string>} versionSourceIdSet |
| 143 | + */ |
| 144 | + function addAllVersionSourceIdsInMetricToSet(metric, versionSourceIdSet) { |
| 145 | + if (metric.content?.cvss_v2?.version !== undefined) { |
| 146 | + versionSourceIdSet.add( |
| 147 | + createIdForVersionAndSource( |
| 148 | + metric.content?.cvss_v2.version, |
| 149 | + metric.source |
| 150 | + ) |
| 151 | + ) |
| 152 | + } |
| 153 | + if (metric.content?.cvss_v3?.version !== undefined) { |
| 154 | + versionSourceIdSet.add( |
| 155 | + createIdForVersionAndSource( |
| 156 | + metric.content?.cvss_v3.version, |
| 157 | + metric.source |
| 158 | + ) |
| 159 | + ) |
| 160 | + } |
| 161 | + if (metric.content?.cvss_v4?.version !== undefined) { |
| 162 | + versionSourceIdSet.add( |
| 163 | + createIdForVersionAndSource( |
| 164 | + metric.content?.cvss_v4.version, |
| 165 | + metric.source |
| 166 | + ) |
| 167 | + ) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + vulnerabilities.forEach((vulnerabilityItem, vulnerabilityIndex) => { |
| 172 | + /** @type {Map<string, Set<string>>} */ |
| 173 | + const versionsSourceIdSetByProduct = new Map() |
| 174 | + |
| 175 | + /** @type {Array<Metric> | undefined} */ |
| 176 | + const metrics = vulnerabilityItem.metrics |
| 177 | + metrics?.forEach((metric, metricIndex) => { |
| 178 | + /** @type {Array<Product> | undefined} */ |
| 179 | + const productsOfMetric = metric.products |
| 180 | + productsOfMetric?.forEach((product, productIndex) => { |
| 181 | + const versionSourceIdsOfProduct = |
| 182 | + versionsSourceIdSetByProduct.get(product) ?? new Set() |
| 183 | + versionsSourceIdSetByProduct.set(product, versionSourceIdsOfProduct) |
| 184 | + |
| 185 | + versionSourceIdsOfProduct.forEach((versionSourceIdOfProduct) => { |
| 186 | + const sameVersion = findCvssVersionWithSameVersionAndSource( |
| 187 | + metric, |
| 188 | + versionSourceIdOfProduct |
| 189 | + ) |
| 190 | + if (sameVersion) { |
| 191 | + isValid = false |
| 192 | + const sourceOfMetric = metric.source ? metric.source : '' |
| 193 | + errors.push({ |
| 194 | + message: `Product is member of more than one CVSS-Vectors with the same version '${sameVersion}' and same source '${sourceOfMetric}'.`, |
| 195 | + instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/products/${productIndex}`, |
| 196 | + }) |
| 197 | + } |
| 198 | + }) |
| 199 | + |
| 200 | + addAllVersionSourceIdsInMetricToSet(metric, versionSourceIdsOfProduct) |
| 201 | + }) |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + return { errors, isValid } |
| 206 | +} |
0 commit comments