From 9041ae4b4b315031f682538e7d60d77ccdbd7390 Mon Sep 17 00:00:00 2001 From: Emily Rodriguez Date: Sat, 9 Dec 2023 12:27:20 -0600 Subject: [PATCH] including log statements that will need removed, but exploring changing types instead of specifying array values Signed-off-by: Emily Rodriguez --- src/parsers/xccdf.ts | 8 ++++++-- src/types/xccdf.d.ts | 2 +- src/utilities/xccdf.ts | 30 ++++++++++++++++-------------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/parsers/xccdf.ts b/src/parsers/xccdf.ts index 5a4a5481..330dfabc 100644 --- a/src/parsers/xccdf.ts +++ b/src/parsers/xccdf.ts @@ -118,8 +118,12 @@ export function processXCCDF(xml: string, removeNewlines: false, useRuleId: 'gro default: throw new Error('useRuleId must be one of "group", "rule", or "version"') } + + if(!(_.isArray(rule.title) && rule.title.length === 1)) { + throw new Error("Rule title is not an array of legnth 1."); + } - control.title = removeXMLSpecialCharacters(rule['@_severity'] ? ensureDecodedXMLStringValue(rule.title, 'undefined title') : `[[[MISSING SEVERITY FROM BENCHMARK]]] ${ensureDecodedXMLStringValue(rule.title,'undefined title')}`) + control.title = removeXMLSpecialCharacters(rule['@_severity'] ? ensureDecodedXMLStringValue(rule.title[0], 'undefined title') : `[[[MISSING SEVERITY FROM BENCHMARK]]] ${ensureDecodedXMLStringValue(rule.title[0],'undefined title')}`) if (typeof extractedDescription === 'object' && !Array.isArray(extractedDescription)) { control.desc = extractedDescription.VulnDiscussion?.split('Satisfies: ')[0] || '' @@ -352,7 +356,7 @@ export function processXCCDF(xml: string, removeNewlines: false, useRuleId: 'gro } } else { logger.warn('Reference parts of invalid length:') - logger.info(referenceParts) + // logger.info(referenceParts) } } } catch (e) { diff --git a/src/types/xccdf.d.ts b/src/types/xccdf.d.ts index 54303f08..664412da 100644 --- a/src/types/xccdf.d.ts +++ b/src/types/xccdf.d.ts @@ -722,7 +722,7 @@ export interface BenchmarkRule { '@_severity': Severity; '@_weight': string; version: string; - title: string; + title: string[]; description: string; rationale: FrontMatter[]; reference: PurpleReference[]; diff --git a/src/utilities/xccdf.ts b/src/utilities/xccdf.ts index aeb7f055..7fa3fa58 100644 --- a/src/utilities/xccdf.ts +++ b/src/utilities/xccdf.ts @@ -8,15 +8,14 @@ import he from 'he' // const alwaysArray = ['cci_item', 'reference', 'Group', 'group', 'Benchmark', 'Rule', 'title', 'rule', 'version', 'title', '@_id', 'check']; // 'title', //STIG -const alwaysArray = ['title', 'dc-status', 'description','notice', 'front-matter', 'rear-matter', 'reference', 'plain-text', 'platform', 'metadata', 'Benchmark', 'Group', 'Rule', 'TestResult', 'Value', 'Profile', 'check', 'ident', 'rationale']; +// const alwaysArray = ['title', 'dc-status', 'description','notice', 'front-matter', 'rear-matter', 'reference', 'plain-text', 'platform', 'metadata', 'Benchmark', 'Group', 'Rule', 'TestResult', 'Value', 'Profile', 'check', 'ident', 'rationale']; //OVAL -// const alwaysArray = ['object_reference', 'definition', 'affected', 'reference', 'xsd:any', 'platform', 'product', 'note', 'criteria', 'criterion', 'extend_definition', 'oval-def:tests', 'oval-def:objects', 'oval-def:filter', 'oval-def:states', 'oval-def:variables', 'possible_value', 'possible_restriction', 'restriction', 'value', 'field']; +const alwaysArray = ['object_reference', 'oval-def:definition', 'definition', 'affected', 'reference', 'xsd:any', 'platform', 'product', 'note', 'criteria', 'criterion', 'extend_definition', 'oval-def:test', 'oval-def:object', 'oval-def:filter', 'oval-def:state', 'oval-def:variable', 'possible_value', 'possible_restriction', 'restriction', 'value', 'field', 'definitions', 'generator']; // arrayMode: () => { // return true; // }//true // needs to be updated to isArray https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/2.XMLparseOptions.md#isarray - export function convertEncodedXmlIntoJson( encodedXml: string ): any { @@ -25,12 +24,12 @@ export function convertEncodedXmlIntoJson( ignoreNameSpace: true, attributeNamePrefix: '@_', stopNodes: ['div', 'p'], - isArray: (tagName: string) => { - if (alwaysArray.includes(tagName)) { + isArray: (_name: string, _jpath: string, isLeafNode: boolean) => { + // if (isLeafNode) { return true; - } else { - return false; - } + // } else { + // return false; + // } } }; const parser = new XMLParser(options); @@ -43,27 +42,30 @@ export function convertJsonIntoXML(data: any) { } export function removeXMLSpecialCharacters(str: string) { - return he.decode(str) + console.log("Remove special characters: ", JSON.stringify(str, null, 2)); + const result = he.decode(str); + console.log("Result of he.decode: ", JSON.stringify(result)); + return result } export function severityStringToImpact(string: string, id: string): number { - if (string.match(/none|na|n\/a|not[\s()*_|]?applicable/i)?.length) { + if (RegExp(/none|na|n\/a|not[\s()*_|]?applicable/i).exec(string)?.length) { return 0.0 } - if (string.match(/low|cat(egory)?\s*(iii|3)/i)?.length) { + if (RegExp(/low|cat(egory)?\s*(iii|3)/i).exec(string)?.length) { return 0.3 } - if (string.match(/med(ium)?|cat(egory)?\s*(ii|2)/)?.length) { + if (RegExp(/med(ium)?|cat(egory)?\s*(ii|2)/).exec(string)?.length) { return 0.5 } - if (string.match(/high|cat(egory)?\s*(i|1)/)?.length) { + if (RegExp(/high|cat(egory)?\s*(i|1)/).exec(string)?.length) { return 0.7 } - if (string.match(/crit(ical)?|severe/)?.length) { + if (RegExp(/crit(ical)?|severe/).exec(string)?.length) { return 1.0 }