From a89e2aa1f4fc0b29c4611edca05182dce1f6e84f Mon Sep 17 00:00:00 2001 From: tangfan Date: Wed, 21 Dec 2022 11:20:36 +0800 Subject: [PATCH] fix json parse Signed-off-by: tangfan --- .../src/componentloader/component_loader.cpp | 60 +++++++++++++++---- .../src/lowlatency/low_latency_listener.cpp | 2 +- .../src/resourcemanager/capability_info.cpp | 37 ++++++++---- .../src/resourcemanager/version_info.cpp | 2 - utils/include/dh_utils_tool.h | 8 ++- utils/src/dh_utils_tool.cpp | 22 ++++++- 6 files changed, 104 insertions(+), 27 deletions(-) diff --git a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp index 7f54475a..2c884c08 100644 --- a/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp +++ b/services/distributedhardwarefwkservice/src/componentloader/component_loader.cpp @@ -105,16 +105,56 @@ std::vector ComponentLoader::GetAllCompTypes() void from_json(const nlohmann::json &json, CompConfig &cfg) { - cfg.name = json.value(COMP_NAME, DEFAULT_NAME); - cfg.type = g_mapDhTypeName[json.value(COMP_TYPE, DEFAULT_TYPE)]; - cfg.compHandlerLoc = json.value(COMP_HANDLER_LOC, DEFAULT_LOC); - cfg.compHandlerVersion = json.value(COMP_HANDLER_VERSION, DEFAULT_VERSION); - cfg.compSourceLoc = json.value(COMP_SOURCE_LOC, DEFAULT_LOC); - cfg.compSourceVersion = json.value(COMP_SOURCE_VERSION, DEFAULT_VERSION); - cfg.compSourceSaId = json.value(COMP_SOURCE_SA_ID, DEFAULT_SA_ID); - cfg.compSinkLoc = json.value(COMP_SINK_LOC, DEFAULT_LOC); - cfg.compSinkVersion = json.value(COMP_SINK_VERSION, DEFAULT_VERSION); - cfg.compSinkSaId = json.value(COMP_SINK_SA_ID, DEFAULT_SA_ID); + if (!IsString(json, COMP_NAME)) { + DHLOGE("COMP_NAME is invalid"); + return; + } + cfg.name = json.at(COMP_NAME).get(); + if (!IsUInt32(json, COMP_TYPE)) { + DHLOGE("COMP_TYPE is invalid"); + return; + } + cfg.type = json.at(COMP_TYPE).get(); + if (!IsString(json, COMP_HANDLER_LOC)) { + DHLOGE("COMP_HANDLER_LOC is invalid"); + return; + } + cfg.compHandlerLoc = json.at(COMP_HANDLER_LOC).get(); + if (!IsString(json, COMP_HANDLER_VERSION)) { + DHLOGE("COMP_HANDLER_VERSION is invalid"); + return; + } + cfg.compHandlerVersion = json.at(COMP_HANDLER_LOC).get(); + if (!IsString(json, COMP_SOURCE_LOC)) { + DHLOGE("COMP_SOURCE_LOC is invalid"); + return; + } + cfg.compSourceLoc = json.at(COMP_SOURCE_LOC).get(); + if (!IsString(json, COMP_SOURCE_VERSION)) { + DHLOGE("COMP_SOURCE_VERSION is invalid"); + return; + } + cfg.compSourceVersion = json.at(COMP_SOURCE_VERSION).get(); + if (!IsInt32(json, COMP_SOURCE_SA_ID)) { + DHLOGE("COMP_SOURCE_SA_ID is invalid"); + return; + } + cfg.compSourceSaId = json.at(COMP_SOURCE_SA_ID).get(); + if (!IsString(json, COMP_SINK_LOC)) { + DHLOGE("COMP_SINK_LOC is invalid"); + return; + } + cfg.compSinkLoc = json.at(COMP_SOURCE_SA_ID).get(); + if (!IsString(json, COMP_SINK_VERSION)) { + DHLOGE("COMP_SINK_VERSION is invalid"); + return; + } + cfg.compSinkVersion = json.at(COMP_SOURCE_SA_ID).get(); + if (!IsInt32(json, COMP_SINK_SA_ID)) { + DHLOGE("COMP_SINK_SA_ID is invalid"); + return; + } + cfg.compSinkSaId = json.at(COMP_SINK_SA_ID).get(); } CompVersion ComponentLoader::GetCompVersionFromComConfig(const CompConfig& cCfg) diff --git a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp index 4ea77e83..ec0bd52d 100644 --- a/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp +++ b/services/distributedhardwarefwkservice/src/lowlatency/low_latency_listener.cpp @@ -54,7 +54,7 @@ void LowLatencyListener::OnMessage(const DHTopic topic, const std::string& messa DHLOGE("jsonStr parse failed"); return; } - if (!IsUint32(jsonObj, DH_TYPE)) { + if (!IsUInt32(jsonObj, DH_TYPE)) { DHLOGE("The DH_TYPE key is invalid"); return; } diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp index fb2783f0..9c795463 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/capability_info.cpp @@ -21,6 +21,7 @@ #include "anonymous_string.h" #include "constants.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" @@ -167,24 +168,36 @@ void ToJson(nlohmann::json &jsonObject, const CapabilityInfo &capability) void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability) { - if (jsonObject.find(DH_ID) != jsonObject.end()) { - capability.SetDHId(jsonObject.at(DH_ID).get()); + if (!IsString(jsonObject, DH_ID)) { + DHLOGE("DH_ID is invalid!"); + return; } - if (jsonObject.find(DEV_ID) != jsonObject.end()) { - capability.SetDeviceId(jsonObject.at(DEV_ID).get()); + capability.SetDHId(jsonObject.at(DH_ID).get()); + if (!IsString(jsonObject, DEV_ID)) { + DHLOGE("DEV_ID is invalid!"); + return; } - if (jsonObject.find(DEV_NAME) != jsonObject.end()) { - capability.SetDeviceName(jsonObject.at(DEV_NAME).get()); + capability.SetDeviceId(jsonObject.at(DEV_ID).get()); + if (!IsString(jsonObject, DEV_NAME)) { + DHLOGE("DEV_NAME is invalid!"); + return; } - if (jsonObject.find(DEV_TYPE) != jsonObject.end()) { - capability.SetDeviceType(jsonObject.at(DEV_TYPE).get()); + capability.SetDeviceName(jsonObject.at(DEV_NAME).get()); + if (!IsInt16(jsonObject, DEV_TYPE)) { + DHLOGE("DEV_TYPE is invalid!"); + return; } - if (jsonObject.find(DH_TYPE) != jsonObject.end()) { - capability.SetDHType(jsonObject.at(DH_TYPE).get()); + capability.SetDeviceType(jsonObject.at(DEV_TYPE).get()); + if (!IsUInt32(jsonObject, DH_TYPE)) { + DHLOGE("DH_TYPE is invalid!"); + return; } - if (jsonObject.find(DH_ATTRS) != jsonObject.end()) { - capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get()); + capability.SetDHType(jsonObject.at(DH_TYPE).get()); + if (!IsString(jsonObject, DH_ATTRS)) { + DHLOGE("DH_ATTRS is invalid!"); + return; } + capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get()); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp index 01e14e44..45193a13 100644 --- a/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp +++ b/services/distributedhardwarefwkservice/src/resourcemanager/version_info.cpp @@ -51,7 +51,6 @@ void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo) { jsonObject[DEV_ID] = versionInfo.deviceId; jsonObject[DH_VER] = versionInfo.dhVersion; - nlohmann::json compVers; for (const auto &compVersion : versionInfo.compVersions) { nlohmann::json compVer; @@ -62,7 +61,6 @@ void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo) compVer[SINK_VER] = compVersion.second.sinkVersion; compVers.push_back(compVer); } - jsonObject[COMP_VER] = compVers; } diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 7abbbad6..5126fcd3 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -43,9 +43,15 @@ std::string GetDeviceIdByUUID(const std::string &uuid); std::string Sha256(const std::string& string); -bool IsUint32(const nlohmann::json& jsonObj, const std::string& key); +bool IsInt16(const nlohmann::json& jsonObj, const std::string& key); + +bool IsInt32(const nlohmann::json& jsonObj, const std::string& key); + +bool IsUInt32(const nlohmann::json& jsonObj, const std::string& key); bool IsBool(const nlohmann::json& jsonObj, const std::string& key); + +bool IsString(const nlohmann::json& jsonObj, const std::string& key); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index 38366ff7..7b241fb2 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -127,7 +127,21 @@ DeviceInfo GetLocalDeviceInfo() return devInfo; } -bool IsUint32(const nlohmann::json& jsonObj, const std::string& key) +bool IsInt16(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && INT16_MIN <= jsonObj[key] && + jsonObj[key] <= INT16_MAX; + return res; +} + +bool IsInt32(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && INT32_MIN <= jsonObj[key] && + jsonObj[key] <= INT32_MAX; + return res; +} + +bool IsUInt32(const nlohmann::json& jsonObj, const std::string& key) { bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; return res; @@ -138,5 +152,11 @@ bool IsBool(const nlohmann::json& jsonObj, const std::string& key) bool res = jsonObj.contains(key) && jsonObj[key].is_boolean(); return res; } + +bool IsString(const nlohmann::json& jsonObj, const std::string& key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size <= MAX_MESSAGE_LEN; + return res; +} } // namespace DistributedHardware } // namespace OHOS -- Gitee