如何让 bandit 在测试中跳过 B101?
How can I make bandit skip B101 within tests?
我正在使用 bandit 检查我的代码是否存在潜在的安全问题:
bandit -r git-repository/
但是,土匪发现的最常见物品是 B101。它由测试中的断言语句触发。我使用 pytest,所以这不是问题,而是一个很好的做法。我现在用
创建了一个 .bandit
文件
[bandit]
skips: B101
但这也跳过了很多其他代码。这个问题有解决办法吗?
基于 documentation,您的配置应该如下所示
skips: ['B101']
,而不是 skips: B101
(你有)。
编辑:
好的,如果我没理解错的话,你想跳过 tests
文件夹中的 B101
。
我不知道有什么方法可以指定这个,但我可以想到一种黑客攻击 - 只是 运行 bandit 两次 - 一次忽略 tests
,一次只在测试中跳过 B101
.我知道,这不是最优雅的方式,但它应该可以解决您的问题。
一个可能的解决方案是告诉 bandit
完全跳过测试。假设您的代码位于 src
子文件夹中,运行
bandit --configfile bandit.yaml --recursive src
在项目根目录下bandit.yaml
# Do not check paths including `/tests/`:
# they use `assert`, leading to B101 false positives.
exclude_dirs:
- '/tests/'
有个bunch of related issues and pull requests.
更新:我更喜欢。
您可以配置跳过此检查的文件。当您在测试用例中使用断言语句时,这通常很有用。
bandit --configfile bandit.yaml
在项目根目录下bandit.yaml
assert_used:
skips: ['*_test.py', 'test_*.py']
Link 到原文档
基于this comment、
when using --recursive
the whole path is fnmatch
ed against the
glob_list
, therefore an --exclude_dir
expression test_*.py
doesn't
matches and excludes (py)test files in subdirectories, for that
*/test_*.py
is needed.
以下配置应该可以解决您的问题:
assert_used:
skips: ["*/test_*.py", "*/test_*.py"]
我正在使用 bandit 检查我的代码是否存在潜在的安全问题:
bandit -r git-repository/
但是,土匪发现的最常见物品是 B101。它由测试中的断言语句触发。我使用 pytest,所以这不是问题,而是一个很好的做法。我现在用
创建了一个.bandit
文件
[bandit]
skips: B101
但这也跳过了很多其他代码。这个问题有解决办法吗?
基于 documentation,您的配置应该如下所示
skips: ['B101']
,而不是 skips: B101
(你有)。
编辑:
好的,如果我没理解错的话,你想跳过 tests
文件夹中的 B101
。
我不知道有什么方法可以指定这个,但我可以想到一种黑客攻击 - 只是 运行 bandit 两次 - 一次忽略 tests
,一次只在测试中跳过 B101
.我知道,这不是最优雅的方式,但它应该可以解决您的问题。
一个可能的解决方案是告诉 bandit
完全跳过测试。假设您的代码位于 src
子文件夹中,运行
bandit --configfile bandit.yaml --recursive src
在项目根目录下bandit.yaml
# Do not check paths including `/tests/`:
# they use `assert`, leading to B101 false positives.
exclude_dirs:
- '/tests/'
有个bunch of related issues and pull requests.
更新:我更喜欢
您可以配置跳过此检查的文件。当您在测试用例中使用断言语句时,这通常很有用。
bandit --configfile bandit.yaml
在项目根目录下bandit.yaml
assert_used:
skips: ['*_test.py', 'test_*.py']
Link 到原文档
基于this comment、
when using
--recursive
the whole path isfnmatch
ed against theglob_list
, therefore an--exclude_dir
expressiontest_*.py
doesn't matches and excludes (py)test files in subdirectories, for that*/test_*.py
is needed.
以下配置应该可以解决您的问题:
assert_used:
skips: ["*/test_*.py", "*/test_*.py"]