From 641ce1116ddbd5327b843efcd990f43cb520f485 Mon Sep 17 00:00:00 2001 From: wangyb0625 Date: Tue, 12 Apr 2022 19:12:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=82=9F=E7=A9=BA=E6=B5=8B=E8=AF=95-=E9=98=B2?= =?UTF-8?q?=E6=AD=A2stoi=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyb0625 --- .../kits/js/include/native_devicemanager_js.h | 2 ++ .../kits/js/src/native_devicemanager_js.cpp | 27 +++++++++++++++++++ utils/include/dm_anonymous.h | 1 + utils/src/dm_anonymous.cpp | 21 +++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index 77a6f6ab..8db0b602 100644 --- a/interfaces/kits/js/include/native_devicemanager_js.h +++ b/interfaces/kits/js/include/native_devicemanager_js.h @@ -24,6 +24,7 @@ #include "dm_device_info.h" #include "dm_native_event.h" #include "dm_subscribe_info.h" +#include "dm_anonymous.h" #include "napi/native_api.h" #include "napi/native_node_api.h" #include "nlohmann/json.hpp" @@ -247,6 +248,7 @@ public: static void DeviceInfoToJsArray(const napi_env &env, const std::vector &vecDevInfo, const int32_t idx, napi_value &arrayResult); + static bool DmAuthParamDetection(const OHOS::DistributedHardware::DmAuthParam &authParam); static void DmAuthParamToJsAuthParam(const napi_env &env, const OHOS::DistributedHardware::DmAuthParam &authParam, napi_value ¶mResult); static void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue, diff --git a/interfaces/kits/js/src/native_devicemanager_js.cpp b/interfaces/kits/js/src/native_devicemanager_js.cpp index 17278a45..acd42777 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -586,9 +586,36 @@ void DeviceManagerNapi::DeviceInfoToJsArray(const napi_env &env, const std::vect } } +bool DeviceManagerNapi::DmAuthParamDetection(const DmAuthParam &authParam) +{ + LOGI("DeviceManagerNapi::DmAuthParamDetection"); + const uint32_t maxIntValueLen = 10; + const std::string maxAuthToken = "2147483647"; + if (authParam.authToken.length() > maxIntValueLen) { + LOGE("The authToken is illegal"); + return false; + } else { + if (!IsNumberString(authParam.authToken)) { + LOGE("The authToken is Error"); + return false; + } else { + if (authParam.authToken > maxAuthToken) { + LOGE("The authToken is Cross the border"); + return false; + } + } + } + return true; +} + void DeviceManagerNapi::DmAuthParamToJsAuthParam(const napi_env &env, const DmAuthParam &authParam, napi_value ¶mResult) { + LOGI("DeviceManagerNapi::DmAuthParamToJsAuthParam"); + if (!DmAuthParamDetection(authParam)) { + LOGE("The authToken is Error"); + return; + } napi_value extraInfo = nullptr; napi_create_object(env, &extraInfo); SetValueInt32(env, "direction", authParam.direction, extraInfo); diff --git a/utils/include/dm_anonymous.h b/utils/include/dm_anonymous.h index 3537598b..16d47661 100644 --- a/utils/include/dm_anonymous.h +++ b/utils/include/dm_anonymous.h @@ -22,6 +22,7 @@ namespace OHOS { namespace DistributedHardware { std::string GetAnonyString(const std::string &value); std::string GetAnonyInt32(const int32_t value); +bool IsNumberString(const std::string &authToken); } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_ANONYMOUS_H diff --git a/utils/src/dm_anonymous.cpp b/utils/src/dm_anonymous.cpp index 3c642541..12ce168e 100644 --- a/utils/src/dm_anonymous.cpp +++ b/utils/src/dm_anonymous.cpp @@ -14,6 +14,7 @@ */ #include "dm_anonymous.h" +#include "dm_log.h" namespace OHOS { namespace DistributedHardware { @@ -56,5 +57,25 @@ std::string GetAnonyInt32(const int32_t value) } return tempString; } + +bool IsNumberString(const std::string &inputString) +{ + LOGI("IsNumberString for DeviceManagerNapi"); + if (inputString.length() == 0) { + LOGE("inputString is Null"); + return false; + } + const int32_t MIN_ASCLL_NUM = 48; + const int32_t MAX_ASCLL_NUM = 57; + for (int i = 0; i < inputString.length(); i++) { + int num = (int)inputString[i]; + if (num >= MIN_ASCLL_NUM && num <= MAX_ASCLL_NUM) { + continue; + } else { + return false; + } + } + return true; +} } // namespace DistributedHardware } // namespace OHOS