diff --git a/README.md b/README.md index 5753a4a1..eb48a6b0 100644 --- a/README.md +++ b/README.md @@ -318,7 +318,6 @@ The following tests are not yet implemented and therefore missing: - Mandatory Test 6.1.48 - Mandatory Test 6.1.49 - Mandatory Test 6.1.50 -- Mandatory Test 6.1.53 - Mandatory Test 6.1.54 - Mandatory Test 6.1.55 @@ -429,6 +428,7 @@ export const mandatoryTest_6_1_44: DocumentTest export const mandatoryTest_6_1_45: DocumentTest export const mandatoryTest_6_1_51: DocumentTest export const mandatoryTest_6_1_52: DocumentTest +export const mandatoryTest_6_1_53: DocumentTest ``` [(back to top)](#bsi-csaf-validator-lib) diff --git a/csaf_2_1/mandatoryTests.js b/csaf_2_1/mandatoryTests.js index c302572e..4edab1cd 100644 --- a/csaf_2_1/mandatoryTests.js +++ b/csaf_2_1/mandatoryTests.js @@ -64,3 +64,4 @@ export { mandatoryTest_6_1_44 } from './mandatoryTests/mandatoryTest_6_1_44.js' export { mandatoryTest_6_1_45 } from './mandatoryTests/mandatoryTest_6_1_45.js' export { mandatoryTest_6_1_51 } from './mandatoryTests/mandatoryTest_6_1_51.js' export { mandatoryTest_6_1_52 } from './mandatoryTests/mandatoryTest_6_1_52.js' +export { mandatoryTest_6_1_53 } from './mandatoryTests/mandatoryTest_6_1_53.js' diff --git a/csaf_2_1/mandatoryTests/mandatoryTest_6_1_53.js b/csaf_2_1/mandatoryTests/mandatoryTest_6_1_53.js new file mode 100644 index 00000000..1a32106f --- /dev/null +++ b/csaf_2_1/mandatoryTests/mandatoryTest_6_1_53.js @@ -0,0 +1,79 @@ +import Ajv from 'ajv/dist/jtd.js' +import { compareZonedDateTimes } from '../dateHelper.js' + +const ajv = new Ajv() + +/* + This is the jtd schema that needs to match the input document so that the + test is activated. If this schema doesn't match it normally means that the input + document does not validate against the csaf json schema or optional fields that + the test checks are not present. + */ +const inputSchema = /** @type {const} */ ({ + additionalProperties: true, + properties: { + vulnerabilities: { + elements: { + additionalProperties: true, + optionalProperties: { + first_known_exploitation_dates: { + elements: { + additionalProperties: true, + optionalProperties: { + date: { type: 'string' }, + exploitation_date: { type: 'string' }, + }, + }, + }, + }, + }, + }, + }, +}) + +const validate = ajv.compile(inputSchema) + +/** + * This implements the mandatory test 6.1.53 of the CSAF 2.1 standard. + * + * @param {any} doc + */ +export function mandatoryTest_6_1_53(doc) { + /* + The `ctx` variable holds the state that is accumulated during the test ran and is + finally returned by the function. + */ + const ctx = { + errors: + /** @type {Array<{ instancePath: string; message: string }>} */ ([]), + isValid: true, + } + + if (!validate(doc)) { + return ctx + } + + doc.vulnerabilities?.forEach((vulnerability, vulnerabilityIndex) => { + const exploitationDates = vulnerability.first_known_exploitation_dates || [] + exploitationDates.forEach((item, Index) => { + const date = item.date + const exploitationDate = item.exploitation_date + + if ( + compareZonedDateTimes( + /** @type {string} */ (date), + /** @type {string} */ (exploitationDate) + ) < 0 + ) { + ctx.isValid = false + ctx.errors.push({ + instancePath: `/vulnerabilities/${vulnerabilityIndex}/first_known_exploitation_dates/${Index}`, + message: + 'the "exploitation_date" is newer than the "date" which states when that information was last updated', + }) + } + }) + }) + + return ctx +} diff --git a/tests/csaf_2_1/mandatoryTest_6_1_53.js b/tests/csaf_2_1/mandatoryTest_6_1_53.js new file mode 100644 index 00000000..3a2ec48b --- /dev/null +++ b/tests/csaf_2_1/mandatoryTest_6_1_53.js @@ -0,0 +1,46 @@ +import assert from 'node:assert/strict' +import { mandatoryTest_6_1_53 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_53.js' + +describe('mandatoryTest_6_1_53', function () { + it('only runs on relevant documents', function () { + assert.equal(mandatoryTest_6_1_53({ document: 'mydoc' }).isValid, true) + }) + + it('skips empty vulnerability', function () { + assert.equal( + mandatoryTest_6_1_53({ + vulnerabilities: [ + {}, // should be ignored + { + first_known_exploitation_dates: [ + { + date: '2024-01-24T12:34:56.789Z', + exploitation_date: '2024-01-24T13:00:00.000Z', + }, + ], + }, + ], + }).isValid, + false + ) + }) + + it('skips empty first_known_exploitation_date', function () { + assert.equal( + mandatoryTest_6_1_53({ + vulnerabilities: [ + { + first_known_exploitation_dates: [ + {}, // should be ignored + { + date: '2024-01-24T12:34:56.789Z', + exploitation_date: '2024-01-24T13:00:00.000Z', + }, + ], + }, + ], + }).isValid, + false + ) + }) +})