1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/device_api/managed_configuration_api.h"
#include <memory>
#include <optional>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_string_value_serializer.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/device_api/managed_configuration_store.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/origin.h"
namespace {
constexpr char kManagedConfigurationDirectoryName[] = "Managed Configuration";
// Maximum configuration size is 5MB.
constexpr int kMaxConfigurationFileSize = 5 * 1024 * 1024;
constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("managed_configuration_loader",
R"(
semantics {
sender: "Web App Managed Configuration Downloader"
description:
"Fetches JSON managed configuration from the DM server, for the "
"origins configured in the ManagedConfigurationPerOrigin policy. "
"This enables the navigatior.device.getManagedConfiguration(keys) "
"JavaScript function, to obtain the values of this configuration "
"that correspond to provided keys. This configuration can only be "
"set for force-installed web applications, which are defined by "
"WebAppInstallForceList policy."
trigger:
"Changes in the ManagedConfigurationPerOrigin policy."
data: "No user data."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting:
"This feature cannot be disabled in settings, but it will only be "
"sent if the device administrator sets up a configuration policy "
"for a particular origin.",
chrome_policy {
ManagedConfigurationPerOrigin {
ManagedConfigurationPerOrigin: "[]"
}
}
})");
// Converts url::Origin into the key that can be used for filenames/dictionary
// keys.
std::string GetOriginEncoded(const url::Origin& origin) {
return base::HexEncode(origin.Serialize());
}
} // namespace
const char ManagedConfigurationAPI::kOriginKey[] = "origin";
const char ManagedConfigurationAPI::kManagedConfigurationUrlKey[] =
"managed_configuration_url";
const char ManagedConfigurationAPI::kManagedConfigurationHashKey[] =
"managed_configuration_hash";
class ManagedConfigurationAPI::ManagedConfigurationDownloader {
public:
explicit ManagedConfigurationDownloader(const std::string& data_hash)
: data_hash_(data_hash) {}
~ManagedConfigurationDownloader() = default;
ManagedConfigurationDownloader(const ManagedConfigurationDownloader&) =
delete;
ManagedConfigurationDownloader& operator=(
const ManagedConfigurationDownloader&) = delete;
void Fetch(const std::string& data_url,
base::OnceCallback<void(std::unique_ptr<std::string>)> callback) {
// URLLoaders should be created at UI thread.
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = GURL(data_url);
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
simple_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), kTrafficAnnotation);
simple_loader_->SetRetryOptions(
/* max_retries=*/3,
network::SimpleURLLoader::RETRY_ON_5XX |
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
SystemNetworkContextManager* system_network_context_manager =
g_browser_process->system_network_context_manager();
network::mojom::URLLoaderFactory* loader_factory =
system_network_context_manager->GetURLLoaderFactory();
simple_loader_->DownloadToString(loader_factory, std::move(callback),
kMaxConfigurationFileSize);
}
std::string hash() const { return data_hash_; }
private:
std::unique_ptr<network::SimpleURLLoader> simple_loader_;
const std::string data_hash_;
};
ManagedConfigurationAPI::ManagedConfigurationAPI(Profile* profile)
: profile_(profile),
stores_path_(
profile->GetPath().AppendASCII(kManagedConfigurationDirectoryName)) {
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(profile_->GetPrefs());
pref_change_registrar_->Add(
prefs::kManagedConfigurationPerOrigin,
base::BindRepeating(
&ManagedConfigurationAPI::OnConfigurationPolicyChanged,
weak_ptr_factory_.GetWeakPtr()));
OnConfigurationPolicyChanged();
}
ManagedConfigurationAPI::~ManagedConfigurationAPI() = default;
// static
void ManagedConfigurationAPI::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterListPref(prefs::kManagedConfigurationPerOrigin);
registry->RegisterDictionaryPref(
prefs::kLastManagedConfigurationHashForOrigin);
}
void ManagedConfigurationAPI::GetOriginPolicyConfiguration(
const url::Origin& origin,
const std::vector<std::string>& keys,
base::OnceCallback<void(std::optional<base::Value::Dict>)> callback) {
if (!CanHaveManagedStore(origin)) {
std::move(callback).Run(std::nullopt);
return;
}
if (!base::Contains(store_map_, origin)) {
std::move(callback).Run(std::nullopt);
return;
}
store_map_[origin]
.AsyncCall(&ManagedConfigurationStore::Get)
.WithArgs(keys)
.Then(std::move(callback));
}
void ManagedConfigurationAPI::AddObserver(Observer* observer) {
url::Origin origin = observer->GetOrigin();
if (CanHaveManagedStore(origin)) {
MaybeCreateStoreForOrigin(origin);
observers_[origin].AddObserver(observer);
} else {
unmanaged_observers_.insert(observer);
}
}
void ManagedConfigurationAPI::RemoveObserver(Observer* observer) {
auto iter = unmanaged_observers_.find(observer);
if (iter != unmanaged_observers_.end()) {
unmanaged_observers_.erase(iter);
return;
}
observers_[observer->GetOrigin()].RemoveObserver(observer);
}
bool ManagedConfigurationAPI::CanHaveManagedStore(const url::Origin& origin) {
return base::Contains(managed_origins_, origin);
}
const std::set<url::Origin>& ManagedConfigurationAPI::GetManagedOrigins()
const {
return managed_origins_;
}
void ManagedConfigurationAPI::OnConfigurationPolicyChanged() {
const base::Value::List& managed_configurations =
profile_->GetPrefs()->GetList(prefs::kManagedConfigurationPerOrigin);
std::set<url::Origin> current_origins;
for (const auto& entry : managed_configurations) {
const auto& entry_dict = entry.GetDict();
const std::string* url = entry_dict.FindString(kOriginKey);
if (!url) {
continue;
}
const url::Origin origin = url::Origin::Create(GURL(*url));
if (origin.opaque()) {
continue;
}
const std::string* configuration_url =
entry_dict.FindString(kManagedConfigurationUrlKey);
const std::string* configuration_hash =
entry_dict.FindString(kManagedConfigurationHashKey);
current_origins.insert(origin);
if (!configuration_url || !configuration_hash) {
continue;
}
UpdateStoredDataForOrigin(origin, *configuration_url, *configuration_hash);
}
// We need to clean configurations for origins that got their entry removed.
for (const auto& store_entry : store_map_) {
if (!base::Contains(current_origins, store_entry.first)) {
UpdateStoredDataForOrigin(store_entry.first, std::string(),
std::string());
}
}
managed_origins_.swap(current_origins);
PromoteObservers();
}
void ManagedConfigurationAPI::MaybeCreateStoreForOrigin(
const url::Origin& origin) {
if (base::Contains(store_map_, origin)) {
return;
}
// Create the store now, and serve the cached policy until the PolicyService
// sends updated values.
store_map_[origin] = base::SequenceBound<ManagedConfigurationStore>(
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}),
origin, GetStoreLocation(origin));
}
base::FilePath ManagedConfigurationAPI::GetStoreLocation(
const url::Origin& origin) {
return stores_path_.AppendASCII(GetOriginEncoded(origin));
}
void ManagedConfigurationAPI::UpdateStoredDataForOrigin(
const url::Origin& origin,
const std::string& configuration_url,
const std::string& configuration_hash) {
const std::string* last_hash_value =
profile_->GetPrefs()
->GetDict(prefs::kLastManagedConfigurationHashForOrigin)
.FindString(GetOriginEncoded(origin));
// Nothing to be stored here, the hash value is the same.
if (last_hash_value && *last_hash_value == configuration_hash) {
return;
}
if (configuration_url.empty()) {
PostStoreConfiguration(origin, base::Value::Dict());
return;
}
// Check whether there is already a downloader.
if (downloaders_[origin]) {
// If it downloads the same data already, do nothing.
if (downloaders_[origin]->hash() == configuration_hash) {
return;
}
// Cancel it otherwise.
downloaders_[origin].reset();
}
downloaders_[origin] =
std::make_unique<ManagedConfigurationDownloader>(configuration_hash);
downloaders_[origin]->Fetch(
configuration_url, base::BindOnce(&ManagedConfigurationAPI::DecodeData,
weak_ptr_factory_.GetWeakPtr(), origin,
configuration_hash));
}
void ManagedConfigurationAPI::DecodeData(const url::Origin& origin,
const std::string& url_hash,
std::unique_ptr<std::string> data) {
downloaders_[origin].reset();
if (!data) {
return;
}
// First, we have to parse JSON file in an isolated sandbox so that we don't
// have to worry about potentially risky values.
data_decoder::DataDecoder::ParseJsonIsolated(
*data,
base::BindOnce(&ManagedConfigurationAPI::ProcessDecodedConfiguration,
weak_ptr_factory_.GetWeakPtr(), origin, url_hash));
}
void ManagedConfigurationAPI::ProcessDecodedConfiguration(
const url::Origin& origin,
const std::string& url_hash,
const data_decoder::DataDecoder::ValueOrError decoding_result) {
if (!decoding_result.has_value() || !decoding_result->is_dict()) {
VLOG(1) << "Could not fetch managed configuration for app with origin = "
<< origin.Serialize();
PostStoreConfiguration(origin, base::Value::Dict());
return;
}
ScopedDictPrefUpdate update(profile_->GetPrefs(),
prefs::kLastManagedConfigurationHashForOrigin);
update->Set(GetOriginEncoded(origin), url_hash);
// We need to transform each value into a string.
base::Value::Dict result_dict;
for (auto item : decoding_result->GetDict()) {
std::string result;
JSONStringValueSerializer serializer(&result);
serializer.Serialize(item.second);
result_dict.SetByDottedPath(item.first, result);
}
PostStoreConfiguration(origin, std::move(result_dict));
}
void ManagedConfigurationAPI::PostStoreConfiguration(
const url::Origin& origin,
base::Value::Dict configuration) {
MaybeCreateStoreForOrigin(origin);
store_map_[origin]
.AsyncCall(&ManagedConfigurationStore::SetCurrentPolicy)
.WithArgs(std::move(configuration))
.Then(base::BindOnce(
&ManagedConfigurationAPI::InformObserversIfConfigurationChanged,
weak_ptr_factory_.GetWeakPtr(), origin));
}
void ManagedConfigurationAPI::InformObserversIfConfigurationChanged(
const url::Origin& origin,
bool has_changed) {
if (!has_changed || !base::Contains(observers_, origin)) {
return;
}
for (auto& observer : observers_[origin]) {
observer.OnManagedConfigurationChanged();
}
}
void ManagedConfigurationAPI::PromoteObservers() {
for (auto it = unmanaged_observers_.begin();
it != unmanaged_observers_.end();) {
if (CanHaveManagedStore((*it)->GetOrigin())) {
auto* observer = (*it).get();
it = unmanaged_observers_.erase(it);
AddObserver(observer);
} else {
++it;
}
}
}
|