-
Notifications
You must be signed in to change notification settings - Fork 9
fix: add optional test 6.2.37 #292
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
Open
christopher-exx
wants to merge
17
commits into
main
Choose a base branch
from
196-csaf-2.1_optional_test_6.2.37
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ee6005f
Merge pull request #303 from secvisogram/feat/286-rename-optional-2-r…
rainer-exxcellent 784c045
feat: add mandatory test 6.1.37
domachine 5036c14
feat(CSAF2.1): #197 copy and adapt mandatory test 6.1.9 from CSAF 2.0…
domachine 10580af
feat(CSAF2.1): #197 test 6.1.9 - add test for safelyParseCVSSV2Vector…
rainer-exxcellent 97d4f11
feat(CSAF2.1): #197 test 6.1.9 - fix findings from review
rainer-exxcellent aa2dd67
refactor: tighten types
domachine c9c6b19
feat(CSAF2.1): #197 test 6.1.9 - rename import CVSS to CVSS30 to be …
rainer-exxcellent 80b5b35
feat(CSAF2.1): #197 test 6.1.9 - fixed invalid cvss4.0 json names
rainer-exxcellent 89bcc25
feat(CSAF2.1): #197 - test 6.1.9 fix and test updateFromVectorString
rainer-exxcellent a135c4a
feat(CSAF2.1): #197 - test 6.1.9 make severityRating uppercase
rainer-exxcellent 23dbddf
feat(CSAF2.1): #197 - test 6.1.9 comment test 'Updating from an inval…
rainer-exxcellent d9dda12
feat(CSAF2.1): #197 - test 6.1.9 - made severity Uppercase according …
rainer-exxcellent 60b5f05
feat(CSAF2.1): #197 - test 6.1.9 - changed optionalProperties to prop…
rainer-exxcellent a7e7792
Merge pull request #261 from secvisogram/197-csaf-2.1-mandatory-test-…
rainer-exxcellent 1a1f0ed
fix: add optional test 6.2.37
christopher-exx 516c8dd
refactor: rename optional test 6.2.37 to recommended test
christopher-exx 96da837
refactor: rename optional test 6.2.37 to recommended test
christopher-exx 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
import cvss2js from 'cvss2js' | ||
import { getEnvironmentalScoreFromVectorString } from '../../lib/shared/cvss2.js' | ||
import { cvss30 as CVSS30, cvss31 as CVSS31 } from '../../lib/shared/first.js' | ||
import Ajv from 'ajv/dist/jtd.js' | ||
import { calculateCvss4_0_Score } from '../../lib/shared/cvss4.js' | ||
|
||
const ajv = new Ajv() | ||
|
||
const inputSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
properties: { | ||
vulnerabilities: { | ||
elements: { | ||
additionalProperties: true, | ||
properties: { | ||
metrics: { | ||
elements: { | ||
additionalProperties: true, | ||
properties: { | ||
content: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
cvss_v2: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
baseScore: { type: 'float64' }, | ||
temporalScore: { type: 'float64' }, | ||
environmentalScore: { type: 'float64' }, | ||
}, | ||
}, | ||
cvss_v3: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
version: { type: 'string' }, | ||
baseScore: { type: 'float64' }, | ||
baseSeverity: { type: 'string' }, | ||
temporalScore: { type: 'float64' }, | ||
temporalSeverity: { type: 'string' }, | ||
environmentalScore: { type: 'float64' }, | ||
environmentalSeverity: { type: 'string' }, | ||
}, | ||
}, | ||
cvss_v4: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
vectorString: { type: 'string' }, | ||
version: { type: 'string' }, | ||
baseScore: { type: 'float64' }, | ||
baseSeverity: { type: 'string' }, | ||
threatScore: { type: 'float64' }, | ||
threatSeverity: { type: 'string' }, | ||
environmentalScore: { type: 'float64' }, | ||
environmentalSeverity: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const validateInput = ajv.compile(inputSchema) | ||
|
||
/** | ||
* @param {any} doc | ||
*/ | ||
export function mandatoryTest_6_1_9(doc) { | ||
/** @type {Array<{ message: string; instancePath: string }>} */ | ||
const errors = [] | ||
let isValid = true | ||
|
||
if (!validateInput(doc)) { | ||
return { errors, isValid } | ||
} | ||
|
||
const vulnerabilities = doc.vulnerabilities | ||
vulnerabilities.forEach((vulnerability, vulnerabilityIndex) => { | ||
const metrics = vulnerability.metrics | ||
metrics?.forEach((metric, metricIndex) => { | ||
calculateCvss2(metric).forEach((failedMetricName) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/scores/${metricIndex}/cvss_v2/${failedMetricName}`, | ||
message: 'invalid calculated value', | ||
}) | ||
}) | ||
|
||
calculateCvss3(metric).forEach((failedMetricName) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/scores/${metricIndex}/cvss_v3/${failedMetricName}`, | ||
message: 'invalid calculated value', | ||
}) | ||
}) | ||
|
||
calculateCvss4(metric).forEach((failedMetricName) => { | ||
errors.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/scores/${metricIndex}/cvss_v4/${failedMetricName}`, | ||
message: 'invalid calculated value', | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
return { errors, isValid: errors.length === 0 } | ||
} | ||
|
||
/** | ||
* @param {string} vectorString | ||
* @returns | ||
*/ | ||
function safelyParseCVSSV2Vector(vectorString) { | ||
try { | ||
return { | ||
success: true, | ||
baseMetricScore: cvss2js.getBaseScore(vectorString), | ||
temporalMetricScore: cvss2js.getTemporalScore(vectorString), | ||
environmentalMetricScore: | ||
getEnvironmentalScoreFromVectorString(vectorString), | ||
} | ||
} catch (e) { | ||
return { | ||
success: false, | ||
baseMetricScore: -1, | ||
temporalMetricScore: -1, | ||
environmentalMetricScore: -1, | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {any} metric | ||
* @return {string[]} | ||
*/ | ||
function calculateCvss2(metric) { | ||
const failedMetrics = [] | ||
if ( | ||
metric.content?.cvss_v2 && | ||
typeof metric.content.cvss_v2.vectorString === 'string' | ||
) { | ||
const cvssV2 = metric.content.cvss_v2 | ||
const result = safelyParseCVSSV2Vector(metric.content.cvss_v2.vectorString) | ||
|
||
if (result.success) { | ||
for (const { score, expectedScore, name } of [ | ||
{ | ||
score: cvssV2.baseScore, | ||
expectedScore: result.baseMetricScore, | ||
name: 'baseScore', | ||
}, | ||
{ | ||
score: cvssV2.temporalScore, | ||
expectedScore: result.temporalMetricScore, | ||
name: 'temporalScore', | ||
}, | ||
{ | ||
score: cvssV2.environmentalScore, | ||
expectedScore: result.environmentalMetricScore, | ||
name: 'environmentalScore', | ||
}, | ||
]) { | ||
if (typeof score === 'number') { | ||
if (score !== Number(expectedScore)) { | ||
failedMetrics.push(name) | ||
} | ||
} | ||
} | ||
} else { | ||
// Invalid CVSS string is tested in test 6.1.8 | ||
} | ||
} | ||
|
||
return failedMetrics | ||
} | ||
|
||
/** | ||
* @param {any} metric | ||
* @return {string[]} | ||
*/ | ||
function calculateCvss3(metric) { | ||
const failedMetrics = [] | ||
if ( | ||
metric.content?.cvss_v3 && | ||
typeof metric.content.cvss_v3.vectorString === 'string' && | ||
(metric.content.cvss_v3.version === '3.1' || | ||
metric.content.cvss_v3.version === '3.0') | ||
) { | ||
const calculator = | ||
metric.content.cvss_v3.version === '3.0' ? CVSS30 : CVSS31 | ||
const result = calculator.calculateCVSSFromVector( | ||
metric.content.cvss_v3.vectorString | ||
) | ||
|
||
if (result.success) { | ||
for (const { score: scoreValue, expectedScore, name } of [ | ||
{ | ||
score: metric.content.cvss_v3.baseScore, | ||
expectedScore: result.baseMetricScore, | ||
name: 'baseScore', | ||
}, | ||
{ | ||
score: metric.content.cvss_v3.temporalScore, | ||
expectedScore: result.temporalMetricScore, | ||
name: 'temporalScore', | ||
}, | ||
{ | ||
score: metric.content.cvss_v3.environmentalScore, | ||
expectedScore: result.environmentalMetricScore, | ||
name: 'environmentalScore', | ||
}, | ||
]) { | ||
if (typeof scoreValue === 'number') { | ||
if (scoreValue !== Number(expectedScore)) { | ||
failedMetrics.push(name) | ||
} | ||
} | ||
} | ||
|
||
for (const { severity, expectedSeverity, name } of [ | ||
{ | ||
severity: metric.content.cvss_v3.baseSeverity, | ||
expectedSeverity: result.baseSeverity, | ||
name: 'baseSeverity', | ||
}, | ||
{ | ||
severity: metric.content.cvss_v3.temporalSeverity, | ||
expectedSeverity: result.temporalSeverity, | ||
name: 'temporalSeverity', | ||
}, | ||
{ | ||
severity: metric.content.cvss_v3.environmentalSeverity, | ||
expectedSeverity: result.environmentalSeverity, | ||
name: 'environmentalSeverity', | ||
}, | ||
]) { | ||
if (typeof severity === 'string') { | ||
if (severity !== expectedSeverity.toUpperCase()) { | ||
failedMetrics.push(name) | ||
} | ||
} | ||
} | ||
} else { | ||
// Invalid CVSS is tested in test 6.1.8 | ||
} | ||
} | ||
return failedMetrics | ||
} | ||
|
||
/** | ||
* @param {any} metric | ||
* @return {string[]} | ||
*/ | ||
function calculateCvss4(metric) { | ||
/** | ||
* @type {string[]} | ||
*/ | ||
const failedMetrics = [] | ||
if ( | ||
metric.content?.cvss_v4 && | ||
typeof metric.content.cvss_v4.vectorString === 'string' | ||
) { | ||
const scores = calculateCvss4_0_Score(metric.content.cvss_v4.vectorString) | ||
scores.forEach((score) => { | ||
const expectedScore = metric.content.cvss_v4[score.scoreJsonName] | ||
const expectedSeverity = metric.content.cvss_v4[score.severityJsonName] | ||
if (typeof expectedScore === 'number' && score.score !== expectedScore) { | ||
failedMetrics.push(score.scoreJsonName) | ||
} | ||
|
||
if ( | ||
typeof expectedSeverity === 'string' && | ||
score.severity.toUpperCase() !== expectedSeverity.toUpperCase() | ||
) { | ||
failedMetrics.push(score.severityJsonName) | ||
} | ||
}) | ||
} | ||
return failedMetrics | ||
} |
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,72 @@ | ||||||
import Ajv from 'ajv/dist/jtd.js' | ||||||
|
||||||
const ajv = new Ajv() | ||||||
|
||||||
const inputSchema = /** @type {const} */ ({ | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
vulnerabilities: { | ||||||
elements: { | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
metrics: { | ||||||
elements: { | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
content: { | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
ssvc_v1: { | ||||||
additionalProperties: true, | ||||||
properties: { | ||||||
role: { | ||||||
type: 'string', | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}) | ||||||
|
||||||
const validate = ajv.compile(inputSchema) | ||||||
|
||||||
/** | ||||||
* This implements the recommended test 6.2.37 of the CSAF 2.1 standard. | ||||||
* | ||||||
* @param {any} doc | ||||||
*/ | ||||||
export function recommendedTest_6_2_37(doc) { | ||||||
/** @type {Array<{ message: string; instancePath: string }>} */ | ||||||
const warnings = [] | ||||||
const context = { warnings } | ||||||
|
||||||
if (!validate(doc)) { | ||||||
return context | ||||||
} | ||||||
|
||||||
/* | ||||||
* Please note that this list can change | ||||||
* */ | ||||||
const registeredSsvcRoles = ['Supplier', 'Deployer', 'Coordinator'] | ||||||
|
||||||
doc.vulnerabilities?.forEach((vulnerability, vulnerabilityIndex) => { | ||||||
vulnerability.metrics.forEach((metric, metricIndex) => { | ||||||
const role = metric.content.ssvc_v1.role | ||||||
if (!registeredSsvcRoles.includes(role)) { | ||||||
context.warnings.push({ | ||||||
message: `The used role "${role}" is not a registered role`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should list here the 3 possibles roles There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/metrics/${metricIndex}/content/ssvc_v1/role`, | ||||||
}) | ||||||
} | ||||||
}) | ||||||
}) | ||||||
|
||||||
return context | ||||||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We (@domachine and I) decided to us a lighter approach using
optionalProperties
. (Applies to all array items.)