Skip to content

Commit

Permalink
safsee
Browse files Browse the repository at this point in the history
  • Loading branch information
cd-rite committed Jan 23, 2024
1 parent d5c49ce commit 8b315a0
Showing 1 changed file with 64 additions and 44 deletions.
108 changes: 64 additions & 44 deletions client/src/js/SM/Parsers.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
(function (exports) {
exports.reviewsFromCkl = function (
{
data,
data,
fieldSettings,
allowAccept,
importOptions,
valueProcessor,
XMLParser
}) {

const maxCommentLength = 32767

if (!XMLParser) {
if (typeof require === 'function') {
const { requireXMLParser } = require('fast-xml-parser')
XMLParser = requireXMLParser
}
else if (typeof fxp === "object" && typeof fxp.XMLParser === 'function') {
XMLParser = fxp.XMLParser
}
else {
throw(new Error('XMLParser not found'))
}
}

const normalizeKeys = function (input) {
// lowercase and remove hyphens
if (typeof input !== 'object') return input;
if (Array.isArray(input)) return input.map(normalizeKeys);
return Object.keys(input).reduce(function (newObj, key) {
let val = input[key];
let newVal = (typeof val === 'object') && val !== null ? normalizeKeys(val) : val;
newObj[key.toLowerCase().replace('-','')] = newVal;
return newObj;
let val = input[key];
let newVal = (typeof val === 'object') && val !== null ? normalizeKeys(val) : val;
newObj[key.toLowerCase().replace('-', '')] = newVal;
return newObj;
}, {});
}

const resultMap = {
NotAFinding: 'pass',
Open: 'fail',
Expand Down Expand Up @@ -64,6 +52,7 @@
if (!parsed.CHECKLIST[0].STIGS) throw (new Error("No STIGS element"))

const comments = parsed['__comment']
// extract the root ES comment
const resultEngineCommon = comments?.length ? processRootXmlComments(comments) : null

let returnObj = {}
Expand All @@ -78,7 +67,7 @@
return (returnObj)

function processAsset(assetElement) {
let obj = {
let obj = {
name: assetElement.HOST_NAME,
description: null,
ip: assetElement.HOST_IP || null,
Expand Down Expand Up @@ -106,24 +95,24 @@
obj.metadata = metadata
return obj
}

function processIStig(iStigElement) {
let checklistArray = []
iStigElement.forEach(iStig => {
let checklist = {}
// get benchmarkId
let stigIdElement = iStig.STIG_INFO[0].SI_DATA.filter( d => d.SID_NAME === 'stigid' )?.[0]
let stigIdElement = iStig.STIG_INFO[0].SI_DATA.filter(d => d.SID_NAME === 'stigid')?.[0]
checklist.benchmarkId = stigIdElement.SID_DATA.replace('xccdf_mil.disa.stig_benchmark_', '')
// get revision data. Extract digits from version and release fields to create revisionStr, if possible.
const stigVersionData = iStig.STIG_INFO[0].SI_DATA.filter( d => d.SID_NAME === 'version' )?.[0].SID_DATA
const stigVersionData = iStig.STIG_INFO[0].SI_DATA.filter(d => d.SID_NAME === 'version')?.[0].SID_DATA
let stigVersion = stigVersionData.match(/(\d+)/)?.[1]
let stigReleaseInfo = iStig.STIG_INFO[0].SI_DATA.filter( d => d.SID_NAME === 'releaseinfo' )?.[0].SID_DATA
let stigReleaseInfo = iStig.STIG_INFO[0].SI_DATA.filter(d => d.SID_NAME === 'releaseinfo')?.[0].SID_DATA
const stigRelease = stigReleaseInfo.match(/Release:\s*(.+?)\s/)?.[1]
const stigRevisionStr = stigVersion && stigRelease ? `V${stigVersion}R${stigRelease}` : null
checklist.revisionStr = stigRevisionStr

if (checklist.benchmarkId) {
let x = processVuln(iStig.VULN)
let x = processVuln(iStig.VULN, iStig.__comment)
checklist.reviews = x.reviews
checklist.stats = x.stats
checklistArray.push(checklist)
Expand All @@ -132,7 +121,7 @@
return checklistArray
}

function processVuln(vulnElements) {
function processVuln(vulnElements,iStigComment) {
// vulnElements is an array of this object:
// {
// COMMENTS
Expand All @@ -154,9 +143,9 @@
error: 0,
fixed: 0,
unknown: 0
}
}
vulnElements?.forEach(vuln => {
const review = generateReview(vuln, resultEngineCommon)
const review = generateReview(vuln, iStigComment)
if (review) {
vulnArray.push(review)
resultStats[review.result]++
Expand All @@ -169,7 +158,7 @@
}
}

function generateReview(vuln, resultEngineCommon) {
function generateReview(vuln, iStigComment) {
let result = resultMap[vuln.STATUS]
if (!result) return
const ruleId = getRuleIdFromVuln(vuln)
Expand All @@ -195,7 +184,7 @@
if (!vuln.FINDING_DETAILS) {
switch (importOptions.emptyDetail) {
case 'ignore':
detail= null
detail = null
break
case 'import':
detail = vuln.FINDING_DETAILS
Expand Down Expand Up @@ -228,8 +217,12 @@
comment
}

// if the current checklist contrains a comment from ES, process it and add it to the review
const iStigCommentProcessed = iStigComment ? processIstigXmlComments(iStigComment) : null;

if (resultEngineCommon) {
review.resultEngine = {...resultEngineCommon}
// combining the root ES comment and the checklist ES comment
review.resultEngine = { ...resultEngineCommon, ...iStigCommentProcessed }
if (vuln['__comment']) {
const overrides = []
for (const comment of vuln['__comment']) {
Expand All @@ -238,7 +231,7 @@
try {
override = parser.parse(comment)['Evaluate-STIG'][0]
}
catch(e) {
catch (e) {
console.log(`Failed to parse Evaluate-STIG VULN XML comment for ${ruleId}`)
}
override = normalizeKeys(override)
Expand All @@ -250,11 +243,11 @@
remark: 'Evaluate-STIG Answer File'
})
}
}
}
}
if (overrides.length) {
review.resultEngine.overrides = overrides
}
}
}
}
else {
Expand All @@ -265,7 +258,7 @@
if (status) {
review.status = status
}

return review
}

Expand Down Expand Up @@ -299,7 +292,7 @@
detailSubmittable = true
}
break
}
}

let commentSubmittable = false
switch (fieldSettings.comment.required) {
Expand All @@ -319,24 +312,50 @@
}

const resultSubmittable = review.result === 'pass' || review.result === 'fail' || review.result === 'notapplicable'

let status = undefined
if (detailSubmittable && commentSubmittable && resultSubmittable) {
switch (importOptions.autoStatus) {
case 'submitted':
status = 'submitted'
break
case 'accepted':
status = allowAccept ? 'accepted' : 'submitted'
status = allowAccept ? 'accepted' : 'submitted'
break
}
}
}
else {
status = 'saved'
}
return status
}

// process the Evaluate-STIG XML coments for each "Checklist" (iSTIG)
function processIstigXmlComments(iStigComment) {
let resultEngineIStig
for (const comment of iStigComment) {

if (comment.toString().startsWith('<Evaluate-STIG>')) {
let esIStigComment
try {
esIStigComment = parser.parse(comment)['Evaluate-STIG'][0]
}
catch (e) {
console.log('Failed to parse Evaluate-STIG root XML comment')
}
esIStigComment = normalizeKeys(esIStigComment)
resultEngineIStig = {
time: esIStigComment?.time,
checkContent: {
location: esIStigComment?.module?.[0]?.name ?? ''
}
}
}
}
return resultEngineIStig || null
}

// process the Evaluate-STIG ROOT XML comments
function processRootXmlComments(comments) {
let resultEngineRoot
for (const comment of comments) {
Expand All @@ -345,25 +364,26 @@
try {
esRootComment = parser.parse(comment)['Evaluate-STIG'][0]
}
catch(e) {
catch (e) {
console.log('Failed to parse Evaluate-STIG root XML comment')
}
esRootComment = normalizeKeys(esRootComment)
resultEngineRoot = {
type: 'script',
product: 'Evaluate-STIG',
version: esRootComment?.global?.[0]?.version,
version: esRootComment?.global?.[0]?.version || esRootComment?.version,
time: esRootComment?.global?.[0]?.time,
checkContent: {
location: esRootComment?.module?.[0]?.name ?? ''
}
}
}
}
}
return resultEngineRoot || null
}
}


exports.reviewsFromXccdf = function (
{
data,
Expand Down Expand Up @@ -517,7 +537,7 @@
if (resultEngineCommon.product === 'stig-manager') {
resultEngine = ruleResult.check?.['check-content']?.resultEngine
}
else {
else {asfsafd
// build the resultEngine value
const timeStr = ruleResult.time ?? DEFAULT_RESULT_TIME
resultEngine = {
Expand Down

0 comments on commit 8b315a0

Please sign in to comment.