Skip to content
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ The following tests are not yet implemented and therefore missing:
- Mandatory Test 6.1.26
- Mandatory Test 6.1.27.13
- Mandatory Test 6.1.47
- Mandatory Test 6.1.48
- Mandatory Test 6.1.49
- Mandatory Test 6.1.50
- Mandatory Test 6.1.54
Expand Down Expand Up @@ -459,6 +458,7 @@ export const mandatoryTest_6_1_43: DocumentTest
export const mandatoryTest_6_1_44: DocumentTest
export const mandatoryTest_6_1_45: DocumentTest
export const mandatoryTest_6_1_46: DocumentTest
export const mandatoryTest_6_1_48: DocumentTest
export const mandatoryTest_6_1_51: DocumentTest
export const mandatoryTest_6_1_52: DocumentTest
export const mandatoryTest_6_1_53: DocumentTest
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 @@ -63,6 +63,7 @@ export { mandatoryTest_6_1_43 } from './mandatoryTests/mandatoryTest_6_1_43.js'
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_46 } from './mandatoryTests/mandatoryTest_6_1_46.js'
export { mandatoryTest_6_1_48 } from './mandatoryTests/mandatoryTest_6_1_48.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'
Expand Down
185 changes: 185 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_48.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { Ajv } from 'ajv/dist/jtd.js'
import ssvcDecisionPoints from '../../lib/ssvc/ssvc_decision_points.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: {
metrics: {
elements: {
additionalProperties: true,
optionalProperties: {
content: {
additionalProperties: true,
optionalProperties: {
ssvc_v1: {
additionalProperties: true,
optionalProperties: {
id: { type: 'string' },
schemaVersion: { type: 'string' },
timestamp: { type: 'string' },
selections: {
elements: {
additionalProperties: true,
optionalProperties: {
name: { type: 'string' },
namespace: { type: 'string' },
values: {
elements: { type: 'string' },
},
version: { type: 'string' },
},
},
},
},
},
},
},
},
},
},
},
},
},
},
})

/** @typedef {import('ajv/dist/jtd.js').JTDDataType<typeof inputSchema>['vulnerabilities'][number]} Vulnerability */

const validateInput = ajv.compile(inputSchema)

/**
* @param {string | undefined} name
* @param {string | undefined} namespace
* @param {string | undefined} version
*/
function decisionPointHash(name, namespace, version) {
Comment thread
christopher-exx marked this conversation as resolved.
return JSON.stringify({
name: name ?? '',
namespace: namespace ?? '',
version: version ?? '',
})
}

/** @type {Map<string,{ name: string; namespace: string; version: string; key?: string; values: { key: string; name: string; description: string; }[]; }>} */
const decisionPointMap = new Map(
ssvcDecisionPoints.decisionPoints.map((obj) => [
decisionPointHash(obj.name, obj.namespace, obj.version),
obj,
])
)

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

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

const registeredSsvcNamespaces = ['ssvc', 'cvss']
Comment thread
christopher-exx marked this conversation as resolved.

/** @type {Array<Vulnerability>} */
const vulnerabilities = doc.vulnerabilities
vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => {
vulnerability.metrics?.forEach((metric, metricIndex) => {
const selectionsWithRegisteredNamespace =
metric.content?.ssvc_v1?.selections?.filter(
(s) =>
s.namespace !== undefined &&
registeredSsvcNamespaces.includes(s.namespace)
)
selectionsWithRegisteredNamespace?.forEach(
(selection, selectionIndex) => {
// check if a decision point with these properties exists
const selectedDecisionPnt = decisionPointMap.get(
decisionPointHash(
selection.name,
selection.namespace,
selection.version
)
)

if (!selectedDecisionPnt) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/selections/${selectionIndex}`,
message: `there exists no decision point with name ${selection.name} and version ${selection.version} in the namespace ${selection.namespace}`,
})
} else {
if (
selection.values &&
!areValuesValidAndinOrder(
selectedDecisionPnt.values,
selection.values
)
) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/selections/${selectionIndex}`,
message: `this decision point contains invalid values or its values are not in order`,
Comment thread
christopher-exx marked this conversation as resolved.
})
}
}
}
)
})
})

return ctx
}

/**
* Check if the elements in the values array of the decision point are valid and if they are in the right order
* according to the specification.
* If values are missing, this is not an issue.
*
* @param {{ key: string; name: string; description: string; }[]} decisionPointValues the valid elements of the values array of the respective decision point
* and their order according to the SSVC specification
* @param {string[]} usedValues the actual used values of the decision point
*/
function areValuesValidAndinOrder(decisionPointValues, usedValues) {
const specifiedValues = decisionPointValues.map((value) => value.name)
//check if there is an invalid value used
for (let i = 0; i < usedValues.length; i++) {
const element = usedValues[i]
if (!specifiedValues.includes(element)) {
return false
}
}

// check for the correct order
for (let valueIndex = 0; valueIndex < usedValues.length; valueIndex++) {
const value = usedValues[valueIndex]
if (valueIndex === 0) {
continue
}
const specifiedIndexCurrentElement = specifiedValues.indexOf(value)
const previousValue = usedValues[valueIndex - 1]
const specifiedIndexPreviousElement = specifiedValues.indexOf(previousValue)

if (specifiedIndexCurrentElement < specifiedIndexPreviousElement) {
return false
}
}
return true
}
Loading
Loading