Skip to content

Commit 719799f

Browse files
authored
Merge pull request #80 from JarLob/sarif
Fix broken sarif export
2 parents 043ad89 + d3ef6db commit 719799f

3 files changed

Lines changed: 68 additions & 49 deletions

File tree

src/finder/finder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class Finder {
110110
const firstLineSample = this.get_sample(fileLines, 0);
111111
const matchedLineSample = this.get_sample(fileLines, m.line - 1);
112112
if (!isDisabledByInlineComment(firstLineSample, matchedLineSample, check, sourceTypes.JAVASCRIPT)) {
113-
const issue = { file, matchedLineSample, location: {line: m.line, column: m.column}, id: m.id, description: m.description, properties: m.properties, severity: m.severity, confidence: m.confidence, manualReview: m.manualReview, shortenedURL: m.shortenedURL };
113+
const issue = { file, sample: matchedLineSample, location: {line: m.line, column: m.column}, id: m.id, description: m.description, properties: m.properties, severity: m.severity, confidence: m.confidence, manualReview: m.manualReview, shortenedURL: m.shortenedURL };
114114
issues.push(issue);
115115
}
116116
}
@@ -132,7 +132,7 @@ export class Finder {
132132
const matchedLineSample = this.get_sample(fileLines, m.line - 1);
133133

134134
if (!isDisabledByInlineComment(firstLineSample, matchedLineSample, check, sourceTypes.HTML)) {
135-
const issue = {file, matchedLineSample, location: {line: m.line, column: m.column}, id: m.id, description: m.description, properties: m.properties, severity: m.severity, confidence: m.confidence, manualReview: m.manualReview, shortenedURL: m.shortenedURL };
135+
const issue = {file, sample: matchedLineSample, location: {line: m.line, column: m.column}, id: m.id, description: m.description, properties: m.properties, severity: m.severity, confidence: m.confidence, manualReview: m.manualReview, shortenedURL: m.shortenedURL };
136136
issues.push(issue);
137137
}
138138
}

src/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export default async function run(options, forCli = false) {
167167
}
168168

169169
if (options.output)
170-
writeIssues(options.output, issues, options.isSarif);
170+
writeIssues(options.input, options.isRelative, options.output, issues, options.isSarif);
171171

172172
if (forCli) {
173173
if (rows.length > 0) {

src/util/file.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import dir from 'node-dir';
33
import os from 'os';
44
import path from 'path';
5+
const VER = require('../../package.json').version;
56

67
export 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

Comments
 (0)