你遇到的情况是 Trivy 默认会扫描项目的整个依赖树,包括所有直接依赖和间接依赖(传递依赖)。要只关注直接依赖的漏洞,可以使用 Trivy 的 --pkg-relationships 或 --dependency-tree 相关功能来过滤结果。
方法:使用 --pkg-relationships 过滤直接依赖
Trivy 支持通过 --pkg-relationships 参数指定要显示的依赖关系类型。要只看直接依赖,可以将其设置为 direct。
命令示例
trivy fs --db-repository ghcr.nju.edu.cn/aquasecurity/trivy-db:latest \
--pkg-relationships direct \
--format table \
--output trivy-direct-deps-report.txt \
pom.xml
参数说明
--pkg-relationships direct:仅显示直接依赖的漏洞(不包括间接依赖)。--format table:以表格形式输出结果(也可以用json格式便于后续处理)。--output trivy-direct-deps-report.txt:将结果保存到文件,避免终端输出过长。
步骤详解
-
执行命令:
trivy fs --pkg-relationships direct pom.xml这会扫描
pom.xml并只显示直接依赖的漏洞。 -
查看结果:
输出会列出所有直接依赖组件及其漏洞,例如:┌─────────────────┬────────────────┬──────────┬───────────────────┬───────────────┬───────────────────────────────────────────────────┐ │ Library │ Vulnerability │ Severity │ Installed Version │ Fixed Version │ Title │ ├─────────────────┼────────────────┼──────────┼───────────────────┼───────────────┼───────────────────────────────────────────────────┤ │ spring-boot │ CVE-2023-xxxx │ HIGH │ 2.7.0 │ 2.7.18 │ Spring Boot Remote Code Execution Vulnerability │ ├─────────────────┼────────────────┼──────────┼───────────────────┼───────────────┼───────────────────────────────────────────────────┤ │ mysql-connector │ CVE-2024-xxxx │ MEDIUM │ 8.0.28 │ 8.0.33 │ MySQL Connector Java Information Disclosure │ └─────────────────┴────────────────┴──────────┴───────────────────┴───────────────┴───────────────────────────────────────────────────┘ -
导出为 JSON(可选):
如果需要进一步处理数据(例如筛选或生成报告),可以用 JSON 格式输出:trivy fs --pkg-relationships direct --format json --output direct-deps-vulns.json pom.xml
总结
通过 --pkg-relationships direct 参数,Trivy 会只扫描并显示 pom.xml 中直接声明的依赖组件及其漏洞,忽略间接依赖。这样你就可以专注于需要直接升级的依赖版本,而不会被庞大的依赖树干扰。
1293

被折叠的 条评论
为什么被折叠?



