Integrates Trivy scanner into GitHub Actions and GitLab CI/CD pipelines to detect vulnerabilities in Docker images, OS packages, app dependencies, Dockerfile misconfigurations, filesystems, Git repos, and enforce severity-based quality gates.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
- 在 CI/CD 中构建 Docker 容器镜像时,需要在推送到仓库前进行自动化漏洞扫描
Integrates Trivy scanner into CI/CD pipelines to detect vulnerabilities and misconfigurations in Docker images, Dockerfiles, filesystems, and git repos, with severity-based quality gates blocking risky deployments.
Integrates Trivy scanner into CI/CD pipelines to detect vulnerabilities in Docker images, OS packages, dependencies, Dockerfiles, and enforce quality gates blocking critical/high-severity issues before deployment.
Scans Docker images with Trivy for vulnerabilities in OS packages/dependencies, misconfigurations, secrets, and licenses. Supports SARIF/CycloneDX/SPDX outputs for CI/CD integration.
Share bugs, ideas, or general feedback.
不适用于:运行时容器安全监控(应使用 Falco)、扫描生产中正在运行的容器(应使用运行时代理)、或仅扫描未容器化的应用源代码(应使用 SAST 工具)。
设置 GitHub Actions 工作流,在推送到容器仓库前构建 Docker 镜像并用 Trivy 进行扫描。
# .github/workflows/container-security.yml
name: Container Security Scan
on:
push:
branches: [main]
pull_request:
branches: [main]
paths:
- 'Dockerfile'
- 'docker-compose*.yml'
- 'src/**'
- 'requirements*.txt'
- 'package*.json'
jobs:
build-and-scan:
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
steps:
- uses: actions/checkout@v4
- name: 构建 Docker 镜像
run: docker build -t app:${{ github.sha }} .
- name: 运行 Trivy 漏洞扫描器
uses: aquasecurity/trivy-action@0.28.0
with:
image-ref: 'app:${{ github.sha }}'
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '1'
ignore-unfixed: true
- name: 上传 Trivy 扫描结果
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'
category: 'trivy-container'
- name: 运行 Trivy 错误配置扫描器
uses: aquasecurity/trivy-action@0.28.0
with:
scan-type: 'config'
scan-ref: '.'
format: 'table'
exit-code: '1'
severity: 'CRITICAL,HIGH'
Trivy 可检测常见的 Dockerfile 安全问题,如以 root 身份运行、使用 latest 标签、暴露不必要的端口等。
# 扫描 Dockerfile 中的错误配置
trivy config --severity HIGH,CRITICAL ./Dockerfile
# 使用自定义策略目录扫描
trivy config --policy ./security-policies --severity MEDIUM,HIGH,CRITICAL .
# Trivy 检查的安全 Dockerfile 实践示例:
# - 存在 USER 指令(不以 root 运行)
# - 已定义 HEALTHCHECK 指令
# - 基础镜像使用特定标签,而非 :latest
# - ENV 或 ARG 指令中无密钥信息
# - 优先使用 COPY 而非 ADD
# .gitlab-ci.yml
stages:
- build
- scan
- push
variables:
TRIVY_CACHE_DIR: .trivycache/
build:
stage: build
script:
- docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
- docker save $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -o image.tar
artifacts:
paths:
- image.tar
trivy-scan:
stage: scan
image:
name: aquasec/trivy:latest
entrypoint: [""]
cache:
paths:
- .trivycache/
script:
- trivy image
--input image.tar
--exit-code 1
--severity CRITICAL,HIGH
--ignore-unfixed
--format json
--output trivy-report.json
- trivy image
--input image.tar
--severity CRITICAL,HIGH,MEDIUM
--format table
artifacts:
reports:
container_scanning: trivy-report.json
paths:
- trivy-report.json
allow_failure: false
push:
stage: push
needs: [trivy-scan]
script:
- docker load -i image.tar
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
通过 Trivy 的忽略文件和 VEX 声明管理误报和已接受的风险。
# .trivyignore.yaml
vulnerabilities:
- id: CVE-2023-44487 # HTTP/2 快速重置 - 已在负载均衡器层面缓解
statement: "已通过入口层 WAF 速率限制缓解"
expires: 2026-06-01
- id: CVE-2024-21626 # runc 容器逃逸 - 基础镜像更新中已修补
statement: "在 JIRA-SEC-1234 中跟踪,基础镜像更新已计划"
expires: 2026-03-15
misconfigurations:
- id: DS002 # 未设置 User - init 容器需要 root 权限
paths:
- "docker/init-container/Dockerfile"
statement: "Init 容器需要 root 权限设置卷权限"
在 CI/CD 中缓存 Trivy 漏洞数据库,以减少扫描时间并支持隔离网络环境。
# 带数据库缓存的 GitHub Actions
- name: 缓存 Trivy DB
uses: actions/cache@v4
with:
path: /tmp/trivy-db
key: trivy-db-${{ hashFiles('.github/workflows/container-security.yml') }}
restore-keys: trivy-db-
- name: 使用缓存 DB 运行 Trivy
uses: aquasecurity/trivy-action@0.28.0
with:
image-ref: 'app:${{ github.sha }}'
cache-dir: /tmp/trivy-db
format: 'json'
output: 'trivy-results.json'
severity: 'CRITICAL,HIGH'
exit-code: '1'
# 隔离网络:手动下载 DB 并挂载
trivy image --download-db-only --cache-dir /path/to/cache
# 将缓存传输到隔离网络系统
trivy image --skip-db-update --cache-dir /path/to/cache myimage:tag
使用 Trivy 在漏洞扫描的同时生成软件物料清单(SBOM)。
# 以 CycloneDX 格式生成 SBOM
trivy image --format cyclonedx --output sbom.cdx.json app:latest
# 以 SPDX 格式生成 SBOM
trivy image --format spdx-json --output sbom.spdx.json app:latest
# 扫描 SBOM 中的漏洞(将生成与扫描解耦)
trivy sbom sbom.cdx.json --severity CRITICAL,HIGH
# 带许可证检测的扫描
trivy image --scanners vuln,license --severity HIGH,CRITICAL app:latest
| 术语 | 定义 |
|---|---|
| CVE | 通用漏洞与披露 — 公开已知安全漏洞的标准化标识符 |
| 漏洞数据库 | Trivy 定期更新的数据库,汇聚来自 NVD、厂商公告和语言特定来源的 CVE 数据 |
| 错误配置 | Dockerfile、Kubernetes 清单或 IaC 模板中与安全相关的配置问题 |
| SBOM | 软件物料清单 — 容器镜像中所有组件和依赖项的完整清单 |
| 忽略未修复 | 跳过没有可用补丁的 CVE 的标志,减少无法采取行动漏洞带来的噪音 |
| VEX | 漏洞可利用性交换 — 关于漏洞在特定上下文中是否可被利用的机器可读声明 |
| 退出码 | Trivy 在发现超过严重性阈值时返回的非零状态码,用于使 CI/CD 流水线失败 |
背景:团队构建多阶段 Docker 镜像,需要在推送到 ECR 前扫描最终生产镜像,同时扫描构建阶段以防范供应链风险。
方法:
--target production 构建 Docker 镜像的最终阶段--severity CRITICAL,HIGH --exit-code 1 --ignore-unfixed 运行 Trivy,阻断可利用的问题注意事项:仅扫描最终阶段会遗漏构建阶段中存在的易受攻击包,这些包可能影响了构建过程。需单独对构建上下文运行 trivy fs。过度缓存 Trivy DB(每周一次)意味着新发布的 CVE 需要数天才能出现在扫描中。
Trivy 容器扫描报告
=============================
镜像: app:a1b2c3d4
基础镜像: python:3.12-slim-bookworm
扫描日期: 2026-02-23
DB 版本: 2026-02-23T00:15:00Z
漏洞摘要:
总计: 47
严重: 2
高危: 5
中危: 18
低危: 22
未修复: 8 (已从门禁中排除)
严重发现:
CVE-2025-12345 libssl3 3.0.11-1 3.0.13-1 OpenSSL 缓冲区溢出
CVE-2025-67890 curl 7.88.1-10 7.88.1-12 curl HSTS 绕过
高危发现:
CVE-2025-11111 zlib1g 1.2.13 1.2.13.1 zlib 堆缓冲区溢出
CVE-2025-22222 python3.12 3.12.1 3.12.3 CPython 路径穿越
CVE-2025-33333 requests 2.31.0 2.32.0 requests 重定向中的 SSRF
错误配置:
DS002 [HIGH] Dockerfile: 未设置 USER 指令(以 root 运行)
DS026 [MEDIUM] Dockerfile: 未定义 HEALTHCHECK
质量门禁: 失败(2 个严重,5 个高危发现)