From 51162f6730e35af9cf432de1bd216481173bf60c Mon Sep 17 00:00:00 2001 From: zhrenqiang Date: Thu, 4 Sep 2025 16:43:35 +0800 Subject: [PATCH] fix check shortcuts map value type Change-Id: I37f0c841f02668f316f49706b9e2b36e6ae1eaeb Signed-off-by: zhrenqiang --- .../appexecfwk_base/include/json_util.h | 35 +++++++++++++++++-- .../appexecfwk_base/src/bms_json_util.cpp | 14 ++++++++ .../appexecfwk_base/src/shortcut_info.cpp | 10 +++--- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/interfaces/inner_api/appexecfwk_base/include/json_util.h b/interfaces/inner_api/appexecfwk_base/include/json_util.h index f9921fd2b4..d49f523744 100644 --- a/interfaces/inner_api/appexecfwk_base/include/json_util.h +++ b/interfaces/inner_api/appexecfwk_base/include/json_util.h @@ -48,6 +48,7 @@ public: static void GetBoolValueIfFindKey(const nlohmann::json &jsonObject, const nlohmann::detail::iter_impl &end, const std::string &key, bool &data, bool isNecessary, int32_t &parseResult); + static bool CheckMapValueType(JsonType valueType, const nlohmann::json &value); }; template @@ -150,7 +151,7 @@ void GetValueIfFindKey(const nlohmann::json &jsonObject, const nlohmann::detail: return; } if (isNecessary) { - APP_LOGE("profile prop %{public}s mission", key.c_str()); + APP_LOGE("profile prop %{public}s missing", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP; } } @@ -205,7 +206,37 @@ void GetBigStringIfFindKey(const nlohmann::json &jsonObject, return; } if (isNecessary) { - APP_LOGE("profile prop %{public}s mission", key.c_str()); + APP_LOGE("profile prop %{public}s missing", key.c_str()); + parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP; + } +} + +template +void GetMapValueIfFindKey(const nlohmann::json &jsonObject, + const nlohmann::detail::iter_impl &end, const std::string &key, dataType &data, + bool isNecessary, int32_t &parseResult, JsonType valueType) +{ + if (parseResult != ERR_OK) { + return; + } + if (jsonObject.find(key) != end) { + if (!jsonObject.at(key).is_object()) { + APP_LOGE("type error %{public}s not map object", key.c_str()); + parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; + return; + } + for (const auto& [mapKey, mapValue] : jsonObject.at(key).items()) { + if (!BMSJsonUtil::CheckMapValueType(valueType, mapValue)) { + APP_LOGE("type error key:%{public}s", mapKey.c_str()); + parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; + return; + } + } + data = jsonObject.at(key).get(); + return; + } + if (isNecessary) { + APP_LOGE("profile prop %{public}s missing", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP; } } diff --git a/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp b/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp index bb702b9156..30e6be2401 100644 --- a/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp @@ -61,5 +61,19 @@ void BMSJsonUtil::GetBoolValueIfFindKey(const nlohmann::json &jsonObject, parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP; } } + +bool BMSJsonUtil::CheckMapValueType(JsonType valueType, const nlohmann::json &value) +{ + switch (valueType) { + case JsonType::BOOLEAN: + return value.is_boolean(); + case JsonType::NUMBER: + return value.is_number(); + case JsonType::STRING: + return value.is_string(); + default: + return false; + } +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp b/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp index 2eb0e34a2a..dba662dba9 100644 --- a/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp @@ -192,14 +192,13 @@ void from_json(const nlohmann::json &jsonObject, ShortcutIntent &shortcutIntent) shortcutIntent.targetClass, false, parseResult); - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, JSON_KEY_BUNDLE_PARAMETERS, shortcutIntent.parameters, - JsonType::OBJECT, false, parseResult, - ArrayType::NOT_ARRAY); + JsonType::STRING); if (parseResult != ERR_OK) { APP_LOGE("read shortcutIntent jsonObject error : %{public}d", parseResult); } @@ -344,14 +343,13 @@ void from_json(const nlohmann::json &jsonObject, ShortcutWant &shortcutWant) shortcutWant.abilityName, false, parseResult); - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, JSON_KEY_BUNDLE_PARAMETERS, shortcutWant.parameters, - JsonType::OBJECT, false, parseResult, - ArrayType::NOT_ARRAY); + JsonType::STRING); if (parseResult != ERR_OK) { APP_LOGE("read shortcutWant module.json error : %{public}d", parseResult); } -- Gitee