2014-01-21 57 views
5

Fority扫描报告“路径操作”的安全性问题在下面的代码片段Fortify的路径操作错误

String filePath = getFilePath(fileLocation, fileName); 
final File file = new File(filePath); 
LOGGER.info("Saving report at : " + filePath); 
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file)); 
fileWriter.write(fileContent); 

,所以我在fileLocation检查列入黑名单的人物和投掷例外,仍然是Fortify的是抛出异常。

try { 
    String filePath = getFilePath(fileLocation, fileName); 
    if (isSecurePath(filePath)) { 
     final File file = new File(filePath); 
     LOGGER.info("Saving report at : " + filePath); 
     BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file)); 
     fileWriter.write(fileContent); 
    } else { 
     throw new Exception("Security Issue. File Path has blacklisted characters"); 
    } 

} catch (final Exception e) { 
    LOGGER.error("Unable to prepare mail attachment : ", e); 
    message = "Mail cannot be send, Unable to prepare mail attachment"; 
} 


private boolean isSecurePath(String filePath) { 
    String[] blackListChars = {".."}; 
    return (StringUtils.indexOfAny(filePath, blackListChars)< 0); 
} 

我应该忽略扫描报告还是会对此进行正确的修复?

+0

瞧这也https://security.stackexchange.com/questions/103884/how-to-resolve-path-manipulation-error-given-by-fortify –

回答

1

首先SCA是一个静态分析工具,所以无法检查您的自定义验证,以确定它是否工作正常与否,这是后话的动态工具,如WebInspect可以是专门做。其次,黑名单是保护任何东西的一种不好的方式,白名单是一种更安全的方法,而且你提到黑名单验证的事实将会吸引攻击者。这是因为您必须考虑到每种可能的被攻击方式,包括可能尚未发现的方式,因此在软件发布前很容易过时。

第三,这肯定是不够的反对停止路径操作,因为你只占人找相对路径更具体地说当前目录上述相对路径。

您无法检测到某人是否指定了完整路径,或者是否有人转到一个目录,该目录完全是一个到单独目录的符号链接,以及其他一些可能的替代攻击。

理想情况下,你应该遵循SCA所示的建议,并有一个非常具体的允许路径&文件名。如果这是不可能的,使用白名单技术,以指定唯一允许的字符,然后验证指定它不是例如SMB共享或指定的完整路径。如果它不根据用户应指定的规范进行验证,则拒绝它。

这样做将摆脱这个问题本身,而是SCA可能仍会显示在结果中的问题(这也是因为VS动态分析静态之间的差异)。这可以通过审核它或者为验证问题的函数创建自定义清理规则来解决。