• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include <sstream>
16 #include <algorithm>
17 #include "domain_url_util.h"
18 #include "white_list_config_mgr.h"
19 #include "app_domain_verify_hilog.h"
20 #include "app_domain_verify_hisysevent.h"
21 
22 namespace OHOS::AppDomainVerify {
23 const static std::string DYNAMIC_WHITE_LIST_PRE_PATH =
24     "/data/service/el1/public/app_domain_verify_mgr_service/whitelist_pref";
25 const static std::string DEFAULT_WHITE_LIST_PRE_PATH = "/system/etc/app_domain_verify/whitelist_pref";
26 const static std::string DEFAULT_URL_KEY = "defaultUrl";
27 const static std::string WHITE_LIST_KEY = "whiteList";
28 const static std::string SPLITOR = ",";
WhiteListConfigMgr()29 WhiteListConfigMgr::WhiteListConfigMgr()
30 {
31     Load();
32 }
~WhiteListConfigMgr()33 WhiteListConfigMgr::~WhiteListConfigMgr()
34 {
35 }
LoadDefault()36 void WhiteListConfigMgr::LoadDefault()
37 {
38     preferences_ = GetPreference(DEFAULT_WHITE_LIST_PRE_PATH);
39     if (preferences_ == nullptr) {
40         UNIVERSAL_ERROR_EVENT(READ_DEFAULT_WHITE_LIST_FAULT);
41         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::Load failed.");
42         return;
43     }
44 
45     defaultWhiteUrl_ = preferences_->GetString(DEFAULT_URL_KEY, "");
46     if (defaultWhiteUrl_.empty()) {
47         UNIVERSAL_ERROR_EVENT(READ_DEFAULT_WHITE_LIST_FAULT);
48         APP_DOMAIN_VERIFY_HILOGW(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::Load defaultWhiteUrl empty.");
49     }
50 }
LoadDynamic()51 void WhiteListConfigMgr::LoadDynamic()
52 {
53     preferences_ = GetPreference(DYNAMIC_WHITE_LIST_PRE_PATH);
54     if (preferences_ == nullptr) {
55         UNIVERSAL_ERROR_EVENT(READ_DYNAMIC_WHITE_LIST_FAULT);
56         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::Load failed.");
57         return;
58     }
59 
60     auto whiteListStr = preferences_->GetString(WHITE_LIST_KEY, "");
61     Split(whiteListStr);
62 }
Load()63 void WhiteListConfigMgr::Load()
64 {
65     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "called");
66     LoadDefault();
67     LoadDynamic();
68     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "called end");
69 }
Split(std::string src)70 void WhiteListConfigMgr::Split(std::string src)
71 {
72     whiteListSet_.clear();
73     if (!SPLITOR.empty()) {
74         size_t pos = 0;
75         while ((pos = src.find(SPLITOR)) != std::string::npos) {
76             // split
77             std::string token = src.substr(0, pos);
78             if (!token.empty()) {
79                 whiteListSet_.insert(token);
80             }
81             src.erase(0, pos + SPLITOR.length());
82         }
83     }
84 
85     if (!src.empty()) {
86         whiteListSet_.insert(src);
87     }
88     if (whiteListSet_.empty()) {
89         APP_DOMAIN_VERIFY_HILOGW(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::Split whiteListSet empty.");
90     }
91 }
GetPreference(const std::string & path)92 std::shared_ptr<NativePreferences::Preferences> WhiteListConfigMgr::GetPreference(const std::string& path)
93 {
94     int status;
95     NativePreferences::Options options(path);
96     std::shared_ptr<NativePreferences::Preferences> preferences = NativePreferences::PreferencesHelper::GetPreferences(
97         options, status);
98     if (status != 0) {
99         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::GetPreference failed.");
100         return nullptr;
101     }
102     return preferences;
103 }
Save()104 bool WhiteListConfigMgr::Save()
105 {
106     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "called");
107     if (preferences_ == nullptr) {
108         APP_DOMAIN_VERIFY_HILOGW(APP_DOMAIN_VERIFY_MODULE_COMMON, "preferences null");
109         return false;
110     }
111     std::stringstream strSteam;
112     for (const auto& element : whiteListSet_) {
113         strSteam << element << ",";
114     }
115     auto ret = preferences_->PutString(WHITE_LIST_KEY, strSteam.str());
116     if (ret != 0) {
117         UNIVERSAL_ERROR_EVENT(WRITE_DYNAMIC_WHITE_LIST_FAULT);
118         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_COMMON, "put dynamic white list error ret:%{public}d.", ret);
119         return false;
120     }
121     preferences_->Flush();
122     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MODULE_COMMON, "WhiteListConfigMgr::Save %{public}s ret%{public}d.",
123         strSteam.str().c_str(), ret);
124     return true;
125 }
126 
IsInWhiteList(const std::string & url)127 bool WhiteListConfigMgr::IsInWhiteList(const std::string& url)
128 {
129     APP_DOMAIN_VERIFY_HILOGD(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called");
130     std::lock_guard<std::mutex> lock(whiteListLock_);
131     bool ret;
132     if (whiteListSet_.empty()) {
133         ret = (url == defaultWhiteUrl_);
134     } else {
135         ret = (whiteListSet_.count(url) != 0) || (url == defaultWhiteUrl_);
136     }
137     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "is count %{public}d url %{private}s",
138         whiteListSet_.count(url) != 0, url.c_str());
139     return ret;
140 }
UpdateWhiteList(const std::unordered_set<std::string> & whiteList)141 void WhiteListConfigMgr::UpdateWhiteList(const std::unordered_set<std::string>& whiteList)
142 {
143     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "called");
144     std::unordered_set<std::string> filtedWhiteList;
145     std::for_each(whiteList.begin(), whiteList.end(), [&filtedWhiteList](const std::string& element) {
146         if (UrlUtil::IsValidUrl(element)) {
147             filtedWhiteList.insert(element);
148         }
149     });
150     std::lock_guard<std::mutex> lock(whiteListLock_);
151     whiteListSet_ = filtedWhiteList;
152     if (!whiteListSet_.empty()) {
153         if (!Save()) {
154             UNIVERSAL_ERROR_EVENT(WRITE_DYNAMIC_WHITE_LIST_FAULT);
155             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "save white list failed.");
156         }
157     }
158 }
159 
160 }
161