Skip to content

feat: add mandatory test 6.1.36 #216

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

Draft
wants to merge 1 commit into
base: 196-csaf-2.1
Choose a base branch
from
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
1 change: 1 addition & 0 deletions csaf_2_1/mandatoryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export { mandatoryTest_6_1_1 } from './mandatoryTests/mandatoryTest_6_1_1.js'
export { mandatoryTest_6_1_8 } from './mandatoryTests/mandatoryTest_6_1_8.js'
export { mandatoryTest_6_1_34 } from './mandatoryTests/mandatoryTest_6_1_34.js'
export { mandatoryTest_6_1_35 } from './mandatoryTests/mandatoryTest_6_1_35.js'
export { mandatoryTest_6_1_36 } from './mandatoryTests/mandatoryTest_6_1_36.js'
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'
Expand Down
221 changes: 221 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_36.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
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'
* | 'known_affected'
* | 'last_affected'
* | 'known_not_affected'
* | 'first_fixed'
* | 'fixed'
* | 'under_investigation'} ProductStatus
*/

/*
The spec groups the product statuses in groups. This grouping is
expressed in this object.
*/
const productStatus = /**
* @type {const}
* @satisfies {Record<string, ProductStatus[]>}
*/ ({
affected: ['first_affected', 'known_affected', 'last_affected'],
notAffected: ['known_not_affected'],
fixed: ['first_fixed', 'fixed'],
underInvestigation: ['under_investigation'],
})

/**
* This map holds prohibited category / product status 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 prohibitionRuleMap = new Map(
/** @satisfies {Array<[Category, ProductStatus[]]>} */ ([
['workaround', [...productStatus.notAffected, ...productStatus.fixed]],
['mitigation', [...productStatus.notAffected, ...productStatus.fixed]],
['vendor_fix', [...productStatus.notAffected, ...productStatus.fixed]],
['optional_patch', [...productStatus.affected]],
['none_available', [...productStatus.notAffected, ...productStatus.fixed]],
['fix_planned', [...productStatus.fixed]],
['no_fix_planned', [...productStatus.fixed]],
]).map((e) => [e[0], new Set(e[1])])
)

const remediationSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
group_ids: {
elements: {
type: 'string',
},
},
product_ids: {
elements: {
type: 'string',
},
},
category: { type: 'string' },
},
})

/*
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,
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: {
remediations: {
elements: remediationSchema,
},
product_status: {
additionalProperties: true,
optionalProperties: {
first_affected: { elements: { type: 'string' } },
known_affected: { elements: { type: 'string' } },
last_affected: { elements: { type: 'string' } },
known_not_affected: { elements: { type: 'string' } },
first_fixed: { elements: { type: 'string' } },
fixed: { elements: { type: 'string' } },
under_investigation: { elements: { type: 'string' } },
},
},
},
},
},
},
})

const validate = ajv.compile(inputSchema)

/**
* This implements the mandatory test 6.1.36 of the CSAF 2.1 standard.
*
* @param {any} doc
*/
export function mandatoryTest_6_1_36(doc) {
/*
The `ctx` variable holds the state that is accumulated during the test ran and is
finally returned by the function.
*/
const ctx = {
/** @type {Array<{ instancePath: string; message: string }>} */
errors: [],
isValid: true,
}

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

for (const [
vulnerabilityIndex,
vulnerability,
] of doc.vulnerabilities.entries()) {
vulnerability.remediations?.forEach((remediation, remediationIndex) => {
const category = remediation.category
if (!category) return

/**
* This map holds the discovered product ids for the remediation and maps them to
* the set of corresponding product status names. Later we can check this map to
* find out if there are any contradicting remediations.
*
* @type {Map<string, Set<string>>}
*/
const productToProductStatusNamesMap = new Map()

/**
* This function adds all product status names for the given product id to the
* `productMap`. If the product does not yet exist in the map, it is added.
*
* @param {string} id
*/
const collectProductStatusNames = (id) => {
const productStatusNames =
/*
To speed things up we first check if the product status names where already
collected and do not search again. The product names are always for a
product in the same vulnerability.
*/
productToProductStatusNamesMap.get(id) ??
new Set(
/** @type {string[]} */ (
Object.entries(vulnerability.product_status ?? {})
.filter((e) =>
Array.isArray(e[1]) ? e[1].includes(id) : false
)
.map((e) => e[0])
)
)
productToProductStatusNamesMap.set(id, productStatusNames)
}

remediation.product_ids?.forEach(collectProductStatusNames)

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(collectProductStatusNames)
})

for (const [
productId,
productStatusNames,
] of productToProductStatusNamesMap) {
for (const productStatus of productStatusNames) {
if (prohibitionRuleMap.get(category)?.has(productStatus)) {
ctx.errors.push({
instancePath: `/vulnerabilities/${vulnerabilityIndex}/remediations/${remediationIndex}`,
message: `contradicting remediation product status combination for product id "${productId}": ${category}, ${productStatus}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we can improve the error message?

Suggested change
message: `contradicting remediation product status combination for product id "${productId}": ${category}, ${productStatus}`,
message: `contradicting remediation product status combination for product id "${productId}": product status is ${productStatus} but remediation category is ${category}`,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@domachine I guess, I'm missing a response to this comment...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about the suggested error message in https://github.com/secvisogram/csaf-validator-lib/pull/257/files#r2074269857? Please ensure that both follow the same pattern.

})
ctx.isValid = false
}
}
}
})
}

return ctx
}
1 change: 0 additions & 1 deletion tests/csaf_2_1/oasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const excluded = [
'6.1.13',
'6.1.14',
'6.1.16',
'6.1.36',
'6.1.37',
'6.1.42',
'6.1.43',
Expand Down