gpt4 book ai didi

java - Fortify 指出了一个问题 : "Portability Flaw: File Separator", 但代码中没有硬编码分隔符

转载 作者:行者123 更新时间:2023-12-02 04:35:11 24 4
gpt4 key购买 nike

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;
}

我还是不明白为什么会出现这个问题。这是误报吗? (但为什么?)

最佳答案

Fortify 似乎在这里过于严格了。偶their website说像这样使用 File.separator 应该没问题。

使用File.separator我看不到任何可移植性问题。即使在 OpenVMS 系统上,文件路径的格式为 devicename:[directory.subdirectory]file.ext;version,Java 运行时也会在 / 分隔符和正确的 VMS 格式。

首先,使用“查找”工具仔细检查 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 可以吗?

关于java - Fortify 指出了一个问题 : "Portability Flaw: File Separator", 但代码中没有硬编码分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56554746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com