• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "hiappevent_verify.h"
17 
18 #include <cctype>
19 #include <iterator>
20 #include <unistd.h>
21 #include <unordered_set>
22 
23 #include "application_context.h"
24 #include "hiappevent_base.h"
25 #include "hiappevent_config.h"
26 #include "hilog/log.h"
27 
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002D07
30 
31 #undef LOG_TAG
32 #define LOG_TAG "Verify"
33 
34 using namespace OHOS::HiviewDFX::ErrorCode;
35 
36 namespace OHOS {
37 namespace HiviewDFX {
38 namespace {
39 constexpr size_t MAX_LEN_OF_WATCHER = 32;
40 constexpr size_t MAX_LEN_OF_DOMAIN = 32;
41 static constexpr int MAX_LENGTH_OF_EVENT_NAME = 48;
42 static constexpr int MAX_LENGTH_OF_PARAM_NAME = 32;
43 static constexpr unsigned int MAX_NUM_OF_PARAMS = 32;
44 static constexpr size_t MAX_LENGTH_OF_STR_PARAM = 8 * 1024;
45 static constexpr size_t MAX_LENGTH_OF_SPECIAL_STR_PARAM = 1024 * 1024;
46 static constexpr int MAX_SIZE_OF_LIST_PARAM = 100;
47 static constexpr int MAX_LENGTH_OF_USER_INFO_NAME = 256;
48 static constexpr int MAX_LENGTH_OF_USER_ID_VALUE = 256;
49 static constexpr int MAX_LENGTH_OF_USER_PROPERTY_VALUE = 1024;
50 static constexpr int MAX_LENGTH_OF_PROCESSOR_NAME = 256;
51 static constexpr int MAX_LEN_OF_BATCH_REPORT = 1000;
52 static constexpr size_t MAX_NUM_OF_CUSTOM_CONFIGS = 32;
53 static constexpr size_t MAX_LENGTH_OF_CUSTOM_CONFIG_NAME = 32;
54 static constexpr size_t MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE = 1024;
55 static constexpr size_t MAX_NUM_OF_CUSTOM_PARAMS = 64;
56 static constexpr size_t MAX_LENGTH_OF_CUSTOM_PARAM = 1024;
57 constexpr int MIN_APP_UID = 20000;
58 
IsValidName(const std::string & name,size_t maxSize,bool allowDollarSign=true)59 bool IsValidName(const std::string& name, size_t maxSize, bool allowDollarSign = true)
60 {
61     if (name.empty() || name.length() > maxSize) {
62         return false;
63     }
64     // start char is [$a-zA-Z] or [a-zA-Z]
65     if (!isalpha(name[0]) && (!allowDollarSign || name[0] != '$')) {
66         return false;
67     }
68     // end char is [a-zA-Z0-9]
69     if (name.length() > 1 && (!isalnum(name.back()))) {
70         return false;
71     }
72     // middle char is [a-zA-Z0-9_]
73     for (size_t i = 1; i < name.length() - 1; ++i) {
74         if (!isalnum(name[i]) && name[i] != '_') {
75             return false;
76         }
77     }
78     return true;
79 }
80 
CheckParamName(const std::string & paramName)81 bool CheckParamName(const std::string& paramName)
82 {
83     return IsValidName(paramName, MAX_LENGTH_OF_PARAM_NAME);
84 }
85 
EscapeStringValue(std::string & value)86 void EscapeStringValue(std::string &value)
87 {
88     std::string escapeValue;
89     for (auto it = value.begin(); it != value.end(); it++) {
90         switch (*it) {
91             case '\\':
92                 escapeValue.append("\\\\");
93                 break;
94             case '\"':
95                 escapeValue.append("\\\"");
96                 break;
97             case '\b':
98                 escapeValue.append("\\b");
99                 break;
100             case '\f':
101                 escapeValue.append("\\f");
102                 break;
103             case '\n':
104                 escapeValue.append("\\n");
105                 break;
106             case '\r':
107                 escapeValue.append("\\r");
108                 break;
109             case '\t':
110                 escapeValue.append("\\t");
111                 break;
112             default:
113                 escapeValue.push_back(*it);
114                 break;
115         }
116     }
117     value = escapeValue;
118 }
119 
CheckStrParamLength(std::string & strParamValue,size_t maxLen=MAX_LENGTH_OF_STR_PARAM)120 bool CheckStrParamLength(std::string& strParamValue, size_t maxLen = MAX_LENGTH_OF_STR_PARAM)
121 {
122     if (strParamValue.empty()) {
123         return true;
124     }
125 
126     if (strParamValue.length() > maxLen) {
127         return false;
128     }
129 
130     EscapeStringValue(strParamValue);
131     return true;
132 }
133 
CheckListValueSize(AppEventParamType type,AppEventParamValue::ValueUnion & vu)134 bool CheckListValueSize(AppEventParamType type, AppEventParamValue::ValueUnion& vu)
135 {
136     if (type == AppEventParamType::BVECTOR && vu.bs_.size() > MAX_SIZE_OF_LIST_PARAM) {
137         vu.bs_.resize(MAX_SIZE_OF_LIST_PARAM);
138     } else if (type == AppEventParamType::CVECTOR && vu.cs_.size() > MAX_SIZE_OF_LIST_PARAM) {
139         vu.cs_.resize(MAX_SIZE_OF_LIST_PARAM);
140     } else if (type == AppEventParamType::SHVECTOR && vu.shs_.size() > MAX_SIZE_OF_LIST_PARAM) {
141         vu.shs_.resize(MAX_SIZE_OF_LIST_PARAM);
142     } else if (type == AppEventParamType::IVECTOR && vu.is_.size() > MAX_SIZE_OF_LIST_PARAM) {
143         vu.is_.resize(MAX_SIZE_OF_LIST_PARAM);
144     } else if (type == AppEventParamType::LLVECTOR && vu.lls_.size() > MAX_SIZE_OF_LIST_PARAM) {
145         vu.lls_.resize(MAX_SIZE_OF_LIST_PARAM);
146     } else if (type == AppEventParamType::FVECTOR && vu.fs_.size() > MAX_SIZE_OF_LIST_PARAM) {
147         vu.fs_.resize(MAX_SIZE_OF_LIST_PARAM);
148     } else if (type == AppEventParamType::DVECTOR && vu.ds_.size() > MAX_SIZE_OF_LIST_PARAM) {
149         vu.ds_.resize(MAX_SIZE_OF_LIST_PARAM);
150     } else if (type == AppEventParamType::STRVECTOR && vu.strs_.size() > MAX_SIZE_OF_LIST_PARAM) {
151         vu.strs_.resize(MAX_SIZE_OF_LIST_PARAM);
152     } else {
153         return true;
154     }
155 
156     return false;
157 }
158 
CheckStringLengthOfList(std::vector<std::string> & strs,size_t maxTotalLen=0)159 bool CheckStringLengthOfList(std::vector<std::string>& strs, size_t maxTotalLen = 0)
160 {
161     if (strs.empty()) {
162         return true;
163     }
164     size_t totalLen = 0;
165     for (auto it = strs.begin(); it != strs.end(); it++) {
166         if (!CheckStrParamLength(*it)) {
167             return false;
168         }
169         totalLen += (*it).length();
170     }
171     if (maxTotalLen > 0 && totalLen > maxTotalLen) {
172         return false;
173     }
174     return true;
175 }
176 
CheckParamsNum(std::list<AppEventParam> & baseParams)177 bool CheckParamsNum(std::list<AppEventParam>& baseParams)
178 {
179     if (baseParams.size() == 0) {
180         return true;
181     }
182 
183     auto listSize = baseParams.size();
184     if (listSize > MAX_NUM_OF_PARAMS) {
185         auto delStartPtr = baseParams.begin();
186         std::advance(delStartPtr, MAX_NUM_OF_PARAMS);
187         baseParams.erase(delStartPtr, baseParams.end());
188         return false;
189     }
190 
191     return true;
192 }
193 
VerifyAppEventParam(AppEventParam & param,std::unordered_set<std::string> & paramNames,int & verifyRes)194 bool VerifyAppEventParam(AppEventParam& param, std::unordered_set<std::string>& paramNames, int& verifyRes)
195 {
196     std::string name = param.name;
197     if (paramNames.find(name) != paramNames.end()) {
198         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because param is duplicate.", name.c_str());
199         verifyRes = ERROR_DUPLICATE_PARAM;
200         return false;
201     }
202 
203     if (!CheckParamName(name)) {
204         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the paramName is invalid.", name.c_str());
205         verifyRes = ERROR_INVALID_PARAM_NAME;
206         return false;
207     }
208 
209     const std::unordered_set<std::string> tempTrueNames = {"crash", "anr"};
210     size_t maxLen = tempTrueNames.find(name) == tempTrueNames.end() ? MAX_LENGTH_OF_STR_PARAM :
211         MAX_LENGTH_OF_SPECIAL_STR_PARAM;
212     if (param.type == AppEventParamType::STRING && !CheckStrParamLength(param.value.valueUnion.str_, maxLen)) {
213         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the string length exceeds %{public}zu.",
214             name.c_str(), maxLen);
215         verifyRes = ERROR_INVALID_PARAM_VALUE_LENGTH;
216         return false;
217     }
218 
219     if (param.type == AppEventParamType::STRVECTOR && !CheckStringLengthOfList(param.value.valueUnion.strs_)) {
220         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the string length of list exceeds 8192.",
221             name.c_str());
222         verifyRes = ERROR_INVALID_PARAM_VALUE_LENGTH;
223         return false;
224     }
225 
226     if (param.type > AppEventParamType::STRING && !CheckListValueSize(param.type, param.value.valueUnion)) {
227         HILOG_WARN(LOG_CORE, "list param=%{public}s is truncated because the list size exceeds 100.", name.c_str());
228         verifyRes = ERROR_INVALID_LIST_PARAM_SIZE;
229         return true;
230     }
231     return true;
232 }
233 
VerifyCustomAppEventParam(AppEventParam & param,std::unordered_set<std::string> & paramNames)234 int VerifyCustomAppEventParam(AppEventParam& param, std::unordered_set<std::string>& paramNames)
235 {
236     std::string name = param.name;
237     if (paramNames.find(name) != paramNames.end()) {
238         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because param is duplicate.", name.c_str());
239         return ERROR_DUPLICATE_PARAM;
240     }
241 
242     if (!CheckParamName(name)) {
243         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the paramName is invalid.", name.c_str());
244         return ERROR_INVALID_PARAM_NAME;
245     }
246 
247     if (param.type == AppEventParamType::STRING
248         && !CheckStrParamLength(param.value.valueUnion.str_, MAX_LENGTH_OF_CUSTOM_PARAM)) {
249         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the string length exceeds %{public}zu.",
250             name.c_str(), MAX_LENGTH_OF_CUSTOM_PARAM);
251         return ERROR_INVALID_PARAM_VALUE_LENGTH;
252     }
253 
254     if (param.type == AppEventParamType::STRVECTOR
255         && !CheckStringLengthOfList(param.value.valueUnion.strs_, MAX_LENGTH_OF_CUSTOM_PARAM)) {
256         HILOG_WARN(LOG_CORE, "param=%{public}s is discarded because the string length of list exceeds %{public}zu.",
257             name.c_str(), MAX_LENGTH_OF_CUSTOM_PARAM);
258         return ERROR_INVALID_PARAM_VALUE_LENGTH;
259     }
260 
261     return HIAPPEVENT_VERIFY_SUCCESSFUL;
262 }
263 
264 using VerifyReportConfigFunc = int (*)(ReportConfig& config);
VerifyNameOfReportConfig(ReportConfig & config)265 int VerifyNameOfReportConfig(ReportConfig& config)
266 {
267     if (!IsValidProcessorName(config.name)) {
268         HILOG_ERROR(LOG_CORE, "invalid name=%{public}s", config.name.c_str());
269         return -1;
270     }
271     return 0;
272 }
273 
VerifyRouteInfoOfReportConfig(ReportConfig & config)274 int VerifyRouteInfoOfReportConfig(ReportConfig& config)
275 {
276     if (!IsValidRouteInfo(config.routeInfo)) {
277         HILOG_WARN(LOG_CORE, "invalid routeInfo.");
278         config.routeInfo = "";
279     }
280     return 0;
281 }
282 
VerifyAppIdOfReportConfig(ReportConfig & config)283 int VerifyAppIdOfReportConfig(ReportConfig& config)
284 {
285     if (!IsValidAppId(config.appId)) {
286         HILOG_WARN(LOG_CORE, "invalid appId.");
287         config.appId = "";
288     }
289     return 0;
290 }
291 
VerifyTriggerCondOfReportConfig(ReportConfig & config)292 int VerifyTriggerCondOfReportConfig(ReportConfig& config)
293 {
294     if (!IsValidBatchReport(config.triggerCond.row)) {
295         HILOG_WARN(LOG_CORE, "invalid triggerCond.row=%{public}d", config.triggerCond.row);
296         config.triggerCond.row = 0;
297     }
298     if (!IsValidPeriodReport(config.triggerCond.timeout)) {
299         HILOG_WARN(LOG_CORE, "invalid triggerCond.timeout=%{public}d", config.triggerCond.row);
300         config.triggerCond.timeout = 0;
301     }
302     // processor does not support the size
303     config.triggerCond.size = 0;
304     return 0;
305 }
306 
VerifyUserIdNamesOfReportConfig(ReportConfig & config)307 int VerifyUserIdNamesOfReportConfig(ReportConfig& config)
308 {
309     for (const auto& name : config.userIdNames) {
310         if (!IsValidUserIdName(name)) {
311             HILOG_WARN(LOG_CORE, "invalid user id name=%{public}s", name.c_str());
312             config.userIdNames.clear();
313             break;
314         }
315     }
316     return 0;
317 }
318 
VerifyUserPropertyNamesOfReportConfig(ReportConfig & config)319 int VerifyUserPropertyNamesOfReportConfig(ReportConfig& config)
320 {
321     for (const auto& name : config.userPropertyNames) {
322         if (!IsValidUserIdName(name)) {
323             HILOG_WARN(LOG_CORE, "invalid user property name=%{public}s", name.c_str());
324             config.userPropertyNames.clear();
325             break;
326         }
327     }
328     return 0;
329 }
330 
VerifyEventConfigsOfReportConfig(ReportConfig & reportConfig)331 int VerifyEventConfigsOfReportConfig(ReportConfig& reportConfig)
332 {
333     for (const auto& eventConfig : reportConfig.eventConfigs) {
334         if (!IsValidEventConfig(eventConfig)) {
335             HILOG_WARN(LOG_CORE, "invalid event configs, domain=%{public}s, name=%{public}s",
336                 eventConfig.domain.c_str(), eventConfig.name.c_str());
337             reportConfig.eventConfigs.clear();
338             break;
339         }
340     }
341     return 0;
342 }
343 
VerifyConfigIdOfReportConfig(ReportConfig & config)344 int VerifyConfigIdOfReportConfig(ReportConfig& config)
345 {
346     if (!IsValidConfigId(config.configId)) {
347         HILOG_WARN(LOG_CORE, "invalid configId=%{public}d", config.configId);
348         config.configId = 0;
349     }
350     return 0;
351 }
352 
VerifyCustomConfigsOfReportConfig(ReportConfig & config)353 int VerifyCustomConfigsOfReportConfig(ReportConfig& config)
354 {
355     if (!IsValidCustomConfigsNum(config.customConfigs.size())) {
356         HILOG_WARN(LOG_CORE, "invalid keys size=%{public}zu", config.customConfigs.size());
357         config.customConfigs.clear();
358         return 0;
359     }
360     for (const auto& item : config.customConfigs) {
361         if (!IsValidCustomConfig(item.first, item.second)) {
362             HILOG_WARN(LOG_CORE, "invalid key name=%{public}s", item.first.c_str());
363             config.customConfigs.clear();
364             break;
365         }
366     }
367     return 0;
368 }
369 }
370 
IsValidDomain(const std::string & eventDomain)371 bool IsValidDomain(const std::string& eventDomain)
372 {
373     return IsValidName(eventDomain, MAX_LEN_OF_DOMAIN, false);
374 }
375 
IsValidEventName(const std::string & eventName)376 bool IsValidEventName(const std::string& eventName)
377 {
378     const std::string eventPrefix = "hiappevent.";
379     return eventName.find(eventPrefix) == 0 ?
380         IsValidName(eventName.substr(eventPrefix.length()), MAX_LENGTH_OF_EVENT_NAME - eventPrefix.length()) :
381         IsValidName(eventName, MAX_LENGTH_OF_EVENT_NAME);
382 }
383 
IsValidWatcherName(const std::string & watcherName)384 bool IsValidWatcherName(const std::string& watcherName)
385 {
386     return IsValidName(watcherName, MAX_LEN_OF_WATCHER, false);
387 }
388 
IsValidEventType(int eventType)389 bool IsValidEventType(int eventType)
390 {
391     return eventType >= 1 && eventType <= 4; // 1-4: value range of event type
392 }
393 
VerifyAppEvent(std::shared_ptr<AppEventPack> event)394 int VerifyAppEvent(std::shared_ptr<AppEventPack> event)
395 {
396     if (HiAppEventConfig::GetInstance().GetDisable()) {
397         HILOG_ERROR(LOG_CORE, "the HiAppEvent function is disabled.");
398         return ERROR_HIAPPEVENT_DISABLE;
399     }
400     if (!IsValidDomain(event->GetDomain())) {
401         HILOG_ERROR(LOG_CORE, "eventDomain=%{public}s is invalid.", event->GetDomain().c_str());
402         return ERROR_INVALID_EVENT_DOMAIN;
403     }
404     if (!IsValidEventName(event->GetName())) {
405         HILOG_ERROR(LOG_CORE, "eventName=%{public}s is invalid.", event->GetName().c_str());
406         return ERROR_INVALID_EVENT_NAME;
407     }
408 
409     int verifyRes = HIAPPEVENT_VERIFY_SUCCESSFUL;
410     std::list<AppEventParam>& baseParams = event->baseParams_;
411     std::unordered_set<std::string> paramNames;
412     for (auto it = baseParams.begin(); it != baseParams.end();) {
413         if (!VerifyAppEventParam(*it, paramNames, verifyRes)) {
414             baseParams.erase(it++);
415             continue;
416         }
417         paramNames.emplace(it->name);
418         it++;
419     }
420 
421     if (!CheckParamsNum(baseParams)) {
422         HILOG_WARN(LOG_CORE, "params that exceed 32 are discarded because the number of params cannot exceed 32.");
423         verifyRes = ERROR_INVALID_PARAM_NUM;
424     }
425 
426     return verifyRes;
427 }
428 
VerifyCustomEventParams(std::shared_ptr<AppEventPack> event)429 int VerifyCustomEventParams(std::shared_ptr<AppEventPack> event)
430 {
431     if (HiAppEventConfig::GetInstance().GetDisable()) {
432         HILOG_ERROR(LOG_CORE, "the HiAppEvent function is disabled.");
433         return ERROR_HIAPPEVENT_DISABLE;
434     }
435     if (!IsValidDomain(event->GetDomain())) {
436         HILOG_ERROR(LOG_CORE, "eventDomain=%{public}s is invalid.", event->GetDomain().c_str());
437         return ERROR_INVALID_EVENT_DOMAIN;
438     }
439     if (!event->GetName().empty() && !IsValidEventName(event->GetName())) {
440         HILOG_ERROR(LOG_CORE, "eventName=%{public}s is invalid.", event->GetName().c_str());
441         return ERROR_INVALID_EVENT_NAME;
442     }
443 
444     std::list<AppEventParam>& baseParams = event->baseParams_;
445     if (baseParams.size() > MAX_NUM_OF_CUSTOM_PARAMS) {
446         HILOG_WARN(LOG_CORE, "params that exceed 64 are discarded because the number of params cannot exceed 64.");
447         return ERROR_INVALID_CUSTOM_PARAM_NUM;
448     }
449     std::unordered_set<std::string> paramNames;
450     for (auto it = baseParams.begin(); it != baseParams.end(); ++it) {
451         if (int ret = VerifyCustomAppEventParam(*it, paramNames); ret != HIAPPEVENT_VERIFY_SUCCESSFUL) {
452             return ret;
453         }
454     }
455     return HIAPPEVENT_VERIFY_SUCCESSFUL;
456 }
457 
IsValidPropName(const std::string & name,size_t maxSize)458 bool IsValidPropName(const std::string& name, size_t maxSize)
459 {
460     if (name.empty() || name.length() > maxSize) {
461         return false;
462     }
463     // start char is [a-zA-Z_$]
464     if (!isalpha(name[0]) && name[0] != '_' && name[0] != '$') {
465         return false;
466     }
467     // other char is [a-zA-Z0-9_$]
468     for (size_t i = 1; i < name.length(); ++i) {
469         if (!isalnum(name[i]) && name[i] != '_' && name[i] != '$') {
470             return false;
471         }
472     }
473     return true;
474 }
475 
IsValidPropValue(const std::string & val,size_t maxSize)476 bool IsValidPropValue(const std::string& val, size_t maxSize)
477 {
478     return !val.empty() && val.length() <= maxSize;
479 }
480 
IsValidProcessorName(const std::string & name)481 bool IsValidProcessorName(const std::string& name)
482 {
483     return IsValidPropName(name, MAX_LENGTH_OF_PROCESSOR_NAME);
484 }
485 
IsValidRouteInfo(const std::string & name)486 bool IsValidRouteInfo(const std::string& name)
487 {
488     return name.length() <= MAX_LENGTH_OF_STR_PARAM;
489 }
490 
IsValidAppId(const std::string & name)491 bool IsValidAppId(const std::string& name)
492 {
493     return name.length() <= MAX_LENGTH_OF_STR_PARAM;
494 }
495 
IsValidPeriodReport(int timeout)496 bool IsValidPeriodReport(int timeout)
497 {
498     return timeout >= 0;
499 }
500 
IsValidBatchReport(int count)501 bool IsValidBatchReport(int count)
502 {
503     return count >= 0 && count <= MAX_LEN_OF_BATCH_REPORT;
504 }
505 
IsValidUserIdName(const std::string & name)506 bool IsValidUserIdName(const std::string& name)
507 {
508     return IsValidPropName(name, MAX_LENGTH_OF_USER_INFO_NAME);
509 }
510 
IsValidUserIdValue(const std::string & value)511 bool IsValidUserIdValue(const std::string& value)
512 {
513     return IsValidPropValue(value, MAX_LENGTH_OF_USER_ID_VALUE);
514 }
515 
IsValidUserPropName(const std::string & name)516 bool IsValidUserPropName(const std::string& name)
517 {
518     return IsValidPropName(name, MAX_LENGTH_OF_USER_INFO_NAME);
519 }
520 
IsValidUserPropValue(const std::string & value)521 bool IsValidUserPropValue(const std::string& value)
522 {
523     return IsValidPropValue(value, MAX_LENGTH_OF_USER_PROPERTY_VALUE);
524 }
525 
IsValidEventConfig(const EventConfig & eventCfg)526 bool IsValidEventConfig(const EventConfig& eventCfg)
527 {
528     if (eventCfg.domain.empty() && eventCfg.name.empty()) {
529         return false;
530     }
531     if (!eventCfg.domain.empty() && !IsValidDomain(eventCfg.domain)) {
532         return false;
533     }
534     if (!eventCfg.name.empty() && !IsValidEventName(eventCfg.name)) {
535         return false;
536     }
537     return true;
538 }
539 
IsValidConfigId(int configId)540 bool IsValidConfigId(int configId)
541 {
542     return configId >= 0;
543 }
544 
IsValidCustomConfigsNum(size_t num)545 bool IsValidCustomConfigsNum(size_t num)
546 {
547     return num <= MAX_NUM_OF_CUSTOM_CONFIGS;
548 }
549 
IsValidCustomConfig(const std::string & name,const std::string & value)550 bool IsValidCustomConfig(const std::string& name, const std::string& value)
551 {
552     if (!IsValidName(name, MAX_LENGTH_OF_CUSTOM_CONFIG_NAME) || value.length() > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
553         return false;
554     }
555     return true;
556 }
557 
VerifyReportConfig(ReportConfig & config)558 int VerifyReportConfig(ReportConfig& config)
559 {
560     const VerifyReportConfigFunc verifyFuncs[] = {
561         VerifyNameOfReportConfig,
562         VerifyRouteInfoOfReportConfig,
563         VerifyAppIdOfReportConfig,
564         VerifyTriggerCondOfReportConfig,
565         VerifyUserIdNamesOfReportConfig,
566         VerifyUserPropertyNamesOfReportConfig,
567         VerifyEventConfigsOfReportConfig,
568         VerifyConfigIdOfReportConfig,
569         VerifyCustomConfigsOfReportConfig,
570     };
571     for (const auto verifyFunc : verifyFuncs) {
572         if (verifyFunc(config) != 0) {
573             return -1;
574         }
575     }
576     return 0;
577 }
578 
IsApp()579 bool IsApp()
580 {
581     if (getuid() < MIN_APP_UID) {
582         return false;
583     }
584     // use startArkChildProcess create a child process has no ApplicationContext
585     // bundle name is empty means no context
586     std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> context =
587         OHOS::AbilityRuntime::Context::GetApplicationContext();
588     if (context == nullptr || context->GetBundleName().empty()) {
589         HILOG_ERROR(LOG_CORE, "context is null.");
590         return false;
591     }
592     return true;
593 }
594 } // namespace HiviewDFX
595 } // namespace OHOS
596