Skip to content

Commit 017c2cc

Browse files
committed
Parse bandit results
1 parent 630b81e commit 017c2cc

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
Security Notes allows the creation of notes within source files, which can be replied to, reacted to using emojis, and assigned statuses such as "TODO", "Vulnerable" and "Not Vulnerable".
1616

17-
Also, it allows importing the output from SAST tools (currently only [Semgrep](https://semgrep.dev/)) into notes, making the processing of the findings much easier.
17+
Also, it allows importing the output from SAST tools into notes, making the processing of the findings much easier. Currently supported tools include:
18+
19+
- semgrep (https://semgrep.dev/)
20+
- bandit (https://bandit.readthedocs.io/en/latest/)
1821

1922
Finally, collaborate with others by using a centralized database for notes that will be automatically synced in **real-time**! Create a note locally, and it will be automatically pushed to whoever is working with you on the project.
2023

@@ -70,7 +73,7 @@ The extension allows you to import the output from SAST tools (currently only [S
7073

7174
## Extension Settings
7275

73-
Various settings for the extension can be configured in VSCode's User Settings page (`CMD+Shift+P` / `Ctrl + Shift + P` -> *Preferences: Open Settings (UI)*):
76+
Various settings for the extension can be configured in VSCode's User Settings page (`CMD+Shift+P` / `Ctrl + Shift + P` -> _Preferences: Open Settings (UI)_):
7477

7578
![Extension Settings](images/settings.png)
7679

src/parsers/bandit.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
import * as vscode from 'vscode';
4+
import { ToolFinding } from '../models/toolFinding';
5+
6+
class BanditParser {
7+
static parse(fileContent: string) {
8+
const toolFindings: ToolFinding[] = [];
9+
10+
try {
11+
const banditFindings = JSON.parse(fileContent).results;
12+
banditFindings.map((banditFinding: any) => {
13+
// uri
14+
let fullPath = '';
15+
if (vscode.workspace.workspaceFolders) {
16+
fullPath = vscode.workspace.workspaceFolders[0].uri.fsPath + '/';
17+
}
18+
const uri = vscode.Uri.file(`${fullPath}${banditFinding.filename}`);
19+
20+
// range
21+
const lineRange = banditFinding.line_range;
22+
const range = new vscode.Range(
23+
lineRange[0] - 1,
24+
0,
25+
(lineRange[1] ? lineRange[1] : lineRange[0]) - 1,
26+
0,
27+
);
28+
29+
// instantiate tool finding and add to list
30+
const toolFinding = new ToolFinding(uri, range, banditFinding.issue_text);
31+
toolFindings.push(toolFinding);
32+
});
33+
} catch {
34+
/* empty */
35+
}
36+
37+
return toolFindings;
38+
}
39+
}
40+
41+
export { BanditParser };

src/webviews/importToolResultsWebview.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as vscode from 'vscode';
44
import { commentController } from '../controllers/comments';
5+
import { BanditParser } from '../parsers/bandit';
56
import { SemgrepParser } from '../parsers/semgrep';
67
import { ToolFinding } from '../models/toolFinding';
78
import { saveNoteComment } from '../helpers';
@@ -42,12 +43,7 @@ export class ImportToolResultsWebview implements vscode.WebviewViewProvider {
4243
webviewView.webview.onDidReceiveMessage((data) => {
4344
switch (data.type) {
4445
case 'processToolFile': {
45-
processToolFile(
46-
data.toolName,
47-
data.fileContent,
48-
this.noteMap,
49-
this.remoteDb,
50-
);
46+
processToolFile(data.toolName, data.fileContent, this.noteMap, this.remoteDb);
5147
}
5248
}
5349
});
@@ -86,6 +82,7 @@ export class ImportToolResultsWebview implements vscode.WebviewViewProvider {
8682
<p>
8783
<select id="toolSelect">
8884
<option value="semgrep">semgrep</option>
85+
<option value="bandit">bandit</option>
8986
</select>
9087
</p>
9188
<p>Select file:</p>
@@ -113,6 +110,11 @@ function processToolFile(
113110
switch (toolName) {
114111
case 'semgrep': {
115112
toolFindings = SemgrepParser.parse(fileContent);
113+
break;
114+
}
115+
case 'bandit': {
116+
toolFindings = BanditParser.parse(fileContent);
117+
break;
116118
}
117119
}
118120

0 commit comments

Comments
 (0)