Fortify Path Manipulation错误

min*_*nil 5 security fortify

Fority Scan在以下代码段中报告了"Path Manipulation"安全问题

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);
Run Code Online (Sandbox Code Playgroud)

所以我在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);
}
Run Code Online (Sandbox Code Playgroud)

我应该忽略扫描报告或者对此有什么正确的解决方法吗?

小智 1

首先,SCA 是一个静态分析工具,因此无法检查您的自定义验证以确定其是否正常工作,因为这是 WebInspect 等动态工具的设计目的。

其次,黑名单是一种保护任何内容的糟糕方法,白名单是更安全的方法,而且您提到对标准输出进行黑名单验证这一事实会吸引攻击者。这是因为您必须考虑每种可能的攻击方式,包括可能尚未发现的方式,因此在软件发布之前很容易就过时了。

第三,这绝对不足以阻止路径操作,因为您只考虑寻找相对路径的人,更具体地说是当前目录之上的相对路径

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

理想情况下,您应该遵循 SCA 显示的建议,并具有非常具体的允许路径和文件名。如果无法做到这一点,请使用白名单技术来指定唯一允许的字符,然后进行验证以指定它不是指定的 SMB 共享或完整路径。如果它不符合用户应指定的规范,则拒绝它。

这样做可以解决问题本身,但 SCA 可能仍会在结果中显示问题(同样是由于静态分析与动态分析之间的差异)。这可以通过对其进行审核或为验证问题的函数创建自定义清理规则来解决。