本篇介绍使用Coverity 扫描基于Spring Boot 项目中的Path Manipulation 漏洞, 进而解决风险,并且可以通过扫描。
什么样的代码会被扫描有路径操纵风险?
在Spring Boot 项目中, 实验了如下的场景:
1. Control 中 file path 作为参数传递的会被扫描,单纯服务方法不会
场景: 控制器方法, 文件名作为请求的参数,
是否被扫描包含风险: 是
@RequestMapping("/canScan")
public void canScan(String fileName) {
Path path = Paths.get(fileName).normalize().toAbsolutePath();
path.toFile();
}
场景: 服务方法, 文件名作为参数,
是否被扫描包含风险: 否
public void noScan(String fileName) {
Path path = Paths.get(fileName).normalize().toAbsolutePath();
path.toFile();
}
2. 使用白名单进行了防御, 但是还是会被扫出
- 定义一个isValidPath() 判断路径是否合法 - 被扫出风险
public boolean isValidPath(String filePath) {
return filePath.startsWith(