1 /*
2 * Copyright (c) 2021-2025 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.timeout);
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
VerifyConfigNameOfReportConfig(ReportConfig & config)370 int VerifyConfigNameOfReportConfig(ReportConfig& config)
371 {
372 if (!IsValidProcessorName(config.configName)) {
373 HILOG_WARN(LOG_CORE, "invalid configName=%{public}s", config.configName.c_str());
374 config.configName = "";
375 }
376 return 0;
377 }
378 }
379
IsValidDomain(const std::string & eventDomain)380 bool IsValidDomain(const std::string& eventDomain)
381 {
382 return IsValidName(eventDomain, MAX_LEN_OF_DOMAIN, false);
383 }
384
IsValidEventName(const std::string & eventName)385 bool IsValidEventName(const std::string& eventName)
386 {
387 const std::string eventPrefix = "hiappevent.";
388 return eventName.find(eventPrefix) == 0 ?
389 IsValidName(eventName.substr(eventPrefix.length()), MAX_LENGTH_OF_EVENT_NAME - eventPrefix.length()) :
390 IsValidName(eventName, MAX_LENGTH_OF_EVENT_NAME);
391 }
392
IsValidWatcherName(const std::string & watcherName)393 bool IsValidWatcherName(const std::string& watcherName)
394 {
395 return IsValidName(watcherName, MAX_LEN_OF_WATCHER, false);
396 }
397
IsValidEventType(int eventType)398 bool IsValidEventType(int eventType)
399 {
400 return eventType >= 1 && eventType <= 4; // 1-4: value range of event type
401 }
402
VerifyAppEvent(std::shared_ptr<AppEventPack> event)403 int VerifyAppEvent(std::shared_ptr<AppEventPack> event)
404 {
405 if (HiAppEventConfig::GetInstance().GetDisable()) {
406 HILOG_ERROR(LOG_CORE, "the HiAppEvent function is disabled.");
407 return ERROR_HIAPPEVENT_DISABLE;
408 }
409 if (!IsValidDomain(event->GetDomain())) {
410 HILOG_ERROR(LOG_CORE, "eventDomain=%{public}s is invalid.", event->GetDomain().c_str());
411 return ERROR_INVALID_EVENT_DOMAIN;
412 }
413 if (!IsValidEventName(event->GetName())) {
414 HILOG_ERROR(LOG_CORE, "eventName=%{public}s is invalid.", event->GetName().c_str());
415 return ERROR_INVALID_EVENT_NAME;
416 }
417
418 int verifyRes = HIAPPEVENT_VERIFY_SUCCESSFUL;
419 std::list<AppEventParam>& baseParams = event->baseParams_;
420 std::unordered_set<std::string> paramNames;
421 for (auto it = baseParams.begin(); it != baseParams.end();) {
422 if (!VerifyAppEventParam(*it, paramNames, verifyRes)) {
423 baseParams.erase(it++);
424 continue;
425 }
426 paramNames.emplace(it->name);
427 it++;
428 }
429
430 if (!CheckParamsNum(baseParams)) {
431 HILOG_WARN(LOG_CORE, "params that exceed 32 are discarded because the number of params cannot exceed 32.");
432 verifyRes = ERROR_INVALID_PARAM_NUM;
433 }
434
435 return verifyRes;
436 }
437
VerifyCustomEventParams(std::shared_ptr<AppEventPack> event)438 int VerifyCustomEventParams(std::shared_ptr<AppEventPack> event)
439 {
440 if (HiAppEventConfig::GetInstance().GetDisable()) {
441 HILOG_ERROR(LOG_CORE, "the HiAppEvent function is disabled.");
442 return ERROR_HIAPPEVENT_DISABLE;
443 }
444 if (!IsValidDomain(event->GetDomain())) {
445 HILOG_ERROR(LOG_CORE, "eventDomain=%{public}s is invalid.", event->GetDomain().c_str());
446 return ERROR_INVALID_EVENT_DOMAIN;
447 }
448 if (!event->GetName().empty() && !IsValidEventName(event->GetName())) {
449 HILOG_ERROR(LOG_CORE, "eventName=%{public}s is invalid.", event->GetName().c_str());
450 return ERROR_INVALID_EVENT_NAME;
451 }
452
453 std::list<AppEventParam>& baseParams = event->baseParams_;
454 if (baseParams.size() > MAX_NUM_OF_CUSTOM_PARAMS) {
455 HILOG_WARN(LOG_CORE, "params that exceed 64 are discarded because the number of params cannot exceed 64.");
456 return ERROR_INVALID_CUSTOM_PARAM_NUM;
457 }
458 std::unordered_set<std::string> paramNames;
459 for (auto it = baseParams.begin(); it != baseParams.end(); ++it) {
460 if (int ret = VerifyCustomAppEventParam(*it, paramNames); ret != HIAPPEVENT_VERIFY_SUCCESSFUL) {
461 return ret;
462 }
463 }
464 return HIAPPEVENT_VERIFY_SUCCESSFUL;
465 }
466
IsValidPropName(const std::string & name,size_t maxSize)467 bool IsValidPropName(const std::string& name, size_t maxSize)
468 {
469 if (name.empty() || name.length() > maxSize) {
470 return false;
471 }
472 // start char is [a-zA-Z_$]
473 if (!isalpha(name[0]) && name[0] != '_' && name[0] != '$') {
474 return false;
475 }
476 // other char is [a-zA-Z0-9_$]
477 for (size_t i = 1; i < name.length(); ++i) {
478 if (!isalnum(name[i]) && name[i] != '_' && name[i] != '$') {
479 return false;
480 }
481 }
482 return true;
483 }
484
IsValidPropValue(const std::string & val,size_t maxSize)485 bool IsValidPropValue(const std::string& val, size_t maxSize)
486 {
487 return !val.empty() && val.length() <= maxSize;
488 }
489
IsValidProcessorName(const std::string & name)490 bool IsValidProcessorName(const std::string& name)
491 {
492 return IsValidPropName(name, MAX_LENGTH_OF_PROCESSOR_NAME);
493 }
494
IsValidRouteInfo(const std::string & name)495 bool IsValidRouteInfo(const std::string& name)
496 {
497 return name.length() <= MAX_LENGTH_OF_STR_PARAM;
498 }
499
IsValidAppId(const std::string & name)500 bool IsValidAppId(const std::string& name)
501 {
502 return name.length() <= MAX_LENGTH_OF_STR_PARAM;
503 }
504
IsValidPeriodReport(int timeout)505 bool IsValidPeriodReport(int timeout)
506 {
507 return timeout >= 0;
508 }
509
IsValidBatchReport(int count)510 bool IsValidBatchReport(int count)
511 {
512 return count >= 0 && count <= MAX_LEN_OF_BATCH_REPORT;
513 }
514
IsValidUserIdName(const std::string & name)515 bool IsValidUserIdName(const std::string& name)
516 {
517 return IsValidPropName(name, MAX_LENGTH_OF_USER_INFO_NAME);
518 }
519
IsValidUserIdValue(const std::string & value)520 bool IsValidUserIdValue(const std::string& value)
521 {
522 return IsValidPropValue(value, MAX_LENGTH_OF_USER_ID_VALUE);
523 }
524
IsValidUserPropName(const std::string & name)525 bool IsValidUserPropName(const std::string& name)
526 {
527 return IsValidPropName(name, MAX_LENGTH_OF_USER_INFO_NAME);
528 }
529
IsValidUserPropValue(const std::string & value)530 bool IsValidUserPropValue(const std::string& value)
531 {
532 return IsValidPropValue(value, MAX_LENGTH_OF_USER_PROPERTY_VALUE);
533 }
534
IsValidEventConfig(const EventConfig & eventCfg)535 bool IsValidEventConfig(const EventConfig& eventCfg)
536 {
537 if (eventCfg.domain.empty() && eventCfg.name.empty()) {
538 return false;
539 }
540 if (!eventCfg.domain.empty() && !IsValidDomain(eventCfg.domain)) {
541 return false;
542 }
543 if (!eventCfg.name.empty() && !IsValidEventName(eventCfg.name)) {
544 return false;
545 }
546 return true;
547 }
548
IsValidConfigId(int configId)549 bool IsValidConfigId(int configId)
550 {
551 return configId >= 0;
552 }
553
IsValidCustomConfigsNum(size_t num)554 bool IsValidCustomConfigsNum(size_t num)
555 {
556 return num <= MAX_NUM_OF_CUSTOM_CONFIGS;
557 }
558
IsValidCustomConfig(const std::string & name,const std::string & value)559 bool IsValidCustomConfig(const std::string& name, const std::string& value)
560 {
561 if (!IsValidName(name, MAX_LENGTH_OF_CUSTOM_CONFIG_NAME) || value.length() > MAX_LENGTH_OF_CUSTOM_CONFIG_VALUE) {
562 return false;
563 }
564 return true;
565 }
566
IsValidConfigNameLength(const std::string & configName)567 bool IsValidConfigNameLength(const std::string& configName)
568 {
569 return configName.length() > 0 && configName.length() <= MAX_LENGTH_OF_PROCESSOR_NAME;
570 }
571
VerifyReportConfig(ReportConfig & config)572 int VerifyReportConfig(ReportConfig& config)
573 {
574 const VerifyReportConfigFunc verifyFuncs[] = {
575 VerifyNameOfReportConfig,
576 VerifyRouteInfoOfReportConfig,
577 VerifyAppIdOfReportConfig,
578 VerifyTriggerCondOfReportConfig,
579 VerifyUserIdNamesOfReportConfig,
580 VerifyUserPropertyNamesOfReportConfig,
581 VerifyEventConfigsOfReportConfig,
582 VerifyConfigIdOfReportConfig,
583 VerifyCustomConfigsOfReportConfig,
584 VerifyConfigNameOfReportConfig,
585 };
586 for (const auto verifyFunc : verifyFuncs) {
587 if (verifyFunc(config) != 0) {
588 return -1;
589 }
590 }
591 return 0;
592 }
593
IsApp()594 bool IsApp()
595 {
596 if (getuid() < MIN_APP_UID) {
597 return false;
598 }
599 // use startArkChildProcess create a child process has no ApplicationContext
600 // bundle name is empty means no context
601 std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> context =
602 OHOS::AbilityRuntime::Context::GetApplicationContext();
603 if (context == nullptr || context->GetBundleName().empty()) {
604 HILOG_ERROR(LOG_CORE, "context is null or the bundleName is empty.");
605 return false;
606 }
607 return true;
608 }
609 } // namespace HiviewDFX
610 } // namespace OHOS
611