NicoENDYs/SecurityModule
GitHub: NicoENDYs/SecurityModule
可复用的 DevSecOps 安全子模块,集成了 CI/CD 工作流、Docker 化扫描脚本和 OWASP 安全清单,为 Node.js 和 React 项目提供标准化的自动化安全测试能力。
Stars: 1 | Forks: 0
# security-testing-template

一个可复用的安全测试脚手架,设计为作为 **Git submodule** 添加到任何 Node.js 或 React 项目中。它标准化了:
- 安全清单(OWASP WSTG、API Security Top 10、Docker Hardening)
- 通过 Docker 运行的本地扫描脚本(Trivy、ZAP、Gitleaks、Docker Bench)——无需在主机上安装
- 可复用的 GitHub Actions 工作流(Semgrep SAST + Trivy + Secret/IaC + ZAP),可通过 `workflow_call` 调用
- 专为 JavaScript / TypeScript / React 定制的 Semgrep SAST 规则
## 目录结构
```
security-testing-template/
├── setup.sh # One-shot scaffold script
├── checklists/
│ ├── owasp-wstg-web.md # OWASP WSTG v4.2 full checklist
│ ├── api-security.md # OWASP API Security Top 10 (2023)
│ └── docker-hardening.md # CIS Docker Benchmark v1.6
├── scripts/
│ ├── scan-full-dockerized.sh # Orchestrator: build → trivy → compose up → ZAP → bench → summary
│ ├── scan-trivy.sh # Local Trivy scan (fs | image | repo)
│ ├── scan-zap-baseline.sh # Local ZAP baseline or full scan
│ └── docker-bench.sh # Docker Bench for Security
├── templates/
│ ├── zap/rules.tsv # ZAP alert filter rules
│ └── reports/ # Output directory for all scan reports
├── .github/workflows/
│ ├── semgrep.yml # Reusable Semgrep SAST workflow
│ ├── trivy.yml # Reusable Trivy workflow
│ ├── secret-iac.yml # Reusable Gitleaks + Checkov workflow
│ └── zap-baseline.yml # Reusable ZAP DAST workflow
├── node-web/
│ ├── semgrep.yml # Semgrep rules for JS/TS/React
│ └── audit.sh # npm audit + Semgrep via Docker
├── docker/
│ └── docker-compose.yml # Isolated security lab
├── dast/
│ └── zap-full-scan.conf # ZAP active scan configuration
└── docs/
├── guia-principiantes.md # Beginner-friendly quick start (start here)
├── como-usarlo-en-nuevo-proyecto.md # Submodule setup + GitHub Actions integration
├── proyecto-dockerizado.md # Full guide for Dockerized apps
└── sin-github.md # GitLab CI, Bitbucket, Jenkins, local-only
```
## 快速开始(本地扫描)
### 前置条件
- Docker(主机上唯一需要的工具)
- bash 4+
### 1. 作为 submodule 添加
```
# 从你的项目根目录
git submodule add https://github.com/nicoendys/securitymodule security
git submodule update --init --recursive
```
### 2. 对 Docker 化项目进行全面扫描(推荐)
```
bash security/scripts/scan-full-dockerized.sh \
--project-root . \
--url http://localhost:3000 \
--compose-file docker-compose.yml \
--service app
```
这一条命令将:运行 SAST → 构建 image → 使用 Trivy 扫描 → 启动 compose → 运行 ZAP → 运行 Docker Bench → 生成一个包含每个工具 PASS/WARN/FAIL 状态的 `summary.txt`。
### 3. 运行单个扫描
```
bash security/scripts/scan-trivy.sh fs . # filesystem
bash security/scripts/scan-trivy.sh image app:latest # Docker image
```
报告将保存到 `security/templates/reports/`。
### 4. 对预发布 URL 运行 ZAP 基线扫描
```
bash security/scripts/scan-zap-baseline.sh https://staging.example.com
```
### 5. 对你的项目运行 npm audit + Semgrep
```
# 从你的项目根目录
bash security/node-web/audit.sh .
```
### 6. 运行 Docker Bench for Security
```
bash security/scripts/docker-bench.sh
```
## 文档
| 指南 | 描述 |
|-------|-------------|
| [`docs/guia-principiantes.md`](docs/guia-principiantes.md) | **从这里开始。** 面向新手的从零到第一次扫描的完整流程、术语表和故障排除 |
| [`docs/como-usarlo-en-nuevo-proyecto.md`](docs/como-usarlo-en-nuevo-proyecto.md) | 作为 git submodule 添加,运行本地扫描,集成 GitHub Actions |
| [`docs/proyecto-dockerizado.md`](docs/proyecto-dockerizado.md) | Docker 化应用的完整工作流:构建 → 扫描 → ZAP → 汇总报告 |
| [`docs/sin-github.md`](docs/sin-github.md) | 不使用 GitHub:GitLab CI、Bitbucket Pipelines、Jenkins 或仅限本地 |
## 使用 GitHub Actions 工作流
`.github/workflows/` 中的工作流专为 `workflow_call` 设计——它们是从你项目自己的工作流中调用的,而不是直接在这里运行。
### 在你的项目 CI 中使用 Semgrep SAST
针对你的源代码运行该模块的 Semgrep 规则(加上 OWASP/JS/TS/React 社区包),并将结果上传到 Security 标签页。
```
jobs:
sast:
uses: nicoendys/securitymodule/.github/workflows/semgrep.yml@v1
with:
# Optional — override the community packs, or add your own:
config: "p/owasp-top-ten p/javascript p/typescript p/react"
fail-on-findings: false
permissions:
contents: read
security-events: write
actions: read
```
### 在你的项目 CI 中使用 Trivy
在**你的项目**中创建 `.github/workflows/security.yml`:
```
name: Security Scans
on:
pull_request:
push:
branches: [main]
jobs:
trivy:
uses: nicoendys/securitymodule/.github/workflows/trivy.yml@v1
with:
severity: "HIGH,CRITICAL"
fail-on-findings: false
permissions:
contents: read
security-events: write
actions: read
```
### 在你的项目 CI 中使用 Secret 和 IaC 扫描
Gitleaks 会扫描你的完整 git 历史记录以查找泄露的凭据;Checkov 则增加了更广泛的 IaC 策略覆盖范围。
```
jobs:
secret-iac:
uses: nicoendys/securitymodule/.github/workflows/secret-iac.yml@v1
permissions:
contents: read
security-events: write
actions: read
```
### 在你的项目 CI 中使用 ZAP DAST
```
jobs:
zap:
uses: nicoendys/securitymodule/.github/workflows/zap-baseline.yml@v1
with:
target-url: "https://staging.example.com"
scan-type: "baseline"
fail-on-warnings: false
permissions:
contents: read
issues: write
pull-requests: write
```
## 清单
所有清单均使用 Markdown 复选框。请在威胁建模期间或每次发布前仔细检查它们:
| 清单 | 何时使用 |
|-----------|-------------|
| `checklists/owasp-wstg-web.md` | 完整的 Web 应用程序渗透测试 |
| `checklists/api-security.md` | REST / GraphQL API 审查 |
| `checklists/docker-hardening.md` | 在将 container image 提升至生产环境之前 |
## 报告位置
所有扫描脚本都会将报告写入 `security/templates/reports/`。按照惯例,此目录已在 `.gitignore` 中列出——报告是临时的构建产物,而非源码。
要在本地浏览 HTML 报告:
```
docker compose -f security/docker/docker-compose.yml --profile reports up -d
open http://localhost:8888
```
## Semgrep 规则
`node-web/semgrep.yml` 规则涵盖:
- `eval()` / `new Function()` —— 任意代码执行
- 通过 `child_process` 造成的命令注入
- SQL 注入字符串拼接
- 硬编码的 JWT 密钥和凭据
- 危险的 React 模式(`dangerouslySetInnerHTML`,`javascript:` hrefs)
- 弱加密算法(`MD5`,`SHA-1`,`Math.random()`)
- 来源于用户输入的路径穿越
- 原型污染
- 通配符 CORS
独立运行 Semgrep(需要 Docker):
```
docker run --rm -v "$(pwd)":/src -v "$(pwd)/security/node-web/semgrep.yml":/semgrep.yml \
returntocorp/semgrep semgrep --config /semgrep.yml /src
```
## 版本控制
| 引用 | 推荐用法 |
|-----|-----------------|
| `@v1` | 浮动标签 → 最新的稳定 1.x 版本 —— **推荐** |
| `@1.4.0` | 固定到特定版本(合规环境) |
| `@main` | 最前沿版本 —— **不推荐**用于生产环境 |
作为 submodule 使用时,请固定到某个标签:
```
cd security && git checkout 1.4.0 && cd ..
git add security && git commit -m "chore(security): pin to 1.4.0"
```
## 贡献
1. Fork 本仓库。
2. 创建分支:`git checkout -b feat/your-change`。
3. 使用清晰的信息进行提交。
4. 发起 pull request。
## 许可证
MIT —— 详情请见 [LICENSE](LICENSE)。
标签:Cutter, DevSecOps, Docker, GitHub Actions, MITM代理, 上游代理, 代码安全审计, 安全扫描, 安全防御评估, 时序注入, 自动笔记, 请求拦截