From 612bf08c77160d17b2e7a93594a3ad726b4c2734 Mon Sep 17 00:00:00 2001 From: archane Date: Thu, 31 Jul 2025 15:18:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?dataShare=E5=AE=89=E5=85=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: archane Change-Id: I860c71cca553d0c60a61819acd775627f82d040b --- .../service/data_share/BUILD.gn | 2 +- .../data_share/common/bundle_mgr_proxy.cpp | 2 +- .../service/data_share/common/context.h | 9 ++- .../service/data_share/common/db_delegate.cpp | 12 ++-- .../common/extension_ability_manager.cpp | 2 +- .../common/extension_connect_adaptor.cpp | 2 +- .../data_share/common/extension_mgr_proxy.cpp | 2 +- .../data_share/common/proxy_data_manager.cpp | 11 ++-- .../data_share/common/rdb_delegate.cpp | 14 +++-- .../service/data_share/common/rdb_delegate.h | 26 ++++---- .../data_share/common/scheduler_manager.cpp | 2 +- .../common/{uri_utils.cpp => utils.cpp} | 26 +++++++- .../common/{uri_utils.h => utils.h} | 19 +++++- .../data_share/data/published_data.cpp | 9 +-- .../data_share/data_provider_config.cpp | 4 +- .../service/data_share/data_provider_config.h | 2 +- .../data_share/data_share_db_config.cpp | 6 +- .../service/data_share/data_share_db_config.h | 2 +- .../data_share/data_share_profile_config.cpp | 16 +++-- .../data_share/data_share_service_impl.cpp | 13 ++-- .../data_share/data_share_service_impl.h | 2 +- .../data_share/data_share_service_stub.cpp | 2 +- .../data_share/data_share_silent_config.cpp | 2 +- ...d_config_from_data_proxy_node_strategy.cpp | 4 +- ...g_from_data_share_bundle_info_strategy.cpp | 2 +- .../general/check_is_data_proxy_strategy.cpp | 2 +- .../general/load_config_common_strategy.cpp | 2 +- .../load_config_data_info_strategy.cpp | 2 +- .../strategies/get_data_strategy.cpp | 4 +- .../strategies/publish_strategy.cpp | 2 +- .../strategies/rdb_notify_strategy.cpp | 2 +- .../strategies/subscribe_strategy.cpp | 2 +- .../strategies/template_strategy.cpp | 2 +- .../proxy_data_subscriber_manager.cpp | 2 +- .../published_data_subscriber_manager.cpp | 2 +- .../rdb_subscriber_manager.cpp | 5 +- .../service/test/BUILD.gn | 5 +- .../test/data_share_uri_utils_test.cpp | 63 +++++++++++++++++++ .../datashareserviceimpl_fuzzer/BUILD.gn | 2 +- .../datashareservicestub_fuzzer/BUILD.gn | 2 +- .../datasharesubscriber_fuzzer/BUILD.gn | 2 +- 41 files changed, 207 insertions(+), 87 deletions(-) rename services/distributeddataservice/service/data_share/common/{uri_utils.cpp => utils.cpp} (87%) rename services/distributeddataservice/service/data_share/common/{uri_utils.h => utils.h} (78%) create mode 100644 services/distributeddataservice/service/test/data_share_uri_utils_test.cpp diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 01d7396bd..788eceec2 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -60,7 +60,7 @@ ohos_source_set("data_share_service") { "common/rdb_delegate.cpp", "common/scheduler_manager.cpp", "common/seq_strategy.cpp", - "common/uri_utils.cpp", + "common/utils.cpp", "data/published_data.cpp", "data/resultset_json_formatter.cpp", "data/template_data.cpp", diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index 267a66c32..afd7e1f4f 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -23,7 +23,7 @@ #include "iservice_registry.h" #include "log_print.h" #include "system_ability_definition.h" -#include "uri_utils.h" +#include "utils.h" #include "ipc_skeleton.h" #include "hiview_fault_adapter.h" diff --git a/services/distributeddataservice/service/data_share/common/context.h b/services/distributeddataservice/service/data_share/common/context.h index d19ebf6d1..e7043d23f 100644 --- a/services/distributeddataservice/service/data_share/common/context.h +++ b/services/distributeddataservice/service/data_share/common/context.h @@ -16,6 +16,7 @@ #ifndef DATASHARESERVICE_CONTEXT_H #define DATASHARESERVICE_CONTEXT_H +#include #include #include #include @@ -34,7 +35,13 @@ class Context { public: explicit Context() {} explicit Context(const std::string &uri) : uri(uri) {} - virtual ~Context() = default; + virtual ~Context() + { + if (secretMetaKey.size() > 0) { + std::fill(secretMetaKey.begin(), secretMetaKey.end(), '\0'); + } + } + std::string uri; int32_t currentUserId = -1; int32_t visitedUserId = -1; diff --git a/services/distributeddataservice/service/data_share/common/db_delegate.cpp b/services/distributeddataservice/service/data_share/common/db_delegate.cpp index 06c408e75..9e77bcbb4 100644 --- a/services/distributeddataservice/service/data_share/common/db_delegate.cpp +++ b/services/distributeddataservice/service/data_share/common/db_delegate.cpp @@ -34,7 +34,7 @@ std::shared_ptr DBDelegate::Create(DistributedData::StoreMetaData &m { if (Account::GetInstance()->IsDeactivating(atoi(metaData.user.c_str()))) { ZLOGW("user %{public}s is deactivating, storeName: %{public}s", metaData.user.c_str(), - metaData.GetStoreAlias().c_str()); + StringUtils::GeneralAnonymous(metaData.GetStoreAlias()).c_str()); return nullptr; } std::shared_ptr store; @@ -63,7 +63,7 @@ std::shared_ptr DBDelegate::Create(DistributedData::StoreMetaData &m if (success) { return store; } - ZLOGE("creator failed, storeName: %{public}s", metaData.GetStoreAlias().c_str()); + ZLOGE("creator failed, storeName: %{public}s", StringUtils::GeneralAnonymous(metaData.GetStoreAlias()).c_str()); auto eraseFunc = [&metaData] (auto &, std::map> &stores) -> bool { stores.erase(metaData.storeId); @@ -167,13 +167,13 @@ void DBDelegate::EraseStoreCache(const int32_t tokenId) storesEncrypt_.Erase(tokenId); } -std::shared_ptr KvDBDelegate::GetInstance(const std::string &dir, - const std::shared_ptr &executors) +std::shared_ptr KvDBDelegate::GetInstance(const std::string &dir, + const std::shared_ptr &executors) { static std::shared_ptr delegate = nullptr; static std::mutex mutex; std::lock_guard lock(mutex); - if (delegate == nullptr && executors != nullptr) { + if (delegate == nullptr && executors != nullptr) { delegate = std::make_shared(dir, executors); } return delegate; @@ -215,4 +215,4 @@ const std::string &KvData::GetId() const } KvData::KvData(const Id &id) : id(DistributedData::Serializable::Marshall(id)) {} -} // namespace OHOS::DataShare +} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/extension_ability_manager.cpp b/services/distributeddataservice/service/data_share/common/extension_ability_manager.cpp index 2328285df..fa1011fde 100644 --- a/services/distributeddataservice/service/data_share/common/extension_ability_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/extension_ability_manager.cpp @@ -19,7 +19,7 @@ #include "extension_mgr_proxy.h" #include "log_print.h" #include "datashare_errno.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { ExtensionAbilityManager &ExtensionAbilityManager::GetInstance() diff --git a/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp index 5578adbc1..e89b5734c 100644 --- a/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp @@ -24,7 +24,7 @@ #include "extension_ability_info.h" #include "extension_mgr_proxy.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { ExtensionConnectAdaptor::ExtensionConnectAdaptor() : data_(std::make_shared>(1)) diff --git a/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp index dacd6ce88..fce328cda 100644 --- a/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp @@ -23,7 +23,7 @@ #include "log_print.h" #include "system_ability_definition.h" #include "want.h" -#include "uri_utils.h" +#include "utils.h" #include "datashare_errno.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/common/proxy_data_manager.cpp b/services/distributeddataservice/service/data_share/common/proxy_data_manager.cpp index 61f7cb37b..18a7b5363 100644 --- a/services/distributeddataservice/service/data_share/common/proxy_data_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/proxy_data_manager.cpp @@ -20,7 +20,7 @@ #include "dataproxy_handle_common.h" #include "log_print.h" #include "proxy_data_manager.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -201,7 +201,8 @@ int32_t PublishedProxyData::InsertProxyData(std::shared_ptr kvDele kvDelegate->Get(KvDBDelegate::PROXYDATA_TABLE, listFilter, "{}", listQueryResult); if (!listQueryResult.empty()) { if (!ProxyDataListNode::Unmarshall(listQueryResult, proxyDataList)) { - ZLOGE("ProxyDataListNode unmarshall failed, %{private}s", listQueryResult.c_str()); + ZLOGE("ProxyDataListNode unmarshall failed, %{public}s", + StringUtils::GeneralAnonymous(listQueryResult).c_str()); return INNER_ERROR; } } @@ -251,7 +252,7 @@ int32_t PublishedProxyData::Query(const std::string &uri, const BundleInfo &call } ProxyDataNode data; if (!ProxyDataNode::Unmarshall(queryResult, data)) { - ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + ZLOGE("Unmarshall failed, %{public}s", StringUtils::GeneralAnonymous(queryResult).c_str()); return INNER_ERROR; } DataShareProxyData tempProxyData(data.proxyData.uri, data.proxyData.value, data.proxyData.allowList); @@ -290,7 +291,7 @@ int32_t PublishedProxyData::Upsert(const DataShareProxyData &proxyData, const Bu } else { ProxyDataNode oldData; if (!ProxyDataNode::Unmarshall(queryResult, oldData)) { - ZLOGE("ProxyDataNode unmarshall failed, %{private}s", queryResult.c_str()); + ZLOGE("ProxyDataNode unmarshall failed, %{public}s", StringUtils::GeneralAnonymous(queryResult).c_str()); return INNER_ERROR; } if (callerBundleInfo.tokenId != oldData.tokenId) { @@ -384,7 +385,7 @@ int32_t ProxyDataList::Query(uint32_t tokenId, int32_t userId, std::vector RdbDelegate::InsertEx(const std::string &tableName, ValuesBucket bucket = RdbDataShareAdapter::RdbUtils::ToValuesBucket(valuesBucket); int ret = store_->Insert(rowId, tableName, bucket); if (ret != E_OK) { - ZLOGE("Insert failed %{public}s %{public}d", tableName.c_str(), ret); + ZLOGE("Insert failed %{public}s %{public}d", StringUtils::GeneralAnonymous(tableName).c_str(), ret); RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_CALL_RDB, RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::INSERT_RDB_ERROR); if (ret == E_SQLITE_ERROR) { @@ -200,7 +202,7 @@ std::pair RdbDelegate::UpdateEx( RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); int ret = store_->Update(changeCount, bucket, predicates); if (ret != E_OK) { - ZLOGE("Update failed %{public}s %{public}d", tableName.c_str(), ret); + ZLOGE("Update failed %{public}s %{public}d", StringUtils::GeneralAnonymous(tableName).c_str(), ret); RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_CALL_RDB, RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::UPDATE_RDB_ERROR); if (ret == E_SQLITE_ERROR) { @@ -222,7 +224,7 @@ std::pair RdbDelegate::DeleteEx(const std::string &tableName, RdbPredicates predicates = RdbDataShareAdapter::RdbUtils::ToPredicates(predicate, tableName); int ret = store_->Delete(changeCount, predicates); if (ret != E_OK) { - ZLOGE("Delete failed %{public}s %{public}d", tableName.c_str(), ret); + ZLOGE("Delete failed %{public}s %{public}d", StringUtils::GeneralAnonymous(tableName).c_str(), ret); RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_CALL_RDB, RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::DELETE_RDB_ERROR); if (ret == E_SQLITE_ERROR) { @@ -252,7 +254,7 @@ std::pair> RdbDelegate::Query(const std if (resultSet == nullptr) { RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_CALL_RDB, RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::QUERY_RDB_ERROR); - ZLOGE("Query failed %{public}s, pid: %{public}d", tableName.c_str(), callingPid); + ZLOGE("Query failed %{public}s, pid: %{public}d", StringUtils::GeneralAnonymous(tableName).c_str(), callingPid); resultSetCount--; return std::make_pair(E_ERROR, nullptr); } diff --git a/services/distributeddataservice/service/data_share/common/rdb_delegate.h b/services/distributeddataservice/service/data_share/common/rdb_delegate.h index 3fe8b3dce..9c8fcd7dd 100644 --- a/services/distributeddataservice/service/data_share/common/rdb_delegate.h +++ b/services/distributeddataservice/service/data_share/common/rdb_delegate.h @@ -24,17 +24,17 @@ #include "rdb_errno.h" #include "rdb_helper.h" #include "rdb_store.h" -#include "uri_utils.h" +#include "utils.h" #include "rdb_utils.h" namespace OHOS::DataShare { using namespace OHOS::NativeRdb; class RdbDelegate final : public DBDelegate { public: - explicit RdbDelegate(); + explicit RdbDelegate(); ~RdbDelegate(); - bool Init(const DistributedData::StoreMetaData &meta, int version, - bool registerFunction, const std::string &extUri, const std::string &backup) override; + bool Init(const DistributedData::StoreMetaData &meta, int version, + bool registerFunction, const std::string &extUri, const std::string &backup) override; std::pair> Query(const std::string &tableName, const DataSharePredicates &predicates, const std::vector &columns, int32_t callingPid, uint32_t callingTokenId) override; @@ -60,15 +60,15 @@ private: static constexpr int RETRY = 3; static constexpr const char *DUAL_WRITE = "dualWrite"; static constexpr const char *PERIODIC = "periodic"; - uint32_t tokenId_ = 0; - std::string bundleName_ = ""; - std::string storeName_ = ""; - int32_t haMode_ = 0; - std::string extUri_ = ""; - std::string backup_ = ""; - std::string user_ = ""; - std::mutex initMutex_; - bool isInited_ = false; + uint32_t tokenId_ = 0; + std::string bundleName_ = ""; + std::string storeName_ = ""; + int32_t haMode_ = 0; + std::string extUri_ = ""; + std::string backup_ = ""; + std::string user_ = ""; + std::mutex initMutex_; + bool isInited_ = false; }; class DefaultOpenCallback : public RdbOpenCallback { public: diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index deb94a7b0..79f19c601 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -20,7 +20,7 @@ #include "log_print.h" #include "timer_info.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" #include "log_debug.h" diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.cpp b/services/distributeddataservice/service/data_share/common/utils.cpp similarity index 87% rename from services/distributeddataservice/service/data_share/common/uri_utils.cpp rename to services/distributeddataservice/service/data_share/common/utils.cpp index 065dfd75e..6f30af21c 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.cpp +++ b/services/distributeddataservice/service/data_share/common/utils.cpp @@ -12,9 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#define LOG_TAG "URIUtils" +#define LOG_TAG "Utils" -#include "uri_utils.h" +#include "utils.h" #include @@ -24,6 +24,10 @@ #include "utils/anonymous.h" namespace OHOS::DataShare { +// Size of the characters that need to be left for both head and tail. +constexpr int32_t REVEAL_SIZE = 4; +constexpr const char *REPLACE_CHAIN = "***"; +constexpr const char *DEFAULT_ANONYMOUS = "******"; bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo) { Uri uriTemp(uri); @@ -147,7 +151,7 @@ std::pair URIUtils::Strtoul(const std::string &str) } char* end = nullptr; errno = 0; - data = strtoul(str.c_str(), &end, 10); + data = strtoul(str.c_str(), &end, END_LENGTH); if (errno == ERANGE || end == nullptr || end == str || *end != '\0') { return std::make_pair(false, data); } @@ -180,4 +184,20 @@ std::map URIUtils::GetQueryParams(const std::string& u } return params; } + +std::string StringUtils::GeneralAnonymous(const std::string &name) +{ + // To short to be partial anonymized + if (name.length() <= REVEAL_SIZE) { + return DEFAULT_ANONYMOUS; + } + + // only leave HEAD + if (name.length() <= (REVEAL_SIZE + REVEAL_SIZE)) { + return (name.substr(0, REVEAL_SIZE) + REPLACE_CHAIN); + } + // leave 4 char at head and tail respectively + return (name.substr(0, REVEAL_SIZE) + REPLACE_CHAIN + name.substr(name.length() - REVEAL_SIZE, REVEAL_SIZE)); +} + } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/uri_utils.h b/services/distributeddataservice/service/data_share/common/utils.h similarity index 78% rename from services/distributeddataservice/service/data_share/common/uri_utils.h rename to services/distributeddataservice/service/data_share/common/utils.h index 8f2298506..4eceac72f 100644 --- a/services/distributeddataservice/service/data_share/common/uri_utils.h +++ b/services/distributeddataservice/service/data_share/common/utils.h @@ -35,6 +35,24 @@ struct UriConfig { std::string scheme; }; +class StringUtils { +public: + /** + * @brief Anonymizes the input string by preserving specified length at head/tail + * and replacing the middle part with placeholder + * @param name Original string to be processed (non-empty assumed) + * @return std::string Anonymized result according to length rules: + * 1. Length 鈮?4: Returns DEFAULT_ANONYMOUS ("******") + * 2. 4 < Length 鈮?8: Returns first 4 chars + REPLACE_CHAIN ("***") + * 3. Length > 8: Returns first 4 chars + REPLACE_CHAIN + last 4 chars + * @example + * "abcdefghi" -> "abcd***fghi" + * "12345" -> "1234***" + * "AB" -> "******" + */ + static std::string GeneralAnonymous(const std::string &name); +}; + class URIUtils { public: static bool GetInfoFromURI(const std::string &uri, UriInfo &uriInfo); @@ -67,7 +85,6 @@ private: PARAM_SIZE }; static constexpr int32_t END_LENGTH = 10; - static constexpr const char *DEFAULT_ANONYMOUS = "******"; }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_URI_UTILS_H diff --git a/services/distributeddataservice/service/data_share/data/published_data.cpp b/services/distributeddataservice/service/data_share/data/published_data.cpp index 359db675e..d8c6960a2 100644 --- a/services/distributeddataservice/service/data_share/data/published_data.cpp +++ b/services/distributeddataservice/service/data_share/data/published_data.cpp @@ -18,6 +18,7 @@ #include "log_print.h" #include "subscriber_managers/published_data_subscriber_manager.h" #include "utils/base64_utils.h" +#include "utils.h" namespace OHOS::DataShare { bool PublishedData::HasVersion() const @@ -151,7 +152,7 @@ int32_t PublishedData::Query(const std::string &filter, PublishedDataNode::Data } PublishedDataNode data; if (!PublishedDataNode::Unmarshall(queryResult, data)) { - ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + ZLOGE("Unmarshall failed, %{public}s", StringUtils::GeneralAnonymous(queryResult).c_str()); return E_ERROR; } publishedData = std::move(data.value); @@ -235,12 +236,12 @@ void PublishedData::UpdateTimestamp( int32_t status = delegate->Get(KvDBDelegate::DATA_TABLE, Id(GenId(key, bundleName, subscriberId), userId), queryResult); if (status != E_OK) { - ZLOGE("db Get failed, %{private}s %{public}d", queryResult.c_str(), status); + ZLOGE("db Get failed, %{public}s %{public}d", StringUtils::GeneralAnonymous(queryResult).c_str(), status); return; } PublishedDataNode data; if (!PublishedDataNode::Unmarshall(queryResult, data)) { - ZLOGE("Unmarshall failed, %{private}s", queryResult.c_str()); + ZLOGE("Unmarshall failed, %{public}s", StringUtils::GeneralAnonymous(queryResult).c_str()); return; } auto now = time(nullptr); @@ -251,7 +252,7 @@ void PublishedData::UpdateTimestamp( data.timestamp = now; auto [errorCode, count] = delegate->Upsert(KvDBDelegate::DATA_TABLE, PublishedData(data)); if (errorCode == E_OK) { - ZLOGI("update timestamp %{private}s", data.key.c_str()); + ZLOGI("update timestamp %{public}s", StringUtils::GeneralAnonymous(data.key).c_str()); } } diff --git a/services/distributeddataservice/service/data_share/data_provider_config.cpp b/services/distributeddataservice/service/data_share/data_provider_config.cpp index 11c5328cf..f6f13b3d3 100644 --- a/services/distributeddataservice/service/data_share/data_provider_config.cpp +++ b/services/distributeddataservice/service/data_share/data_provider_config.cpp @@ -27,7 +27,7 @@ #include "log_print.h" #include "strategies/general/load_config_common_strategy.h" #include "tokenid_kit.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -243,7 +243,7 @@ std::pair DataProviderConfig::GetProvider if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId) || (result == Security::AccessToken::RET_SUCCESS && !IsInExtList(tokenInfo.bundleName))) { ZLOGE("The URI in the extension, is not allowed for silent access.! ret: %{public}d, bundleName: %{public}s," - "uri: %{public}s", ret, tokenInfo.bundleName.c_str(), providerInfo_.uri.c_str()); + "uri: %{public}s", ret, tokenInfo.bundleName.c_str(), URIUtils::Anonymous(providerInfo_.uri).c_str()); } ret = GetFromExtension(); if (ret != E_OK) { diff --git a/services/distributeddataservice/service/data_share/data_provider_config.h b/services/distributeddataservice/service/data_share/data_provider_config.h index 99099ce8a..c1acce432 100644 --- a/services/distributeddataservice/service/data_share/data_provider_config.h +++ b/services/distributeddataservice/service/data_share/data_provider_config.h @@ -22,7 +22,7 @@ #include "bundle_mgr_proxy.h" #include "data_share_profile_config.h" #include "hap_module_info.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { using ExtensionAbility = OHOS::AppExecFwk::ExtensionAbilityInfo; diff --git a/services/distributeddataservice/service/data_share/data_share_db_config.cpp b/services/distributeddataservice/service/data_share/data_share_db_config.cpp index fb72f0c29..dc25bcafd 100644 --- a/services/distributeddataservice/service/data_share/data_share_db_config.cpp +++ b/services/distributeddataservice/service/data_share/data_share_db_config.cpp @@ -26,7 +26,7 @@ #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" #include "ipc_skeleton.h" @@ -74,8 +74,8 @@ std::tuple> Dat auto [errCode, metaData] = GetMetaData(dbConfig); if (errCode != E_OK) { ZLOGE("DB not exist,bundleName:%{public}s,storeName:%{public}s,user:%{public}d,err:%{public}d,uri:%{public}s", - dbConfig.bundleName.c_str(), dbConfig.storeName.c_str(), dbConfig.userId, errCode, - URIUtils::Anonymous(dbConfig.uri).c_str()); + dbConfig.bundleName.c_str(), StringUtils::GeneralAnonymous(dbConfig.storeName).c_str(), dbConfig.userId, + errCode, URIUtils::Anonymous(dbConfig.uri).c_str()); RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_MATEDATA_EXISTS, RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::META_DATA_NOT_EXISTS); return std::make_tuple(errCode, metaData, nullptr); diff --git a/services/distributeddataservice/service/data_share/data_share_db_config.h b/services/distributeddataservice/service/data_share/data_share_db_config.h index 88efe930e..ff4e32b9f 100644 --- a/services/distributeddataservice/service/data_share/data_share_db_config.h +++ b/services/distributeddataservice/service/data_share/data_share_db_config.h @@ -23,7 +23,7 @@ #include "db_delegate.h" #include "extension_ability_info.h" #include "metadata/store_meta_data.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { class DataShareDbConfig { diff --git a/services/distributeddataservice/service/data_share/data_share_profile_config.cpp b/services/distributeddataservice/service/data_share/data_share_profile_config.cpp index 354a196ed..de4a8f657 100644 --- a/services/distributeddataservice/service/data_share/data_share_profile_config.cpp +++ b/services/distributeddataservice/service/data_share/data_share_profile_config.cpp @@ -26,7 +26,7 @@ #include "bundle_mgr_proxy.h" #include "datashare_errno.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" #include "log_debug.h" @@ -36,6 +36,7 @@ constexpr const char *PROFILE_FILE_PREFIX = "$profile:"; constexpr const char *SEPARATOR = "/"; static constexpr int PATH_SIZE = 2; static constexpr int MAX_ALLOWLIST_COUNT = 256; +static constexpr size_t MAX_FILE_SIZE = 10 * 1024 * 1024; const size_t PROFILE_PREFIX_LEN = strlen(PROFILE_FILE_PREFIX); bool Config::Marshal(json &node) const { @@ -281,12 +282,13 @@ std::string DataShareProfileConfig::GetResFromResMgr( std::string resPath; RState ret = resMgr.GetProfileByName(profileName.c_str(), resPath); if (ret != RState::SUCCESS) { - ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret, profileName.c_str()); + ZLOGE("profileName not found, ret is %{public}d, profileName is %{public}s", ret, + StringUtils::GeneralAnonymous(profileName).c_str()); return profileInfo; } std::string profile = ReadProfile(resPath); if (profile.empty()) { - ZLOGE("Read profile failed, resPath is %{public}s", resPath.c_str()); + ZLOGE("Read profile failed, resPath is %{public}s", URIUtils::Anonymous(resPath).c_str()); return profileInfo; } return profile; @@ -298,7 +300,8 @@ bool DataShareProfileConfig::IsFileExisted(const std::string &filePath) return false; } if (access(filePath.c_str(), F_OK) != 0) { - ZLOGE("can not access file, errno is %{public}d, filePath is %{public}s", errno, filePath.c_str()); + ZLOGE("can not access file, errno is %{public}d, filePath is %{public}s", errno, + URIUtils::Anonymous(filePath).c_str()); return false; } return true; @@ -319,7 +322,10 @@ std::string DataShareProfileConfig::ReadProfile(const std::string &resPath) tmp << in.rdbuf(); std::string content = tmp.str(); if (content.empty()) { - ZLOGE("the file is empty, resPath is %{public}s", resPath.c_str()); + ZLOGE("the file is empty, resPath is %{public}s", URIUtils::Anonymous(resPath).c_str()); + return ""; + } else if (content.length() > MAX_FILE_SIZE) { + ZLOGE("the file is too large, resPath is %{public}s,", URIUtils::Anonymous(resPath).c_str()); return ""; } return content; 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 9fca1eee9..2632e5064 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -44,7 +44,7 @@ #include "ipc_skeleton.h" #include "iservice_registry.h" #include "log_print.h" -#include "common/uri_utils.h" +#include "common/utils.h" #include "metadata/auto_launch_meta_data.h" #include "metadata/meta_data_manager.h" #include "matching_skills.h" @@ -868,15 +868,16 @@ int32_t DataShareServiceImpl::OnAppUpdate(const std::string &bundleName, int32_t void DataShareServiceImpl::NotifyObserver(const std::string &uri) { - ZLOGD_MACRO("%{private}s try notified", uri.c_str()); + auto anonymous = URIUtils::Anonymous(uri); + ZLOGD_MACRO("%{private}s try notified", anonymous.c_str()); auto context = std::make_shared(uri); if (!GetCallerBundleName(context->callerBundleName)) { - ZLOGE("get bundleName error, %{private}s", uri.c_str()); + ZLOGE("get bundleName error, %{private}s", anonymous.c_str()); return; } auto ret = rdbNotifyStrategy_.Execute(context); if (ret) { - ZLOGI("%{private}s start notified", uri.c_str()); + ZLOGI("%{private}s start notified", anonymous.c_str()); RdbSubscriberManager::GetInstance().Emit(uri, context); } } @@ -1191,7 +1192,7 @@ std::pair DataShareServiceImpl::ExecuteEx(const std::string &u auto [code, metaData, dbDelegate] = dbConfig.GetDbConfig(config); if (code != E_OK) { ZLOGE("Get dbConfig fail,bundleName:%{public}s,tableName:%{public}s,tokenId:0x%{public}x, uri:%{public}s", - providerInfo.bundleName.c_str(), providerInfo.tableName.c_str(), tokenId, + providerInfo.bundleName.c_str(), StringUtils::GeneralAnonymous(providerInfo.tableName).c_str(), tokenId, URIUtils::Anonymous(providerInfo.uri).c_str()); return std::make_pair(code, 0); } @@ -1260,7 +1261,7 @@ int32_t DataShareServiceImpl::GetBMSAndMetaDataStatus(const std::string &uri, co auto [code, metaData] = dbConfig.GetMetaData(dbArg); if (code != E_OK) { ZLOGE("Get metaData fail,bundleName:%{public}s,tableName:%{public}s,tokenId:0x%{public}x, uri:%{public}s", - calledInfo.bundleName.c_str(), DistributedData::Anonymous::Change(calledInfo.tableName).c_str(), tokenId, + calledInfo.bundleName.c_str(), StringUtils::GeneralAnonymous(calledInfo.tableName).c_str(), tokenId, URIUtils::Anonymous(calledInfo.uri).c_str()); return E_METADATA_NOT_EXISTS; } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index 04448e729..41b2965e5 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -39,7 +39,7 @@ #include "rdb_notify_strategy.h" #include "subscribe_strategy.h" #include "template_strategy.h" -#include "uri_utils.h" +#include "utils.h" #include "visibility.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp index a1548b4f6..35586c108 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_stub.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_stub.cpp @@ -28,7 +28,7 @@ #include "itypes_util.h" #include "log_print.h" #include "qos.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" #include "dataproxy_handle_common.h" diff --git a/services/distributeddataservice/service/data_share/data_share_silent_config.cpp b/services/distributeddataservice/service/data_share/data_share_silent_config.cpp index 2f71fd3ac..eee4709aa 100644 --- a/services/distributeddataservice/service/data_share/data_share_silent_config.cpp +++ b/services/distributeddataservice/service/data_share/data_share_silent_config.cpp @@ -23,7 +23,7 @@ #include "hap_token_info.h" #include "ipc_skeleton.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp index 60d643d4b..1810afacd 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp @@ -17,7 +17,7 @@ #include "load_config_from_data_proxy_node_strategy.h" #include "bundle_mgr_proxy.h" -#include "common/uri_utils.h" +#include "common/utils.h" #include "data_share_profile_config.h" #include "datashare_errno.h" #include "log_print.h" @@ -71,7 +71,7 @@ bool LoadConfigFromDataProxyNodeStrategy::operator()(std::shared_ptr co return true; } context->errCode = E_URI_NOT_EXIST; - ZLOGI("not find DataProperties! %{private}s is private", context->uri.c_str()); + ZLOGI("not find DataProperties! %{public}s is private", URIUtils::Anonymous(context->uri).c_str()); return false; } diff --git a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp index e5f3f5337..cd5a5aa8a 100644 --- a/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp @@ -20,7 +20,7 @@ #include "data_share_profile_config.h" #include "datashare_errno.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp index 6842ed8bb..8d94c9e61 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp @@ -14,7 +14,7 @@ */ #include "check_is_data_proxy_strategy.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { bool CheckIsDataProxyStrategy::operator()(std::shared_ptr context) 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 9a64d37c7..042ba7796 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 @@ -20,7 +20,7 @@ #include "hap_token_info.h" #include "ipc_skeleton.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { using namespace OHOS::DistributedData; diff --git a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp index bbd1bd332..d3966097e 100644 --- a/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/general/load_config_data_info_strategy.cpp @@ -22,7 +22,7 @@ #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "rdb_errno.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { LoadConfigDataInfoStrategy::LoadConfigDataInfoStrategy() diff --git a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp index 1b8307812..a532d7c52 100644 --- a/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/get_data_strategy.cpp @@ -20,7 +20,7 @@ #include "data_proxy/load_config_from_data_proxy_node_strategy.h" #include "general/load_config_common_strategy.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { Data GetDataStrategy::Execute(std::shared_ptr context, int &errorCode) @@ -40,7 +40,7 @@ Data GetDataStrategy::Execute(std::shared_ptr context, int &errorCode) Data data; for (auto &item : result) { if (!CheckPermission(context, item.value.key)) { - ZLOGI("uri: %{private}s not allowed", context->uri.c_str()); + ZLOGI("uri: %{public}s not allowed", URIUtils::Anonymous(context->uri).c_str()); continue; } if (item.GetVersion() > data.version_) { diff --git a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp index fd277bd4a..10d4ecba2 100644 --- a/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/publish_strategy.cpp @@ -22,7 +22,7 @@ #include "general/permission_strategy.h" #include "log_print.h" #include "published_data.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { int32_t PublishStrategy::Execute(std::shared_ptr context, const PublishedDataItem &item) diff --git a/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp index be4d1c548..552617a4e 100644 --- a/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp @@ -20,7 +20,7 @@ #include "general/load_config_data_info_strategy.h" #include "general/load_config_from_bundle_info_strategy.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" #include "log_debug.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp index 376756243..36b15b452 100644 --- a/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/subscribe_strategy.cpp @@ -21,7 +21,7 @@ #include "general/load_config_data_info_strategy.h" #include "general/permission_strategy.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { int32_t SubscribeStrategy::Execute(std::shared_ptr context, std::function process) diff --git a/services/distributeddataservice/service/data_share/strategies/template_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/template_strategy.cpp index 7c3439e5c..e771c6151 100644 --- a/services/distributeddataservice/service/data_share/strategies/template_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/template_strategy.cpp @@ -20,7 +20,7 @@ #include "datashare_errno.h" #include "general/load_config_common_strategy.h" #include "log_print.h" -#include "uri_utils.h" +#include "utils.h" namespace OHOS::DataShare { int32_t TemplateStrategy::Execute(std::shared_ptr context, std::function process) diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/proxy_data_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/proxy_data_subscriber_manager.cpp index 467a8f5e5..cea47b58b 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/proxy_data_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/proxy_data_subscriber_manager.cpp @@ -21,7 +21,7 @@ #include "ipc_skeleton.h" #include "log_print.h" #include "proxy_data_manager.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp index 466fd0545..46ddd990c 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp @@ -22,7 +22,7 @@ #include "general/load_config_data_info_strategy.h" #include "log_print.h" #include "published_data.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp index 6c33d72e0..79ccfd8a6 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp @@ -24,7 +24,7 @@ #include "log_print.h" #include "scheduler_manager.h" #include "template_data.h" -#include "uri_utils.h" +#include "utils.h" #include "utils/anonymous.h" namespace OHOS::DataShare { @@ -116,7 +116,8 @@ int RdbSubscriberManager::Add(const Key &key, const sptr { int result = E_OK; rdbCache_.Compute(key, [&observer, &context, executorPool, this](const auto &key, auto &value) { - ZLOGI("add subscriber, uri %{private}s tokenId 0x%{public}x", key.uri.c_str(), context->callerTokenId); + ZLOGI("add subscriber, uri %{public}s tokenId 0x%{public}x", + URIUtils::Anonymous(key.uri).c_str(), context->callerTokenId); auto callerTokenId = IPCSkeleton::GetCallingTokenID(); auto callerPid = IPCSkeleton::GetCallingPid(); value.emplace_back(observer, context->callerTokenId, callerTokenId, callerPid, context->visitedUserId); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 791ef3b46..07a022bc4 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1175,7 +1175,7 @@ ohos_unittest("DataShareServiceImplTest") { "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", "${data_service_path}/service/data_share/common/seq_strategy.cpp", - "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/common/utils.cpp", "${data_service_path}/service/data_share/data/published_data.cpp", "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", "${data_service_path}/service/data_share/data/template_data.cpp", @@ -1219,6 +1219,7 @@ ohos_unittest("DataShareServiceImplTest") { "data_share_types_util_test.cpp", "kv_dalegate_test.cpp", "mock/account_delegate_mock.cpp", + "data_share_uri_utils_test.cpp", ] configs = [ ":module_private_config" ] @@ -1318,7 +1319,7 @@ ohos_unittest("DataShareServiceImplMockTest") { "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", "${data_service_path}/service/data_share/common/seq_strategy.cpp", - "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/common/utils.cpp", "${data_service_path}/service/data_share/data/published_data.cpp", "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", "${data_service_path}/service/data_share/data/template_data.cpp", diff --git a/services/distributeddataservice/service/test/data_share_uri_utils_test.cpp b/services/distributeddataservice/service/test/data_share_uri_utils_test.cpp new file mode 100644 index 000000000..baccbc53a --- /dev/null +++ b/services/distributeddataservice/service/test/data_share_uri_utils_test.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "DatashareURIUtilsTest" +#include "utils.h" + +#include +#include + +namespace OHOS { +namespace DataShare { +using namespace testing::ext; +using namespace OHOS::DataShare; +class DatashareURIUtilsTest : public testing::Test { +public: + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp(){}; + void TearDown(){}; +}; + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_001, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous("DataShareTable"), "Data***able"); +} + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_002, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous("1bc"), "******"); +} + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_003, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous("contact"), "cont***"); +} + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_004, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous("name"), "******"); +} + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_005, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous(""), "******"); +} + +HWTEST_F(DatashareURIUtilsTest, GeneralAnonymous_006, TestSize.Level0) +{ + EXPECT_EQ(StringUtils::GeneralAnonymous("store.db"), "stor***"); +} +} // namespace DataShare +} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn index 17ca41e7e..baa4fa6ef 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn @@ -64,7 +64,7 @@ ohos_fuzztest("DataShareServiceImplFuzzTest") { "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", "${data_service_path}/service/data_share/common/seq_strategy.cpp", - "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/common/utils.cpp", "${data_service_path}/service/data_share/data/published_data.cpp", "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", "${data_service_path}/service/data_share/data/template_data.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn index 8f2dc9fbb..582498375 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn @@ -62,7 +62,7 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", "${data_service_path}/service/data_share/common/seq_strategy.cpp", - "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/common/utils.cpp", "${data_service_path}/service/data_share/data/published_data.cpp", "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", "${data_service_path}/service/data_share/data/template_data.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn index 42efc4c2c..ddfb2cd7b 100644 --- a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn @@ -64,7 +64,7 @@ ohos_fuzztest("DataShareSubscriberFuzzTest") { "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", "${data_service_path}/service/data_share/common/seq_strategy.cpp", - "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/common/utils.cpp", "${data_service_path}/service/data_share/data/published_data.cpp", "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", "${data_service_path}/service/data_share/data/template_data.cpp", -- Gitee From c07fdd6200d08dff3aea1264c3f1d596e48735e1 Mon Sep 17 00:00:00 2001 From: archane Date: Fri, 8 Aug 2025 09:29:42 +0000 Subject: [PATCH 2/2] update services/distributeddataservice/service/data_share/common/utils.h. Signed-off-by: archane --- .../distributeddataservice/service/data_share/common/utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/utils.h b/services/distributeddataservice/service/data_share/common/utils.h index 4eceac72f..de55c6b03 100644 --- a/services/distributeddataservice/service/data_share/common/utils.h +++ b/services/distributeddataservice/service/data_share/common/utils.h @@ -42,8 +42,8 @@ public: * and replacing the middle part with placeholder * @param name Original string to be processed (non-empty assumed) * @return std::string Anonymized result according to length rules: - * 1. Length 鈮?4: Returns DEFAULT_ANONYMOUS ("******") - * 2. 4 < Length 鈮?8: Returns first 4 chars + REPLACE_CHAIN ("***") + * 1. Length <= 4: Returns DEFAULT_ANONYMOUS ("******") + * 2. 4 < Length <= 8: Returns first 4 chars + REPLACE_CHAIN ("***") * 3. Length > 8: Returns first 4 chars + REPLACE_CHAIN + last 4 chars * @example * "abcdefghi" -> "abcd***fghi" -- Gitee