Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bandit linter incorrectly highlights the first column in a line #12118

Closed
tonybaloney opened this issue Jun 3, 2020 · 6 comments
Closed

Bandit linter incorrectly highlights the first column in a line #12118

tonybaloney opened this issue Jun 3, 2020 · 6 comments
Assignees
Labels
area-linting bug Issue identified by VS Code Team member as probable bug

Comments

@tonybaloney
Copy link

Environment data

  • VS Code version: 1.45.0

  • Extension version: 2020.5.80290 (and master)

  • OS and version: macOS 10.15

  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv

  • Relevant/affected Python packages and their versions: bandit 1.6.3

Reproduce

  1. Enable the bandit linter in the user settings
  2. Install the latest version of bandit
  3. Create a script where the offending code is nested by whitespace (i.e. within a function or block), e.g.,
import subprocess

def main(opt):
    subprocess.call(opt), shell=True)

Expected behavior

Linter to highlight the entire line or highlight the correct column

Actual behavior

Linter highlights the whitespace on the 0th column in the whitespace instead of in the right position

Screen Shot 2020-06-03 at 10 52 39 am

This is because bandit.ts hardcodes the column position to 0:
https://github.com/microsoft/vscode-python/blob/master/src/client/linters/bandit.ts#L27

Bandit doesn't currently support reporting on column offset (see Fixes).

Logs

##########Linting Output - bandit##########
1,0,LOW,B404:Consider possible security implications associated with subprocess module.
12,0,HIGH,B602:subprocess call with shell=True identified, security issue.
13,0,HIGH,B602:subprocess call with shell=True identified, security issue.

Fix

Option 1: Report the column offset from bandit

I've raised a PR with bandit to expose the col_offset of the AST node in the custom format reporter, PyCQA/bandit#618

Once this PR is merged, update the bandit custom format string to include the column offset. I've tried this in a branch of this plugin and it works nicely:

Screen Shot 2020-06-03 at 12 17 22 am

However, it would assume that the user has the latest version of bandit installed

Option 2: Change the Linter API to highlight the entire line

Currently, the linter service doesn't distinguish between the reported column being 0 or the column being unknown.

LinterMessage just has a non-nullable field for column which defaults to 0. If any of the linters can't work out the column, then you get this issue by highlighting the wrong part of the line.

Alternatively, the LinterMessage interface could be extended to set column as nullable and then underline the whole line if the column is null, or have an extra field like isWholeLine.

P.S. I'm happy to submit a PR for either fix if you share the preferred approach

@tonybaloney tonybaloney added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Jun 3, 2020
@karthiknadig karthiknadig self-assigned this Jun 3, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Jun 3, 2020
@karthiknadig
Copy link
Member

What happens when the user has a older version of bandit? If you do Option 2, when people install the latest bandit, it will start behaving similar to how it was for option 1 right? That is if a column is available it will work as expected. But if a column is not available then it will highlight the whole line.

@karthiknadig karthiknadig added the info-needed Issue requires more information from poster label Jun 19, 2020
@brettcannon
Copy link
Member

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.

@ghost ghost removed the triage label Jul 21, 2020
@tonybaloney
Copy link
Author

The PR to the bandit project has just been merged PyCQA/bandit#618

Please can you reopen this issue and I can submit a PR to use the column offset field to highlight the correct token in the editor.

To answer the question - what happens if the user is using an old version of bandit? It will default to the old behaviour

@brettcannon brettcannon reopened this Dec 17, 2020
@ghost ghost added the triage-needed Needs assignment to the proper sub-team label Dec 17, 2020
@brettcannon
Copy link
Member

@tonybaloney re-opened.

@brettcannon brettcannon added needs PR area-linting and removed triage-needed Needs assignment to the proper sub-team info-needed Issue requires more information from poster labels Dec 17, 2020
@tonybaloney
Copy link
Author

Has a PR attached now

@luabud
Copy link
Member

luabud commented Jan 22, 2021

Closing as fixed!

@luabud luabud closed this as completed Jan 22, 2021
@ghost ghost removed the needs PR label Jan 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-linting bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

4 participants