@@ -51,7 +51,12 @@ function parse_audit_results(err, data, threshold, ignoreDev, jsonOutput = false
5151 } else {
5252 const advisories = Object . entries ( data . advisories ) ;
5353
54- const flaggedDependencies = filter_advisories ( advisories , ignoreDev , threshold , whitelist ) ;
54+ let moduleInfo = { } ;
55+ if ( data . hasOwnProperty ( 'actions' ) ) {
56+ moduleInfo = process_actions ( data . actions ) ;
57+ }
58+
59+ const flaggedDependencies = filter_advisories ( advisories , ignoreDev , threshold , moduleInfo , whitelist ) ;
5560
5661 // If `-j` or `--json` passed, return the json data with the appropriate filters applied
5762 if ( jsonOutput ) {
@@ -103,19 +108,21 @@ function parse_audit_results(err, data, threshold, ignoreDev, jsonOutput = false
103108 * @param {Object[] } advisories An array of Advisory objects returned from NPM Audit
104109 * @param {boolean } ignoreDev Should dev dependencies be ignored?
105110 * @param {number } threshold The severity threshold above which a vulnerability will not be ignored
111+ * @param {Object } moduleInfo A Key/Value Map of module name to dev/prod status
106112 * @param {string[] } whitelist A (possibly empty) list of modules/versions which should be ignored
107113 * @returns An array (possibly empty) of advisory objects
108114 */
109- function filter_advisories ( advisories , ignoreDev , threshold , whitelist = [ ] ) {
110- const filteredByThreshold = advisories . filter ( ( advisory , idx ) => {
111- return ( ! ( advisory [ 1 ] . findings [ 0 ] . dev && ignoreDev ) ) ; // Filter out Dev dependencies when indicated
115+ function filter_advisories ( advisories , ignoreDev , threshold , moduleInfo = { } , whitelist = [ ] ) {
116+ const filteredByDev = advisories . filter ( ( advisory , idx ) => {
117+ const isDev = advisory [ 1 ] . findings [ 0 ] . dev || moduleInfo [ advisory [ 1 ] . module_name ] ;
118+ return ( ! ( isDev && ignoreDev ) ) ; // Filter out Dev dependencies when indicated
112119 } ) ;
113120
114- const filteredByDev = filteredByThreshold . filter ( ( advisory , idx ) => {
121+ const filteredByThreshold = filteredByDev . filter ( ( advisory , idx ) => {
115122 return ( validThresholds . indexOf ( advisory [ 1 ] . severity ) >= threshold ) ; // Filter out lower severities when indicated
116123 } ) ;
117124
118- return filteredByDev . filter ( ( advisory , idx ) => {
125+ return filteredByThreshold . filter ( ( advisory , idx ) => {
119126 const moduleName = advisory [ 1 ] . module_name ;
120127 const moduleVersion = advisory [ 1 ] . findings [ 0 ] . version ;
121128 for ( let i = 0 ; i < whitelist . length ; i ++ ) {
@@ -132,7 +139,22 @@ function filter_advisories(advisories, ignoreDev, threshold, whitelist = []) {
132139 } ) ;
133140}
134141
142+ /**
143+ * Parse the "Actions" section of the NPM Audit report and determine which modules are dev dependencies and which are not
144+ * @param {Object[] } actions An array/list of Action objects from NPM Audit
145+ */
146+ function process_actions ( actions ) {
147+ let moduleInfo = { } ;
148+ actions . forEach ( action => {
149+ const module_name = action . module ;
150+ const is_dev_dependency = action . resolves . filter ( path => path . dev ) . length > 0 ;
151+ moduleInfo [ module_name ] = is_dev_dependency ;
152+ } ) ;
153+ return moduleInfo ;
154+ }
155+
135156module . exports = {
136157 parse_audit_results,
137- filter_advisories : filter_advisories
158+ filter_advisories : filter_advisories ,
159+ process_actions
138160} ;
0 commit comments