根据 Bandit 的文档,导入 subprocess 模块被认为是低安全问题 (B404)。不幸的是,它没有提供替代方案或解释原因。因此,我有两个问题:
我的 Python 脚本必须运行只能通过控制台访问的二进制文件,所以我使用subprocess.run它,它看起来像这样:
CMD = [
"C:\\Program Files\\Azure DevOps Server 2019\\Tools\\TFSSecurity.exe",
"/gd",
f"[{ARGS.projectName}]\\{ARGS.groupName}",
f"/collection:{ARGS.organization}",
]
DELETE_OUTPUT = subprocess.run(
CMD, check=True, stdout=subprocess.PIPE, shell=True
).stdout.decode("utf-8")
print(f"[DEBUG] DELETE_OUTPUT: {DELETE_OUTPUT}")
Run Code Online (Sandbox Code Playgroud)
它工作正常,但Bandit报告了一些问题:
[B404:blacklist] Consider possible security implications associated with subprocess module.
[B602:subprocess_popen_with_shell_equals_true] subprocess call with shell=True identified, security issue
有没有办法以更安全的方式运行 CLI 以使 Bandit 满意?
我正在使用 bandit 检查我的代码是否存在潜在的安全问题:
bandit -r git-repository/
Run Code Online (Sandbox Code Playgroud)
然而,bandit 发现的最常见的物品是B101。它由测试中的断言语句触发。我使用 pytest,所以这不是一个问题,而是一个很好的做法。我现在已经创建了一个.bandit文件
[bandit]
skips: B101
Run Code Online (Sandbox Code Playgroud)
但这也跳过了很多其他代码。这个问题有解决方案吗?
我正在尝试使用pyproject.toml排除venv/目录。但它不承认这个选项。
[tool.bandit]
exclude = "/venv"
[tool.black]
exclude = "(venv)"
[tool.isort]
profile = "black"
skip = "venv"
balanced_wrapping = true
atomic = true
Run Code Online (Sandbox Code Playgroud)
如果我像这样使用 CLI 选项:
$ bandit -v -r . --exclude "/venv"
Run Code Online (Sandbox Code Playgroud)
该目录被排除。但如果我只是运行 bandit,它不会排除该目录,即使我将它放在pyproject.toml.
我的强盗版本是:1.7.1。
我正在使用 SonarQube 7.4.0.18908 收集代码覆盖率并为 Python 3.6 项目执行静态代码分析。服务器在AWS中运行。一切都按预期进行(参见下面的屏幕截图)。
现在我想为该项目添加安全扫描。我“选择”了Bandit,但实际上这似乎是当前与 SonarQube for Python 集成的唯一工具,如Import Bandit Issues Reports中所述。SonarPython 插件支持 Bandit 分析,该插件安装在 SonarQube 服务器上。为了在本地生成漏洞报告,我使用 Bandit 1.5.1 pip3 模块。
漏洞报告不会显示 - 甚至可能不会上传 - 到 SonarQube(请参见下面的相同屏幕截图)。
我将此行添加到项目的sonar-project.properties文件中:
sonar.python.bandit.reportPaths=bandit-report.json
Run Code Online (Sandbox Code Playgroud)
然后我运行了报告:
pip3 install bandit==1.5.1
bandit --format json --output bandit-report.json --recursive src
Run Code Online (Sandbox Code Playgroud)
我验证bandit-report.json包含正确的数据:
{
"errors": [],
"generated_at": "2019-01-30T14:49:18Z",
"metrics": {
...
"results": [
{
"code": "8 def prepare_df_for_comparison(df, name, ignore_columns=None, sort_columns=None):\n9 assert df is not None\n10 \n11 # …Run Code Online (Sandbox Code Playgroud) 由于预提交挂钩甚至不允许bandit发出警告和提交,因此我需要找到一种方法从 python 脚本执行 bash 命令,而不会有 bandit 抱怨。
使用subprocess python 包,到目前为止,无论我做了什么,强盗总是抱怨。我使用了“.run()”、“.check_call()”、“.Popen()”等,但都没有使用,shell=True但没有任何效果。
如果有一个安全的子流程替代方案,我也会感兴趣,但我确信它也必须以某种方式与子流程一起工作。
强盗不接受的示例:
import shlex
import subprocess
...
bash_command = (
f'aws s3 cp {source_dir} s3://{target_bucket_name} --recursive'
f' --profile {profile_name}')
subprocess.check_call(shlex.split(bash_command), text=True)
Run Code Online (Sandbox Code Playgroud) 我想获取 python 代码以使用 Bandit 静态分析器进行分析。主要重点是安全性,对于 python 2.7。
任何人都可以帮忙吗?
谢谢。
我正在配置我的 pyproject.toml 以便 bandit 排除测试文件,但它给了我错误 ERROR pyproject.toml : toml parser not available, reinstall with toml extra
这是我的 pyproject.toml
[tool.bandit]
exclude_dirs = ["*/test/*"]
tests = ["B201", "B301"]
skips = ["B101", "B601"]
[tool.pre-commit-hooks.bandit]
exclude = ["test*"]
Run Code Online (Sandbox Code Playgroud)
我运行以下命令:bandit -c pyproject.toml -r .
pre-commit bandit pre-commit.com pyproject.toml bandit-python
python ×7
security ×3
subprocess ×2
analyzer ×1
bandit ×1
bash ×1
pre-commit ×1
python-3.x ×1
sonarqube ×1