Matheus-Max-Lima/devsecops-secure-pipeline
GitHub: Matheus-Max-Lima/devsecops-secure-pipeline
基于GitHub Actions的DevSecOps安全流水线模板,集成SAST、SCA和密钥检测,在代码提交时自动扫描并阻断不安全构建。
Stars: 0 | Forks: 0
# 安全的 DevSecOps Pipeline






## 本项目演示了什么
本仓库展示了如何在开发 Pipeline 中集成**从一开始就保障安全** (security from the start) 的理念,
在每次 `git push` 时自动执行四个层级的检查:
| 层级 | 工具 | 检查内容 |
|---|---|---|
| 测试 | pytest | 代码是否正常运行 |
| SAST | Bandit | Python 代码中的漏洞 |
| SCA | pip-audit | 第三方依赖中的 CVE |
| Secrets | detect-secrets | 代码中硬编码的凭据 |
该 Pipeline 会**自动阻止**任何包含严重安全问题的代码,
防止漏洞到达生产环境。
## Pipeline 架构
```
git push
|
v
GitHub Actions (ubuntu-latest)
|
|-- [1] Setup: Python 3.11 + ferramentas
|
|-- [2] pytest
| Falha imediata se testes quebrarem
|
|-- [3] Bandit (SAST) --> reports/bandit-report.json
|-- [4] pip-audit (SCA) --> reports/pip-audit-report.json
|-- [5] detect-secrets --> reports/secrets-report.json
| (rodam em paralelo, todos salvam resultados)
|
|-- [6] generate_report.py
| Le os 3 JSONs, gera reports/security-report.md
| Exit code 1 se encontrar issues criticos
|
|-- [7] Upload de artefatos (sempre executa, salva evidencias)
|
|-- [8] Security Gate
Bloqueia o pipeline se [6] retornou exit code 1
```
## 仓库结构
```
devsecops-secure-pipeline/
|
|-- .github/
| +-- workflows/
| +-- security-pipeline.yml # Pipeline completo do GitHub Actions
|
|-- src/
| +-- app.py # Aplicacao Python com vulnerabilidades intencionais
|
|-- tests/
| +-- test_app.py # 27 testes automatizados com pytest
|
|-- scripts/
| +-- generate_report.py # Gerador do relatorio consolidado Markdown
|
|-- reports/
| +-- bandit-report.json # Saida do Bandit (gerado pelo pipeline)
| +-- pip-audit-report.json # Saida do pip-audit (gerado pelo pipeline)
| +-- secrets-report.json # Saida do detect-secrets (gerado pelo pipeline)
| +-- security-report.md # Relatorio final consolidado
|
|-- .bandit # Configuracao do Bandit
|-- .secrets.baseline # Baseline do detect-secrets
|-- requirements.txt # Dependencias da aplicacao
|-- requirements-dev.txt # Ferramentas de seguranca e teste
+-- README.md
```
## 演示的漏洞
`src/app.py` 文件包含**故意的、伪造的**漏洞,用于演示安全工具的工作原理:
| # | 漏洞 | CWE | 检测工具 |
|---|---|---|---|
| 1 | 通过 f-string 实现的 SQL Injection | CWE-89 | Bandit B608 |
| 2 | 使用外部输入的 eval() | CWE-78 | Bandit B307 |
| 3 | 使用 shell=True 的 subprocess | CWE-78 | Bandit B602 |
| 4 | 用于密码的 MD5 哈希 | CWE-327 | Bandit B324 |
| 5 | 不安全的 pickle.load() | CWE-502 | Bandit B301/B403 |
| 6 | 硬编码的 API key | CWE-259 | detect-secrets |
| 7 | 硬编码的密码 | CWE-259 | detect-secrets |
| 8 | 硬编码的 GitHub Token | CWE-259 | detect-secrets |
| 9 | 带有 CVE 的依赖项 | N/A | pip-audit |
## 本地运行
### 前置条件
- Python 3.11 或更高版本
- Git
### 安装
```
# Clonar o repositorio
git clone https://github.com/Matheus-Max-Lima/devsecops-secure-pipeline.git
cd devsecops-secure-pipeline
# Instalar ferramentas de seguranca
pip install -r requirements-dev.txt
```
### 手动运行检查
```
# Testes automatizados
pytest tests/ -v
# Analise de codigo (Bandit)
bandit -r src/ -f json -o reports/bandit-report.json
# Auditoria de dependencias
pip-audit -r requirements.txt -f json -o reports/pip-audit-report.json
# Deteccao de secrets
detect-secrets scan --all-files src/ \
| python -c "import sys; open('reports/secrets-report.json','w',encoding='utf-8').write(sys.stdin.read())"
# Gerar relatorio consolidado
python scripts/generate_report.py
```
### 预期结果
```
=======================================================
SUMARIO DE SEGURANCA DO PIPELINE
=======================================================
Bandit HIGH: 2 issue(s)
Bandit MEDIUM: 3 issue(s)
Bandit LOW: 4 issue(s)
pip-audit CVEs: 11 CVE(s)
Secrets detectados: 4 secret(s)
=======================================================
STATUS: PIPELINE BLOQUEADO
=======================================================
```
Pipeline 被阻止,因为发现了为了演示目的而故意插入到代码中的严重安全问题。
## GitHub Actions 结果
每次 `git push` 之后,仓库的 **Actions** 标签页会显示完整的执行过程:
- 每个步骤的通过/失败状态
- 每个工具的详细日志
- 可供下载的包含所有报告的构建产物
- 由于发现的问题,Pipeline 被标记为红色(已阻止)
构建产物将保留 90 天,包括:
- `bandit-report.json`
- `pip-audit-report.json`
- `secrets-report.json`
- `security-report.md`
## 技术栈
| 技术 | 版本 | 功能 |
|---|---|---|
| Python | 3.11 | 主语言 |
| pytest | 8.3.5 | 测试框架 |
| Bandit | 1.8.3 | SAST — 静态安全分析 |
| pip-audit | 2.9.0 | SCA — 依赖审计 |
| detect-secrets | 1.5.0 | 硬编码凭据检测 |
| GitHub Actions | - | CI/CD 平台 |
## 演示的概念
- **DevSecOps:** 将安全集成到开发生命周期中,而不是作为最后的步骤
- **Shift Left Security:** 安全检查在提交 (commit) 时尽早进行
- **SAST:** Static Application Security Testing — 不执行代码的静态分析
- **SCA:** Software Composition Analysis — 依赖链分析
- **Security Gate:** 自动阻止不安全代码的安全门
- **CWE:** Common Weakness Enumeration — 国际标准的漏洞分类
- **CVE:** Common Vulnerabilities and Exposures — 已知漏洞的标识符
- **Pipeline as Code:** Pipeline 配置与代码一起进行版本控制
## 后续步骤 (Roadmap)
- [ ] 使用 OWASP ZAP 添加 DAST
- [ ] 集成 Semgrep 作为第二个 SAST 分析器
- [ ] 添加 Pipeline 失败时的 Slack 通知
- [ ] 使用 syft 实施 SBOM (Software Bill of Materials)
- [ ] 使用 Trivy 添加容器分析
- [ ] 配置本地 pre-commit hooks
- [ ] 使用 HashiCorp Vault 实施 secrets 管理
## 作者
作为 DevSecOps 和 Cybersecurity 的作品集项目开发。
## 许可证
MIT License — 详见 [LICENSE](LICENSE) 文件。
标签:Bandit, Claude, CVE检测, detect-secrets, DevSecOps, GitHub Actions, pip-audit, pytest, Python, SAST, 上游代理, 代码安全, 依赖扫描, 安全Pipeline, 安全助手, 安全左移, 安全规则引擎, 安全门禁, 对抗攻击, 敏感信息检测, 无后门, 模型提供商, 漏洞枚举, 盲注攻击, 自动化代码分析, 自动笔记, 软件供应链安全, 软件开发工具包, 远程方法调用, 逆向工具, 阻断构建