Skip to content

Commit

Permalink
including log statements that will need removed, but exploring changi…
Browse files Browse the repository at this point in the history
…ng types instead of specifying array values

Signed-off-by: Emily Rodriguez <ecrodriguez@mitre.org>
  • Loading branch information
em-c-rod committed Dec 9, 2023
1 parent 66c673b commit 9041ae4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/parsers/xccdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {

Check failure on line 122 in src/parsers/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

Expected space(s) after "if"
throw new Error("Rule title is not an array of legnth 1.");

Check failure on line 123 in src/parsers/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

Strings must use singlequote
}

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] || ''
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/xccdf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ export interface BenchmarkRule {
'@_severity': Severity;
'@_weight': string;
version: string;
title: string;
title: string[];
description: string;
rationale: FrontMatter[];
reference: PurpleReference[];
Expand Down
30 changes: 16 additions & 14 deletions src/utilities/xccdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Check warning on line 13 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

'alwaysArray' is assigned a value but never used

// 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 {
Expand All @@ -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) => {

Check warning on line 27 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

'_name' is defined but never used

Check warning on line 27 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

'_jpath' is defined but never used

Check warning on line 27 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

'isLeafNode' is defined but never used
// if (isLeafNode) {
return true;

Check failure on line 29 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

Expected indentation of 6 spaces but found 8
} else {
return false;
}
// } else {
// return false;
// }
}
};
const parser = new XMLParser(options);
Expand All @@ -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));

Check failure on line 45 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

Strings must use singlequote
const result = he.decode(str);
console.log("Result of he.decode: ", JSON.stringify(result));

Check failure on line 47 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

Strings must use singlequote
return result
}

export function severityStringToImpact(string: string, id: string): number {

Check warning on line 51 in src/utilities/xccdf.ts

View workflow job for this annotation

GitHub Actions / Lint TS-InSpec-Objects

'id' is defined but never used
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
}

Expand Down

0 comments on commit 9041ae4

Please sign in to comment.