From d4d86993465222a72ed509e35ffee90ea0fba196 Mon Sep 17 00:00:00 2001 From: Philipp Backes Date: Thu, 12 Nov 2026 10:04:12 +0100 Subject: [PATCH 1/3] enrich CSAF 2.1 properties and markdown fields for the HTML template --- lib/enrichDocument.js | 213 ++++++++++++++++++++++++++++++++++++++++-- lib/markdownParser.js | 12 +++ 2 files changed, 218 insertions(+), 7 deletions(-) diff --git a/lib/enrichDocument.js b/lib/enrichDocument.js index 9ecca14..8cb534f 100644 --- a/lib/enrichDocument.js +++ b/lib/enrichDocument.js @@ -112,6 +112,20 @@ const collectProductIds = (document) => { } } + const productPaths = document.product_tree?.product_paths + if (productPaths) { + for (let i = 0; i < productPaths.length; ++i) { + const fullProductName = productPaths[i]?.full_product_name + if (fullProductName?.product_id) { + entries.push({ + id: fullProductName.product_id, + name: fullProductName.name ?? '', + instancePath: `/product_tree/product_paths/${i}/full_product_name/product_id`, + }) + } + } + } + const branches = document.product_tree?.branches if (branches) { traverseBranches(branches, entries, '/product_tree/branches') @@ -147,8 +161,10 @@ const collectGroupIds = (document) => { /** * @param {any} document + * @param {Map} productIds + * @param {Map} groupIds */ -const addDocumentNotesPreviewAttributes = (document) => { +const addDocumentNotesPreviewAttributes = (document, productIds, groupIds) => { const summary = [] const details = [] const general = [] @@ -162,6 +178,7 @@ const addDocumentNotesPreviewAttributes = (document) => { if (notes) { for (let i = 0; i < notes.length; ++i) { const note = notes[i] + extendScopedItem(note, productIds, groupIds) switch (note.category) { case 'summary': summary.push(note) @@ -202,8 +219,14 @@ const addDocumentNotesPreviewAttributes = (document) => { /** * @param {any} vulnerability + * @param {Map} productIds + * @param {Map} groupIds */ -const addVulnerabilityNotesPreviewAttributes = (vulnerability) => { +const addVulnerabilityNotesPreviewAttributes = ( + vulnerability, + productIds, + groupIds, +) => { const summary = [] const details = [] const general = [] @@ -217,6 +240,7 @@ const addVulnerabilityNotesPreviewAttributes = (vulnerability) => { if (notes) { for (let i = 0; i < notes.length; ++i) { const note = notes[i] + extendScopedItem(note, productIds, groupIds) switch (note.category) { case 'summary': summary.push(note) @@ -378,6 +402,23 @@ const extendRemediationOrThreat = ( } } +/** + * Like `extendRemediationOrThreat`, but for items where `product_ids` and + * `group_ids` are optional extras (e.g. CSAF 2.1 notes, vulnerability ids, + * first known exploitation dates, involvement actions): items without either + * property are left untouched, so documents that don't use them (all of CSAF + * 2.0) keep their exact shape. + * + * @param {any} item + * @param {Map} extProductIds + * @param {Map} extGroupIds + */ +const extendScopedItem = (item, extProductIds, extGroupIds) => { + if (item && (item.product_ids || item.group_ids)) { + extendRemediationOrThreat(item, extProductIds, extGroupIds) + } +} + /** * Sorts by `date` descending (most recent first), with entries that have no * `date` at all (a valid, optional field on CSAF remediations/threats) @@ -410,8 +451,10 @@ const addRemediationsPreviewAttributes = ( groupIds, ) => { const vendorFix = [] + const optionalPatch = [] const mitigation = [] const workaround = [] + const fixPlanned = [] const noneAvailable = [] const noFixPlanned = [] const unknown = [] @@ -424,12 +467,18 @@ const addRemediationsPreviewAttributes = ( case 'vendor_fix': vendorFix.push(remediation) break + case 'optional_patch': + optionalPatch.push(remediation) + break case 'mitigation': mitigation.push(remediation) break case 'workaround': workaround.push(remediation) break + case 'fix_planned': + fixPlanned.push(remediation) + break case 'none_available': noneAvailable.push(remediation) break @@ -443,8 +492,10 @@ const addRemediationsPreviewAttributes = ( } vulnerability.remediations_vendor_fix = vendorFix.sort(sortByDate) + vulnerability.remediations_optional_patch = optionalPatch.sort(sortByDate) vulnerability.remediations_mitigation = mitigation.sort(sortByDate) vulnerability.remediations_workaround = workaround.sort(sortByDate) + vulnerability.remediations_fix_planned = fixPlanned.sort(sortByDate) vulnerability.remediations_none_available = noneAvailable.sort(sortByDate) vulnerability.remediations_no_fix_planned = noFixPlanned.sort(sortByDate) vulnerability.remediations_unknown = unknown.sort(sortByDate) @@ -746,7 +797,132 @@ const addProductStatusPreviewAttributesV2_1 = ( extendedMetricIds, productIds, ) + productStatus.unknown = extendProductStatus( + productStatus.unknown, + extendedMetricIds, + productIds, + ) + } +} + +/** + * Resolves the CSAF 2.1 `document.involvement` references into display names: + * entity/entity group refs in actions and entity ids in entity groups become + * `{id, name}` pairs (falling back to the id when the entity has no `name`), + * and product/group ids of actions are resolved like for remediations. + * + * @param {any} document + * @param {Map} productIds + * @param {Map} groupIds + */ +const addInvolvementPreviewAttributes = (document, productIds, groupIds) => { + const involvement = document?.involvement + if (!involvement) return + + /** @type {Map} */ + const entityNames = new Map() + for (const entity of involvement.entities ?? []) { + entity.display_name = entity.name ?? entity.entity_id + entityNames.set(entity.entity_id, entity.display_name) + } + for (const group of involvement.entity_groups ?? []) { + entityNames.set(group.entity_group_id, group.name ?? group.entity_group_id) + } + + /** @param {string[] | undefined} ids */ + const toEntityRefs = (ids) => + ids + ? ids + .filter(Boolean) + .map((id) => ({ id, name: entityNames.get(id) ?? id })) + : undefined + + for (const group of involvement.entity_groups ?? []) { + group.entity_ids = toEntityRefs(group.entity_ids) + } + for (const action of involvement.actions ?? []) { + action.acting_entity_refs = toEntityRefs(action.acting_entity_refs) + action.receiving_entity_refs = toEntityRefs(action.receiving_entity_refs) + extendScopedItem(action, productIds, groupIds) + } +} + +/** + * Condenses a (SSVC v2 selection list) into plain strings the template can + * print without having to pick between optional `name` and required `key` + * fields itself. + * + * @param {any} ssvc + */ +const summarizeSsvc = (ssvc) => ({ + timestamp: ssvc.timestamp, + selections: (ssvc.selections ?? []).map((/** @type {any} */ selection) => ({ + label: selection.name ?? selection.key, + namespace: selection.namespace, + version: selection.version, + values: (selection.values ?? []) + .map((/** @type {any} */ value) => value.name ?? value.key) + .join(', '), + })), +}) + +/** + * Collects the parts of CSAF 2.1 `vulnerability.metrics[]` that are not CVSS + * (which is already shown in the product status tables): the qualitative + * severity rating, EPSS and SSVC v2 data, together with the products and + * source they belong to. Written to `vulnerability.metrics_details`. + * + * @param {any} vulnerability + * @param {Map} productIds + */ +const addMetricsDetailsPreviewAttributes = (vulnerability, productIds) => { + const details = [] + for (const metric of vulnerability.metrics ?? []) { + const content = metric?.content + if (!content) continue + const rating = content.qualitative_severity_rating + const epss = content.epss + const ssvc = content.ssvc_v2 + if (!rating && !epss && !ssvc) continue + details.push({ + products: (metric.products ?? []) + .filter(Boolean) + .map((/** @type {string} */ id) => ({ + id, + name: productIds.get(id)?.name || id, + })), + source: metric.source, + qualitative_severity_rating: rating, + epss, + ssvc: ssvc ? summarizeSsvc(ssvc) : undefined, + }) + } + vulnerability.metrics_details = details +} + +/** + * Resolves product/group ids of the CSAF 2.1 vulnerability properties that + * can be scoped to products (`ids`, `first_known_exploitation_dates`) and + * collects the `ids` that actually are scoped in `vulnerability.ids_scoped`. + * + * @param {any} vulnerability + * @param {Map} productIds + * @param {Map} groupIds + */ +const addVulnerabilityScopePreviewAttributes = ( + vulnerability, + productIds, + groupIds, +) => { + for (const item of vulnerability.first_known_exploitation_dates ?? []) { + extendScopedItem(item, productIds, groupIds) + } + const scopedIds = [] + for (const id of vulnerability.ids ?? []) { + extendScopedItem(id, productIds, groupIds) + if (id?.product_ids?.length || id?.group_ids?.length) scopedIds.push(id) } + vulnerability.ids_scoped = scopedIds } // --------------------------------------------------------------------------- @@ -769,7 +945,11 @@ export function enrichDocumentV2_0(document) { templateDoc.document.max_base_score = retrieveMaxBaseScoreV2_0( templateDoc.vulnerabilities, ) - addDocumentNotesPreviewAttributes(templateDoc.document) + addDocumentNotesPreviewAttributes( + templateDoc.document, + productIds, + groupIds, + ) } if (templateDoc.product_tree) { @@ -787,7 +967,11 @@ export function enrichDocumentV2_0(document) { ) addRemediationsPreviewAttributes(vulnerability, productIds, groupIds) addThreatsPreviewAttributes(vulnerability, productIds, groupIds) - addVulnerabilityNotesPreviewAttributes(vulnerability) + addVulnerabilityNotesPreviewAttributes( + vulnerability, + productIds, + groupIds, + ) } } @@ -801,7 +985,7 @@ export function enrichDocumentV2_0(document) { /** * Enriches a CSAF 2.1 document for use with HTMLTemplate2_1. - * Uses `vulnerability.metrics[i].content.cvss_v3` for CVSS data. + * Uses `vulnerability.metrics[i].content` (CVSS v2/v3/v4) for CVSS data. * * @param {any} document * @returns {{ document: any }} @@ -819,7 +1003,12 @@ export function enrichDocumentV2_1(document) { templateDoc.document.max_base_score_cvss_label = maxBaseScoreCvssVersion ? `CVSSv${maxBaseScoreCvssVersion}` : 'CVSS' - addDocumentNotesPreviewAttributes(templateDoc.document) + addDocumentNotesPreviewAttributes( + templateDoc.document, + productIds, + groupIds, + ) + addInvolvementPreviewAttributes(templateDoc.document, productIds, groupIds) } if (templateDoc.product_tree) { @@ -837,7 +1026,17 @@ export function enrichDocumentV2_1(document) { ) addRemediationsPreviewAttributes(vulnerability, productIds, groupIds) addThreatsPreviewAttributes(vulnerability, productIds, groupIds) - addVulnerabilityNotesPreviewAttributes(vulnerability) + addVulnerabilityNotesPreviewAttributes( + vulnerability, + productIds, + groupIds, + ) + addVulnerabilityScopePreviewAttributes( + vulnerability, + productIds, + groupIds, + ) + addMetricsDetailsPreviewAttributes(vulnerability, productIds) } } diff --git a/lib/markdownParser.js b/lib/markdownParser.js index cfd9496..eb01f47 100644 --- a/lib/markdownParser.js +++ b/lib/markdownParser.js @@ -13,7 +13,11 @@ const { get, set } = lodash const markdownFields = [ 'document.acknowledgments.*.summary', 'document.distribution.text', + 'document.involvement.actions.*.summary', + 'document.involvement.entities.*.contact.details', + 'document.involvement.entity_groups.*.summary', 'document.notes.*.text', + 'document.publisher.contact.details', 'document.publisher.issuing_authority', 'document.references.*.summary', 'document.tracking.revision_history.*.summary', @@ -36,6 +40,14 @@ const markdownFields = [ // as raw, unrendered markdown wherever it's referenced via group_ids. 'vulnerabilities.*.remediations.*.group_ids.*.name', 'vulnerabilities.*.threats.*.group_ids.*.name', + // CSAF 2.1: same reasoning as above, for every other item enrichDocument + // resolves `group_ids` on (notes, involvement actions, vulnerability ids + // and first known exploitation dates). + 'document.notes.*.group_ids.*.name', + 'document.involvement.actions.*.group_ids.*.name', + 'vulnerabilities.*.notes.*.group_ids.*.name', + 'vulnerabilities.*.ids.*.group_ids.*.name', + 'vulnerabilities.*.first_known_exploitation_dates.*.group_ids.*.name', ] /** From d5c66a546f9fa1fdc6d0aa30af51fc120e33fa59 Mon Sep 17 00:00:00 2001 From: Philipp Backes Date: Thu, 12 Nov 2026 10:04:39 +0100 Subject: [PATCH 2/3] render CSAF 2.1 specific content separately from CSAF 2.0 in the templates --- lib/HTMLTemplate2_0.js | 2 + lib/HTMLTemplate2_1.js | 4 + lib/mustachePartials.js | 39 ++++++- lib/templates/csaf_2_0.js | 31 ++++-- lib/templates/csaf_2_1.js | 152 +++++++++++++++++++++++++-- lib/templates/sharedTemplateParts.js | 106 ++++++++++++++----- 6 files changed, 290 insertions(+), 44 deletions(-) diff --git a/lib/HTMLTemplate2_0.js b/lib/HTMLTemplate2_0.js index 4c6b0ef..1d36384 100644 --- a/lib/HTMLTemplate2_0.js +++ b/lib/HTMLTemplate2_0.js @@ -5,6 +5,7 @@ import { DOCUMENT_NOTE, REFERENCE, REMEDIATION, + SCOPE, THREAT, URL, VULNERABILITY_NOTE, @@ -41,6 +42,7 @@ export default function HTMLTemplate2_0({ document }) { document_note: DOCUMENT_NOTE, acknowledgment: ACKNOWLEDGEMENT, reference: REFERENCE, + scope: SCOPE, url: URL, }) } diff --git a/lib/HTMLTemplate2_1.js b/lib/HTMLTemplate2_1.js index db27d33..c32bc83 100644 --- a/lib/HTMLTemplate2_1.js +++ b/lib/HTMLTemplate2_1.js @@ -2,9 +2,11 @@ import Mustache from 'mustache' import Template from './templates/csaf_2_1.js' import { ACKNOWLEDGEMENT, + CONTACT, DOCUMENT_NOTE, REFERENCE, REMEDIATION, + SCOPE, THREAT, URL, VULNERABILITY_NOTE, @@ -43,6 +45,8 @@ export default function HTMLTemplate2_1({ document }) { document_note: DOCUMENT_NOTE, acknowledgment: ACKNOWLEDGEMENT, reference: REFERENCE, + scope: SCOPE, + contact: CONTACT, url: URL, }) } diff --git a/lib/mustachePartials.js b/lib/mustachePartials.js index 5438b87..bd2d1ec 100644 --- a/lib/mustachePartials.js +++ b/lib/mustachePartials.js @@ -1,5 +1,7 @@ // Mustache partials shared between HTMLTemplate2_0.js and HTMLTemplate2_1.js. -// These are identical between CSAF 2.0 and 2.1, unlike PRODUCT_STATUS_HEADER +// These are identical between CSAF 2.0 and 2.1 (the 2.1-only properties they +// render, like note product_ids, are simply absent in 2.0 documents), unlike +// PRODUCT_STATUS_HEADER // and PRODUCT_STATUS_ROW (which differ - CSAF 2.1 adds a CVSS Version // column), so those two stay defined separately in each HTMLTemplate file. @@ -51,14 +53,45 @@ export const THREAT = ` {{/group_ids.length}}` +// Lists the products/groups an item is scoped to. Only CSAF 2.1 has items +// carrying `product_ids`/`group_ids` besides remediations and threats (notes, +// vulnerability ids, first known exploitation dates, involvement actions); for +// all other items, and for all CSAF 2.0 documents, this renders nothing. +export const SCOPE = ` +{{#product_ids.length}} +
For products:
+
    + {{#product_ids}} +
  • {{name}}
  • + {{/product_ids}} +
+{{/product_ids.length}} +{{#group_ids.length}} +
For groups:
+
    + {{#group_ids}} +
  • {{{name}}}
  • + {{/group_ids}} +
+{{/group_ids.length}}` + +// CSAF 2.1 `contact_t` (publisher.contact, involvement.entities[].contact). +export const CONTACT = ` +{{#details}}

{{{.}}}

{{/details}} +{{#email}}

Email: {{.}}

{{/email}} +{{#url}}

URL: {{> url}}

{{/url}} +{{#public_openpgp_key_url}}

Public OpenPGP key: {{> url}}

{{/public_openpgp_key_url}}` + export const VULNERABILITY_NOTE = ` {{#title}}{{.}}{{/title}}{{#audience}} ({{.}}){{/audience}} -{{#text}}

{{{text}}}

{{/text}}` +{{#text}}

{{{text}}}

{{/text}} +{{> scope}}` export const DOCUMENT_NOTE = ` {{#title}}

{{.}}

{{/title}} {{#audience}}{{.}}{{/audience}} -{{#text}}

{{{text}}}

{{/text}}` +{{#text}}

{{{text}}}

{{/text}} +{{> scope}}` export const ACKNOWLEDGEMENT = ` {{#.}} diff --git a/lib/templates/csaf_2_0.js b/lib/templates/csaf_2_0.js index 4e59d50..5f2ad67 100644 --- a/lib/templates/csaf_2_0.js +++ b/lib/templates/csaf_2_0.js @@ -1,4 +1,4 @@ -import { BIG, HEAD, MID, TAIL } from './sharedTemplateParts.js' +import { buildTemplate } from './sharedTemplateParts.js' const CVSS = ` CVSSv3.1 Base Score: {{document.max_base_score}}` @@ -25,9 +25,26 @@ const CWE = ` {{/release_date}}` -export default `${HEAD} -${CVSS} -${MID} -${CWE} -${BIG} -${TAIL}` +// CSAF 2.0 only: removed in CSAF 2.1 in favor of document.involvement. +const INVOLVEMENTS = ` + + {{#involvements.length}} +

Involvement

+
    + {{#involvements}} + {{#.}} +
  • {{#date}}{{.}} {{/date}}{{#upperCase}}{{party}}{{/upperCase}}{{#status}} ({{#replaceUnderscores}}{{.}}{{/replaceUnderscores}}){{/status}}{{#summary}}: {{{.}}}{{/summary}}
  • + {{/.}} + {{/involvements}} +
+ {{/involvements.length}}` + +// CSAF 2.0: `contact_details` is a plain string. +const PUBLISHER_CONTACT = `

{{contact_details}}

` + +export default buildTemplate({ + cvss: CVSS, + vulnerabilityRows: CWE, + involvements: INVOLVEMENTS, + publisherContact: PUBLISHER_CONTACT, +}) diff --git a/lib/templates/csaf_2_1.js b/lib/templates/csaf_2_1.js index 2ca7d17..db94046 100644 --- a/lib/templates/csaf_2_1.js +++ b/lib/templates/csaf_2_1.js @@ -1,7 +1,14 @@ -import { BIG, HEAD, MID, TAIL } from './sharedTemplateParts.js' +import { buildTemplate } from './sharedTemplateParts.js' const CVSS = ` {{document.max_base_score_cvss_label}} Base Score: {{document.max_base_score}}` +const LICENSE_EXPRESSION = ` + {{#document.license_expression}} + + License expression: {{.}} + + {{/document.license_expression}}` + const CWE = `

CWE's

{{#cwes}} @@ -25,7 +32,131 @@ const CWE = `

CWE's

Disclosure date: {{disclosure_date}} - {{/disclosure_date}}` + {{/disclosure_date}} + {{#dl_vuln_id}} + + Document-local ID: + {{.}} + + {{/dl_vuln_id}}` + +const PRODUCT_STATUS_UNKNOWN = ` + + {{#product_status.unknown.length}} +
Unknown
+
    + {{#product_status.unknown}} +
  • {{name}}
  • + {{/product_status.unknown}} +
+ {{/product_status.unknown.length}}` + +const VULNERABILITY_EXTRA = ` + + {{#first_known_exploitation_dates.length}} +

First known exploitation dates

+ {{#first_known_exploitation_dates}} +
{{exploitation_date}}{{#date}} (information from {{.}}){{/date}}
+ {{> scope}} + {{/first_known_exploitation_dates}} + {{/first_known_exploitation_dates.length}} + + {{#ids_scoped.length}} +

Scoped IDs

+ {{#ids_scoped}} +
{{text}}{{#system_name}} ({{.}}){{/system_name}}
+ {{> scope}} + {{/ids_scoped}} + {{/ids_scoped.length}} + + {{#metrics_details.length}} +

Metrics

+ {{#metrics_details}} +
    + {{#qualitative_severity_rating}} +
  • Qualitative severity rating: {{#upperCase}}{{.}}{{/upperCase}}
  • + {{/qualitative_severity_rating}} + {{#epss}} +
  • EPSS: probability {{probability}}, percentile {{percentile}} ({{timestamp}})
  • + {{/epss}} + {{#ssvc}} +
  • SSVC{{#timestamp}} ({{.}}){{/timestamp}} +
      + {{#selections}} +
    • {{label}}: {{values}}
    • + {{/selections}} +
    +
  • + {{/ssvc}} + {{#products.length}} +
  • For products: {{#removeTrailingComma}}{{#products}}{{name}}, {{/products}}{{/removeTrailingComma}}
  • + {{/products.length}} + {{#source}} +
  • Source: {{> url}}
  • + {{/source}} +
+ {{/metrics_details}} + {{/metrics_details.length}}` + +const PUBLISHER_CONTACT = ` {{#contact}}{{> contact}}{{/contact}}` + +const INVOLVEMENT = ` + + {{#document.involvement}} +

Involvement

+ + {{#entities.length}} +

Entities

+
    + {{#entities}} +
  • + {{display_name}} ({{#replaceUnderscores}}{{category}}{{/replaceUnderscores}}, {{entity_id}}) + {{#contact}}{{> contact}}{{/contact}} +
  • + {{/entities}} +
+ {{/entities.length}} + + {{#entity_groups.length}} +

Entity groups

+
    + {{#entity_groups}} +
  • + {{name}} ({{entity_group_id}}) + {{#summary}}

    {{{.}}}

    {{/summary}} + {{#entity_ids.length}} +

    Members: {{#removeTrailingComma}}{{#entity_ids}}{{name}}, {{/entity_ids}}{{/removeTrailingComma}}

    + {{/entity_ids.length}} +
  • + {{/entity_groups}} +
+ {{/entity_groups.length}} + + {{#actions.length}} +

Actions

+
    + {{#actions}} +
  • + {{date}}: {{#replaceUnderscores}}{{#upperCase}}{{category}}{{/upperCase}}{{/replaceUnderscores}} ({{#replaceUnderscores}}{{status}}{{/replaceUnderscores}}, {{action_id}}) + {{#acting_entity_refs.length}} +
    By: {{#removeTrailingComma}}{{#acting_entity_refs}}{{name}}, {{/acting_entity_refs}}{{/removeTrailingComma}} + {{/acting_entity_refs.length}} + {{#receiving_entity_refs.length}} +
    To: {{#removeTrailingComma}}{{#receiving_entity_refs}}{{name}}, {{/receiving_entity_refs}}{{/removeTrailingComma}} + {{/receiving_entity_refs.length}} + {{#dl_vuln_ids.length}} +
    Vulnerabilities: {{#removeTrailingComma}}{{#dl_vuln_ids}}{{.}}, {{/dl_vuln_ids}}{{/removeTrailingComma}} + {{/dl_vuln_ids.length}} + {{#referenced_action_ids.length}} +
    Refers to actions: {{#removeTrailingComma}}{{#referenced_action_ids}}{{.}}, {{/referenced_action_ids}}{{/removeTrailingComma}} + {{/referenced_action_ids.length}} + {{#summary}}

    {{{.}}}

    {{/summary}} + {{> scope}} +
  • + {{/actions}} +
+ {{/actions.length}} + {{/document.involvement}}` const SHARING_GROUP = ` {{#sharing_group}} Sharing Group
@@ -35,10 +166,13 @@ const SHARING_GROUP = ` {{#sharing_group}}

` -export default `${HEAD} -${CVSS} -${MID} -${CWE} -${BIG} -${SHARING_GROUP} -${TAIL}` +export default buildTemplate({ + cvss: CVSS, + headerRows: LICENSE_EXPRESSION, + vulnerabilityRows: CWE, + productStatusExtra: PRODUCT_STATUS_UNKNOWN, + vulnerabilityExtra: VULNERABILITY_EXTRA, + publisherContact: PUBLISHER_CONTACT, + documentExtra: INVOLVEMENT, + sharingGroup: SHARING_GROUP, +}) diff --git a/lib/templates/sharedTemplateParts.js b/lib/templates/sharedTemplateParts.js index 9fddd23..5f06099 100644 --- a/lib/templates/sharedTemplateParts.js +++ b/lib/templates/sharedTemplateParts.js @@ -2,11 +2,27 @@ import gutenbergCss from '../css/gutenberg.js' import modernCss from '../css/modern.js' import previewCss from '../css/preview.js' -// Shared, version-independent building blocks of the csaf_2_0.js and -// csaf_2_1.js top-level HTML templates. These two CSAF versions render an -// almost identical document; the only real differences are the CVSS label -// row, the CWE/date fields, and the CSAF 2.1-only sharing_group block, which -// stay defined separately in each templates/csaf_2_*.js file. +// Shared building blocks of the csaf_2_0.js and csaf_2_1.js top-level HTML +// templates. Both CSAF versions render the same overall document layout, but +// differ in several places (CVSS source, publisher contact, vulnerability +// involvements, 2.1-only properties such as document.involvement, +// license_expression, product_status.unknown, metrics, ...). Everything that +// differs is injected through the named slots of `buildTemplate`, so each +// templates/csaf_2_*.js file only defines its own version-specific +// fragments. Every slot defaults to an empty string. + +/** + * @typedef {Object} TemplateSlots + * @property {string} cvss Last cell of the document header table's "Current version" row. + * @property {string} [headerRows] Extra rows appended to the document header table. + * @property {string} [vulnerabilityRows] Rows inside the vulnerability details table. + * @property {string} [productStatusExtra] Extra product status blocks, after "Under investigation". + * @property {string} [involvements] Vulnerability-level involvements block. + * @property {string} [vulnerabilityExtra] Extra vulnerability sections, after "Threats". + * @property {string} [publisherContact] Publisher contact markup inside the publisher block. + * @property {string} [documentExtra] Extra document-level sections, after the publisher block. + * @property {string} [sharingGroup] Sharing group markup inside the "Sharing rules" block. + */ export const HEAD = ` @@ -40,7 +56,8 @@ export const HEAD = ` ` -export const MID = ` Severity: +/** @param {string} headerRows */ +export const MID = (headerRows) => ` Severity: {{#document.aggregate_severity.namespace}} {{document.aggregate_severity.text}}{{/document.aggregate_severity.namespace}} {{^document.aggregate_severity.namespace}} {{document.aggregate_severity.text}}{{/document.aggregate_severity.namespace}} @@ -51,7 +68,7 @@ export const MID = ` Severity: Also referred to: {{#removeTrailingComma}}{{#document.tracking.aliases}}{{.}}, {{/document.tracking.aliases}}{{/removeTrailingComma}} - + ${headerRows} {{#document.notes_summary}} @@ -116,7 +133,16 @@ export const MID = ` Severity: ` -export const BIG = `
+/** + * @param {Pick, 'productStatusExtra' | 'involvements' | 'vulnerabilityExtra' | 'publisherContact' | 'documentExtra'>} slots + */ +export const BIG = ({ + productStatusExtra, + involvements, + vulnerabilityExtra, + publisherContact, + documentExtra, +}) => `

Product status

{{#product_status.known_affected.length}} @@ -194,25 +220,31 @@ export const BIG = ` {{/product_status.first_fixed.length}} {{#product_status.under_investigation.length}} -
Under investgation
+
Under investigation
    {{#product_status.under_investigation}}
  • {{name}}
  • {{/product_status.under_investigation}}
- {{/product_status.under_investigation.length}} + {{/product_status.under_investigation.length}}${productStatusExtra} {{#remediations.length}}

Remediations

{{#remediations_vendor_fix}} {{> remediation}} {{/remediations_vendor_fix}} + {{#remediations_optional_patch}} + {{> remediation}} + {{/remediations_optional_patch}} {{#remediations_mitigation}} {{> remediation}} {{/remediations_mitigation}} {{#remediations_workaround}} {{> remediation}} {{/remediations_workaround}} + {{#remediations_fix_planned}} + {{> remediation}} + {{/remediations_fix_planned}} {{#remediations_none_available}} {{> remediation}} {{/remediations_none_available}} @@ -231,18 +263,7 @@ export const BIG = ` {{> acknowledgment}} {{/acknowledgments}} - {{/acknowledgments.length}} - - {{#involvements.length}} -

Involvement

-
    - {{#involvements}} - {{#.}} -
  • {{#date}}{{.}} {{/date}}{{#upperCase}}{{party}}{{/upperCase}}{{#status}} ({{#replaceUnderscores}}{{.}}{{/replaceUnderscores}}){{/status}}{{#summary}}: {{{.}}}{{/summary}}
  • - {{/.}} - {{/involvements}} -
- {{/involvements.length}} + {{/acknowledgments.length}}${involvements} {{#references.length}}

References

@@ -267,7 +288,7 @@ export const BIG = ` {{#threats_unknown}} {{> threat}} {{/threats_unknown}} - {{/threats.length}} + {{/threats.length}}${vulnerabilityExtra} {{/vulnerabilities}} @@ -288,9 +309,9 @@ export const BIG = ` {{#document.publisher}}

{{name}}

Namespace: {{namespace}}

-

{{contact_details}}

+${publisherContact}

{{{issuing_authority}}}

- {{/document.publisher}} + {{/document.publisher}}${documentExtra} {{#document.references.length}}

References

@@ -340,3 +361,38 @@ export const TAIL = ` {{#tlp}} ` + +/** + * Assembles the complete top-level template from the shared parts and the + * version-specific slots. + * + * @param {TemplateSlots} slots + */ +export function buildTemplate(slots) { + const { + cvss, + headerRows = '', + vulnerabilityRows = '', + productStatusExtra = '', + involvements = '', + vulnerabilityExtra = '', + publisherContact = '', + documentExtra = '', + sharingGroup = '', + } = slots + + const big = BIG({ + productStatusExtra, + involvements, + vulnerabilityExtra, + publisherContact, + documentExtra, + }) + + return `${HEAD} +${cvss} +${MID(headerRows)} +${vulnerabilityRows} +${big}${sharingGroup ? `\n${sharingGroup}` : ''} +${TAIL}` +} From cab39151e41ed4dd2a603f0fc7d1adce3592eedc Mon Sep 17 00:00:00 2001 From: Philipp Backes Date: Thu, 12 Nov 2026 10:04:54 +0100 Subject: [PATCH 3/3] add tests for CSAF 2.0/2.1 template differences --- lib/templates/sharedTemplateParts.js | 16 +- tests/HTMLTemplate.test.js | 422 +++++++++++++++++++++++++++ tests/enrichDocument.test.js | 283 ++++++++++++++++++ tests/markdownParser.test.js | 85 ++++++ 4 files changed, 798 insertions(+), 8 deletions(-) diff --git a/lib/templates/sharedTemplateParts.js b/lib/templates/sharedTemplateParts.js index 5f06099..eb762bc 100644 --- a/lib/templates/sharedTemplateParts.js +++ b/lib/templates/sharedTemplateParts.js @@ -2,14 +2,14 @@ import gutenbergCss from '../css/gutenberg.js' import modernCss from '../css/modern.js' import previewCss from '../css/preview.js' -// Shared building blocks of the csaf_2_0.js and csaf_2_1.js top-level HTML -// templates. Both CSAF versions render the same overall document layout, but -// differ in several places (CVSS source, publisher contact, vulnerability -// involvements, 2.1-only properties such as document.involvement, -// license_expression, product_status.unknown, metrics, ...). Everything that -// differs is injected through the named slots of `buildTemplate`, so each -// templates/csaf_2_*.js file only defines its own version-specific -// fragments. Every slot defaults to an empty string. +// Shared building blocks of the csaf_2_0.js and csaf_2_1.js top-level HTML templates. +// Both CSAF versions render the same overall document layout, +// but differ in several places (CVSS source, publisher contact, +// vulnerability involvements, 2.1-only properties such as document.involvement, +// license_expression, product_status.unknown, metrics, ...). +// Everything that differs is injected through the named slots of `buildTemplate`, +// so each templates/csaf_2_*.js file only defines its own version-specific fragments. +// Every slot defaults to an empty string. /** * @typedef {Object} TemplateSlots diff --git a/tests/HTMLTemplate.test.js b/tests/HTMLTemplate.test.js index c84a321..0e14cfc 100644 --- a/tests/HTMLTemplate.test.js +++ b/tests/HTMLTemplate.test.js @@ -179,6 +179,52 @@ describe('HTMLTemplate2_0 (end-to-end with enrichDocumentV2_0 + renderMarkdown)' }) }) +describe('HTMLTemplate2_0 (CSAF 2.0 specific content)', () => { + it('renders the publisher contact_details string', () => { + const doc = /** @type {any} */ (structuredClone(minimalV2_0Doc)) + doc.document.publisher.contact_details = 'Reach us at CONTACT_DETAILS_20' + const html = HTMLTemplate2_0(enrichDocumentV2_0(doc)) + + expect(html).toContain('Reach us at CONTACT_DETAILS_20') + }) + + it('renders vulnerability-level involvements', () => { + const doc = /** @type {any} */ (structuredClone(minimalV2_0Doc)) + doc.vulnerabilities[0].involvements = [ + { + party: 'vendor', + status: 'in_progress', + date: '2024-02-01T00:00:00Z', + summary: 'INVOLVEMENT_SUMMARY_20', + }, + ] + const html = HTMLTemplate2_0(enrichDocumentV2_0(doc)) + + expect(html).toContain('

Involvement

') + expect(html).toContain('Vendor (in progress)') + expect(html).toContain('INVOLVEMENT_SUMMARY_20') + }) + + it('does not render any CSAF 2.1-only section', () => { + const html = HTMLTemplate2_0(enrichDocumentV2_0(minimalV2_0Doc)) + + expect(html).not.toContain('License expression') + expect(html).not.toContain('
Unknown
') + expect(html).not.toContain('

Metrics

') + expect(html).not.toContain('First known exploitation dates') + }) + + it('spells the "Under investigation" heading correctly', () => { + const doc = /** @type {any} */ (structuredClone(minimalV2_0Doc)) + doc.product_tree = { full_product_names: [{ product_id: 'P1', name: 'N' }] } + doc.vulnerabilities[0].product_status = { under_investigation: ['P1'] } + const html = HTMLTemplate2_0(enrichDocumentV2_0(doc)) + + expect(html).toContain('
Under investigation
') + expect(html).not.toContain('investgation') + }) +}) + describe('HTMLTemplate2_1 (end-to-end with enrichDocumentV2_1)', () => { it('renders a full HTML document containing the advisory content', () => { const enriched = enrichDocumentV2_1(minimalV2_1Doc) @@ -279,3 +325,379 @@ describe('HTMLTemplate2_1 (end-to-end with enrichDocumentV2_1)', () => { expect(html).toContain('Vulnerability-level legal disclaimer text') }) }) + +const renderV2_1 = (/** @type {(doc: any) => void} */ modify) => { + const doc = /** @type {any} */ (structuredClone(minimalV2_1Doc)) + modify(doc) + const { document: enriched } = enrichDocumentV2_1(doc) + return HTMLTemplate2_1({ document: renderMarkdown(enriched) }) +} + +describe('HTMLTemplate2_1 (CSAF 2.1 specific content)', () => { + describe('publisher contact', () => { + it('renders all fields of the publisher contact object', () => { + const html = renderV2_1((doc) => { + doc.document.publisher.contact = { + details: 'Call **us** anytime', + email: 'psirt@example.com', + url: 'https://example.com/contact', + public_openpgp_key_url: 'https://example.com/key', + } + }) + + expect(html).toContain('Call us anytime') + expect(html).toContain('') + expect(html).toContain('psirt@example.com') + expect(html).toContain( + 'href="https://example.com/contact"', + ) + expect(html).toContain('href="https://example.com/key"') + }) + + it('renders a contact with only some fields set', () => { + const html = renderV2_1((doc) => { + doc.document.publisher.contact = { email: 'only@example.com' } + }) + + expect(html).toContain('only@example.com') + expect(html).not.toContain('OpenPGP') + }) + + it('does not render the legacy contact_details property', () => { + const html = renderV2_1((doc) => { + doc.document.publisher.contact_details = 'LEGACY_DETAILS' + }) + + expect(html).not.toContain('LEGACY_DETAILS') + }) + }) + + it('renders license_expression', () => { + const html = renderV2_1((doc) => { + doc.document.license_expression = 'CC-BY-4.0' + }) + + expect(html).toContain('License expression: CC-BY-4.0') + }) + + it('does not render a license expression row when there is none', () => { + expect(renderV2_1(() => {})).not.toContain('License expression') + }) + + describe('document.involvement', () => { + /** @param {any} doc */ + const addInvolvement = (doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + product_groups: [ + { group_id: 'G1', product_ids: ['P1'], summary: 'Group One' }, + ], + } + doc.document.involvement = { + entities: [ + { + entity_id: 'EID-1', + name: 'Acme Inc', + category: 'vendor', + contact: { email: 'acme@example.com' }, + }, + { entity_id: 'EID-2', category: 'reporting_authority' }, + ], + entity_groups: [ + { + entity_group_id: 'EGID-1', + name: 'Everyone', + summary: 'All **involved** parties', + entity_ids: ['EID-1', 'EID-2'], + }, + ], + actions: [ + { + action_id: 'AID-1', + category: 'fix_release', + status: 'completed', + date: '2024-03-03T00:00:00Z', + acting_entity_refs: ['EID-1'], + receiving_entity_refs: ['EGID-1'], + dl_vuln_ids: ['DLV-1'], + referenced_action_ids: ['AID-0'], + product_ids: ['P1'], + group_ids: ['G1'], + summary: 'We **released** a fix', + }, + ], + } + } + + it('renders entities, entity groups and actions with resolved names', () => { + const html = renderV2_1(addInvolvement) + + expect(html).toContain('

Involvement

') + expect(html).toContain('Acme Inc (vendor, EID-1)') + expect(html).toContain('acme@example.com') + // entity without a name falls back to its id + expect(html).toContain('EID-2 (reporting authority, EID-2)') + expect(html).toContain('Everyone (EGID-1)') + expect(html).toContain('All involved parties') + expect(html).toContain('Members: Acme Inc, EID-2') + expect(html).toContain('Fix release (completed, AID-1)') + expect(html).toContain('By: Acme Inc') + expect(html).toContain('To: Everyone') + expect(html).toContain('Vulnerabilities: DLV-1') + expect(html).toContain('Refers to actions: AID-0') + expect(html).toContain('We released a fix') + expect(html).toContain('
  • Product One
  • ') + expect(html).toContain('
  • Group One
  • ') + }) + + it('renders nothing when there is no involvement', () => { + expect(renderV2_1(() => {})).not.toContain('

    Involvement

    ') + }) + + it('does not render the CSAF 2.0 vulnerability involvements', () => { + const html = renderV2_1((doc) => { + doc.vulnerabilities = [ + { + title: 'V', + involvements: [{ party: 'vendor', summary: 'LEGACY_INVOLVEMENT' }], + }, + ] + }) + + expect(html).not.toContain('LEGACY_INVOLVEMENT') + expect(html).not.toContain('

    Involvement

    ') + }) + }) + + describe('note product_ids / group_ids', () => { + it('renders the products and groups a document note and a vulnerability note apply to', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + product_groups: [ + { group_id: 'G1', product_ids: ['P1'], summary: 'Group One' }, + ], + } + doc.document.notes = [ + { category: 'general', text: 'doc note', product_ids: ['P1'] }, + ] + doc.vulnerabilities = [ + { + title: 'V', + notes: [ + { category: 'description', text: 'vuln note', group_ids: ['G1'] }, + ], + }, + ] + }) + + expect(html).toContain('doc note') + expect(html).toContain('
  • Product One
  • ') + expect(html).toContain('vuln note') + expect(html).toContain('
  • Group One
  • ') + }) + + it('renders notes without product_ids / group_ids unchanged', () => { + const html = renderV2_1((doc) => { + doc.document.notes = [{ category: 'general', text: 'plain note' }] + }) + + expect(html).toContain('plain note') + expect(html).not.toContain('For products:') + expect(html).not.toContain('For groups:') + }) + }) + + describe('product_tree.product_paths', () => { + it('resolves product names defined only via a product path', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P0', name: 'Base' }], + product_paths: [ + { + beginning_product_reference: 'P0', + subpaths: [], + full_product_name: { product_id: 'P1', name: 'Path Product' }, + }, + ], + } + doc.vulnerabilities = [ + { title: 'V', product_status: { known_not_affected: ['P1'] } }, + ] + }) + + expect(html).toContain('Path Product') + }) + }) + + describe('vulnerabilities', () => { + it('renders the product_status "unknown" group', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Mystery Product' }], + } + doc.vulnerabilities = [ + { title: 'V', product_status: { unknown: ['P1'] } }, + ] + }) + + expect(html).toContain('
    Unknown
    ') + expect(html).toContain('Mystery Product') + }) + + it.each([ + ['fix_planned', 'Fix planned', 'FIX_PLANNED_DETAILS'], + ['optional_patch', 'Optional patch', 'OPTIONAL_PATCH_DETAILS'], + ])('renders %s remediations', (category, heading, details) => { + const html = renderV2_1((doc) => { + doc.vulnerabilities = [ + { title: 'V', remediations: [{ category, details }] }, + ] + }) + + expect(html).toContain('

    Remediations

    ') + expect(html).toContain(`
    ${heading}`) + expect(html).toContain(details) + }) + + it('renders first_known_exploitation_dates with their scope', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Exploited Product' }], + } + doc.vulnerabilities = [ + { + title: 'V', + first_known_exploitation_dates: [ + { + exploitation_date: '2024-02-02T00:00:00Z', + date: '2024-02-03T00:00:00Z', + product_ids: ['P1'], + }, + ], + }, + ] + }) + + expect(html).toContain('First known exploitation dates') + expect(html).toContain('2024-02-02T00:00:00Z') + expect(html).toContain('information from 2024-02-03T00:00:00Z') + expect(html).toContain('
  • Exploited Product
  • ') + }) + + it('renders the document-local vulnerability id', () => { + const html = renderV2_1((doc) => { + doc.vulnerabilities = [{ title: 'V', dl_vuln_id: 'DLV-42' }] + }) + + expect(html).toContain('Document-local ID:') + expect(html).toContain('DLV-42') + }) + + it('renders the product/group scope of vulnerability ids', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Scoped Product' }], + } + doc.vulnerabilities = [ + { + title: 'V', + ids: [ + { + system_name: 'Bug Tracker', + text: 'BUG-1', + product_ids: ['P1'], + }, + { system_name: 'Other', text: 'GLOBAL-ID' }, + ], + }, + ] + }) + + expect(html).toContain('

    Scoped IDs

    ') + expect(html).toContain('BUG-1 (Bug Tracker)') + expect(html).toContain('
  • Scoped Product
  • ') + // ids without a scope are not part of this block + expect(html).not.toContain('GLOBAL-ID') + }) + + it('renders epss, qualitative severity rating and ssvc_v2 metrics', () => { + const html = renderV2_1((doc) => { + doc.product_tree = { + full_product_names: [{ product_id: 'P1', name: 'Rated Product' }], + } + doc.vulnerabilities = [ + { + title: 'V', + metrics: [ + { + products: ['P1'], + source: 'https://example.com/source', + content: { + qualitative_severity_rating: 'high', + epss: { + probability: '0.12', + percentile: '0.85', + timestamp: '2024-01-01T00:00:00Z', + }, + ssvc_v2: { + timestamp: '2024-01-02T00:00:00Z', + schemaVersion: '2.0.0', + selections: [ + { + namespace: 'ssvc', + key: 'E', + version: '1.1.0', + name: 'Exploitation', + values: [{ key: 'P', name: 'PoC' }, { key: 'A' }], + }, + ], + }, + }, + }, + ], + }, + ] + }) + + expect(html).toContain('

    Metrics

    ') + expect(html).toContain('Qualitative severity rating: High') + expect(html).toContain('EPSS: probability 0.12, percentile 0.85') + expect(html).toContain('Exploitation: PoC, A') + expect(html).toContain('For products: Rated Product') + expect(html).toContain('href="https://example.com/source"') + }) + + it('does not render a metrics section for CVSS-only metrics', () => { + const html = renderV2_1((doc) => { + doc.vulnerabilities = [ + { + title: 'V', + metrics: [ + { + products: ['P1'], + content: { + cvss_v3: { version: '3.1', baseScore: 5, vectorString: 'V' }, + }, + }, + ], + }, + ] + }) + + expect(html).not.toContain('

    Metrics

    ') + }) + }) + + it('renders the CSAF 2.1 TLP labels and sharing group', () => { + const html = renderV2_1((doc) => { + doc.document.distribution = { + tlp: { label: 'AMBER+STRICT' }, + sharing_group: { id: 'SG-ID', name: 'SG Name' }, + } + }) + + expect(html).toContain('TLP:AMBER+STRICT') + expect(html).toContain('Name: SG Name') + }) +}) diff --git a/tests/enrichDocument.test.js b/tests/enrichDocument.test.js index c8cbc62..46b3990 100644 --- a/tests/enrichDocument.test.js +++ b/tests/enrichDocument.test.js @@ -247,6 +247,25 @@ describe('enrichDocumentV2_0', () => { expect(document.vulnerabilities[0].remediations_mitigation).toEqual([]) }) + it('buckets the CSAF 2.1 categories fix_planned and optional_patch instead of treating them as unknown', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + vulnerabilities: [ + { + remediations: [ + { category: 'fix_planned', details: 'a' }, + { category: 'optional_patch', details: 'b' }, + { category: 'made_up', details: 'c' }, + ], + }, + ], + }) + const vulnerability = document.vulnerabilities[0] + expect(vulnerability.remediations_fix_planned).toHaveLength(1) + expect(vulnerability.remediations_optional_patch).toHaveLength(1) + expect(vulnerability.remediations_unknown).toHaveLength(1) + }) + it('sorts remediations within a category by date, most recent first', () => { const { document } = enrichDocumentV2_0({ document: {}, @@ -708,4 +727,268 @@ describe('enrichDocumentV2_1', () => { expect(document.document.max_base_score_cvss_label).toBe('CVSSv4.0') }) }) + + describe('product_paths', () => { + it('indexes products defined in product_tree.product_paths', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + product_tree: { + product_groups: [{ group_id: 'G1', product_ids: ['P2'] }], + product_paths: [ + { + beginning_product_reference: 'P1', + subpaths: [], + full_product_name: { product_id: 'P2', name: 'Path Product' }, + }, + ], + }, + }) + expect(document.product_tree.product_groups[0].product_ids).toEqual([ + { id: 'P2', name: 'Path Product' }, + ]) + }) + }) + + describe('product_status.unknown', () => { + it('resolves product names and CVSS data for the unknown group', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + product_tree: { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + }, + vulnerabilities: [ + { + metrics: [ + { + products: ['P1'], + content: { + cvss_v3: { + version: '3.1', + baseScore: 5, + vectorString: 'VEC', + }, + }, + }, + ], + product_status: { unknown: ['P1'] }, + }, + ], + }) + expect(document.vulnerabilities[0].product_status.unknown).toEqual([ + { + id: 'P1', + name: 'Product One', + vectorString: 'VEC', + baseScore: 5, + cvssVersion: '3.1', + }, + ]) + }) + }) + + describe('product_ids / group_ids on notes, ids and first_known_exploitation_dates', () => { + const productTree = { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + product_groups: [{ group_id: 'G1', product_ids: ['P1'], summary: 'S1' }], + } + + it('resolves them to id/name pairs', () => { + const { document } = enrichDocumentV2_1({ + document: { notes: [{ category: 'general', product_ids: ['P1'] }] }, + product_tree: productTree, + vulnerabilities: [ + { + notes: [{ category: 'general', group_ids: ['G1'] }], + ids: [{ system_name: 'S', text: 'T', product_ids: ['P1'] }], + first_known_exploitation_dates: [ + { date: 'd', exploitation_date: 'e', group_ids: ['G1'] }, + ], + }, + ], + }) + const vulnerability = document.vulnerabilities[0] + expect(document.document.notes_general[0].product_ids).toEqual([ + { id: 'P1', name: 'Product One' }, + ]) + expect(vulnerability.notes_general[0].group_ids).toEqual([ + { id: 'G1', name: 'S1' }, + ]) + expect(vulnerability.ids[0].product_ids).toEqual([ + { id: 'P1', name: 'Product One' }, + ]) + expect(vulnerability.first_known_exploitation_dates[0].group_ids).toEqual( + [{ id: 'G1', name: 'S1' }], + ) + }) + + it('only collects ids that are scoped to products or groups in ids_scoped', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + product_tree: productTree, + vulnerabilities: [ + { + ids: [ + { system_name: 'S', text: 'scoped', product_ids: ['P1'] }, + { system_name: 'S', text: 'global' }, + ], + }, + ], + }) + expect( + document.vulnerabilities[0].ids_scoped.map( + (/** @type {any} */ id) => id.text, + ), + ).toEqual(['scoped']) + }) + + it('leaves notes without product_ids/group_ids untouched', () => { + const note = { category: 'general', text: 'a' } + const { document } = enrichDocumentV2_1({ + document: { notes: [note] }, + }) + expect(document.document.notes_general[0]).toEqual(note) + }) + }) + + describe('document.involvement', () => { + it('resolves entity refs to names, falling back to the id, and resolves action scopes', () => { + const { document } = enrichDocumentV2_1({ + document: { + involvement: { + entities: [ + { entity_id: 'EID-1', name: 'Acme', category: 'vendor' }, + { entity_id: 'EID-2', category: 'user' }, + ], + entity_groups: [ + { + entity_group_id: 'EGID-1', + name: 'Group', + entity_ids: ['EID-1', 'EID-2'], + }, + ], + actions: [ + { + action_id: 'A1', + category: 'fix_release', + status: 'completed', + date: 'd', + acting_entity_refs: ['EID-1'], + receiving_entity_refs: ['EGID-1', 'EID-unknown'], + product_ids: ['P1'], + }, + ], + }, + }, + product_tree: { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + }, + }) + const involvement = document.document.involvement + expect( + involvement.entities.map((/** @type {any} */ e) => e.display_name), + ).toEqual(['Acme', 'EID-2']) + expect(involvement.entity_groups[0].entity_ids).toEqual([ + { id: 'EID-1', name: 'Acme' }, + { id: 'EID-2', name: 'EID-2' }, + ]) + const action = involvement.actions[0] + expect(action.acting_entity_refs).toEqual([{ id: 'EID-1', name: 'Acme' }]) + expect(action.receiving_entity_refs).toEqual([ + { id: 'EGID-1', name: 'Group' }, + { id: 'EID-unknown', name: 'EID-unknown' }, + ]) + expect(action.product_ids).toEqual([{ id: 'P1', name: 'Product One' }]) + }) + + it('leaves a document without involvement untouched', () => { + const { document } = enrichDocumentV2_1({ document: {} }) + expect(document.document.involvement).toBeUndefined() + }) + }) + + describe('metrics_details', () => { + it('collects epss, qualitative severity rating and a condensed ssvc_v2 selection list', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + product_tree: { + full_product_names: [{ product_id: 'P1', name: 'Product One' }], + }, + vulnerabilities: [ + { + metrics: [ + { + products: ['P1', 'P-unknown'], + source: 'https://example.com', + content: { + qualitative_severity_rating: 'low', + epss: { probability: '1', percentile: '2', timestamp: 't' }, + ssvc_v2: { + timestamp: 'ts', + selections: [ + { + namespace: 'ssvc', + key: 'KEY', + version: '1.0.0', + values: [{ key: 'V1' }, { key: 'V2', name: 'Two' }], + }, + ], + }, + }, + }, + ], + }, + ], + }) + expect(document.vulnerabilities[0].metrics_details).toEqual([ + { + products: [ + { id: 'P1', name: 'Product One' }, + { id: 'P-unknown', name: 'P-unknown' }, + ], + source: 'https://example.com', + qualitative_severity_rating: 'low', + epss: { probability: '1', percentile: '2', timestamp: 't' }, + ssvc: { + timestamp: 'ts', + selections: [ + { + label: 'KEY', + namespace: 'ssvc', + version: '1.0.0', + values: 'V1, Two', + }, + ], + }, + }, + ]) + }) + + it('skips metrics that only carry CVSS data', () => { + const { document } = enrichDocumentV2_1({ + document: {}, + vulnerabilities: [ + { + metrics: [ + { + products: ['P1'], + content: { cvss_v3: { version: '3.1', baseScore: 1 } }, + }, + ], + }, + ], + }) + expect(document.vulnerabilities[0].metrics_details).toEqual([]) + }) + }) +}) + +describe('enrichDocumentV2_0 does not add CSAF 2.1-only attributes', () => { + it('adds no metrics_details, ids_scoped or involvement handling', () => { + const { document } = enrichDocumentV2_0({ + document: {}, + vulnerabilities: [{ title: 'V' }], + }) + expect(document.vulnerabilities[0].metrics_details).toBeUndefined() + expect(document.vulnerabilities[0].ids_scoped).toBeUndefined() + }) }) diff --git a/tests/markdownParser.test.js b/tests/markdownParser.test.js index 139bd57..3711907 100644 --- a/tests/markdownParser.test.js +++ b/tests/markdownParser.test.js @@ -114,4 +114,89 @@ describe('renderMarkdown', () => { const doc = { document: { distribution: { text: 'plain' } } } expect(renderMarkdown(doc)).toBe(doc) }) + + it.each([ + [ + 'document.publisher.contact.details', + (/** @type {any} */ d) => d.document.publisher.contact.details, + { document: { publisher: { contact: { details: '**bold**' } } } }, + ], + [ + 'document.involvement.actions.*.summary', + (/** @type {any} */ d) => d.document.involvement.actions[0].summary, + { document: { involvement: { actions: [{ summary: '**bold**' }] } } }, + ], + [ + 'document.involvement.entities.*.contact.details', + (/** @type {any} */ d) => + d.document.involvement.entities[0].contact.details, + { + document: { + involvement: { entities: [{ contact: { details: '**bold**' } }] }, + }, + }, + ], + [ + 'document.involvement.entity_groups.*.summary', + (/** @type {any} */ d) => d.document.involvement.entity_groups[0].summary, + { + document: { involvement: { entity_groups: [{ summary: '**bold**' }] } }, + }, + ], + [ + 'document.notes.*.group_ids.*.name', + (/** @type {any} */ d) => d.document.notes[0].group_ids[0].name, + { + document: { notes: [{ group_ids: [{ id: 'G1', name: '**bold**' }] }] }, + }, + ], + [ + 'document.involvement.actions.*.group_ids.*.name', + (/** @type {any} */ d) => + d.document.involvement.actions[0].group_ids[0].name, + { + document: { + involvement: { + actions: [{ group_ids: [{ id: 'G1', name: '**bold**' }] }], + }, + }, + }, + ], + [ + 'vulnerabilities.*.notes.*.group_ids.*.name', + (/** @type {any} */ d) => d.vulnerabilities[0].notes[0].group_ids[0].name, + { + vulnerabilities: [ + { notes: [{ group_ids: [{ id: 'G1', name: '**bold**' }] }] }, + ], + }, + ], + [ + 'vulnerabilities.*.ids.*.group_ids.*.name', + (/** @type {any} */ d) => d.vulnerabilities[0].ids[0].group_ids[0].name, + { + vulnerabilities: [ + { ids: [{ group_ids: [{ id: 'G1', name: '**bold**' }] }] }, + ], + }, + ], + [ + 'vulnerabilities.*.first_known_exploitation_dates.*.group_ids.*.name', + (/** @type {any} */ d) => + d.vulnerabilities[0].first_known_exploitation_dates[0].group_ids[0] + .name, + { + vulnerabilities: [ + { + first_known_exploitation_dates: [ + { group_ids: [{ id: 'G1', name: '**bold**' }] }, + ], + }, + ], + }, + ], + ])('renders the CSAF 2.1 markdown field %s', (_field, read, doc) => { + renderMarkdown(doc) + expect(read(doc)).toBe('

    bold

    ') + }) })