maxwellsarpong/Code-Security-platform

GitHub: maxwellsarpong/Code-Security-platform

一个整合了Semgrep、Bandit、Checkov和pip-audit的代码安全扫描平台,提供API驱动的多语言静态分析、IaC配置检测和依赖漏洞扫描能力。

Stars: 0 | Forks: 0

# 安全与合规监控 — 后端 (FastAPI) 🛡 安全与合规监控 API (FastAPI) 的最小脚手架。 你将获得 - 可运行的 FastAPI 应用,包含简单的扫描 API (POST /api/v1/scans) - **真实的安全扫描器**: Bandit (Python 静态分析), Checkov (IaC 安全), pip-audit (依赖漏洞) - SQLite 默认开发数据库 (可通过 DATABASE_URL 配置) - 用于本地开发的 Dockerfile + docker-compose - 单元测试 + GitHub Actions CI 脚手架 ## 安全扫描器 该平台集成了四种行业标准的安全扫描器: 1. **Semgrep** - 多语言静态分析 - 支持 18+ 种语言:Python, JavaScript, TypeScript, Java, Go, Ruby, PHP, C/C++, C#, Rust, Kotlin, Scala, Swift - 使用 Semgrep Registry 社区规则 - 涵盖 OWASP Top 10 和 CWE 2. **Bandit** - Python 静态安全分析 - 检测硬编码机密、SQL 注入、Shell 注入、不安全的加密 - 68+ 项内置安全检查 - 基于严重性的风险评分 3. **Checkov** - 基础设施即代码安全 - 扫描 Terraform, Dockerfile, Kubernetes, CloudFormation - 1000+ 项内置策略 (CIS, PCI-DSS, HIPAA 合规) - 在部署前识别配置错误 4. **pip-audit** - Python 依赖漏洞扫描 - 检查依赖中的已知 CVE - 使用 PyPI Advisory Database - 提供升级建议 ### 扫描器工作流 1. 仓库被克隆到临时目录 2. 所有适用的扫描器并行运行 3. 结果被聚合并存储在数据库中 4. 基于严重性计算风险评分 (0-10 分制) 5. 清理临时文件 Architecture Diagram 快速开始: 1. 创建虚拟环境并在本地运行 ``` python -m venv .venv source .venv/bin/activate pip install -r requirements.txt uvicorn app.main:app --reload ``` 2. 使用 Docker Compose ``` docker compose up --build # API -> http://localhost:8000 ``` ### 身份验证 平台支持 **JWT 身份验证** (通过登录) 和 **API Key 身份验证** (用于自动化服务)。 #### 1. 注册与登录 (JWT) ``` # 注册 curl -X POST http://localhost:8000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "yourpassword" }' # 登录 curl -X POST http://localhost:8000/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "yourpassword" }' # Response: { "access_token": "...", "token_type": "bearer" } ``` 在随后的所有请求中,使用 `Authorization: Bearer ` 头部发送 `access_token`。 #### 2. API Key 身份验证 登录后生成 API Key (`POST /api/v1/user/api-key`),然后通过 `x-api-key` 头部使用它: ``` curl http://localhost:8000/api/v1/scans \ -H "x-api-key: " ``` ### 计划与配额 每位用户都会被分配一个计划,该计划控制其每月的扫描和解决限制。 | 计划 | 每月扫描次数 | 每月解决次数 | |---|---|---| | `free` | 2 | 2 | | `team` | 500 | 500 | | `enterprise` | 2000 | 2000 | - 新用户会自动加入 **Free** 计划。 - 在 `GET /api/v1/user/profile` 查看你当前的计划和配额。 - 在 `GET /api/v1/user/usage` 查看当月使用情况。 - 当超过配额时,API 会返回 `403` 并附带描述性消息,指出达到了哪个配额限制以及需要升级。 - **升级/变更计划**: - `POST /api/v1/user/subscription/team`: 移至 **Team** 等级。 - `POST /api/v1/user/subscription/enterprise`: 移至 **Enterprise** 等级。 - 使用 `POST /api/v1/user/subscription/renew` 续订/重置你当前的月度配额。 ### API 参考 所有受保护的端点接受 `Authorization: Bearer ` 或 `x-api-key: `。 #### 身份验证 | 方法 | 端点 | 描述 | |--------|----------|-------------| | `POST` | `/api/v1/auth/register` | 注册一个新用户账户 | | `POST` | `/api/v1/auth/init-superuser` | 通过创建首位超级用户来引导系统。如果超级用户已存在则返回 `403`。 | | `POST` | `/api/v1/auth/login` | JSON 登录 — 返回一个 JWT。如果邮箱未找到返回 `404`,密码错误返回 `401` | | `POST` | `/api/v1/auth/token` | OAuth2 表单登录 — 返回一个 JWT | | `POST` | `/api/v1/auth/request-password-recovery` | 发送密码恢复邮件。需要 `email`。 | | `POST` | `/api/v1/auth/reset-password` | 重置密码。需要有效的重置 `token` 和 `new_password`。 | ``` # 注册 curl -X POST http://localhost:8000/api/v1/auth/register \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "yourpassword" }' # Create Initial Superuser (Run only once to bootstrap) curl -X POST http://localhost:8000/api/v1/auth/init-superuser \ -H "Content-Type: application/json" \ -d '{ "email": "admin@example.com", "password": "secureadminpassword" }' # Login (JSON) curl -X POST http://localhost:8000/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "yourpassword" }' # Response: { "access_token": "...", "token_type": "bearer" } # Request Password Recovery curl -X POST http://localhost:8000/api/v1/auth/request-password-recovery \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com" }' # Reset Password (using token from email/logs) curl -X POST http://localhost:8000/api/v1/auth/reset-password \ -H "Content-Type: application/json" \ -d '{ "token": "", "new_password": "newsecurepassword" }' ``` #### 用户 | 方法 | 端点 | 需要认证 | 描述 | |--------|----------|---------------|-------------| | `GET` | `/api/v1/user/profile` | ✅ | 获取已认证用户的个人资料和配额信息 | | `PUT` | `/api/v1/user/profile` | ✅ | 更新可选属性 (如 `slack_webhook_url` 或 `github_token`) | | `GET` | `/api/v1/user/usage` | ✅ | 获取使用历史和当月额度摘要 | | `POST` | `/api/v1/user/api-key` | ✅ | 为已认证用户生成一个新的 API Key | | `POST` | `/api/v1/user/subscription/team` | ✅ | 将用户升级到 Team 等级 | | `POST` | `/api/v1/user/subscription/enterprise` | ✅ | 将用户升级到 Enterprise 等级 | | `POST` | `/api/v1/user/subscription/renew` | ✅ | 续订当前月度配额 (可选传递 `?amount=100.0`) | ``` # Update user profile curl -X PUT http://localhost:8000/api/v1/user/profile \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{ "slack_webhook_url": "https://hooks.slack.com/services/...", "github_token": "ghp_..." }' # Get usage curl http://localhost:8000/api/v1/user/usage \ -H "Authorization: Bearer " # Renew quota curl -X POST "http://localhost:8000/api/v1/user/subscription/renew?amount=100.0" \ -H "Authorization: Bearer " # Upgrade to Team Plan curl -X POST http://localhost:8000/api/v1/user/subscription/team \ -H "Authorization: Bearer " ``` #### 管理 | 方法 | 端点 | 需要认证 | 描述 | |--------|----------|---------------|-------------| | `GET` | `/api/v1/admin/users` | ✅ (超级用户) | 列出所有注册用户 | | `PUT` | `/api/v1/admin/users/{user_id}` | ✅ (超级用户) | 更新用户的计划、配额或 `is_superuser` 状态 | | `GET` | `/api/v1/admin/scans` | ✅ (超级用户) | 列出平台上的所有安全扫描 | | `GET` | `/api/v1/admin/findings/fixed` | ✅ (超级用户) | 列出全平台已修复的漏洞 | | `GET` | `/api/v1/admin/health/stats` | ✅ (超级用户) | 获取全系统健康百分比和安全统计 | | `GET` | `/api/v1/admin/events` | ✅ (超级用户) | 列出最新的全平台事件 (扫描、解决等)。支持 `offset` 和 `limit` (默认 3)。 | ``` # List users curl http://localhost:8000/api/v1/admin/users \ -H "Authorization: Bearer " # Promote user to enterprise curl -X PUT http://localhost:8000/api/v1/admin/users/ \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{ "plan": "enterprise" }' # List all scans on platform curl http://localhost:8000/api/v1/admin/scans \ -H "Authorization: Bearer " # List all fixed vulnerabilities on platform curl http://localhost:8000/api/v1/admin/findings/fixed \ -H "Authorization: Bearer " # Get overall system health percentage curl http://localhost:8000/api/v1/admin/health/stats \ -H "Authorization: Bearer " # Get latest platform events (paginated, shows 3 by default) curl http://localhost:8000/api/v1/admin/events \ -H "Authorization: Bearer " # Get legacy events with custom offset/limit curl "http://localhost:8000/api/v1/admin/events?offset=3&limit=5" \ -H "Authorization: Bearer " ``` #### 扫描 | 方法 | 端点 | 需要认证 | 描述 | |--------|----------|---------------|-------------| | `POST` | `/api/v1/scans` | ✅ (强制配额) | 启动新的安全扫描 | | `GET` | `/api/v1/scans` | ✅ | 列出已认证用户的所有扫描 | | `GET` | `/api/v1/scans/{scan_id}` | ✅ | 获取特定扫描的详情 | ``` # Start a scan curl -X POST http://localhost:8000/api/v1/scans \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{ "repo_url": "https://github.com/owner/repo" }' ``` #### 发现与解决 | 方法 | 端点 | 需要认证 | 描述 | |--------|----------|---------------|-------------| | `GET` | `/api/v1/findings/fixed` | ✅ | 列出所有成功解决的漏洞 | | `POST` | `/api/v1/findings/{target_id}/resolve` | ✅ (强制配额) | 解决一项发现或扫描中的所有发现 | 传递 **Finding ID** 以修复单个漏洞,或传递 **Scan ID** 以修复该扫描中的所有发现。 添加 `?force_sync=true` 以同步等待结果。 ``` curl -X POST "http://localhost:8000/api/v1/findings//resolve?force_sync=true" \ -H "Authorization: Bearer " \ -H "Content-Type: application/json" \ -d '{ "github_token": "your_personal_access_token" }' ``` #### 可观测性 | 方法 | 端点 | 描述 | |--------|----------|-------------| | `GET` | `/api/v1/metrics` | Prometheus 指标 (CPU, 请求计数, 延迟等) | ## 运行 Worker (队列模式) - 本地 (开发): ``` # start a local redis (homebrew) or use docker-compose (recommended) redis-server --port 6379 & REDIS_URL=redis://localhost:6379 rq worker scans # or use the convenience entrypoint REDIS_URL=redis://localhost:6379 python -m app.worker ``` - 使用 Docker Compose (推荐): ``` docker compose up --build # api -> http://localhost:8000 # worker logs are visible in the `worker` service ``` ## 可观测性 (Prometheus + Sentry) 📈 - API 指标: `GET /api/v1/metrics` (Prometheus 格式) - Worker 指标: 运行 worker 时默认暴露在 `9100` 端口 - Sentry: 设置 `SENTRY_DSN` 以启用来自 API 和 worker 的错误报告 示例 (本地): ``` # run everything with docker-compose SENTRY_DSN="" docker compose up --build # scrape metrics from http://localhost:8000/metrics and http://localhost:9100/ ``` ## CI / 无 Redis 运行 - 脚手架支持本地开发和 CI 的同步回退机制。设置 `WORKER_SYNC=true` 以同步运行扫描任务 (测试/CI 中的默认设置)。
标签:Anthropic, API安全, AV绕过, Bandit, Chrome Headless, CIS基准, DevSecOps, DNS 反向解析, Docker, FastAPI, GitHub Actions, GPT, IaC安全, JSON输出, Kubernetes安全, pip-audit, Python, SAST, Semgrep, SQLite, SQL注入检测, StruQ, Terraform扫描, Web截图, WordPress安全扫描, 上游代理, 依赖漏洞检测, 合规性监控, 安全漏洞扫描器, 安全防御评估, 容器安全, 开源框架, 持续集成, 提示注入防御, 搜索引擎查询, 文档安全, 无后门, 源代码安全, 漏洞管理, 盲注攻击, 硬编码密钥检测, 自动笔记, 自定义请求头, 请求拦截, 软件供应链安全, 远程方法调用, 逆向工具, 错误基检测, 静态代码分析