Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asking bandit to use pyproject.toml without a bandit config section causes exception #1027

Closed
apirogov opened this issue May 11, 2023 · 0 comments · Fixed by #1073
Closed
Labels
bug Something isn't working

Comments

@apirogov
Copy link

apirogov commented May 11, 2023

Describe the bug

Apparently bandit does not like it if asked to use pyproject.toml and not find a dedicated config section.

Reproduction steps

I use the following pre-commit hook to run bandit:

  - repo: https://github.com/PyCQA/bandit
    rev: '1.7.5'
    hooks:
    - id: bandit
      args: ['-q', '-c', 'pyproject.toml']
      additional_dependencies: [ '.[toml]' ]

with a pyproject.toml that has no configuration for bandit.

This results in:

bandit...................................................................Failed
- hook id: bandit
- exit code: 1

Traceback (most recent call last):
  File "/local/home/a.pirogov/.cache/pre-commit/repo_hijlu0c/py_env-python3/bin/bandit", line 8, in <module>
    sys.exit(main())
  File "/local/home/a.pirogov/.cache/pre-commit/repo_hijlu0c/py_env-python3/lib/python3.8/site-packages/bandit/cli/main.py", line 455, in main
    b_conf = b_config.BanditConfig(config_file=args.config_file)
  File "/local/home/a.pirogov/.cache/pre-commit/repo_hijlu0c/py_env-python3/lib/python3.8/site-packages/bandit/core/config.py", line 56, in __init__
    self._config = tomllib.load(f)["tool"]["bandit"]
KeyError: 'bandit'

Expected behavior

For most tools with some config files, if the config does not "override" behavior, it is using default values.

So I expect the same when running bandit with a pyproject.toml. A missing section in the toml to me feels the same as an empty config file.

Currently, I have to create an empty bandit config section in order to be able to keep the hook as it is (anticipating that IF I change the config, I will put it in the toml).

Bandit version

1.7.5 (Default)

Python version

3.8

Additional context

No response

@apirogov apirogov added the bug Something isn't working label May 11, 2023
javajawa added a commit to javajawa/bandit that referenced this issue Dec 3, 2023
This is a naive fix for bandit crashing when it encounters a
`pyproject.toml` which does not contain any specific bandit
configuration.

This resolves the common failure mode that is seen, but does not cause
bandit to fall back to another configuration source if the
`pyproject.toml` does not contain any `tool.bandit` block.

Resolves PyCQA#1027
ericwb added a commit that referenced this issue Dec 8, 2023
* Fix crash on pyproject.toml without bandit config

This is a naive fix for bandit crashing when it encounters a
`pyproject.toml` which does not contain any specific bandit
configuration.

This resolves the common failure mode that is seen, but does not cause
bandit to fall back to another configuration source if the
`pyproject.toml` does not contain any `tool.bandit` block.

Resolves #1027

* Update bandit/core/config.py

---------

Co-authored-by: Eric Brown <ericwb@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant