diff --git a/interfaces/common/include/sandbox_helper.h b/interfaces/common/include/sandbox_helper.h index 43e92eb27861a6fcb11f1974598c9462e185e34f..73745dc84abe47e9f0344f9ca56acb7d7b4516c2 100644 --- a/interfaces/common/include/sandbox_helper.h +++ b/interfaces/common/include/sandbox_helper.h @@ -24,6 +24,8 @@ namespace AppFileService { class SandboxHelper { static std::unordered_map sandboxPathMap_; public: + static std::string Encode(const std::string &uri); + static std::string Decode(const std::string &uri); static bool CheckValidPath(const std::string &filePath); static int32_t GetPhysicalPath(const std::string &fileUri, const std::string &userId, std::string &physicalPath); diff --git a/interfaces/common/src/common_func.cpp b/interfaces/common/src/common_func.cpp index c3aedeee5bcb22281a5cc9371104db55707eb900..875f05381b386f70f5ba045ec858dd62fa64659e 100644 --- a/interfaces/common/src/common_func.cpp +++ b/interfaces/common/src/common_func.cpp @@ -17,13 +17,13 @@ #include -#include "bundle_info.h" #include "bundle_mgr_proxy.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "system_ability_definition.h" #include "log.h" +#include "sandbox_helper.h" using namespace std; @@ -104,7 +104,7 @@ string CommonFunc::GetUriFromPath(const string &path) string packageName = GetSelfBundleName(); realPath = FILE_SCHEME_PREFIX + packageName + realPath; - return realPath; + return SandboxHelper::Encode(realPath); } } // namespace AppFileService } // namespace OHOS diff --git a/interfaces/common/src/json_utils.cpp b/interfaces/common/src/json_utils.cpp index eb6ffc6cd120482f764271a2118dbd1b3d885910..2bc4eaff4d192b4538aa23dee669b02d01fd5e4b 100644 --- a/interfaces/common/src/json_utils.cpp +++ b/interfaces/common/src/json_utils.cpp @@ -17,7 +17,6 @@ #include #include -#include "common_func.h" #include "log.h" namespace OHOS { diff --git a/interfaces/common/src/sandbox_helper.cpp b/interfaces/common/src/sandbox_helper.cpp index 0a69e0b502273aac65a4d18948046769eda97541..e30b78e7c195317c17d769990c1e8eebb19effec 100644 --- a/interfaces/common/src/sandbox_helper.cpp +++ b/interfaces/common/src/sandbox_helper.cpp @@ -15,6 +15,9 @@ #include "sandbox_helper.h" +#include +#include +#include #include #include "log.h" @@ -35,6 +38,52 @@ namespace { } std::unordered_map SandboxHelper::sandboxPathMap_; +string SandboxHelper::Encode(const string &uri) +{ + const unordered_set uriCompentsSet = { + ';', ',', '/', '?', ':', '@', '&', + '=', '+', '$', '-', '_', '.', '!', + '~', '*', '(', ')', '#', '\'' + }; + const int32_t encodeLen = 2; + ostringstream outPutStream; + outPutStream.fill('0'); + outPutStream << std::hex; + + for (unsigned char tmpChar : uri) { + if (std::isalnum(tmpChar) || uriCompentsSet.find(tmpChar) != uriCompentsSet.end()) { + outPutStream << tmpChar; + } else { + outPutStream << std::uppercase; + outPutStream << '%' << std::setw(encodeLen) << static_cast(tmpChar); + outPutStream << std::nouppercase; + } + } + + return outPutStream.str(); +} + +string SandboxHelper::Decode(const string &uri) +{ + std::ostringstream outPutStream; + const int32_t encodeLen = 2; + size_t index = 0; + while (index < uri.length()) { + if (uri[index] == '%') { + int hex = 0; + std::istringstream inputStream(uri.substr(index + 1, encodeLen)); + inputStream >> std::hex >> hex; + outPutStream << static_cast(hex); + index += encodeLen + 1; + } else { + outPutStream << uri[index]; + index++; + } + } + + return outPutStream.str(); +} + static string GetLowerPath(string &lowerPathHead, const string &lowerPathTail, const string &userId, const string &bundleName) { diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 2810c8a4ef60cfae63411561e0584dbfd864bbb1..25f54d03c9af5bcbd7d244e0e4afbfdb5d5b78a3 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -58,17 +58,14 @@ ohos_shared_library("fileuri_native") { sources = [ "../../common/src/common_func.cpp", "../../common/src/json_utils.cpp", + "../../common/src/sandbox_helper.cpp", "file_uri/src/file_uri.cpp", ] public_configs = [ ":file_uri_config" ] external_deps = [ - "ability_base:base", - "ability_base:want", "ability_base:zuri", - "ability_runtime:abilitykit_native", - "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", @@ -111,8 +108,6 @@ ohos_shared_library("remote_file_share_native") { public_configs = [ ":remote_file_share_config" ] external_deps = [ - "ability_base:base", - "ability_base:want", "ability_base:zuri", "c_utils:utils", "hilog:libhilog", diff --git a/interfaces/innerkits/native/file_share/src/file_share.cpp b/interfaces/innerkits/native/file_share/src/file_share.cpp index 8045d85522a1f7e667b9e2129a5fa497ad327a82..3374bb7c8ea864c9db490df04221eb54ee66dce4 100644 --- a/interfaces/innerkits/native/file_share/src/file_share.cpp +++ b/interfaces/innerkits/native/file_share/src/file_share.cpp @@ -214,7 +214,10 @@ static int32_t PreparePreShareDir(FileShareInfo &info) int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) { FileShareInfo info; - int32_t ret = GetFileShareInfo(uri, tokenId, flag, info); + string decodeUri = SandboxHelper::Decode(uri); + LOGD("CreateShareFile begin with uri %{private}s decodeUri %{private}s", + uri.c_str(), decodeUri.c_str()); + int32_t ret = GetFileShareInfo(decodeUri, tokenId, flag, info); if (ret != 0) { LOGE("Failed to get FileShareInfo with %{public}d", ret); return ret; @@ -250,7 +253,7 @@ int32_t CreateShareFile(const string &uri, uint32_t tokenId, uint32_t flag) static void UmountDelUris(vector sharePathList, string currentUid, string bundleNameSelf) { for (size_t i = 0; i < sharePathList.size(); i++) { - Uri uri(sharePathList[i]); + Uri uri(SandboxHelper::Decode(sharePathList[i])); string path = uri.GetPath(); string bundleName = uri.GetAuthority(); string delRPath = DATA_APP_EL2_PATH + currentUid + SHARE_PATH + bundleNameSelf + SHARE_R_PATH + diff --git a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp index 1f5fdf38f7221a6d145d80e4a4f98d3297c4fe70..9a8d5e4121b74e28c958c005190c6fc1a70aba4a 100644 --- a/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp +++ b/interfaces/innerkits/native/remote_file_share/src/remote_file_share.cpp @@ -295,7 +295,8 @@ static void SetHmdfsUriInfo(struct HmdfsUriInfo &hdi, Uri &uri, uint64_t fileSiz std::string bundleName = uri.GetAuthority(); std::string path = uri.GetPath(); - hdi.uriStr = FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + REMOTE_SHARE_PATH_DIR + path; + hdi.uriStr = SandboxHelper::Encode(FILE_SCHEME + "://" + bundleName + DISTRIBUTED_DIR_PATH + + REMOTE_SHARE_PATH_DIR + path); hdi.fileSize = fileSize; return; } @@ -303,8 +304,9 @@ static void SetHmdfsUriInfo(struct HmdfsUriInfo &hdi, Uri &uri, uint64_t fileSiz int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int32_t &userId, struct HmdfsUriInfo &hui) { - Uri uri(uriStr); - LOGD("GetDfsUriFromLocal begin with %{public}s", uriStr.c_str()); + Uri uri(SandboxHelper::Decode(uriStr)); + LOGD("GetDfsUriFromLocal begin with uri:%{private}s, decode uri:%{private}s", + uriStr.c_str(), uri.ToString().c_str()); std::string physicalPath = GetPhysicalPath(uri, std::to_string(userId)); if (physicalPath == "") { LOGE("Failed to get physical path"); @@ -320,7 +322,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int struct HmdfsDstInfo hdi; std::string bundleName = uri.GetAuthority(); - LOGD("PhysicalPath: %{public}s DistributedPath: %{public}s BundleName: %{public}s", + LOGD("PhysicalPath: %{private}s DistributedPath: %{private}s BundleName: %{private}s", physicalPath.c_str(), distributedPath.c_str(), bundleName.c_str()); InitHmdfsInfo(hdi, physicalPath, distributedPath, bundleName); @@ -340,7 +342,7 @@ int32_t RemoteFileShare::GetDfsUriFromLocal(const std::string &uriStr, const int close(dirFd); SetHmdfsUriInfo(hui, uri, hdi.size); - LOGD("GetDfsUriFromLocal successfully with %{public}s", hui.uriStr.c_str()); + LOGD("GetDfsUriFromLocal successfully with %{private}s", hui.uriStr.c_str()); return 0; } } // namespace ModuleRemoteFileShare diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index bc8a7129dd510a50fdb575133336b7e132bdf310..81108577a8d4469f770617533a0de0183d281662 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -83,6 +83,7 @@ ohos_shared_library("fileuri") { sources = [ "../../common/src/common_func.cpp", + "../../common/src/sandbox_helper.cpp", "file_uri/file_uri_n_exporter.cpp", "file_uri/get_uri_from_path.cpp", ]