Skip to content

fix: exclude the test directory from bandit - #4989

Merged
kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/bandit-exclude-test-dir
Nov 4, 2026
Merged

kddejong merged 1 commit into
aws-cloudformation:mainfrom
kddejong:fix/bandit-exclude-test-dir

Conversation

@kddejong

@kddejong kddejong commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This repo's test directory is test/ (singular), so the "tests" entry in [tool.bandit] exclude_dirs has never matched it and bandit has been scanning the whole test suite.

bandit's exclusion is a bare substring test, not a path match — from bandit/core/manager.py::_is_file_included in 1.7.9 (the version pinned in .pre-commit-config.yaml):

if not _matches_glob_list(path, excluded_path_strings) and not any(
    x in path for x in excluded_path_strings
):

Because of that, the trailing slash matters: "test" alone would also exclude any src/ file whose path contains that substring (e.g. a future src/cfnlint/.../testing_helper.py). "./test" does not work either, since pre-commit passes bare relative paths with no ./ prefix.

Measured with pre-commit run bandit --all-files on this branch vs main:

  • Lines scanned drop from 88,719 to 30,603.
  • src/ is still scanned: adding _bandit_sanity = "/tmp/sanity" to src/cfnlint/helpers.py fails B108 at src/cfnlint/helpers.py:869, and passes again once reverted.
  • A /tmp string in a tracked file under test/ fails B108 with the old config and passes with this one.

This unblocks #4914, where two test fixtures containing "cwd": "/tmp" fail CI on B108.

Full local gate: pytest test 3445 passed / 0 failed, pre-commit run --all-files all hooks pass.

@codecov

codecov Bot commented Nov 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.80%. Comparing base (f8018a2) to head (67788ed).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4989   +/-   ##
=======================================
  Coverage   94.80%   94.80%           
=======================================
  Files         441      441           
  Lines       16165    16165           
  Branches     3135     3135           
=======================================
  Hits        15326    15326           
  Misses        463      463           
  Partials      376      376           
Flag Coverage Δ
unittests 94.80% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kddejong
kddejong merged commit c4746e8 into aws-cloudformation:main Nov 4, 2026
24 checks passed
@kddejong
kddejong deleted the fix/bandit-exclude-test-dir branch November 4, 2026 20:31
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