Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security 强化路径操作错误_Security_Fortify - Fatal编程技术网

Security 强化路径操作错误

Security 强化路径操作错误,security,fortify,Security,Fortify,Fority Scan在以下代码段中报告了“路径操纵”安全问题 String filePath = getFilePath(fileLocation, fileName); final File file = new File(filePath); LOGGER.info("Saving report at : " + filePath); BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file)); fileWrit

Fority Scan在以下代码段中报告了“路径操纵”安全问题

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);
}
试试看{
字符串filePath=getFilePath(fileLocation,fileName);
if(isSecurePath(文件路径)){
最终文件=新文件(文件路径);
LOGGER.info(“将报告保存在:“+filePath”);
BufferedWriter fileWriter=新的BufferedWriter(新的fileWriter(文件));
fileWriter.write(fileContent);
}否则{
抛出新异常(“安全问题。文件路径包含黑名单字符”);
}
}捕获(最终异常e){
LOGGER.错误(“无法准备邮件附件:”,e);
message=“邮件无法发送,无法准备邮件附件”;
}
专用布尔值isSecurePath(字符串文件路径){
字符串[]blackListChars={.“};
return(StringUtils.indexOfAny(filePath,blackListChars)<0);
}

我是否应该忽略扫描报告,或者如何解决这个问题?

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

其次,黑名单是一种很差的保护任何东西的方法,白名单是一种更安全的方法,而且您向stdout提到黑名单验证会诱使攻击者。这是因为您必须考虑每一种可能的攻击方式,包括可能尚未被发现的方式,因此在软件发布之前很容易过时

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

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

理想情况下,您应该遵循SCA所示的建议,并具有非常特定的允许路径和文件名。如果不可能,请使用白名单技术指定只允许的字符,然后验证以指定它不是指定的SMB共享或完整路径。如果它没有按照用户应该指定的规范进行验证,则拒绝它

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

看看这个