Empty or scalar YAML config crashes before type validation

Open
#1,484 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
security, tooling

Research direction

Start with BanditConfig.validate() in bandit/core/config.py and the existing dictionary check that currently runs after validation. Read the configuration tests and add malformed-root cases for empty, null, boolean, numeric, string, and sequence YAML values, keeping {} valid. Done means invalid roots use the ConfigError/CLI error path without a traceback.

Written by the indexing model from the issue text.

Description

bug
Describe the bug

At main commit 48ef27bd1bbc32f4e585c92310f7a574e87f6b04, an empty YAML config raises TypeError in BanditConfig.validate(). The existing dictionary check runs after validation, too late to reject the parsed value. Scalar roots and a sequence containing profiles have the same ordering problem.

This resembles the symptom in historical #139, but the current failure is in legacy-profile validation, before the existing non-dictionary check.

Reproduction steps
printf '' > /tmp/bandit-empty.yaml
printf 'x = 1\n' > /tmp/bandit-clean.py
bandit -c /tmp/bandit-empty.yaml /tmp/bandit-clean.py

# Traceback: bandit/core/config.py line 252, in validate
# if "profiles" in self._config:
# TypeError: argument of type 'NoneType' is not iterable
Expected behavior

Reject invalid configuration through the existing ConfigError/CLI error path, without a traceback. An empty dictionary ({}) should remain valid.

Bandit version

1.9.1 (Default)

Python version

3.12

Additional context

Tested source checkout: 48ef27bd1bbc32f4e585c92310f7a574e87f6b04, Python 3.12.14 on Linux. The editable install reports version 0.0.0; the required Bandit version dropdown has no source-checkout option, so its default is not the tested release version.

Tested patch: https://github.com/arin30/bandit/commit/f769648f3c7be0a7d2adfc13a3095f809ac94137

The fix moves the existing dictionary check ahead of self.validate(config_file). Added malformed-root coverage for empty, null, boolean, numeric, string and sequence values, plus a valid empty-mapping control.

The new malformed-root regression fails on the base. With the patch, all 181 unit tests pass, including 28 configuration tests. Seven actual CLI checks confirm invalid roots exit 2 without a traceback and {} scans successfully. Black 25.12.0 with repository settings, Flake8 on both changed files, and git diff --check pass.

I couldn't open a PR because the repository currently limits PR creation to collaborators. The patch is available above for review. No detection rules change.

Dominant language
Python
Stars
8.4k
Forks
893
Avg merge
6h 52m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/bandit

All issues in PyCQA/bandit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.