Skip to content

fix: security (bandit) detector ignores config-level excludes#620

Open
Loreweavr wants to merge 1 commit into
peteromallet:mainfrom
Loreweavr:fix/security-detector-honors-config-excludes
Open

fix: security (bandit) detector ignores config-level excludes#620
Loreweavr wants to merge 1 commit into
peteromallet:mainfrom
Loreweavr:fix/security-detector-honors-config-excludes

Conversation

@Loreweavr

Copy link
Copy Markdown

Problem

desloppify exclude <dir> is honoured by the file-discovery detectors (duplication, orphaned, unused, …) but not by the Python security / bandit detector — it keeps reporting findings from excluded directories (e.g. a vendored library you don't own).

Minimal repro:

mkdir -p proj/vendor
printf 'import subprocess\nsubprocess.Popen("x", shell=True)\n' > proj/vendor/bad.py
printf 'x = 1\n' > proj/app.py
cd proj
desloppify exclude vendor
desloppify scan --path .
desloppify show security --status open   # vendor/bad.py is STILL reported

Root cause

detect_python_security (languages/python/_security.py) builds bandit's --exclude list from collect_exclude_dirs(scan_root). That uses get_exclusions(runtime=None), which resolves to a runtime context whose .exclusions is empty here — so only the built-in DEFAULT_EXCLUSIONS (.git, node_modules, …) reach bandit. The persisted config excludes (config["exclude"], what desloppify exclude writes) are never threaded in, so bandit -r recursively rescans them.

The bandit adapter already supports --exclude correctly — the excludes simply never arrive.

Fix

Load the config excludes in detect_python_security and pass them through collect_exclude_dirs(extra_exclusions=…) alongside get_exclusions().

Tests

There was no bandit exclude-flag test (only ruff ones), which is how this slipped through. Adds TestBanditSecurityExcludeFlag to tests/detectors/test_external_adapters_exclude_flags.py. Confirmed it fails without the fix (the exclude list contains only the defaults) and passes with it. Full languages/python/ + detectors/security/ suites: 379 passed.

Environment where observed: Windows, Python 3.13, bandit 1.9.4.

🤖 Generated with Claude Code

`desloppify exclude <dir>` is respected by the file-discovery detectors but not
by the Python security detector: detect_python_security built bandit's --exclude
from collect_exclude_dirs(scan_root), which only sees the runtime context's
exclusions (empty there) plus the built-in defaults. The persisted config
excludes were never threaded in, so bandit recursively re-scanned excluded
directories and reported security findings from them.

Load the config excludes and pass them via collect_exclude_dirs(extra_exclusions=)
alongside get_exclusions(). Adds a regression test (there was no bandit
exclude-flag test, which is how this slipped through).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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