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 uint32_t MAX_CONTENT_SIZE = 900;
28 }
29
CheckRiskContent(std::string content)30 bool DataFormat::CheckRiskContent(std::string content)
31 {
32 auto size = static_cast<uint32_t>(content.size());
33 if (size > MAX_CONTENT_SIZE) {
34 SGLOGE("size error, size=%{public}u", 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
ParseConditions(std::string conditions,RequestCondition & reqCondition)46 void DataFormat::ParseConditions(std::string conditions, RequestCondition &reqCondition)
47 {
48 nlohmann::json jsonObj = nlohmann::json::parse(conditions, nullptr, false);
49 if (jsonObj.is_discarded()) {
50 SGLOGE("json parse error");
51 return;
52 }
53 std::set<int64_t> set;
54 auto iter = jsonObj.find(EVENT_CFG_EVENT_ID_KEY);
55 if (iter != jsonObj.end() && (*iter).is_array()) {
56 for (const auto &event : *iter) {
57 if (!event.is_number()) {
58 SGLOGE("event type is error");
59 continue;
60 }
61 set.emplace(event);
62 }
63 }
64
65 for (auto it = set.begin(); it != set.end(); it++) {
66 std::string table = ConfigDataManager::GetInstance().GetTableFromEventId(*it);
67 if (table == RISK_TABLE) {
68 reqCondition.riskEvent.emplace_back(*it);
69 } else if (table == AUDIT_TABLE) {
70 reqCondition.auditEvent.emplace_back(*it);
71 }
72 }
73
74 iter = jsonObj.find("beginTime");
75 if (iter != jsonObj.end() && (*iter).is_string()) {
76 reqCondition.beginTime = *iter;
77 }
78
79 iter = jsonObj.find("endTime");
80 if (iter != jsonObj.end() && (*iter).is_string()) {
81 reqCondition.endTime = *iter;
82 }
83 }
84 }