Honor ruff # noqa: S### suppressions in the Bandit adapter#622
Open
willfrey wants to merge 1 commit into
Open
Conversation
Ruff's flake8-bandit codes (`S<NNN>`) map one-to-one onto Bandit's `B<NNN>` tests — the numeric part is identical (`S608` <-> `B608`). A ruff-based project that has reviewed and suppressed a site with `noqa: S608` was still re-flagged by the bundled Bandit run, because the adapter relied only on Bandit's own `nosec`. Every such site was reported twice (silenced by ruff, surfaced by Bandit), producing a wall of security false positives on any project that uses ruff's `S` rules as its security linter. Honor the ruff form the way Bandit honors `nosec`: skip a finding when its flagged line carries a bare `noqa` or a `noqa: S<NNN>` whose number matches the Bandit test. Bandit strips its own `nosec` lines before results reach the adapter, so only the ruff form needs handling here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Ruff's flake8-bandit rules (
S###) are a common way to run Bandit's checks as part of a singleruffpass. Those codes map one-to-one onto Bandit's tests — the numeric part is identical (S608⇄B608,S310⇄B310).When a project uses ruff's
Srules as its security linter and suppresses a reviewed site with# noqa: S608, the bundled Bandit run in this adapter still re-reports it. The adapter only benefits from Bandit's own# nosec, which Bandit strips before results reach us — it has no knowledge of ruff's# noqa. The result: every reviewed-and-suppressed site is reported twice (silenced by ruff, surfaced here), producing a wall of security false positives on any ruff-based project.Minimal repro: a file containing
is reported as a
B608finding even though the project has explicitly accepted it via ruff.Fix
Honor ruff's
# noqathe same way Bandit honors# nosec: when a Bandit finding's flagged line carries a bare# noqa, or a# noqa: S<NNN>whose number matches the Bandit test (B<NNN>), skip it. Bandit strips its own# noseclines before results reach the adapter, so only the ruff form needs handling here.The check reads the flagged line from the source file and matches a small
noqaregex; it is scoped toB<NNN>-shaped test IDs and fails safe (reports the finding) on any read error.Tests
# noqa: S608suppresses aB608finding; a bare# noqasuppresses it;# noqa: E501(unrelated) does not; an un-annotated line is still reported.