diff --git a/interfaces/kits/js/include/native_devicemanager_js.h b/interfaces/kits/js/include/native_devicemanager_js.h index 77a6f6abde025cec38fa33b34faeae5c4940dcf4..8db0b602289e72159620283e76511310c2349748 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 01503e91d33424b694336885ec8fc24ffdac0fc5..6f9c57510cd58cee0588752e481208b6b6afd89b 100644 --- a/interfaces/kits/js/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js/src/native_devicemanager_js.cpp @@ -585,9 +585,37 @@ 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 3537598b8a1e8abe624d59c3513bdf079cb81ee8..16d476612ca47825450ca412b014246b5c063e60 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 3c642541b25867b9407d8c5c48351055eec9b003..12ce168e1deb129452c909dc7792859134fd9010 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