Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ The following tests are not yet implemented and therefore missing:
- Mandatory Test 6.1.46
- Mandatory Test 6.1.47
- Mandatory Test 6.1.48
- Mandatory Test 6.1.49
- Mandatory Test 6.1.50
- Mandatory Test 6.1.51
- Mandatory Test 6.1.52
Expand Down Expand Up @@ -435,6 +434,7 @@ export const mandatoryTest_6_1_38: DocumentTest
export const mandatoryTest_6_1_39: DocumentTest
export const mandatoryTest_6_1_40: DocumentTest
export const mandatoryTest_6_1_41: DocumentTest
export const mandatoryTest_6_1_49: DocumentTest
```

[(back to top)](#bsi-csaf-validator-lib)
Expand Down
1 change: 1 addition & 0 deletions csaf_2_1/mandatoryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ export { mandatoryTest_6_1_38 } from './mandatoryTests/mandatoryTests_6_1_38.js'
export { mandatoryTest_6_1_39 } from './mandatoryTests/mandatoryTest_6_1_39.js'
export { mandatoryTest_6_1_40 } from './mandatoryTests/mandatoryTest_6_1_40.js'
export { mandatoryTest_6_1_41 } from './mandatoryTests/mandatoryTest_6_1_41.js'
export { mandatoryTest_6_1_49 } from './mandatoryTests/mandatoryTest_6_1_49.js'
121 changes: 121 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_49.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Ajv from 'ajv/dist/jtd.js'
import { compareZonedDateTimes } from '../../lib/shared/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: {
document: {
additionalProperties: true,
properties: {
tracking: {
additionalProperties: true,
properties: {
revision_history: {
elements: {
additionalProperties: true,
optionalProperties: {
date: { type: 'string' },
},
},
},
status: { type: 'string' },
},
},
},
},
vulnerabilities: {
elements: {
additionalProperties: true,
optionalProperties: {
metrics: {
elements: {
additionalProperties: true,
optionalProperties: {
content: {
additionalProperties: true,
properties: {
ssvc_v1: {
additionalProperties: true,
optionalProperties: {
timestamp: { type: 'string' },
},
},
},
},
},
},
},
},
},
},
},
})

const validateInput = ajv.compile(inputSchema)

/**
* This implements the mandatory test 6.1.49 of the CSAF 2.1 standard.
*
* @param {any} doc
*/
export function mandatoryTest_6_1_49(doc) {
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}

if (!validateInput(doc)) {
return ctx
}

if (
doc.document.tracking.status === 'final' ||
doc.document.tracking.status === 'interim'
) {
const revisionHistory = doc.document.tracking.revision_history
if (revisionHistory) {
// sort the revision history (descending) and save the newest entry
const newestRevisionHistoryItem = revisionHistory
.filter((item) => item.date !== undefined)
.sort((a, b) =>
compareZonedDateTimes(
/** @type {string} */ (b.date),
/** @type {string} */ (a.date)
)
)[0]
doc.vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => {
vulnerability.metrics?.forEach((metric, metricIndex) => {
const ssvcTimestamp = metric.content?.ssvc_v1.timestamp
if (ssvcTimestamp) {
// compare the ssvcTimestamp with the date of the newest item in the revision history
if (
compareZonedDateTimes(
ssvcTimestamp,
/** @type {string} */ (newestRevisionHistoryItem.date)
) > 0
) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/timestamp`,
message:
`The document is in status ${doc.document.status} but the SSVC timestamp is newer ` +
`than the date of newest item in the revision_history`,
})
}
}
})
})
}
}

return ctx
}
53 changes: 53 additions & 0 deletions tests/csaf_2_1/mandatoryTest_6_1_49.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import assert from 'node:assert/strict'
import { mandatoryTest_6_1_49 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_49.js'
import { expect } from 'chai'

const failingInputSchemaTestWithEmptyVulnerability6_1_49 = {
document: {
tracking: {
revision_history: [
{
date: '2024-01-24T10:00:00.000Z',
},
],
status: 'final',
},
},
vulnerabilities: [
{}, // even this vulnerability is empty, the test should not fail due to the inputSchema
{
cve: 'CVE-1900-0001',
metrics: [
{
content: {
ssvc_v1: {
id: 'CVE-1900-0001',
schemaVersion: '1-0-1',
selections: [
{
name: 'Exploitation',
namespace: 'ssvc',
values: ['Active'],
version: '1.1.0',
},
],
timestamp: '2024-07-13T10:00:00.000Z',
},
},
},
],
},
],
}

describe('mandatoryTest_6_1_49', function () {
it('only runs on relevant documents', function () {
assert.equal(mandatoryTest_6_1_49({ document: 'mydoc' }).isValid, true)
})
it('test input schema with empty json object in vulnerabilities', async function () {
const result = mandatoryTest_6_1_49(
failingInputSchemaTestWithEmptyVulnerability6_1_49
)
expect(result.errors.length).to.eq(1)
})
})
1 change: 0 additions & 1 deletion tests/csaf_2_1/oasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const excluded = [
'6.1.46',
'6.1.47',
'6.1.48',
'6.1.49',
'6.1.50',
'6.1.51',
'6.1.52',
Expand Down
Loading