glsec/glsec
GitHub: glsec/glsec
专注于 GitLab CI 流水线安全审计的静态检查工具,帮助团队发现 .gitlab-ci.yml 中的错误配置和供应链风险。
Stars: 0 | Forks: 0
# glsec
**glsec** 是一个针对 `.gitlab-ci.yml` 文件的安全检查工具。它能够检测可能导致供应链攻击、机密泄露和令牌窃取的错误配置——这与 [zizmor](https://github.com/zizmorcore/zizmor) 和 [actionlint](https://github.com/rhysd/actionlint) 在 GitHub Actions 中捕捉的问题属于同一类别,但专为 GitLab CI 打造。
```
$ glsec .gitlab-ci.yml
ERROR .gitlab-ci.yml:7 GL001 image "node:latest" uses mutable tag "latest" — pin to a specific version or digest
WARN .gitlab-ci.yml:14 GL002 variable $CI_COMMIT_REF_NAME in script is user-controlled and unquoted — wrap in double quotes
ERROR .gitlab-ci.yml:3 GL003 project include "company/templates" missing "ref" — defaults to HEAD of default branch (mutable)
```
## 为什么选择 glsec?
| 工具 | 范围 |
|------|-------|
| [zizmor](https://github.com/zizmorcore/zizmor) | GitHub Actions 安全 |
| [actionlint](https://github.com/rhysd/actionlint) | GitHub Actions 语法 + 部分安全 |
| [gitlab-ci-lint](https://docs.gitlab.com/api/lint/) | 仅 GitLab CI 语法(需要 API 访问权限) |
| **glsec** | GitLab CI 安全 —— 可离线工作,无需 API 密钥 |
## 安装
**Go 安装(需要 Go 1.21+):**
```
go install github.com/glsec/glsec@latest
```
**二进制文件下载:**
适用于 Linux、macOS 和 Windows(amd64 + arm64)的预编译二进制文件可在 [Releases 页面](https://github.com/glsec/glsec/releases)获取。
```
# 示例: Linux amd64
curl -sSL https://github.com/glsec/glsec/releases/latest/download/glsec_linux_amd64.tar.gz | tar xz
./glsec --version
```
Homebrew 支持即将推出。
## 用法
```
# 扫描文件 (发现错误时以退出码 1 退出,无问题时为 0,解析错误时为 2)
glsec .gitlab-ci.yml
# JSON output,供机器读取
glsec --format json .gitlab-ci.yml
# SARIF output,用于 GitHub Code Scanning / GitLab SAST
glsec --format sarif .gitlab-ci.yml > gl.sarif
# 将 warn findings 视为严重失败 (退出码 1)
glsec --strict .gitlab-ci.yml
# advisory mode:即使有 findings 也总是以退出码 0 退出
glsec --no-exit-codes .gitlab-ci.yml
# 从扫描中排除指定的文件或目录
glsec --exclude vendor/ .gitlab-ci.yml
# 基于现有 findings 设定 baseline,以便仅报告新的违规
glsec --generate-ignore .gitlab-ci.yml
glsec .gitlab-ci.yml # now exits 0; new violations will be caught
```
### 退出代码
| 代码 | 含义 |
|------|---------|
| 0 | 无 `error` 级别的发现(默认情况下,仅有警告级别的发现会以代码 0 退出) |
| 1 | 出现一个或多个 `error` 发现;或在设置了 `--strict` 时出现的任何发现 |
| 2 | 用法错误或无法解析文件 |
使用 `--strict` 可将 `warn` 发现视为严重失败(退出代码为 1)。使用 `--no-exit-codes` 可无论发现任何结果都以代码 0 退出(建议/信息模式)。所有标志也可以在 `.glsec.yml` 中进行设置:
```
strict: true # warn findings cause exit 1
no-exit-codes: true # never exit 1 (overrides strict)
exclude_paths:
- legacy/.gitlab-ci.yml
- infra/old-pipelines/**
- vendor/
```
## 规则
涵盖 5 个 [OWASP CI/CD 安全类别](https://owasp.org/www-project-top-10-ci-cd-security-risks/) 的 26 条规则:
| 类别 | OWASP | 规则 |
|----------|-------|-------|
| 凭证管理 | CICD-SEC-6 | GL002, GL004, GL006, GL010, GL014, GL018, GL021 |
| 依赖项与镜像固定 | CICD-SEC-3 | GL001, GL003, GL015, GL022, GL023, GL026 |
| 供应链完整性 | CICD-SEC-9 | GL011, GL020, GL025 |
| 流水线流程与访问控制 | CICD-SEC-1, CICD-SEC-5 | GL008, GL009, GL012, GL013, GL017, GL019 |
| 不安全配置 | CICD-SEC-7 | GL005, GL007, GL016, GL024 |
→ **[包含描述和示例的完整规则参考](docs/rules.md)**
## CI 集成
### GitLab CI — Docker 镜像(推荐)
最快的方式:使用来自 GHCR 的预构建镜像。无需 Go 工具链。
```
glsec:
stage: test
image: ghcr.io/glsec/glsec:latest
script:
- glsec .gitlab-ci.yml
```
固定到特定版本以实现可复现的流水线:
```
glsec:
stage: test
image: ghcr.io/glsec/glsec:0.1.0
script:
- glsec .gitlab-ci.yml
```
### GitLab CI — 二进制下载
适用于无法从 GHCR 拉取镜像的流水线:
```
glsec:
stage: test
image: alpine:3.19
script:
- |
curl -sSLO https://github.com/glsec/glsec/releases/latest/download/glsec_linux_amd64.tar.gz
echo "$(curl -sSL https://github.com/glsec/glsec/releases/latest/download/checksums.txt | grep glsec_linux_amd64.tar.gz)" | sha256sum -c
tar xzf glsec_linux_amd64.tar.gz
- ./glsec .gitlab-ci.yml
```
### GitLab SAST 集成
通过生成 SARIF 并将其作为 SAST 报告构件公开,将发现结果发布到 GitLab 的安全仪表板:
```
glsec:
stage: test
image: ghcr.io/glsec/glsec:latest
script:
- glsec --format sarif .gitlab-ci.yml > glsec.sarif || true
artifacts:
reports:
sast: glsec.sarif
```
发现结果将显示在流水线的“安全”选项卡和项目的安全仪表板中。使用 `|| true` 可防止 glsec 作业本身阻塞流水线——无论结果如何,SAST 报告都会被上传。
### GitHub Actions
```
- name: Run glsec
run: |
curl -sSLO https://github.com/glsec/glsec/releases/latest/download/glsec_linux_amd64.tar.gz
tar xzf glsec_linux_amd64.tar.gz
./glsec .gitlab-ci.yml
```
## 贡献
请参阅 [CONTRIBUTING.md](CONTRIBUTING.md)。
## 许可证
Apache 2.0
标签:actionlint, DevSecOps, EVTX分析, GitLab, GitLab CI, Golang, Linter, pptx, SARIF, SAST, StruQ, YAML, zizmor, 上游代理, 云安全监控, 代码安全, 令牌窃取, 安全库, 安全检查, 安全编程, 文档安全, 日志审计, 漏洞枚举, 盲注攻击, 离线工具, 秘密泄露, 误配置检测, 请求拦截, 静态分析