• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "privacy_controller.h"
17 
18 #include "bundle_mgr_client.h"
19 #include "hiview_logger.h"
20 #include "plugin_factory.h"
21 #include "privacy_manager.h"
22 #include "string_util.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 REGISTER(PrivacyController);
28 DEFINE_LOG_TAG("PrivacyController");
29 constexpr uint8_t THROW_TYPE_EVENT = 1;
30 
IsPreInstallApp(const std::string & bundleName)31 bool IsPreInstallApp(const std::string& bundleName)
32 {
33     AppExecFwk::BundleInfo info;
34     AppExecFwk::BundleMgrClient client;
35     if (!client.GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, info,
36         AppExecFwk::Constants::ALL_USERID)) {
37         return false;
38     }
39     return info.isPreInstallApp;
40 }
41 }
42 
IsBundleNameAllow(const std::string & bundleName,const std::string & allowListFile)43 bool PrivacyController::IsBundleNameAllow(const std::string& bundleName, const std::string& allowListFile)
44 {
45     if (bundleName.empty()) {
46         return true;
47     }
48     if (PrivacyManager::IsBundleNameInList(bundleName, allowListFile)) {
49         return true;
50     }
51     // name of pre-installed bundle is always allowed
52     return IsPreInstallApp(bundleName);
53 }
54 
IsValidParam(std::shared_ptr<SysEvent> & sysEvent,const std::string & paramName,const std::string & allowListFile)55 bool PrivacyController::IsValidParam(std::shared_ptr<SysEvent>& sysEvent,
56     const std::string& paramName, const std::string& allowListFile)
57 {
58     std::vector<std::string> values;
59     if (sysEvent->GetEventStringArrayValue(paramName, values)) {
60         bool isNeedHideParam = false;
61         for (auto& value : values) {
62             if (!IsBundleNameAllow(value, allowListFile)) {
63                 value = "*";
64                 isNeedHideParam = true;
65             }
66         }
67         if (isNeedHideParam) {
68             sysEvent->SetEventValue(paramName, values);
69         }
70         return true;
71     } else {
72         return IsBundleNameAllow(sysEvent->GetEventValue(paramName), allowListFile);
73     }
74 }
75 
OnLoad()76 void PrivacyController::OnLoad()
77 {
78     HIVIEW_LOGI("load privacy controller.");
79 }
80 
OnEvent(std::shared_ptr<Event> & event)81 bool PrivacyController::OnEvent(std::shared_ptr<Event>& event)
82 {
83     auto sysEvent = std::static_pointer_cast<SysEvent>(event);
84     if (sysEvent == nullptr) {
85         return false;
86     }
87     PARAM_INFO_MAP_PTR invalidParams = sysEvent->GetInvalidParams();
88     if (invalidParams == nullptr || invalidParams->empty()) {
89         return true;
90     }
91     std::string droppedParam;
92     for (const auto& iter : *invalidParams) {
93         if (!sysEvent->IsParamExist(iter.first)) {
94             // param not exist in this event, no need check
95             continue;
96         }
97         if (iter.second != nullptr) {
98             if (IsValidParam(sysEvent, iter.first, iter.second->allowListFile)) {
99                 continue;
100             }
101             if (iter.second->throwType == THROW_TYPE_EVENT) {
102                 HIVIEW_LOGI("event[%{public}s|%{public}s|%{public}s] is not allowed",
103                     sysEvent->domain_.c_str(), sysEvent->eventName_.c_str(), iter.first.c_str());
104                 return sysEvent->OnFinish();
105             }
106         }
107         // remove the param from event and record it
108         sysEvent->RemoveParam(iter.first);
109         if (!droppedParam.empty()) {
110             droppedParam.append(",");
111         }
112         droppedParam.append(iter.first);
113     }
114     if (!droppedParam.empty()) {
115         sysEvent->SetEventValue("DroppedParam", droppedParam);
116     }
117     return true;
118 }
119 } // namespace HiviewDFX
120 } // namespace OHOS
121