我在通过checkmarx分析代码时面临路径遍历漏洞。我正在获取路径与下面的代码:
String path = System.getenv(variableName);
和"path">变量值遍历了许多函数,最终在一个函数中使用,代码片段如下:
File file = new File(path);
Checkmarx将其标记为中等严重性漏洞。
请帮助。如何解决它,使其与checkmarx兼容?
我相信Checkmarx会接受的其他答案包括Path.normalize:
import java.nio.file.*;
String path = System.getenv(variableName);
Path p = Paths.get(path);
Path normalizedPath = p.normalize();
path = new File(normalizedPath.toString());
或filenameutil .normalize方法:
import org.apache.commons.io.FilenameUtils;
String path = System.getenv(variableName);
File file = new File(FilenameUtils.normalize(path));
可以通过调用File.getCanonicalPath()
生成规范化路径。
在你的例子中:
String path = System.getenv(variableName);
path = new File(path).getCanonicalPath();
有关更多信息,请阅读Java Doc