Path Manipulation Fortify

来源:互联网 发布:js 单选按钮改变事件 编辑:程序博客网 时间:2024/06/02 06:51

Fortify扫描遇到了Path Manipulation问题,定位代码如下:

File publisFile = null;File publisFileDir = new File(OSSFileUtils.getDeploy()+"/"+corpPK);
涉及到安全性问题,在文件目录下,没有限制文件目录,可能存在的问题是:可以退回上一级目录,继而访问上一级内容,

解决办法:step1增加工具方法对文件目录的特殊字符进行处理

public static String validFilePath(String filepath) throws Exception{List<String> allowedExtensions = new ArrayList<>();boolean result = false;allowedExtensions.add(".xml");allowedExtensions.add(".jar");allowedExtensions.add(".api");allowedExtensions.add(".properties");allowedExtensions.add(".xsd");allowedExtensions.add(".flw");allowedExtensions.add(".schema");allowedExtensions.add(".MF");allowedExtensions.add(".log");for(String suf:allowedExtensions){if(filepath.endsWith(suf)){result = true;break;}}if(!result){   // 按指定模式在字符串查找      String pattern1 = "(.*)(\\.log\\.{0,1}\\d{0,})$";      // 创建 Pattern 对象      Pattern r1 = Pattern.compile(pattern1);      // 现在创建 matcher 对象      Matcher m1 = r1.matcher(filepath);  if(!m1.matches()){Exception Exception = new Exception("file name is Illage") ;throw Exception   ;  }}filepath.replaceAll("../", "");filepath.replaceAll("..\\\\", "");return filepath;}

step2.引用工具方法

String temp;temp = ESAPIUtil.validFilePath(OSSFileUtils.getProduct_package());File f = new File(temp, p.getFileName());
问题解决!




0 1
原创粉丝点击