Light Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix B613 crash when reading from stdin#1361

Merged
ericwb merged 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-trojansource-stdin
Feb 22, 2026
Merged

Fix B613 crash when reading from stdin#1361
ericwb merged 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-trojansource-stdin

Conversation

Copy link
Contributor

worksbyfriday commented Feb 17, 2026

Summary

  • The trojansource plugin (B613) crashed with FileNotFoundError when bandit reads from stdin (bandit -)
  • The plugin opened context.filename directly, but for stdin input the filename is the sentinel string ''
  • Fixed by using context.file_data (the already-opened binary stream) instead of re-opening the file
  • This also avoids an unnecessary second file open for regular files

Fixes #1182

Test plan

  • Existing test_trojansource and test_trojansource_latin1 tests pass
  • Full functional test suite passes (79/79)
  • Manual test: cat examples/trojansource.py | bandit - now correctly detects bidi characters instead of crashing
  • Manual test: echo 'print("hello")' | bandit - works cleanly with no errors
  • ruff check and ruff format pass

Friday and others added 2 commits February 17, 2026 09:08
The trojansource plugin opened `context.filename` directly as a file,
but when reading from stdin the filename is the sentinel string
`''`, causing a FileNotFoundError.

Use `context.file_data` (the already-opened binary stream) instead of
re-opening the file. This fixes stdin support and avoids an unnecessary
second file open for regular files.

Fixes PyCQA#1182

Co-Authored-By: Claude Opus 4.6
Copy link
Member

ericwb commented Feb 18, 2026 *
edited
Loading

Note: unit tests are failing due stestr. Two PRs are proposed to fix: mtreinish/stestr#378 and mtreinish/stestr#377

ericwb approved these changes Feb 22, 2026
ericwb merged commit b69b336 into PyCQA:main Feb 22, 2026
17 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

ericwb ericwb approved these changes

lukehinds Awaiting requested review from lukehinds lukehinds is a code owner

sigmavirus24 Awaiting requested review from sigmavirus24 sigmavirus24 is a code owner

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Traceback when using passing file via stdin

2 participants