moh*_*han 7 security path-manipulation fortify
以下简单的java代码获取Fortify Path Manipulation错误.请帮我解决这个问题.我很长时间都在苦苦挣扎.
public class Test {
public static void main(String[] args) {
File file=new File(args[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
查看Path Manipulation的OWASP页面,它说
攻击者可以指定文件系统上的操作中使用的路径
您正在打开由用户指定的输入定义的文件.您的代码几乎就是漏洞的完美示例!或
或者重新考虑您的应用程序的设计.
尝试在使用之前规范化URL
https://docs.oracle.com/javase/7/docs/api/java/net/URI.html#normalize()
Path path = Paths.get("/foo/../bar/../baz").normalize();
Run Code Online (Sandbox Code Playgroud)
或使用org.apache.commons.io.FilenameUtils中的规范化
Stirng path = FilenameUtils.normalize("/foo/../bar/../baz");
Run Code Online (Sandbox Code Playgroud)
对于这两个结果将是 \baz
小智 6
即使路径/文件不像属性文件那样来自用户输入,Fortify 也会标记代码。处理这些问题的最佳方法是首先规范化路径,然后根据允许路径的白名单对其进行验证。
坏的:
public class Test {
public static void main(String[] args) {
File file=new File(args[0]);
}
}
Run Code Online (Sandbox Code Playgroud)
好的:
public class Test {
public static void main(String[] args) {
File file=new File(args[0]);
if (!isInSecureDir(file)) {
throw new IllegalArgumentException();
}
String canonicalPath = file.getCanonicalPath();
if (!canonicalPath.equals("/img/java/file1.txt") &&
!canonicalPath.equals("/img/java/file2.txt")) {
// Invalid file; handle error
}
FileInputStream fis = new FileInputStream(f);
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //www.securecoding.cert.org/confluence/display/java/FIO16-J.+Canonicalize+path+names+before+validating+them
| 归档时间: |
|
| 查看次数: |
51870 次 |
| 最近记录: |