Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] add resource full name check #15757

Merged
merged 7 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,16 @@ public Result<PageInfo<StorageEntity>> queryResourceListPaging(User loginUser, S
String tenantCode = getTenantCode(user);
String baseDir = isAdmin(loginUser) ? storageOperate.getDir(ResourceType.ALL, tenantCode)
: storageOperate.getDir(type, tenantCode);
if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)
|| (StringUtils.isNotBlank(fullName) && !StringUtils.startsWith(fullName, baseDir))) {

if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
log.error("current user does not have permission");
putMsg(result, Status.NO_CURRENT_OPERATING_PERMISSION);
return result;
}

List<StorageEntity> resourcesList = new ArrayList<>();
checkFullName(baseDir, fullName);

List<StorageEntity> resourcesList;
try {
resourcesList = queryStorageEntityList(loginUser, fullName, type, tenantCode, false);
} catch (ServiceException e) {
Expand Down Expand Up @@ -646,45 +648,32 @@ public Map<String, Object> queryResourceList(User loginUser, ResourceType type,

String tenantCode = getTenantCode(user);

String defaultPath = "";
List<StorageEntity> resourcesList = new ArrayList<>();
String baseDir = storageOperate.getDir(type, tenantCode);
checkFullName(baseDir, fullName);

List<StorageEntity> resourcesList = new ArrayList<>();
if (StringUtils.isBlank(fullName)) {
if (isAdmin(loginUser)) {
List<User> userList = userMapper.selectList(null);
Set<String> visitedTenantEntityCode = new HashSet<>();
for (User userEntity : userList) {

String tenantEntityCode = getTenantCode(userEntity);
if (!visitedTenantEntityCode.contains(tenantEntityCode)) {
defaultPath = storageOperate.getResDir(tenantEntityCode);
if (type.equals(ResourceType.UDF)) {
defaultPath = storageOperate.getUdfDir(tenantEntityCode);
}
resourcesList.addAll(storageOperate.listFilesStatusRecursively(defaultPath, defaultPath,
baseDir = storageOperate.getDir(type, tenantEntityCode);
resourcesList.addAll(storageOperate.listFilesStatusRecursively(baseDir, baseDir,
tenantEntityCode, type));
visitedTenantEntityCode.add(tenantEntityCode);
}
}
} else {
defaultPath = storageOperate.getResDir(tenantCode);
if (type.equals(ResourceType.UDF)) {
defaultPath = storageOperate.getUdfDir(tenantCode);
}

resourcesList = storageOperate.listFilesStatusRecursively(defaultPath, defaultPath, tenantCode, type);
resourcesList = storageOperate.listFilesStatusRecursively(baseDir, baseDir, tenantCode, type);
}
} else {
defaultPath = storageOperate.getResDir(tenantCode);
if (type.equals(ResourceType.UDF)) {
defaultPath = storageOperate.getUdfDir(tenantCode);
}

resourcesList = storageOperate.listFilesStatusRecursively(fullName, defaultPath, tenantCode, type);
resourcesList = storageOperate.listFilesStatusRecursively(fullName, baseDir, tenantCode, type);
}

Visitor resourceTreeVisitor = new ResourceTreeVisitor(resourcesList);
result.put(Constants.DATA_LIST, resourceTreeVisitor.visit(defaultPath).getChildren());
result.put(Constants.DATA_LIST, resourceTreeVisitor.visit(baseDir).getChildren());
putMsg(result, Status.SUCCESS);

return result;
Expand Down Expand Up @@ -768,10 +757,12 @@ public Result<Object> delete(User loginUser, String fullName, String resTenantCo
return result;
}

String defaultPath = storageOperate.getResDir(tenantCode);
String baseDir = storageOperate.getResDir(tenantCode);
checkFullName(baseDir, fullName);

StorageEntity resource;
try {
resource = storageOperate.getFileStatus(fullName, defaultPath, resTenantCode, null);
resource = storageOperate.getFileStatus(fullName, baseDir, resTenantCode, null);
} catch (Exception e) {
log.error(e.getMessage() + " Resource path: {}", fullName, e);
putMsg(result, Status.RESOURCE_NOT_EXIST);
Expand All @@ -786,7 +777,7 @@ public Result<Object> delete(User loginUser, String fullName, String resTenantCo

// recursively delete a folder
List<String> allChildren =
storageOperate.listFilesStatusRecursively(fullName, defaultPath, resTenantCode, resource.getType())
storageOperate.listFilesStatusRecursively(fullName, baseDir, resTenantCode, resource.getType())
.stream().map(storageEntity -> storageEntity.getFullName()).collect(Collectors.toList());

String[] allChildrenFullNameArray = allChildren.stream().toArray(String[]::new);
Expand Down Expand Up @@ -863,11 +854,7 @@ public Result<Object> queryResourceByFileName(User loginUser, String fileName, R
return result;
}

String defaultPath = storageOperate.getResDir(resTenantCode);
if (type.equals(ResourceType.UDF)) {
defaultPath = storageOperate.getUdfDir(resTenantCode);
}

String defaultPath = storageOperate.getDir(type, resTenantCode);
StorageEntity file;
try {
file = storageOperate.getFileStatus(defaultPath + fileName, defaultPath, resTenantCode, type);
Expand Down Expand Up @@ -911,6 +898,9 @@ public Result<Object> readResource(User loginUser, String fullName, String resTe
return result;
}

String baseDir = storageOperate.getResDir(tenantCode);
checkFullName(baseDir, fullName);

// check preview or not by file suffix
String nameSuffix = Files.getFileExtension(fullName);
String resourceViewSuffixes = FileUtils.getResourceViewSuffixes();
Expand All @@ -924,7 +914,7 @@ public Result<Object> readResource(User loginUser, String fullName, String resTe
}
}

List<String> content = new ArrayList<>();
List<String> content;
try {
if (storageOperate.exists(fullName)) {
content = storageOperate.vimFile(tenantCode, fullName, skipLineNum, limit);
Expand Down Expand Up @@ -1003,13 +993,8 @@ public Result<Object> createResourceFile(User loginUser, ResourceType type, Stri

String name = fileName.trim() + "." + nameSuffix;

String fullName = "";
String userResRootPath = storageOperate.getResDir(tenantCode);
if (!currentDir.contains(userResRootPath)) {
fullName = userResRootPath + name;
} else {
fullName = currentDir + name;
}
String fullName = currentDir.contains(userResRootPath) ? currentDir + name : userResRootPath + name;

result = verifyResourceName(fullName, type, loginUser);
if (!result.getCode().equals(Status.SUCCESS.getCode())) {
Expand Down Expand Up @@ -1063,18 +1048,17 @@ public Result<Object> updateResourceContent(User loginUser, String fullName, Str
putMsg(result, Status.USER_NOT_EXIST, loginUser.getId());
return result;
}
if (!fullName.startsWith(storageOperate.getResDir(resTenantCode))) {
throw new ServiceException("Resource file: " + fullName + " is illegal");
}

String tenantCode = getTenantCode(user);

if (!isUserTenantValid(isAdmin(loginUser), tenantCode, resTenantCode)) {
log.error("current user does not have permission");
putMsg(result, Status.NO_CURRENT_OPERATING_PERMISSION);
return result;
}

String baseDir = storageOperate.getResDir(resTenantCode);
checkFullName(baseDir, fullName);

StorageEntity resource;
try {
resource = storageOperate.getFileStatus(fullName, "", resTenantCode, ResourceType.FILE);
Expand Down Expand Up @@ -1178,6 +1162,8 @@ public org.springframework.core.io.Resource downloadResource(User loginUser, Str
}

String tenantCode = getTenantCode(user);
String baseDir = storageOperate.getResDir(tenantCode);
checkFullName(baseDir, fullName);

String[] aliasArr = fullName.split("/");
String alias = aliasArr[aliasArr.length - 1];
Expand Down Expand Up @@ -1306,16 +1292,13 @@ public Result<Object> queryResourceBaseDir(User loginUser, ResourceType type) {
*/
private boolean isUserTenantValid(boolean isAdmin, String userTenantCode,
String resTenantCode) throws ServiceException {
if (!isAdmin) {
resTenantCode = resTenantCode == null ? "" : resTenantCode;
if (!StringUtils.isBlank(resTenantCode) && !resTenantCode.equals(userTenantCode)) {
// if an ordinary user directly send a query API with a different tenantCode and fullName "",
// still he/she does not have read permission.
return false;
}
if (isAdmin) {
return true;
}

return true;
if (StringUtils.isEmpty(resTenantCode)) {
return true;
}
return resTenantCode.equals(userTenantCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the resTenantCode be empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when resource create or upload, the resource tenant code is empty. But the better way is to remove checking if not need. I will update it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource tenant code will be empty when query resources list, so I keep the logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

}

private String getTenantCode(User user) {
Expand All @@ -1325,4 +1308,10 @@ private String getTenantCode(User user) {
}
return tenant.getTenantCode();
}

private void checkFullName(String baseDir, String fullName) {
if (StringUtils.isNotBlank(fullName) && !StringUtils.startsWith(fullName, baseDir)) {
throw new ServiceException("Resource file: " + fullName + " is illegal");
}
}
}
Loading
Loading