-
Notifications
You must be signed in to change notification settings - Fork 9
Feat/197 mandatory tests csaf2.1 6.1.10 #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rainer-exxcellent
merged 10 commits into
main
from
feat/197-Mandatory_Tests_CSAF2_1_6.1.10
Jun 24, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
51b596d
feat(CSAF2.1): #197 copy and adapt mandatory test 6.1.10 from CSAF 2.…
rainer-exxcellent f3da6d5
feat(CSAF2.1): #197 copy and adapt mandatory test 6.1.10 from CSAF 2.…
rainer-exxcellent d16f011
feat(CSAF2.1): #197 copy and adapt mandatory test 6.1.10 from CSAF 2.…
rainer-exxcellent 67be39b
feat(CSAF2.1): #197 - test 6.1.10 - add input schema, fixed instanceP…
rainer-exxcellent 87662a6
feat(CSAF2.1): #197 - test 6.1.10 - removed convertMetricsCvss40.js
rainer-exxcellent 1c9bb6c
feat(CSAF2.1): #197 - test 6.1.10 - refactor some names
rainer-exxcellent d8311b9
feat(CSAF2.1): #197 - test 6.1.10 - changed input schema so that it r…
rainer-exxcellent 06b7247
feat(CSAF2.1): #197 - test 6.1.10 - use types from inputSchema
rainer-exxcellent 20fe65e
feat(CSAF2.1): #197 mandatory test 6.1.10 - move test in README to th…
rainer-exxcellent 628cd2a
feat(CSAF2.1): #197 mandatory test 6.1.7 - add version to cvss_v2 for…
rainer-exxcellent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
import * as cvss2 from '../../lib/shared/cvss2.js' | ||
import * as cvss3 from '../../lib/shared/cvss3.js' | ||
import * as cvss4 from '../../lib/shared/cvss4.js' | ||
import Ajv from 'ajv/dist/jtd.js' | ||
|
||
/** @typedef {import('ajv/dist/jtd.js').JTDDataType<typeof inputSchema>} InputSchema */ | ||
|
||
/** @typedef {InputSchema['vulnerabilities'][number]} Vulnerability */ | ||
|
||
/** @typedef {NonNullable<Vulnerability['metrics']>[number]} Metric */ | ||
|
||
/** @typedef {NonNullable<Metric['content']>} MetricContent */ | ||
|
||
const inputSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
properties: { | ||
vulnerabilities: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
metrics: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
content: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
cvss_v2: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
version: { type: 'string' }, | ||
}, | ||
}, | ||
cvss_v3: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
version: { type: 'string' }, | ||
}, | ||
}, | ||
cvss_v4: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
version: { type: 'string' }, | ||
rainer-exxcellent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
const ajv = new Ajv() | ||
const validateInput = ajv.compile(inputSchema) | ||
|
||
/** @type { Record<string, { jsonName:string, optionsByKey:Record<string, string>}>} */ | ||
const cvssV3MappingByMetricKey = Object.fromEntries( | ||
cvss3.mapping.map((mapping) => { | ||
return [ | ||
mapping[1], | ||
{ | ||
jsonName: mapping[0], | ||
optionsByKey: Object.fromEntries( | ||
Object.entries(mapping[2]).map(([key, value]) => [value, key]) | ||
), | ||
}, | ||
] | ||
}) | ||
) | ||
|
||
/** @type { Record<string, { jsonName:string, optionsByKey:Record<string, string>}>} */ | ||
const cvssV2MappingByMetricKey = Object.fromEntries( | ||
cvss2.mapping.map((mapping) => { | ||
return [ | ||
mapping[1], | ||
{ | ||
jsonName: mapping[0], | ||
optionsByKey: Object.fromEntries( | ||
Object.entries(mapping[2]).map(([key, value]) => [value.id, key]) | ||
), | ||
}, | ||
] | ||
}) | ||
) | ||
|
||
/** | ||
* @param {{optionName: string, optionValue: string, optionKey: string}[]} optionsArray | ||
* @return {Record<string, string>} | ||
*/ | ||
function convertOptionsArrayToObject(optionsArray) { | ||
/** @type {Record<string, string>} */ | ||
const result = {} | ||
optionsArray.forEach((option) => { | ||
result[option.optionKey] = option.optionValue | ||
}) | ||
return result | ||
} | ||
|
||
/** @type { Record<string, { jsonName:string, optionsByKey:Record<string, string>}>} */ | ||
const cvssV4MappingByMetricKey = Object.fromEntries( | ||
cvss4.flatMetrics.map((flatMetric) => { | ||
return [ | ||
flatMetric.metricShort, | ||
{ | ||
jsonName: flatMetric.jsonName, | ||
optionsByKey: convertOptionsArrayToObject(flatMetric.options), | ||
}, | ||
] | ||
}) | ||
) | ||
|
||
/** | ||
* @param {Metric} metric | ||
*/ | ||
function validateCvss2(metric) { | ||
if (typeof metric.content?.cvss_v2?.vectorString === 'string') { | ||
return validateCVSSAttributes( | ||
cvssV2MappingByMetricKey, | ||
metric.content.cvss_v2 | ||
) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
/** | ||
* @param {Metric} metric | ||
*/ | ||
function validateCvss3(metric) { | ||
if ( | ||
typeof metric?.content?.cvss_v3?.vectorString === 'string' && | ||
(metric.content.cvss_v3.version === '3.1' || | ||
metric.content.cvss_v3.version === '3.0') | ||
) { | ||
return validateCVSSAttributes( | ||
cvssV3MappingByMetricKey, | ||
metric.content.cvss_v3 | ||
) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
/** | ||
* @param {Metric} metric | ||
*/ | ||
function validateCvss4(metric) { | ||
if (typeof metric?.content?.cvss_v4?.vectorString === 'string') { | ||
return validateCVSSAttributes( | ||
cvssV4MappingByMetricKey, | ||
metric.content.cvss_v4 | ||
) | ||
} else { | ||
return [] | ||
} | ||
} | ||
|
||
/** | ||
* @param {unknown} doc | ||
*/ | ||
export function mandatoryTest_6_1_10(doc) { | ||
/** @type {Array<{ message: string; instancePath: string }>} */ | ||
const errors = [] | ||
|
||
if (!validateInput(doc)) { | ||
return { errors, isValid: true } | ||
} | ||
|
||
if (Array.isArray(doc.vulnerabilities)) { | ||
/** @type {Array<Vulnerability>} */ | ||
const vulnerabilities = doc.vulnerabilities | ||
vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => { | ||
if (!Array.isArray(vulnerability.metrics)) return | ||
/** @type {Array<Metric>} */ | ||
const metrics = vulnerability.metrics | ||
metrics.forEach((metric, metricIndex) => { | ||
validateCvss2(metric).forEach((attributeKey) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/cvss_v2/${attributeKey}`, | ||
message: 'value is not consistent with the vector string', | ||
}) | ||
}) | ||
|
||
validateCvss3(metric).forEach((attributeKey) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/cvss_v3/${attributeKey}`, | ||
message: 'value is not consistent with the vector string', | ||
}) | ||
}) | ||
|
||
validateCvss4(metric).forEach((attributeKey) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/cvss_v4/${attributeKey}`, | ||
message: 'value is not consistent with the vector string', | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
return { errors, isValid: errors.length === 0 } | ||
} | ||
|
||
/** | ||
* validate the cvss vector against the cvss properties | ||
* @param {Record<string, { jsonName:string, optionsByKey:Record<string, string>}>}mappingByMetricKey cvss version specific mapping | ||
* @param {Record<string, unknown>} cvss cvss object | ||
|
||
*/ | ||
function validateCVSSAttributes(mappingByMetricKey, cvss) { | ||
const vectorString = /** @type {string} */ (cvss.vectorString) | ||
const vectorValues = vectorString.split('/').slice(1) | ||
/** | ||
* @type {string[]} | ||
*/ | ||
const invalidKeys = [] | ||
vectorValues.forEach((vectorValue) => { | ||
const [vectorMetricKey, vectorOptionKey] = vectorValue.split(':') | ||
const mapping = mappingByMetricKey[vectorMetricKey] | ||
if (mapping) { | ||
const metricOptionValue = cvss[mapping.jsonName] | ||
if (typeof metricOptionValue == 'string') { | ||
const expectedOptionValue = mapping.optionsByKey[vectorOptionKey] | ||
if (metricOptionValue !== expectedOptionValue) { | ||
invalidKeys.push(mapping.jsonName) | ||
} | ||
} | ||
} | ||
}) | ||
return invalidKeys | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import assert from 'node:assert/strict' | ||
import { mandatoryTest_6_1_10 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_10.js' | ||
|
||
describe('mandatoryTest_6_1_10', function () { | ||
it('only runs on relevant documents', function () { | ||
assert.equal(mandatoryTest_6_1_10({ document: 'mydoc' }).isValid, true) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.