import * as XmlParser from 'xmljson' ; import { AlertResult } from './../interfaces/types/AlertResult' ; import { AlertItem , ScanReport , Site } from './../interfaces/types/ZapReport' ; import { Constants } from './Constants' ; export class Helper { constructor ( ) { } ProcessAlerts ( xmlResult , targetUrl ) { const high = new Array < AlertItem > ( ) ; const mid = new Array < AlertItem > ( ) ; const low = new Array < AlertItem > ( ) ; const info = new Array < AlertItem > ( ) ; let alerts ; const alertResult = { HighAlerts : 0 , MediumAlerts : 0 , LowAlerts : 0 , InformationalAlerts : 0 , Alerts : new Array < AlertItem > ( ) } ; XmlParser . to_json ( xmlResult , ( err , res ) => { if ( err ) { return ; } const reportJson = res ; const siteCollection = reportJson . OWASPZAPReport . site ; const sites = Object . keys ( siteCollection ) [ 0 ] === '0' ? siteCollection : [ siteCollection as Site ] ; for ( const idx in sites ) { if ( targetUrl . includes ( sites [ idx ] . $ . host ) ) { alerts = sites [ idx ] . alerts . alertitem ; } } if ( ! alerts ) { return ; } const _alerts = alerts ; alerts = Object . keys ( _alerts ) [ 0 ] === '0' ? _alerts : [ _alerts as AlertItem ] ; for ( const idx of Object . keys ( alerts ) ) { const i = Number ( idx ) ; if ( alerts [ i ] . riskcode === Constants . HIGH_RISK ) { high . push ( alerts [ i ] ) ; alertResult . HighAlerts ++ ; } if ( alerts [ i ] . riskcode === Constants . MEDIUM_RISK ) { mid . push ( alerts [ i ] ) ; alertResult . MediumAlerts ++ ; } if ( alerts [ i ] . riskcode === Constants . LOW_RISK ) { low . push ( alerts [ i ] ) ; alertResult . LowAlerts ++ ; } if ( alerts [ i ] . riskcode === Constants . INFO_RISK ) { info . push ( alerts [ i ] ) ; alertResult . InformationalAlerts ++ ; } } const sorted = high . concat ( mid ) . concat ( low ) . concat ( info ) ; alertResult . Alerts = sorted ; } ) ; return alertResult ; } }