@@ -2,6 +2,7 @@ import fs from 'fs';
22import dir from 'node-dir' ;
33import os from 'os' ;
44import path from 'path' ;
5+ const VER = require ( '../../package.json' ) . version ;
56
67export function is_directory ( input ) {
78 return fs . statSync ( input ) . isDirectory ( ) ;
@@ -43,75 +44,93 @@ export function list_files(input){
4344 . catch ( console . error ) ;
4445}
4546
46- export function writeIssues ( filename , result , isSarif ) {
47- let issues = '' ;
47+ export function writeIssues ( root , isRelative , filename , result , isSarif ) {
48+ let output = '' ;
4849 let fileFlag = 'w' ;
4950
5051 if ( isSarif ) {
51- issues =
52- {
53- $schema : "http://json.schemastore.org/sarif-2.0.0" ,
54- version : "2.0.0" ,
55- runs : [
56- {
57- tool : {
52+ let issues =
53+ {
54+ $schema : "http://json.schemastore.org/sarif-2.1.0" ,
55+ version : "2.1.0" ,
56+ runs : [
57+ {
58+ tool : {
59+ driver : {
60+ version : `${ VER } ` ,
61+ informationUri : "https://github.com/doyensec/electronegativity" ,
5862 name : "Electronegativity" ,
59- fullName : "Electronegativity is a tool to identify misconfigurations and security anti-patterns in Electron applications"
60- } ,
61- results : [ ] ,
62- resources : {
63- rules : {
64- }
63+ fullName : "Electronegativity is a tool to identify misconfigurations and security anti-patterns in Electron applications" ,
64+ rules : [ ]
6565 }
66- }
67- ]
68- } ;
66+ } ,
67+ results : [ ]
68+ }
69+ ]
70+ } ;
71+
72+ if ( isRelative ) {
73+ issues . runs [ 0 ] . invocations = [
74+ {
75+ workingDirectory : {
76+ uri : `file:///${ root } `
77+ } ,
78+ executionSuccessful : true
79+ } ,
80+ ] ;
81+ }
6982
7083 result . forEach ( issue => {
71- if ( issues . runs [ 0 ] . resources . rules [ issue . id ] === undefined ) {
72- issues . runs [ 0 ] . resources . rules [ issue . id ] = {
84+ if ( issues . runs [ 0 ] . tool . driver . rules [ issue . id ] === undefined ) {
85+ issues . runs [ 0 ] . tool . driver . rules . push ( {
7386 id : issue . id ,
74- name : {
75- text : issue . description
76- } ,
7787 fullDescription : {
7888 text : issue . description
7989 } ,
80- configuration : {
81- defaultLevel : ` ${ issue . manualReview ? 'warning' : 'error' } `
90+ properties : {
91+ category : "Security"
8292 } ,
83- helpUri : `https://github.com/doyensec/electronegativity/wiki/${ issue . id } `
84- } ;
93+ helpUri : `https://github.com/doyensec/electronegativity/wiki/${ issue . id } ` ,
94+ help : {
95+ text : `https://github.com/doyensec/electronegativity/wiki/${ issue . id } `
96+ }
97+ } ) ;
98+ issues . runs [ 0 ] . tool . driver . rules [ issue . id ] = true ;
8599 }
86- issues . runs [ 0 ] . results . push ( {
100+
101+ let result = {
87102 ruleId : issue . id ,
103+ level : `${ issue . manualReview ? 'note' : 'warning' } ` ,
88104 message : {
89105 text : issue . description
90- } ,
91- locations : [
92- {
93- physicalLocation : {
94- fileLocation : {
95- uri : issue . file
96- } ,
97- region : {
98- startLine : issue . location . line ,
99- startColumn : issue . location . column ,
100- charLength : issue . sample . length
101- }
106+ }
107+ } ;
108+
109+ result . locations = [
110+ {
111+ physicalLocation : {
112+ artifactLocation : {
113+ uri : issue . file !== "N/A" ? issue . file : "file:///"
114+ } ,
115+ region : {
116+ startLine : issue . location && issue . location . line !== undefined ? ( issue . location . line === 0 ? 1 : issue . location . line ) : 1 , // This is odd, VS and VS Code highlight the line correctly, but min value is 1
117+ startColumn : issue . location && issue . location . column !== undefined ? issue . location . column + 1 : 1 , // sarif columns start from 1
118+ charLength : issue . sample ? issue . sample . length : 0
102119 }
103120 }
104- ]
105- } ) ;
121+ }
122+ ] ;
123+
124+ issues . runs [ 0 ] . results . push ( result ) ;
106125 } ) ;
107126
108- issues = JSON . stringify ( issues , null , 2 ) ;
127+ output = JSON . stringify ( issues , null , 2 ) ;
109128 }
110129 else {
111130 writeCsvHeader ( filename ) ;
112131 fileFlag = 'a' ;
113132 result . forEach ( issue => {
114- issues += [
133+ output += [
115134 issue . id ,
116135 escapeCsv ( issue . severity . name ) ,
117136 escapeCsv ( issue . confidence . name ) ,
@@ -121,11 +140,11 @@ export function writeIssues(filename, result, isSarif){
121140 escapeCsv ( issue . description ) ,
122141 `https://github.com/doyensec/electronegativity/wiki/${ issue . id } `
123142 ] . toString ( ) ;
124- issues += os . EOL ;
143+ output += os . EOL ;
125144 } ) ;
126145 }
127146
128- fs . writeFile ( filename , issues , { flag : fileFlag } , ( err ) => {
147+ fs . writeFile ( filename , output , { flag : fileFlag } , ( err ) => {
129148 if ( err ) throw err ;
130149 } ) ;
131150}
0 commit comments