Skip to content

Commit 2411004

Browse files
Fix lack of col field in bandit breaking linter output (#16337)
* Allow bandit without col output * Add a test * Use a custom regex to fix the bug * Make sure its called with the custom regex * Add news entry * Update news/2 Fixes/15561.md Co-authored-by: Karthik Nadig <[email protected]>
1 parent ccc5bf1 commit 2411004

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

news/2 Fixes/15561.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes a bug in the bandit linter where messages weren't being propagated to the editor.
2+
(thanks [Anthony Shaw](https://github.com/tonybaloney))

src/client/linters/bandit.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const severityMapping: Record<string, LintMessageSeverity | undefined> = {
1616
HIGH: LintMessageSeverity.Error,
1717
};
1818

19+
export const BANDIT_REGEX =
20+
'(?<line>\\d+),(?<column>(col)?(\\d+)?),(?<type>\\w+),(?<code>\\w+\\d+):(?<message>.*)\\r?(\\n|$)';
21+
1922
export class Bandit extends BaseLinter {
2023
constructor(outputChannel: OutputChannel, serviceContainer: IServiceContainer) {
2124
super(Product.bandit, outputChannel, serviceContainer);
@@ -35,6 +38,7 @@ export class Bandit extends BaseLinter {
3538
],
3639
document,
3740
cancellation,
41+
BANDIT_REGEX,
3842
);
3943

4044
messages.forEach((msg) => {

src/test/linters/bandit.unit.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import { expect } from 'chai';
7+
import { parseLine } from '../../client/linters/baseLinter';
8+
import { BANDIT_REGEX } from '../../client/linters/bandit';
9+
10+
import { ILintMessage, LinterId } from '../../client/linters/types';
11+
12+
suite('Linting - Bandit', () => {
13+
test('parsing new bandit with col', () => {
14+
const newOutput = `\
15+
1,0,LOW,B404:Consider possible security implications associated with subprocess module.
16+
19,4,HIGH,B602:subprocess call with shell=True identified, security issue.
17+
`;
18+
19+
const lines = newOutput.split('\n');
20+
const tests: [string, ILintMessage | undefined][] = [
21+
[
22+
lines[0],
23+
{
24+
code: 'B404',
25+
message: 'Consider possible security implications associated with subprocess module.',
26+
column: 0,
27+
line: 1,
28+
type: 'LOW',
29+
provider: 'bandit',
30+
},
31+
],
32+
[
33+
lines[1],
34+
{
35+
code: 'B602',
36+
message: 'subprocess call with shell=True identified, security issue.',
37+
column: 3,
38+
line: 19,
39+
type: 'HIGH',
40+
provider: 'bandit',
41+
},
42+
],
43+
];
44+
for (const [line, expected] of tests) {
45+
const msg = parseLine(line, BANDIT_REGEX, LinterId.Bandit, 1);
46+
47+
expect(msg).to.deep.equal(expected);
48+
}
49+
});
50+
test('parsing old bandit with no col', () => {
51+
const newOutput = `\
52+
1,col,LOW,B404:Consider possible security implications associated with subprocess module.
53+
19,col,HIGH,B602:subprocess call with shell=True identified, security issue.
54+
`;
55+
56+
const lines = newOutput.split('\n');
57+
const tests: [string, ILintMessage | undefined][] = [
58+
[
59+
lines[0],
60+
{
61+
code: 'B404',
62+
message: 'Consider possible security implications associated with subprocess module.',
63+
column: 0,
64+
line: 1,
65+
type: 'LOW',
66+
provider: 'bandit',
67+
},
68+
],
69+
[
70+
lines[1],
71+
{
72+
code: 'B602',
73+
message: 'subprocess call with shell=True identified, security issue.',
74+
column: 0,
75+
line: 19,
76+
type: 'HIGH',
77+
provider: 'bandit',
78+
},
79+
],
80+
];
81+
for (const [line, expected] of tests) {
82+
const msg = parseLine(line, BANDIT_REGEX, LinterId.Bandit, 1);
83+
84+
expect(msg).to.deep.equal(expected);
85+
}
86+
});
87+
});

0 commit comments

Comments
 (0)