1 /*
2 * Copyright (c) 2022 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 "data_format.h"
17
18 #include "json_cfg.h"
19 #include "model_analysis_define.h"
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "config_data_manager.h"
23 #include "store_define.h"
24
25 namespace OHOS::Security::SecurityGuard {
26 namespace {
27 constexpr size_t MAX_CONTENT_SIZE = 10240;
28 }
29
CheckRiskContent(std::string content)30 bool DataFormat::CheckRiskContent(std::string content)
31 {
32 auto size = content.size();
33 if (size > MAX_CONTENT_SIZE) {
34 SGLOGE("size error, size=%{public}zu", size);
35 return false;
36 }
37
38 nlohmann::json jsonObj = nlohmann::json::parse(content, nullptr, false);
39 if (jsonObj.is_discarded()) {
40 SGLOGE("json parse error");
41 return false;
42 }
43 return true;
44 }
45
46 // LCOV_EXCL_START
ParseConditions(std::string conditions,RequestCondition & reqCondition)47 void DataFormat::ParseConditions(std::string conditions, RequestCondition &reqCondition)
48 {
49 nlohmann::json jsonObj = nlohmann::json::parse(conditions, nullptr, false);
50 if (jsonObj.is_discarded()) {
51 SGLOGE("json parse error");
52 return;
53 }
54 std::set<int64_t> set;
55 auto iter = jsonObj.find(EVENT_CFG_EVENT_ID_KEY);
56 if (iter != jsonObj.end() && (*iter).is_array()) {
57 for (const auto &event : *iter) {
58 if (!event.is_number()) {
59 SGLOGE("event type is error");
60 continue;
61 }
62 set.emplace(event);
63 }
64 }
65
66 for (auto it = set.begin(); it != set.end(); it++) {
67 std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(*it);
68 if (table == RISK_TABLE) {
69 reqCondition.riskEvent.emplace_back(*it);
70 } else if (table == AUDIT_TABLE) {
71 reqCondition.auditEvent.emplace_back(*it);
72 }
73 }
74
75 iter = jsonObj.find("beginTime");
76 if (iter != jsonObj.end() && (*iter).is_string()) {
77 reqCondition.beginTime = *iter;
78 }
79
80 iter = jsonObj.find("endTime");
81 if (iter != jsonObj.end() && (*iter).is_string()) {
82 reqCondition.endTime = *iter;
83 }
84 }
85 // LCOV_EXCL_STOP
86 }