Sta*_*low 8 java security secure-coding docker checkmarx
Checkmarx - v 9.3.0 HF11
我将 env 值作为数据目录路径传递到 dev/uat 服务器中使用的 docker 文件中
ENV DATA /app/data/
Run Code Online (Sandbox Code Playgroud)
在本地,使用以下环境变量
数据=C:\projects\app\data\
getDataDirectory("MyDirectoryName"); // MyDirectoryName 存在于数据文件夹中
public String getDataDirectory(String dirName)
{
String path = System.getenv("DATA");
if (path != null) {
path = sanitizePathValue(path);
path = encodePath(path);
dirName = sanitizePathValue(dirName);
if (!path.endsWith(File.separator)) {
path = path + File.separator;
} else if (!path.contains("data")) {
throw new MyRuntimeException("Data Directory path is incorrect");
}
} else {
return null;
}
File file = new File(dirName); // NOSONAR
if (!file.isAbsolute()) {
File tmp = new File(SecurityUtil.decodePath(path)); // NOSONAR
if (!tmp.getAbsolutePath().endsWith(Character.toString(File.separatorChar))) {
dirName = tmp.getAbsolutePath() + File.separatorChar + dirName;
} else {
dirName = tmp.getAbsolutePath() + dirName;
}
}
return dirName;
}
public static String encodePath(String path) {
try {
return URLEncoder.encode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error("Exception while encoding path", e);
}
return "";
}
public static String validateAndNormalizePath(String path) {
path = path.replaceAll("/../", "/");
path = path.replaceAll("/%46%46/", "/");
path = SecurityUtil.cleanIt(path);
path = FilenameUtils.normalize(path); // normalize path
return path;
}
public static String sanitizePathValue(String filename){
filename = validateAndNormalizePath(filename);
String regEx = "..|\\|/";
// compile the regex to create pattern
// using compile() method
Pattern pattern = Pattern.compile(regEx);
// get a matcher object from pattern
Matcher matcher = pattern.matcher(filename);
// check whether Regex string is
// found in actualString or not
boolean matches = matcher.matches();
if(matches){
throw new MyAppRuntimeException("filename:'"+filename+"' is bad.");
}
return filename;
}
public static String validateAndNormalizePath(String path) {
path = path.replaceAll("/../", "/");
path = path.replaceAll("/%46%46/", "/");
path = SecurityUtil.cleanIt(path);
path = FilenameUtils.normalize(path); // normalize path
return path;
}
Run Code Online (Sandbox Code Playgroud)
[尝试] - 更新我在少数成员的帮助下尝试防止路径遍历问题的代码。
试图清理字符串和规范化字符串,但没有运气并遇到同样的问题。
如何解决存储绝对路径遍历问题?
基于阅读 Checkmarx 查询的绝对路径遍历漏洞(我相信一般的缓解方法之一),是在前面添加硬编码路径以避免攻击者遍历文件系统:
文件有一个接受第二个参数的构造函数,该参数允许您执行一些前置操作
String filename = System.getEnv("test");
File dictionaryFile = new File("/home/", filename);
Run Code Online (Sandbox Code Playgroud)
更新: validateAndNormalizePath在技术上就足够了,但我相信 Checkmarx 无法将其识别为消毒剂(作为自定义编写的函数)。我建议与您的应用程序安全团队合作,让他们使用 CxAudit 并覆盖基本存储路径遍历 Checkmarx 查询,以将validateAndNormalizePath识别为有效的清理程序。
| 归档时间: |
|
| 查看次数: |
4350 次 |
| 最近记录: |