Skip to content

SONARPY-4248 Add support for bandit's # nosec directive#2290

Draft
teemu-rytilahti-sonarsource wants to merge 1 commit into
masterfrom
feature/nosonar-parser-nosec-support
Draft

SONARPY-4248 Add support for bandit's # nosec directive#2290
teemu-rytilahti-sonarsource wants to merge 1 commit into
masterfrom
feature/nosonar-parser-nosec-support

Conversation

@teemu-rytilahti-sonarsource
Copy link
Copy Markdown

@teemu-rytilahti-sonarsource teemu-rytilahti-sonarsource commented Jun 2, 2026

Summary

  • Extend NoSonarInfoParser to recognize # nosec comments (case-insensitive, colon or whitespace separator, trailing description allowed).
  • # nosec produces a NoSonarLineInfo with an empty suppressed-rule-key set, which the existing NoSonarIssueFilter already interprets as "suppress all rules on this line" — no filter changes needed. As a result, # nosec silences e.g. S4790 on the offending line, matching bandit's own semantics.
  • Tests added to NoSonarInfoParserTest (bare, # nosec B101, with reason, colon form, uppercase, length-truncated, mixed with another # segment) and end-to-end cases in NoSonarLineInfoCollectorTest.

Sibling of SONARGO-809 (Go //nolint). Tracked as SONARPY-4248.

Follow-up

Bandit also supports test IDs (# nosec B303, B607). The repo has no Bandit-ID → Sonar-rule mapping today, so anything after nosec is parsed as free-form description and BXXX IDs are not honored.

A nicer behaviour for bare # nosec would be to suppress only security-typed rules (71 rules with type: VULNERABILITY or SECURITY_HOTSPOT under python-checks/.../l10n/py/rules/python/*.json) rather than everything on the line, closer to user intent. That would require:

  1. Adding a discriminator on NoSonarLineInfo (e.g. SuppressionScope.SECURITY_ONLY).
  2. Pre-computing the set of security rule keys at scanner startup (the metadata is already loaded by AbstractPythonRuleRepository via RuleMetadataLoader).
  3. Updating NoSonarIssueFilter to consult that set when the line's scope is SECURITY_ONLY.

Out of scope for this PR; can land as a follow-up under the same ticket.

Test plan

  • mvn test -Dtest=NoSonarInfoParserTest in python-frontend (68 tests pass).
  • mvn test -Dtest=NoSonarLineInfoCollectorTest in python-commons (collector test cases added but not run locally — python-commons needs python-frontend installed; CI will cover).
  • Manually verify # nosec on a hashlib.md5(...) line suppresses S4790.

Extend NoSonarInfoParser to recognize `# nosec` comments. A bare `# nosec`
(with or without trailing description text) is parsed the same way as bare
`# NOSONAR` / `# noqa`, producing a NoSonarLineInfo with an empty
suppressed-rule-key set — which the existing NoSonarIssueFilter already
treats as "suppress all rules on this line". So e.g. S4790 is silenced
when a `# nosec` is added to the offending line, matching bandit's
own semantics.

Bandit also accepts test IDs (e.g. `# nosec B303, B607`). Those are not
parsed here: the repo has no Bandit-ID → Sonar-rule mapping, and any
text after `nosec` is treated as a free-form description. Narrowing
bare `# nosec` to security-typed rules only is tracked as a follow-up
on SONARPY-4248.
@gitar-bot
Copy link
Copy Markdown

gitar-bot Bot commented Jun 2, 2026

Code Review ✅ Approved

Extends the NoSonarInfoParser to recognize Bandit's # nosec directive, enabling line-level suppression of all rules. Comprehensive tests verify the parser's case-insensitive handling and support for trailing descriptions.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant