Fortify SCA工具发现了一个称为可移植性缺陷的问题:文件分隔符,但是这些问题的根源是没有诸如"/“或"\”之类的硬编码文件分隔符,只有诸如“”之类的文件扩展名。存在。
我们的客户使用Fortify SCA扫描他们的遗留系统源代码。Fortify发现可移植性缺陷:文件分离问题。它说在字符串数组中声明的文件名包含硬编码的文件分隔符(这个字符串数组是问题的根源),但我在这些文件名字符串中看不到任何文件分隔符,如"/“或"\”。
public static final String SYS_SPRAT = File.separator; //this is declared as a class attribute
String[] fileNames = { //fortify points out here is the source of this issue
"",
"2.5.1aaaaa.pdf",
"2.5.2bbbbb.pdf",
"2.5.3ccccc.pdf",
.......
"5.1.4甲甲甲甲甲.pdf",
};
String fileName = null;
File file = null;
int iParam = Integer.parseInt(sParam);
if (iParam >= 1 && iParam <= 26) {
fileName = fileNames[iParam];
String filePath = SYS_SPRAT + "home" + SYS_SPRAT + "xxx" + SYS_SPRAT + "ooo" + SYS_SPRAT + "Resource" + SYS_SPRAT + fileName;
file = new File(filePath);
else {
addFacesMessage("wrong parameter");
return null;
}我仍然不明白为什么会有这样的问题。是假阳性吗?(但为什么呢?)
发布于 2019-06-27 17:59:10
似乎Fortify在这里可能过于严格了。即使是their website也说,像这样使用File.separator应该没问题。
使用File.separator我看不到任何可移植性问题。即使在文件路径格式为devicename:[directory.subdirectory]file.ext;version的OpenVMS系统上,Java运行时也会在/分隔符和适当的VMS格式之间进行内部转换。
首先,使用"Find“工具仔细检查,以确保filenames[]中的任何字符串中都没有任何\或/字符(不要仅仅依靠视觉检查)。如果确定没有这样的字符,则继续执行下面的建议。
尽量避免使用File.separator。相反,可以尝试使用Paths.get
public static final Path RESOURCE_DIR = Paths.get(
"home", "xxx", "ooo", "Resource");
String[] fileNames = {
"",
"2.5.1aaaaa.pdf",
"2.5.2bbbbb.pdf",
"2.5.3ccccc.pdf",
.......
"5.1.4甲甲甲甲甲.pdf",
};
String fileName = null;
File file = null;
int iParam = Integer.parseInt(sParam);
if (iParam >= 1 && iParam <= 26) {
fileName = fileNames[iParam];
file = RESOURCE_DIR.resolve(filePath).toFile();
else {
addFacesMessage("wrong parameter");
return null;
}当你这样做的时候,Fortify可以吗?
https://stackoverflow.com/questions/56554746
复制相似问题