VirtualSpaceGit/sarif-pretty
GitHub: VirtualSpaceGit/sarif-pretty
一个无依赖的轻量 Python CLI,将 SARIF 2.1.0 静态分析扫描结果转换为可读的终端、Markdown 或 JSON 报告。
Stars: 0 | Forks: 0
# sarif-pretty
[](https://virtualspacesec.com)
[](LICENSE)
[](https://www.python.org)
一个轻量级、无依赖的 Python CLI,用于将 **SARIF 2.1.0** 扫描输出转换为可读的终端、markdown 或 JSON 报告。
`sarif-pretty` 由 [Verse](https://virtualspacesec.com/pages/about-us) 发布,作为一个小型开源伴侣工具,适用于任何输出 SARIF 的本地 SAST 工作流——包括 [**VirtualSpace AppSec**](https://virtualspacesec.com),这是 Verse 专为 Windows 打造的安全代码审查工具,完全在用户自己的机器上运行。
## 为什么需要
SARIF (Static Analysis Results Interchange Format) 是一种广泛用于静态分析结果的 JSON schema。它非常适合用于工具集成和 CI 管道,但原始文档并不适合开发者在终端中阅读。`sarif-pretty` 读取 `.sarif` 文件并将其渲染为:
- **带颜色、按严重程度分组的终端摘要**(默认),适合快速浏览,
- **GitHub 风格的 markdown 报告**(`--format md`),适合粘贴到 PR 描述或 CI 步骤摘要中,
- **每个发现对应一条规范化的 JSON 记录**(`--format json`),适合通过管道传递给 `jq`、`grep` 或你自己的脚本。
它是一个仅使用标准库的单 Python 文件——没有第三方依赖。
## 安装
要求 Python 3.9 或更高版本。`sarif-pretty` 是单一文件;将其放在你的 `$PATH` 中的任意位置即可(或使用 `python sarif_pretty.py` 运行)。
```
curl -fsSL https://raw.githubusercontent.com/VirtualSpaceGit/sarif-pretty/main/sarif_pretty.py -o sarif-pretty
chmod +x sarif-pretty
```
## 用法
```
sarif-pretty findings.sarif # colored terminal output
sarif-pretty findings.sarif --no-color # plain text
sarif-pretty findings.sarif --format md # GitHub-flavored markdown
sarif-pretty findings.sarif --format json # newline-delimited JSON
# CI 集成:如果存在任何 warning 或更高严重级别的发现,则以状态码 1 退出
sarif-pretty findings.sarif --fail-on warning
# 从 stdin 读取
cat findings.sarif | sarif-pretty -
```
### CLI 选项
| Flag | Default | Description |
|---|---|---|
| `path` | (必填) | SARIF 2.1.0 JSON 文件的路径。使用 `-` 从 stdin 读取。 |
| `--format {ansi,md,json}` | `ansi` | 输出格式。 |
| `--no-color` | off | 在 `ansi` 格式中禁止使用 ANSI 颜色。 |
| `--fail-on {none,note,warning,error}` | `none` | 如果任何发现达到或超过给定级别,则以退出代码 1 退出。在 CI 中非常有用。 |
## 示例
本仓库中的 `sample.sarif` 包含三个发现——两个 `error` 级别和一个 `warning` 级别——由 VirtualSpace AppSec 针对一个小型 Python 项目生成:
```
$ sarif-pretty sample.sarif --format md
# 扫描发现
**Total:** 3
## HIGH (2)
| Rule | CWE | Location | Message |
|---|---|---|---|
| `VS-PY-502-PICKLE` (InsecureDeserialization) | CWE-502 | `src/loader.py:42` | pickle.load on attacker-controllable input |
| `VS-PY-89-SQLI` (SqlInjection) | CWE-89 | `src/users.py:18` | User input concatenated into SQL query |
## MEDIUM (1)
| Rule | CWE | Location | Message |
|---|---|---|---|
| `VS-PY-327-MD5` (WeakHash) | CWE-327, CWE-916 | `src/auth.py:7` | MD5 used for password storage |
```
## 识别的 SARIF 字段
`sarif-pretty` 读取 SAST 工具最常输出的字段:
- `runs[].tool.driver.name`
- `runs[].tool.driver.rules[]` - 当发现仅带有 id 时,用于查找规则名称
- `runs[].results[].ruleId`, `level`, `message.text`
- `runs[].results[].locations[0].physicalLocation.artifactLocation.uri`
- `runs[].results[].locations[0].physicalLocation.region.startLine`(以及 `startColumn`、`snippet.text`)
- `runs[].results[].properties.cwe` - 许多工具用来附加 CWE 标识符的非标准约定
缺少任何这些字段的发现会进行优雅降级,而不是直接报错失败。
## 许可证
MIT - 见 [LICENSE](LICENSE)。
标签:Python, SARIF, 文档结构分析, 无后门, 逆向工具