sudeshgutta/secure-scan-action
GitHub: sudeshgutta/secure-scan-action
一款结合 Trivy 和 AST-Grep 的 GitHub Action,能扫描依赖漏洞并追踪其在源码中的实际使用情况,通过可达性分析帮助筛选真正需要关注的安全风险。
Stars: 0 | Forks: 0
Secure Scan Action 是您 GitHub 仓库的智能安全守门员。它由 [Trivy](https://github.com/aquasecurity/trivy) 和 [AST-Grep](https://github.com/ast-grep/ast-grep) 驱动,不仅能检测依赖项中的已知漏洞,还能准确显示它们在代码中的使用位置。通过将深度漏洞扫描与智能代码分析相结合,Secure Scan 能帮助您专注于真正重要的事情——修复真实风险,而非误报。实现安全左移,让每一次提交都发布更安全的代码。

## 快速开始
将以下工作流文件添加到您仓库的 `.github/workflows/vuln-pkg-scan.yml` 中:
```
name: Vulnerable Package Scan
on:
push:
workflow_dispatch:
jobs:
vuln-pkg-scan:
name: Vulnerable Package Scan Job
runs-on: ubuntu-latest
permissions:
security-events: write # required to upload SARIF
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Run Vulnerable Package Scan
uses: sudeshgutta/secure-scan-action@v1.0.0-beta
with:
severity: "HIGH,CRITICAL" # optional, this is the default
sarif-output: "results.sarif" # optional, set empty to disable
- name: Upload SARIF to GitHub Security tab
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
```
## 功能特性
- **多语言支持**:扫描 Go、JavaScript、TypeScript、Python 和 Java 代码库
- **漏洞扫描**:使用 Trivy 检测依赖项中 `HIGH` 和 `CRITICAL` 级别的 CVE(可配置)
- **使用情况分析**:使用 AST-Grep 精确查找源代码中导入易受攻击包的位置 —— 不会对仅传递性依赖产生误报
- **可达性评分**:根据导入易受攻击包的非测试源文件数量,将每个发现评级为 LOW / MEDIUM / HIGH
- **SARIF 输出**:生成 GitHub Security 标签页原生支持的 SARIF 2.1.0 报告(默认为 `results.sarif`)
- **步骤摘要**:将格式化的发现表格打印到 GitHub Actions 作业摘要(`$GITHUB_STEP_SUMMARY`)
- **生态系统感知**:自动将 Trivy 生态系统(`gomod`、`npm`、`yarn`、`pnpm`、`pip`、`poetry`、`uv`、`pom`、`gradle` 等)映射到正确的语言扫描器
- **容器化**:在预装所有工具的安全 Docker 容器中运行;无需任何设置
- **非 root 权限**:容器以非特权 `scanner` 用户身份运行
## 工作原理
1. **Trivy 扫描** -- 在所有支持的生态系统中,根据您配置的严重性阈值扫描检出仓库中的 CVE
2. **包提取** -- 对易受攻击的包名进行去重,收集 CVE ID + 最高严重级别,并解析每个生态系统要扫描的语言
3. **AST-Grep 分析** -- 对于每个易受攻击的包/语言对,渲染内联 YAML 规则并对源代码树运行 `sg scan`
4. **可达性评分** -- 计算导入每个易受攻击包的唯一非测试源文件数量(LOW / MEDIUM / HIGH)
5. **结果报告** -- 将发现表格打印到 stdout 和 GitHub Actions 作业摘要;写入 SARIF 文件;如果发现任何结果则退出并返回状态码 `2`
## 支持的语言与生态系统
| 语言 | Trivy 生态系统 | 检测内容 |
|------------|-------------------------------------------|-----------------------------|
| Go | `gomod` | `import` 语句 |
| JavaScript | `npm`, `yarn`, `pnpm`, `node-pkg` | `import` / `require` 调用 |
| TypeScript | `npm`, `yarn`, `pnpm`, `node-pkg` | `import` 语句 |
| Python | `pip`, `poetry`, `pipenv`, `uv` | `import` / `from ... import`|
| Java | `pom`, `gradle`, `gradle-lockfile` | `import` 声明 |
## 输入参数
| 输入参数 | 默认值 | 描述 |
|-------|---------|-------------|
| `severity` | `HIGH,CRITICAL` | 要扫描的 Trivy 严重级别,以逗号分隔(例如 `MEDIUM,HIGH,CRITICAL`) |
| `sarif-output` | `results.sarif` | SARIF 2.0 输出文件的路径。设置为空字符串以禁用 SARIF 输出。 |
## 退出代码
| 代码 | 含义 |
|------|---------|
| `0` | 干净 —— 源代码中未发现易受攻击的包 |
| `1` | 内部错误(Trivy 失败、解析错误等) |
| `2` | 检测到存在易受攻击包的使用 |
使用这些代码可在发现真实使用情况时阻止 PR 合并或部署。
## 本地开发
```
# 构建 Docker 镜像
make build
# 构建镜像并针对当前目录运行 scanner
make scan
# 运行 tests
go test ./...
```
## 项目结构
```
secure-scan-action/
├── internal/
│ ├── astgrep/
│ │ ├── rules/
│ │ │ ├── templates/ # YAML rule templates per language (go.yml, javascript.yml, ...)
│ │ │ ├── dispatcher.go # BuildRule() + ecosystem-to-language mapping
│ │ │ └── render.go # Template renderer with YAML-injection protection
│ │ ├── scanner.go # Orchestrates Trivy report -> AST-Grep scans -> []Finding
│ │ ├── analyzer.go # Parses and aggregates AST-Grep matches
│ │ └── types.go # ASTGrepMatch, PackageDetectionResult, Finding, ReachabilityScore
│ ├── sarif/ # SARIF 2.1.0 writer (internal/sarif/sarif.go)
│ ├── report/ # Console + step summary reporter (internal/report/report.go)
│ ├── trivy/ # Trivy CLI wrapper and report types
│ └── logger/ # JSON structured logging (log/slog)
├── main.go # Entry point
├── Dockerfile # 3-stage build: Go -> Trivy -> node:bookworm-slim runtime
├── action.yml # GitHub Action metadata
└── Makefile # build / scan targets
```
## 添加新语言
1. 创建 `internal/astgrep/rules/templates/.yml` —— 一个针对该语言导入 AST 节点的 Go `text/template` YAML 规则
2. 在 `dispatcher.go` 中向 `BuildRule()` 添加一个 case(或者扩展现有的 `switch` 以支持共享简单模式的语言)
3. 在 `ecosystemLanguages` 中将相关的 Trivy 生态系统映射到该语言
4. 在 `dispatcher_test.go` 中添加测试
## 贡献
1. Fork 本仓库
2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
3. 提交您的更改 (`git commit -m 'Add amazing feature'`)
4. 推送到分支 (`git push origin feature/amazing-feature`)
5. 发起 Pull Request
## 许可证
本项目基于 Apache License 2.0 授权 —— 详情请参阅 [LICENSE](LICENSE) 文件。
标签:AST-Grep, CI/CD 安全, CVE 扫描, DevSecOps, EVTX分析, GitHub Actions, IPv6支持, SARIF, SAST, 上游代理, 代码分析, 凭证管理, 可到达性分析, 安全扫描, 数据投毒防御, 日志审计, 时序注入, 源码审计, 漏洞追踪, 盲注攻击, 自动笔记, 请求拦截