PyCQA / PyCQA/bandit

Allow # nosec on the preceeding line

Open
#469 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
8.3k
Forks
835
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Is your feature request related to a problem? Please describe.
I've been asked/required to elaborate on the rationale for any # nosec comments in the codebase. (This is a good idea/practice, IMO.) However, generally, there's little room in the margin on the line on which a # nosec currently needs to be placed.

Describe the solution you'd like
I'd love it if I could place the # nosec above the offending line. E.g.,

Currently,

        subprocess.check_call(['gzip', '-r', dst])  # nosec <little room>

This could be:

        # nosec: `dst` here come from <source>, which we know to be safe
        # due to <rationale.>
        subprocess.check_call(['gzip', '-r', dst])

Describe alternatives you've considered
We can just do both, e.g.,

        # nosec: `dst` here come from <source>, which we know to be safe
        # due to <rationale.>
        subprocess.check_call(['gzip', '-r', dst])  # nosec

(Note that the line itself is also flagged.) My co-worker would like to be able to grep out all nosecs and hopefully find all/any that lack rationale. E.g., with /# nosec *$/. However, that would catch the above incorrectly: the rationale is just slightly detached from the notice, so these show up as false positives.

Additional context
Keep in mind #418, which I'd also like to see. 😄

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing handling of inline # nosec comments and review issue #418 for related context. Done means a # nosec on the immediately preceding line suppresses the following finding while preserving rationale text and existing inline behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
security, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.