-
Portability Flaw: File Separator (Code Quality, Data Flow)
调用第 40 行的 File() 可能会导致可移植性问题,因为它使用硬编码文件分隔符。
Explanation:
不同的操作系统使用不同的字符作为文件分隔符。例如,Microsoft Windows 系统使用“”,而 UNIX 系统则使用“/”。应用程序需要在不同的平台上运行时,使用硬编码文件分隔符会导致应用程序逻辑执行错误,并有可能导致 denial of service。
在这种情况下,在 FileOper.java 中第 40 行的 File() 调用中使用了硬编码文件分隔符。
例 1:以下代码使用硬编码文件分隔符来打开文件:
...
File file = new File(directoryName + "\" + fileName);
修改方法:
使用File.seprator代替/或
-
Path Manipulation
/**
* 设置路径黑名单
* 检查路径是否合法,路径中不允许带有..,否则将抛出路径不合法异常
*
* @param path 待检查的路径
*/
public static String validateFilePath(String path) {
TTPSysLog.logDebug("过滤前路径。。。。" + path);
if (path.contains("../") || path.contains("..\") || path.contains("..") || path.contains("|") || path.contains("<") || path.contains(">") || path.contains("*") || path.contains(""")) {
throw new TTPError("非法路径! " + path);
}
// 将上传路径还原
path = realUploadPath(path);
HashMap<String, String> map = new HashMap<String, String>();
map.put("a", "a");
map.put("b", "b");
map.put("c", "c");
map.put("d", "d");
map.put("e", "e");
map.put("f", "f");
map.put("g", "g");
map.put("h", "h");
map.put("i", "i");
map.put("j", "j");
map.put("k", "k");
map.put("l", "l");
map.put("m", "m");
map.put("n", "n");
map.put("o", "o");
map.put("p", "p");
map.put("q", "q");
map.put("r", "r");
map.put("s", "s");
map.put("t", "t");
map.put("u", "u");
map.put("v", "v");
map.put("w", "w");
map.put("x", "x");
map.put("y", "y");
map.put("z", "z");
map.put("A", "A");
map.put("B", "B");
map.put("C", "C");
map.put("D", "D");
map.put("E", "E");
map.put("F", "F");
map.put("G", "G");
map.put("H", "H");
map.put("I", "I");
map.put("J", "J");
map.put("K", "K");
map.put("L", "L");
map.put("M", "M");
map.put("N", "N");
map.put("O", "O");
map.put("P", "P");
map.put("Q", "Q");
map.put("R", "R");
map.put("S", "S");
map.put("T", "T");
map.put("U", "U");
map.put("V", "V");
map.put("W", "W");
map.put("X", "X");
map.put("Y", "Y");
map.put("Z", "Z");
map.put("0", "0");
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
map.put("5", "5");
map.put("6", "6");
map.put("7", "7");
map.put("8", "8");
map.put("9", "9");
map.put("_", "_");
map.put("-", "-");
map.put(".", ".");
map.put("(", "(");
map.put(")", ")");
map.put("{", "{");
map.put("}", "}");
map.put("[", "[");
map.put("]", "]");
map.put(":", ":");
map.put("/", "/");
map.put("\", "/");
map.put("+", "+");
map.put("@", "@");
map.put("$", "$");
map.put(" ", " ");
map.put("?", "?");
map.put("?", "?");
map.put("u0000", " ");
StringBuilder temp = new StringBuilder();
String regx = "[\u4e00-\u9fcb]";
for (int i = 0; i < path.length(); i++) {
if (map.get(path.charAt(i) + "") != null) {
temp.append(map.get(path.charAt(i) + ""));
} else {
Pattern p = Pattern.compile(regx);
Matcher m = p.matcher("\u" + path.charAt(i));
System.out.println("\u" + path.charAt(i));
if (m.find()) {
//temp.append(path.charAt(i));
throw new TTPError("非法路径! 路径名或文件名不支持带有中文!" + path);
} else {
temp.append(map.get(path.charAt(i) + ""));
}
}
}
TTPSysLog.logDebug("过滤后路径。。。。" + temp);
if (path.length() != temp.length()) {
throw new TTPError("非法路径! " + path);
}
return temp.toString();
}