diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.cpp b/services/distributeddataservice/service/data_share/common/uri_utils.cpp index 26447121a..0de857bee 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/common/uri_utils.cpp @@ -62,6 +62,21 @@ bool URIUtils::GetBundleNameFromProxyURI(const std::string &uri, std::string &bu return true; } +bool URIUtils::GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex) +{ + auto queryParams = URIUtils::GetQueryParams(uri); + if (!queryParams[APP_INDEX].empty()) { + auto [success, data] = URIUtils::Strtoul(queryParams[APP_INDEX]); + if (!success) { + appIndex = -1; + ZLOGE("appIndex is invalid! appIndex: %{public}s", queryParams[APP_INDEX].c_str()); + return false; + } + appIndex = data; + } + return true; +} + void URIUtils::FormatUri(std::string &uri) { auto pos = uri.find_last_of('?'); diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/uri_utils.h index ffd4286c3..77ebfdc9e 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/uri_utils.h @@ -39,6 +39,7 @@ class URIUtils { public: static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo); static bool GetBundleNameFromProxyURI(const std::string &uri, std::string &bundleName); + static bool GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex); static bool IsDataProxyURI(const std::string &uri); static void FormatUri(std::string &uri); static UriConfig GetUriConfig(const std::string &uri); @@ -50,6 +51,7 @@ public: static constexpr const char *PARAM_URI_SEPARATOR = ":///"; static constexpr const char *SCHEME_SEPARATOR = "://"; static constexpr const char *URI_SEPARATOR = "/"; + static constexpr const char *APP_INDEX = "appIndex"; // for Application Clone static constexpr int DATA_PROXY_SCHEMA_LEN = sizeof(DATA_PROXY_SCHEMA) - 1; static constexpr uint32_t PARAM_URI_SEPARATOR_LEN = 4; diff --git a/services/distributeddataservice/service/data_share/data_provider_config.cpp b/services/distributeddataservice/service/data_share/data_provider_config.cpp index 4b8aca965..3689b802f 100644 --- a/services/distributeddataservice/service/data_share/data_provider_config.cpp +++ b/services/distributeddataservice/service/data_share/data_provider_config.cpp @@ -33,7 +33,7 @@ DataProviderConfig::DataProviderConfig(const std::string &uri, uint32_t callerTo { providerInfo_.uri = uri; providerInfo_.currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(callerTokenId); - LoadConfigCommonStrategy::GetAppIndexFromProxyURI(providerInfo_.uri, providerInfo_.appIndex); + URIUtils::GetAppIndexFromProxyURI(providerInfo_.uri, providerInfo_.appIndex); if (providerInfo_.currentUserId == 0) { LoadConfigCommonStrategy::GetInfoFromProxyURI(providerInfo_.uri, providerInfo_.currentUserId, callerTokenId, providerInfo_.bundleName); diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index f36d3800d..fe13493f6 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -42,6 +42,7 @@ #include "ipc_skeleton.h" #include "iservice_registry.h" #include "log_print.h" +#include "common/uri_utils.h" #include "metadata/auto_launch_meta_data.h" #include "metadata/meta_data_manager.h" #include "matching_skills.h" @@ -843,7 +844,12 @@ int32_t DataShareServiceImpl::GetSilentProxyStatus(const std::string &uri, bool return E_OK; } std::string calledBundleName = uriInfo.bundleName; - uint32_t calledTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(currentUserId, calledBundleName, 0); + int32_t appIndex = 0; + if (!URIUtils::GetAppIndexFromProxyURI(uri, appIndex)) { + return E_APPINDEX_INVALID; + } + uint32_t calledTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID( + currentUserId, calledBundleName, appIndex); if (calledTokenId == 0) { calledTokenId = Security::AccessToken::AccessTokenKit::GetHapTokenID(0, calledBundleName, 0); } diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp index 80cf59a7d..f0ce98cec 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.cpp @@ -26,14 +26,13 @@ namespace OHOS::DataShare { constexpr const char USER_PARAM[] = "user"; constexpr const char TOKEN_ID_PARAM[] = "srcToken"; constexpr const char DST_BUNDLE_NAME_PARAM[] = "dstBundleName"; -constexpr const char APP_INDEX[] = "appIndex"; // for Application Clone bool LoadConfigCommonStrategy::operator()(std::shared_ptr context) { if (context->callerTokenId == 0) { context->callerTokenId = IPCSkeleton::GetCallingTokenID(); } context->currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId); - if (!GetAppIndexFromProxyURI(context->uri, context->appIndex)) { + if (!URIUtils::GetAppIndexFromProxyURI(context->uri, context->appIndex)) { return false; } // sa, userId is in uri, caller token id is from first caller tokenId @@ -77,19 +76,4 @@ bool LoadConfigCommonStrategy::GetInfoFromProxyURI( } return true; } - -bool LoadConfigCommonStrategy::GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex) -{ - auto queryParams = URIUtils::GetQueryParams(uri); - if (!queryParams[APP_INDEX].empty()) { - auto [success, data] = URIUtils::Strtoul(queryParams[APP_INDEX]); - if (!success) { - appIndex = -1; - ZLOGE("appIndex is invalid! appIndex: %{public}s", queryParams[APP_INDEX].c_str()); - return false; - } - appIndex = data; - } - return true; -} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h index 5b633a15c..cf6ddfcbd 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_common_strategy.h @@ -23,7 +23,6 @@ public: bool operator()(std::shared_ptr context) override; static bool GetInfoFromProxyURI( const std::string &uri, int32_t &user, uint32_t &callerTokenId, std::string &calledBundleName); - static bool GetAppIndexFromProxyURI(const std::string &uri, int32_t &appIndex); }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_LOAD_CONFIG_COMMON_STRAGETY_H