已开启
ConvertSandboxRealPath添加路径校验 #2258
dongtingchi创建于 8月20日
ConvertSandboxRealPath添加路径校验 #2258
已开启
共 2 个文件变更+43-0
| @@ -27,6 +27,7 @@ | |||
| 27 | 27 | ||
| 28 | #include "b_anony/b_anony.h" | 28 | #include "b_anony/b_anony.h" |
| 29 | #include "b_error/b_error.h" | 29 | #include "b_error/b_error.h" |
| 30 | +#include "b_filesystem/b_dir.h" | ||
| 30 | #include "b_resources/b_constants.h" | 31 | #include "b_resources/b_constants.h" |
| 31 | #include "b_utils/string_utils.h" | 32 | #include "b_utils/string_utils.h" |
| 32 | #include "filemgmt_libhilog.h" | 33 | #include "filemgmt_libhilog.h" |
| @@ -373,6 +374,10 @@ void StorageManagerService::ConvertSandboxRealPath(const uint32_t userId, const | |||
| 373 | const std::string &sandboxPathStr, std::vector<std::string> &realPaths, | 374 | const std::string &sandboxPathStr, std::vector<std::string> &realPaths, |
| 374 | std::map<std::string, std::string>& pathMap) | 375 | std::map<std::string, std::string>& pathMap) |
| 375 | { | 376 | { |
| 377 | + if (sandboxPathStr.empty() || !BDir::IsFilePathValid(sandboxPathStr)) { | ||
| 378 | + HILOGE("Invalid sandboxPathStr: %{public}s", GetAnonyPath(sandboxPathStr).c_str()); | ||
| 379 | + return; | ||
| 380 | + } | ||
| 376 | std::string uriString; | 381 | std::string uriString; |
| 377 | if (sandboxPathStr.find(NORMAL_SAND_PREFIX) == 0) { | 382 | if (sandboxPathStr.find(NORMAL_SAND_PREFIX) == 0) { |
| 378 | // for normal hap, start with file://bundleName | 383 | // for normal hap, start with file://bundleName |
| @@ -241,6 +241,44 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRe | |||
| 241 | EXPECT_EQ(pathMap[realPaths[0]], sandboxPathStr); | 241 | EXPECT_EQ(pathMap[realPaths[0]], sandboxPathStr); |
| 242 | } | 242 | } |
| 243 | 243 | ||
| 244 | +/** | ||
| 245 | + * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_003 | ||
| 246 | + * @tc.desc: check ConvertSandboxRealPath with empty sandboxPathStr | ||
| 247 | + * @tc.type: FUNC | ||
| 248 | + * @tc.require: AR000IGCR7 | ||
| 249 | + */ | ||
| 250 | +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_003, | ||
| 251 | + testing::ext::TestSize.Level1) | ||
| 252 | +{ | ||
| 253 | + uint32_t userId = 100; | ||
| 254 | + std::string bundleName = "com.example.app"; | ||
| 255 | + std::string sandboxPathStr; | ||
| 256 | + std::vector<std::string> realPaths; | ||
| 257 | + std::map<std::string, std::string> pathMap; | ||
| 258 | + StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); | ||
| 259 | + EXPECT_TRUE(realPaths.empty()); | ||
| 260 | + EXPECT_TRUE(pathMap.empty()); | ||
| 261 | +} | ||
| 262 | + | ||
| 263 | +/** | ||
| 264 | + * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_004 | ||
| 265 | + * @tc.desc: check ConvertSandboxRealPath with invalid path containing traversal | ||
| 266 | + * @tc.type: FUNC | ||
| 267 | + * @tc.require: AR000IGCR7 | ||
| 268 | + */ | ||
| 269 | +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_004, | ||
| 270 | + testing::ext::TestSize.Level1) | ||
| 271 | +{ | ||
| 272 | + uint32_t userId = 100; | ||
| 273 | + std::string bundleName = "com.example.app"; | ||
| 274 | + std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/../etc/passwd"; | ||
| 275 | + std::vector<std::string> realPaths; | ||
| 276 | + std::map<std::string, std::string> pathMap; | ||
| 277 | + StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); | ||
| 278 | + EXPECT_TRUE(realPaths.empty()); | ||
| 279 | + EXPECT_TRUE(pathMap.empty()); | ||
| 280 | +} | ||
| 281 | + | ||
| 244 | /** | 282 | /** |
| 245 | * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_001 | 283 | * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_001 |
| 246 | * @tc.desc: check the CheckIfDirForIncludes function | 284 | * @tc.desc: check the CheckIfDirForIncludes function |