Divolika/docker-compose-security-linter
GitHub: Divolika/docker-compose-security-linter
一个基于 Python 的 Docker Compose 文件静态安全 linter,依据 CIS Docker Benchmark 检测容器加固缺失并能无缝接入 CI 流水线。
Stars: 0 | Forks: 0
# Compose-Linter
一个用于 Docker Compose 文件的静态安全 linter。它会解析
`docker-compose.yml` 并标记常见的容器加固问题——
不会启动任何容器,不会执行任何操作,也不会进行任何网络调用。
专为接入 CI 而构建:当发现达到或超过所选
严重程度的问题时,它会以非零状态退出。
## 检查
| ID | 严重程度 | 检查项 |
|--------|----------|---------------------------------------------------|
| DCL001 | HIGH | `privileged: true` |
| DCL002 | HIGH | `network_mode: host` |
| DCL003 | HIGH | `pid: host` |
| DCL004 | HIGH | 挂载了 Docker socket (`/var/run/docker.sock`) |
| DCL005 | MEDIUM | 绑定挂载了敏感的宿主机路径 (`/etc`, `/`, …) |
| DCL006 | MEDIUM | 以 root 运行 / 没有明确的非 root 用户 |
| DCL007 | LOW | 缺少 `no-new-privileges` |
| DCL008 | LOW | 没有 CPU/内存限制 |
| DCL009 | MEDIUM | 添加了危险的 capability (`SYS_ADMIN`, …) |
| DCL010 | MEDIUM | 敏感的后端端口绑定到 `0.0.0.0` |
安全指导源自 CIS Docker Benchmark 和官方 Docker
安全文档。
## 安装
```
git clone https://github.com/Divolika/docker-compose-linter.git
cd docker-compose-linter
pip install -e .
```
唯一的依赖项是 `PyYAML`。
## 用法
```
# 人类可读的报告
compose-linter docker-compose.yml
# JSON(用于 dashboards / 进一步处理)
compose-linter docker-compose.yml -f json
# 在 CI 中:遇到任何 MEDIUM 或更高严重级别的发现时使构建失败
compose-linter docker-compose.yml --fail-on MEDIUM
```
退出代码:`0` 表示正常(低于阈值),`1` 表示发现了达到/超过 `--fail-on` 的问题,
`2` 表示读取/解析文件时出错。
## 示例
针对 `samples/insecure-compose.yml` 运行:
```
[HIGH] DCL001 web: Service runs in privileged mode
`privileged: true` grants the container nearly all host capabilities...
fix: Remove `privileged: true`. Grant only the specific capabilities...
[HIGH] DCL004 web: Docker socket mounted into container
Mounting /var/run/docker.sock gives the container full control...
fix: Avoid mounting the socket...
Summary: 13 findings (3 high, 6 medium, 4 low)
```
`samples/hardened-compose.yml` 返回 `No issues found.` 且退出代码为 0。
## 在 GitHub Actions 中使用
```
- name: Lint compose file
run: |
pip install -e .
compose-linter docker-compose.yml --fail-on HIGH
```
## 扩展
每项检查都是 `compose_linter/checks.py` 中的一个小型已注册函数:
```
@check
def my_rule(doc, name, svc):
if svc.get("something_bad"):
yield Finding(service=name, check_id="DCL011", severity="MEDIUM",
title="...", detail="...", remediation="...")
```
## 运行测试
```
pip install pytest pyyaml
pytest -q
```
标签:DevSecOps, Docker, LNA, Python, 上游代理, 云安全监控, 关系图谱, 基线检查, 安全检查, 安全规则引擎, 安全防御评估, 开源框架, 恶意代码分类, 持续集成, 无后门, 版权保护, 逆向工具, 静态分析