-
Notifications
You must be signed in to change notification settings - Fork 9
196 csaf 2.1 optional test 6.2.27 #257
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ba87bf
feat: add optional test 6.2.10 to the list of excluded tests
christopher-exx 13024c4
feat: remove optional test 6.2.10 from the list of excluded tests again
christopher-exx c2ecfc6
feat: add optional test 6.2.27
bendo-eXX 9eca33f
rename Test to recommendedTest_6_2_27.js
bendo-eXX ce8f41d
fix: optional test 6.2.27 schema
bendo-eXX 2b2f141
fix: add recommendedTest_6_2_27.js for better codeCoverage
bendo-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,192 @@ | ||
import Ajv from 'ajv/dist/jtd.js' | ||
|
||
const ajv = new Ajv() | ||
|
||
/** | ||
* @typedef {'workaround' | ||
* | 'mitigation' | ||
* | 'vendor_fix' | ||
* | 'optional_patch' | ||
* | 'none_available' | ||
* | 'fix_planned' | ||
* | 'no_fix_planned'} Category | ||
*/ | ||
|
||
/** | ||
* @typedef {'first_affected' | ||
* | 'first_fixed' | ||
* | 'fixed' | ||
* | 'known_affected' | ||
* | 'known_not_affected' | ||
* | 'last_affected' | ||
* | 'recommended' | ||
* | 'under_investigation' | ||
* | 'unknown'} ProductStatus | ||
*/ | ||
|
||
/** | ||
* This map holds discouraged category combinations. | ||
* See https://github.com/oasis-tcs/csaf/blob/master/csaf_2.1/prose/share/csaf-v2.1-draft.md#324131-vulnerabilities-property---remediations---category- | ||
* | ||
* @type {Map<string, Set<string>>} | ||
*/ | ||
const discouragedRuleMap = new Map( | ||
/** @satisfies {Array<[Category, ProductStatus[]]>} */ ([ | ||
['workaround', ['under_investigation', 'unknown']], | ||
['mitigation', ['under_investigation', 'unknown']], | ||
['vendor_fix', ['under_investigation', 'unknown']], | ||
['optional_patch', ['first_fixed', 'fixed']], | ||
['fix_planned', ['known_not_affected', 'under_investigation', 'unknown']], | ||
['no_fix_planned', ['known_not_affected']], | ||
]).map((e) => [e[0], new Set(e[1])]) | ||
) | ||
|
||
const productStatusSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
optionalProperties: { | ||
first_affected: { elements: { type: 'string' } }, | ||
first_fixed: { elements: { type: 'string' } }, | ||
fixed: { elements: { type: 'string' } }, | ||
known_affected: { elements: { type: 'string' } }, | ||
known_not_affected: { elements: { type: 'string' } }, | ||
last_affected: { elements: { type: 'string' } }, | ||
recommended: { elements: { type: 'string' } }, | ||
under_investigation: { elements: { type: 'string' } }, | ||
unknown: { elements: { type: 'string' } }, | ||
}, | ||
}) | ||
|
||
const remediationSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
optionalProperties: { | ||
category: { type: 'string' }, | ||
group_ids: { | ||
elements: { type: 'string' }, | ||
}, | ||
product_ids: { | ||
elements: { type: 'string' }, | ||
}, | ||
}, | ||
}) | ||
|
||
const inputSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
optionalProperties: { | ||
product_tree: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
product_groups: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
group_id: { type: 'string' }, | ||
product_ids: { | ||
elements: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
properties: { | ||
vulnerabilities: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
product_status: productStatusSchema, | ||
remediations: { | ||
elements: remediationSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const validateInput = ajv.compile(inputSchema) | ||
|
||
/** | ||
* @param {any} doc | ||
*/ | ||
export function recommendedTest_6_2_27(doc) { | ||
const ctx = { | ||
warnings: | ||
/** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
} | ||
|
||
if (!validateInput(doc)) { | ||
return ctx | ||
} | ||
|
||
if (!doc.vulnerabilities) { | ||
return ctx // No vulnerabilities to check | ||
} | ||
|
||
for (const [vulnerabilityIndex, vulnerability] of Object.entries( | ||
doc.vulnerabilities | ||
)) { | ||
/** | ||
* This map holds all discovered product ids and maps them to the set of corresponding | ||
* remediation categories. Later we can check this map to find out if there are any | ||
* contradicting remediations. | ||
* | ||
* @type {Map<string, Set<string>>} | ||
*/ | ||
const productToCategoriesMap = new Map() | ||
|
||
const productStatus = new Map( | ||
Object.entries(vulnerability.product_status || {}) | ||
); | ||
vulnerability.remediations?.forEach((remediation, remediationIndex) => { | ||
const category = remediation.category | ||
if (!category) return | ||
|
||
/** | ||
* This function adds the current category to the given product id in the | ||
* `productMap`. If the product does not yet exist in the map, it is added. | ||
* | ||
* @param {string} id | ||
*/ | ||
const collectCategory = (id) => { | ||
productToCategoriesMap.set( | ||
id, | ||
new Set(productToCategoriesMap.get(id)).add(category) | ||
) | ||
} | ||
|
||
remediation.product_ids?.forEach(collectCategory) | ||
|
||
remediation.group_ids?.forEach((id) => { | ||
const group = doc.product_tree?.product_groups?.find( | ||
(g) => g.group_id === id | ||
) | ||
if (!group) return | ||
group.product_ids?.forEach(collectCategory) | ||
}) | ||
|
||
/** | ||
* Check for discouraged combinations of product status and remediation category. | ||
*/ | ||
for (const [productId, categories] of productToCategoriesMap) { | ||
for (const category of categories) { | ||
const status = discouragedRuleMap.get(category) | ||
if (!status) continue// There are no discouraged rules for this category. | ||
status.forEach((s) => { | ||
const statusList = productStatus.get(s); | ||
if (Array.isArray(statusList) && statusList.includes(productId)) { | ||
ctx.warnings.push({ | ||
instancePath: `/vulnerabilities/${vulnerabilityIndex}/remediations/${remediationIndex}`, | ||
message: `discouraged combination of product status ${s} and remediation category ${category} for product id "${productId}"`, | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
return ctx | ||
} |
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 |
---|---|---|
|
@@ -51,7 +51,6 @@ const excluded = [ | |
'6.2.24', | ||
'6.2.25', | ||
'6.2.26', | ||
'6.2.27', | ||
'6.2.30', | ||
'6.2.31', | ||
'6.2.32', | ||
|
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,11 @@ | ||
import assert from 'node:assert' | ||
import { recommendedTest_6_2_27 } from '../../csaf_2_1/recommendedTests.js' | ||
|
||
describe('recommendedTest_6_2_27', function () { | ||
it('only runs on relevant documents', function () { | ||
assert.equal( | ||
recommendedTest_6_2_27({ vulnerabilities: 'mydoc' }).warnings.length, | ||
0 | ||
) | ||
}) | ||
}) |
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.