Skip to content

Commit 4918ae9

Browse files
committed
fix: upload zip
1 parent fcdd1f3 commit 4918ae9

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

backend/services/data-management-service/src/main/java/com/datamate/datamanagement/application/DatasetFileApplicationService.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,13 +550,33 @@ private void addFileToDataset(String datasetId, List<FileUploadResult> unpacked)
550550
relativePath = relativePath.substring(1);
551551
}
552552

553-
// 构建目标路径:数据集根目录 + 相对路径
554-
String targetPath = datasetBasePath + File.separator + datasetId;
553+
// 构建安全的目标路径:防止目录遍历攻击
554+
// 校验 datasetId,防止目录遍历或非法路径片段
555+
if (datasetId.contains("..") || datasetId.contains("/") || datasetId.contains("\\")) {
556+
throw BusinessException.of(CommonErrorCode.PARAM_ERROR, "Invalid datasetId: " + datasetId);
557+
}
558+
559+
// 校验相对路径,防止目录遍历
560+
if (relativePath.contains("..")) {
561+
throw BusinessException.of(CommonErrorCode.PARAM_ERROR, "Invalid relative path: " + relativePath);
562+
}
563+
564+
// 使用 Path API 安全地构建路径
565+
Path datasetBaseDirPath = Paths.get(datasetBasePath).resolve(datasetId).normalize();
566+
Path targetPath;
555567
if (!relativePath.isEmpty()) {
556-
targetPath = targetPath + File.separator + relativePath;
568+
targetPath = datasetBaseDirPath.resolve(relativePath).normalize();
569+
} else {
570+
targetPath = datasetBaseDirPath;
571+
}
572+
573+
// 确保目标路径仍然位于数据集根目录之下
574+
if (!targetPath.startsWith(datasetBaseDirPath)) {
575+
throw BusinessException.of(CommonErrorCode.PARAM_ERROR,
576+
"Path traversal detected: " + relativePath);
557577
}
558578

559-
File targetFile = new File(targetPath);
579+
File targetFile = targetPath.toFile();
560580
// 创建父目录
561581
FileUtils.createParentDirectories(targetFile);
562582

0 commit comments

Comments
 (0)