From 862549cef763c854d2393b5a94042bc91c0455e9 Mon Sep 17 00:00:00 2001 From: wanxiaoqing Date: Tue, 24 Jun 2025 14:09:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EappID=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wanxiaoqing --- .../service/test/BUILD.gn | 2 + .../test/udmf_preprocess_utils_test.cpp | 14 ++ .../service/test/udmf_service_impl_test.cpp | 157 ++++++++++++++++++ .../service/udmf/BUILD.gn | 1 + .../udmf/preprocess/preprocess_utils.cpp | 30 ++++ .../udmf/preprocess/preprocess_utils.h | 1 + .../service/udmf/udmf_service_impl.cpp | 67 ++++++-- .../service/udmf/udmf_service_impl.h | 3 + 8 files changed, 264 insertions(+), 11 deletions(-) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 7c20c4aa2..46425ddb4 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1562,6 +1562,8 @@ ohos_unittest("UdmfPreProcessUtilsTest") { external_deps = [ "ability_runtime:uri_permission_mgr", "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp index 8ffe52b9e..c42f223a7 100644 --- a/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp +++ b/services/distributeddataservice/service/test/udmf_preprocess_utils_test.cpp @@ -179,4 +179,18 @@ HWTEST_F(UdmfPreProcessUtilsTest, GetHtmlFileUris001, TestSize.Level1) PreProcessUtils preProcessUtils; EXPECT_NO_FATAL_FAILURE(preProcessUtils.GetHtmlFileUris(tokenId, data, isLocal, uris)); } + +/** +* @tc.name: GetAppId001 +* @tc.desc: Abnormal test of GetAppId, samgrProxy is nullptr +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfPreProcessUtilsTest, GetAppId001, TestSize.Level1) +{ + std::string bundleName = "test"; + PreProcessUtils preProcessUtils; + std::string appId = preProcessUtils.GetAppId(bundleName); + EXPECT_EQ(appId, ""); +} }; // namespace UDMF \ No newline at end of file diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index bcb18a864..0bad354cb 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -27,6 +27,7 @@ #include "preprocess_utils.h" #include "runtime_store.h" #include "text.h" +#include "plain_text.h" #include "token_setproc.h" using namespace testing::ext; @@ -431,5 +432,161 @@ HWTEST_F(UdmfServiceImplTest, IsValidInput004, TestSize.Level1) bool result = impl.IsValidInput(query, unifiedData, key); EXPECT_FALSE(result); } + +/** + * @tc.name: CheckAppId001 + * @tc.desc: invalid bundleName + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, CheckAppId001, TestSize.Level1) +{ + std::shared_ptr runtime = std::make_shared(); + runtime->appId = "appId"; + std::string bundleName = "ohos.test.demo1"; + + UdmfServiceImpl impl; + int32_t result = impl.CheckAppId(runtime, bundleName); + EXPECT_EQ(result, E_INVALID_PARAMETERS); +} + +/** + * @tc.name: CheckAppId002 + * @tc.desc: invalid runtime + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, CheckAppId002, TestSize.Level1) +{ + std::shared_ptr runtime = std::make_shared(); + std::string bundleName = "ohos.test.demo1"; + + UdmfServiceImpl impl; + int32_t result = impl.CheckAppId(runtime, bundleName); + EXPECT_EQ(result, E_INVALID_PARAMETERS); +} + +/** + * @tc.name: ValidateAndProcessRuntimeData001 + * @tc.desc: invalid appId + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData001, TestSize.Level1) +{ + UnifiedData data; + std::shared_ptr record = std::make_shared(UDType::PLAIN_TEXT, "plainTextContent"); + data.AddRecord(record); + std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); + std::string tag = "this is a tag of test ValidateAndProcessRuntimeData001"; + properties->tag = tag; + data.SetProperties(std::move(properties)); + + std::vector<UnifiedData> dataSet = {data}; + std::shared_ptr<Runtime> runtime; + std::vector<UnifiedData> unifiedDataSet; + std::vector<std::string> deleteKeys; + QueryOption query; + + UdmfServiceImpl impl; + int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); + EXPECT_EQ(result, UDMF::E_DB_ERROR); +} + +/** + * @tc.name: ValidateAndProcessRuntimeData002 + * @tc.desc: Normal test + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData002, TestSize.Level1) +{ + UnifiedData data; + data.runtime_ = std::make_shared<Runtime>(); + data.runtime_->tokenId = 1; + std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); + data.AddRecord(record); + std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); + std::string tag = "this is a tag of test ValidateAndProcessRuntimeData002"; + properties->tag = tag; + data.SetProperties(std::move(properties)); + + std::vector<UnifiedData> dataSet = {data}; + std::shared_ptr<Runtime> runtime; + std::vector<UnifiedData> unifiedDataSet; + std::vector<std::string> deleteKeys; + QueryOption query; + query.tokenId = 1; + + UdmfServiceImpl impl; + int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); + EXPECT_EQ(result, UDMF::E_OK); +} + +/** + * @tc.name: ValidateAndProcessRuntimeData003 + * @tc.desc: invalid appId + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData003, TestSize.Level1) +{ + UnifiedData data; + data.runtime_ = std::make_shared<Runtime>(); + data.runtime_->tokenId = 1; + data.runtime_ = std::make_shared<Runtime>(); + data.runtime_->appId = "appId"; + std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); + data.AddRecord(record); + std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); + std::string tag = "this is a tag of test ValidateAndProcessRuntimeData003"; + properties->tag = tag; + data.SetProperties(std::move(properties)); + + std::vector<UnifiedData> dataSet = {data}; + std::shared_ptr<Runtime> runtime; + std::vector<UnifiedData> unifiedDataSet; + std::vector<std::string> deleteKeys; + QueryOption query; + query.tokenId = 2; + + UdmfServiceImpl impl; + int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); + EXPECT_EQ(result, UDMF::E_OK); +} + +/** + * @tc.name: ValidateAndProcessRuntimeData004 + * @tc.desc: invalid appId + * @tc.type: FUNC + */ +HWTEST_F(UdmfServiceImplTest, ValidateAndProcessRuntimeData004, TestSize.Level1) +{ + UnifiedData data1; + data1.runtime_ = std::make_shared<Runtime>(); + data1.runtime_->tokenId = 1; + data1.runtime_ = std::make_shared<Runtime>(); + data1.runtime_->appId = "appId"; + std::shared_ptr<UnifiedRecord> record = std::make_shared<PlainText>(UDType::PLAIN_TEXT, "plainTextContent"); + data1.AddRecord(record); + std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>(); + std::string tag = "this is a tag of test ValidateAndProcessRuntimeData004"; + properties->tag = tag; + data1.SetProperties(std::move(properties)); + + UnifiedData data2; + data2.runtime_ = std::make_shared<Runtime>(); + data2.runtime_->tokenId = 1; + data2.runtime_ = std::make_shared<Runtime>(); + data2.runtime_->appId = "appId"; + data2.AddRecord(record); + data2.SetProperties(std::move(properties)); + + std::vector<UnifiedData> dataSet = { data1, data2 }; + std::shared_ptr<Runtime> runtime; + std::vector<UnifiedData> unifiedDataSet; + std::vector<std::string> deleteKeys; + QueryOption query; + query.tokenId = 2; + + UdmfServiceImpl impl; + int32_t result = impl.ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); + EXPECT_EQ(result, UDMF::E_OK); +} }; // namespace DistributedDataTest }; // namespace OHOS::Test diff --git a/services/distributeddataservice/service/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index f8a62d82c..c908469df 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -19,6 +19,7 @@ config("module_public_config") { include_dirs = [ "${data_service_path}/adapter/include/communicator", "${data_service_path}/service/matrix/include", + "${data_service_path}/service/permission/include", "${data_service_path}/service/udmf/lifecycle", "${data_service_path}/service/udmf/permission", "${data_service_path}/service/udmf/preprocess", diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp index 828031589..361358841 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.cpp @@ -32,6 +32,8 @@ #include "udmf_utils.h" #include "utils/crypto.h" #include "uri_permission_manager_client.h" +#include "ipc_skeleton.h" +#include "bundle_mgr_interface.h" namespace OHOS { namespace UDMF { static constexpr int ID_LEN = 32; @@ -69,6 +71,7 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option UnifiedKey key(intention, bundleName, GenerateId()); Privilege privilege; privilege.tokenId = option.tokenId; + std::string appId = GetAppId(bundleName); Runtime runtime; runtime.key = key; runtime.privileges.emplace_back(privilege); @@ -80,6 +83,7 @@ int32_t PreProcessUtils::FillRuntimeInfo(UnifiedData &data, CustomOption &option runtime.tokenId = option.tokenId; runtime.sdkVersion = GetSdkVersionByToken(option.tokenId); runtime.visibility = option.visibility; + runtime.appId = appId; data.SetRuntime(runtime); return E_OK; } @@ -147,6 +151,32 @@ bool PreProcessUtils::GetNativeProcessNameByToken(int tokenId, std::string &proc return true; } +std::string PreProcessUtils::GetAppId(const std::string &bundleName) +{ + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy == nullptr) { + ZLOGE("Failed to get system ability mgr, bundleName:%{public}s", bundleName.c_str()); + return ""; + } + auto bundleMgrProxy = samgrProxy->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleMgrProxy == nullptr) { + ZLOGE("Failed to Get BMS SA, bundleName:%{public}s", bundleName.c_str()); + return ""; + } + auto bundleManager = iface_cast<AppExecFwk::IBundleMgr>(bundleMgrProxy); + if (bundleManager == nullptr) { + ZLOGE("Failed to get bundle manager, bundleName:%{public}s", bundleName.c_str()); + return ""; + } + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE; + std::string appId = bundleManager->GetAppIdByBundleName(bundleName, userId); + if (appId.empty()) { + ZLOGE("GetAppIdByBundleName failed, bundleName:%{public}s, uid:%{public}d", bundleName.c_str(), uid); + } + return appId; +} + std::string PreProcessUtils::GetLocalDeviceId() { auto info = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice(); diff --git a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h index 5d602b626..366dc17c6 100644 --- a/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h +++ b/services/distributeddataservice/service/udmf/preprocess/preprocess_utils.h @@ -44,6 +44,7 @@ public: static bool GetDetailsFromUData(const UnifiedData &data, UDDetails &details); static Status GetSummaryFromDetails(const UDDetails &details, Summary &summary); static bool GetSpecificBundleNameByTokenId(uint32_t tokenId, std::string &bundleName); + static std::string GetAppId(const std::string &bundleName); static sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); private: static bool CheckUriAuthorization(const std::vector<std::string>& uris, uint32_t tokenId); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index 1ceb45cc8..9f9fe21c4 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -428,10 +428,13 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi } std::shared_ptr<Runtime> runtime = data.GetRuntime(); if (runtime == nullptr) { + ZLOGE("Invalid parameter, runtime is nullptr."); return E_DB_ERROR; } - if (runtime->tokenId != query.tokenId && !HasDatahubPriviledge(bundleName)) { - ZLOGE("Update failed: tokenId mismatch"); + + if (runtime->tokenId != query.tokenId && !HasDatahubPriviledge(bundleName) && + CheckAppId(runtime, bundleName) != E_OK) { + ZLOGE("Update failed: tokenId or appId mismatch, bundleName: %{public}s", bundleName.c_str()); return E_INVALID_PARAMETERS; } runtime->lastModifiedTime = PreProcessUtils::GetTimestamp(); @@ -444,6 +447,21 @@ int32_t UdmfServiceImpl::UpdateData(const QueryOption &query, UnifiedData &unifi return E_OK; } +int32_t UdmfServiceImpl::CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &bundleName) +{ + if (runtime->appId.empty()) { + ZLOGE("Update failed: Invalid parameter, runtime->appId is empty"); + return E_INVALID_PARAMETERS; + } + std::string appId = PreProcessUtils::GetAppId(bundleName); + if (appId.empty() || appId != runtime->appId) { + ZLOGE("Update failed: runtime->appId %{public}s and bundleName appId %{public}s mismatch", + runtime->appId.c_str(), appId.c_str()); + return E_INVALID_PARAMETERS; + } + return E_OK; +} + int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<UnifiedData> &unifiedDataSet) { ZLOGD("start"); @@ -470,15 +488,10 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie } std::shared_ptr<Runtime> runtime; std::vector<std::string> deleteKeys; - for (const auto &data : dataSet) { - runtime = data.GetRuntime(); - if (runtime == nullptr) { - return E_DB_ERROR; - } - if (runtime->tokenId == query.tokenId) { - unifiedDataSet.push_back(data); - deleteKeys.push_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); - } + status = ValidateAndProcessRuntimeData(dataSet, runtime, unifiedDataSet, query, deleteKeys); + if (status != E_OK) { + ZLOGE("ValidateAndProcessRuntimeData failed."); + return status; } if (deleteKeys.empty()) { ZLOGE("No data to delete for this application"); @@ -492,6 +505,38 @@ int32_t UdmfServiceImpl::DeleteData(const QueryOption &query, std::vector<Unifie return E_OK; } +int32_t UdmfServiceImpl::ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, + std::shared_ptr<Runtime> runtime, std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, + std::vector<std::string> &deleteKeys) +{ + std::string appId; + bool isFirstInvoke = false; + for (const auto &data : dataSet) { + runtime = data.GetRuntime(); + if (runtime == nullptr) { + ZLOGE("Invalid runtime."); + return E_DB_ERROR; + } + if (runtime->tokenId != query.tokenId) { + if (runtime->appId.empty()) { + continue; + } + if (!isFirstInvoke) { + std::string bundleName; + PreProcessUtils::GetHapBundleNameByToken(query.tokenId, bundleName); + appId = PreProcessUtils::GetAppId(bundleName); + isFirstInvoke = true; + } + if (appId.empty() || appId != runtime->appId) { + continue; + } + } + unifiedDataSet.push_back(data); + deleteKeys.emplace_back(UnifiedKey(runtime->key.key).GetKeyCommonPrefix()); + } + return E_OK; +} + int32_t UdmfServiceImpl::GetSummary(const QueryOption &query, Summary &summary) { ZLOGD("start"); diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.h b/services/distributeddataservice/service/udmf/udmf_service_impl.h index 8f366463f..b728d8199 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.h +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.h @@ -80,6 +80,9 @@ private: std::string FindIntentionMap(const Intention &queryintention); bool IsValidOptionsNonDrag(UnifiedKey &key, const std::string &intention); bool IsValidInput(const QueryOption &query, UnifiedData &unifiedData, UnifiedKey &key); + int32_t ValidateAndProcessRuntimeData(const std::vector<UnifiedData> &dataSet, std::shared_ptr<Runtime> runtime, + std::vector<UnifiedData> &unifiedDataSet, const QueryOption &query, std::vector<std::string> &deleteKeys); + int32_t CheckAppId(std::shared_ptr<Runtime> runtime, const std::string &bundleName); class Factory { public: Factory(); -- Gitee