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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_management.h"
#include <algorithm>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/version.h"
#include "chrome/browser/extensions/extension_management_constants.h"
#include "chrome/browser/extensions/extension_management_internal.h"
#include "chrome/browser/extensions/external_policy_loader.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/permissions_based_management_policy_provider.h"
#include "chrome/browser/extensions/standard_management_policy_provider.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "components/crx_file/id_util.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/api_permission_set.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/url_pattern.h"
#include "url/gurl.h"
namespace extensions {
ExtensionManagement::ExtensionManagement(PrefService* pref_service)
: pref_service_(pref_service) {
pref_change_registrar_.Init(pref_service_);
base::Closure pref_change_callback = base::Bind(
&ExtensionManagement::OnExtensionPrefChanged, base::Unretained(this));
pref_change_registrar_.Add(pref_names::kInstallAllowList,
pref_change_callback);
pref_change_registrar_.Add(pref_names::kInstallDenyList,
pref_change_callback);
pref_change_registrar_.Add(pref_names::kInstallForceList,
pref_change_callback);
pref_change_registrar_.Add(pref_names::kAllowedInstallSites,
pref_change_callback);
pref_change_registrar_.Add(pref_names::kAllowedTypes, pref_change_callback);
pref_change_registrar_.Add(pref_names::kExtensionManagement,
pref_change_callback);
// Note that both |global_settings_| and |default_settings_| will be null
// before first call to Refresh(), so in order to resolve this, Refresh() must
// be called in the initialization of ExtensionManagement.
Refresh();
providers_.push_back(new StandardManagementPolicyProvider(this));
providers_.push_back(new PermissionsBasedManagementPolicyProvider(this));
}
ExtensionManagement::~ExtensionManagement() {
}
void ExtensionManagement::Shutdown() {
pref_change_registrar_.RemoveAll();
pref_service_ = nullptr;
}
void ExtensionManagement::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void ExtensionManagement::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
std::vector<ManagementPolicy::Provider*> ExtensionManagement::GetProviders()
const {
return providers_.get();
}
bool ExtensionManagement::BlacklistedByDefault() const {
return default_settings_->installation_mode == INSTALLATION_BLOCKED;
}
ExtensionManagement::InstallationMode ExtensionManagement::GetInstallationMode(
const Extension* extension) const {
// Check per-extension installation mode setting first.
auto iter_id = settings_by_id_.find(extension->id());
if (iter_id != settings_by_id_.end())
return iter_id->second->installation_mode;
std::string update_url;
// Check per-update-url installation mode setting.
if (extension->manifest()->GetString(manifest_keys::kUpdateURL,
&update_url)) {
auto iter_update_url = settings_by_update_url_.find(update_url);
if (iter_update_url != settings_by_update_url_.end())
return iter_update_url->second->installation_mode;
}
// Fall back to default installation mode setting.
return default_settings_->installation_mode;
}
scoped_ptr<base::DictionaryValue> ExtensionManagement::GetForceInstallList()
const {
scoped_ptr<base::DictionaryValue> install_list(new base::DictionaryValue());
for (SettingsIdMap::const_iterator it = settings_by_id_.begin();
it != settings_by_id_.end();
++it) {
if (it->second->installation_mode == INSTALLATION_FORCED) {
ExternalPolicyLoader::AddExtension(
install_list.get(), it->first, it->second->update_url);
}
}
return install_list.Pass();
}
scoped_ptr<base::DictionaryValue>
ExtensionManagement::GetRecommendedInstallList() const {
scoped_ptr<base::DictionaryValue> install_list(new base::DictionaryValue());
for (SettingsIdMap::const_iterator it = settings_by_id_.begin();
it != settings_by_id_.end();
++it) {
if (it->second->installation_mode == INSTALLATION_RECOMMENDED) {
ExternalPolicyLoader::AddExtension(
install_list.get(), it->first, it->second->update_url);
}
}
return install_list.Pass();
}
bool ExtensionManagement::IsInstallationExplicitlyAllowed(
const ExtensionId& id) const {
SettingsIdMap::const_iterator it = settings_by_id_.find(id);
// No settings explicitly specified for |id|.
if (it == settings_by_id_.end())
return false;
// Checks if the extension is on the automatically installed list or
// install white-list.
InstallationMode mode = it->second->installation_mode;
return mode == INSTALLATION_FORCED || mode == INSTALLATION_RECOMMENDED ||
mode == INSTALLATION_ALLOWED;
}
bool ExtensionManagement::IsOffstoreInstallAllowed(
const GURL& url,
const GURL& referrer_url) const {
// No allowed install sites specified, disallow by default.
if (!global_settings_->has_restricted_install_sources)
return false;
const URLPatternSet& url_patterns = global_settings_->install_sources;
if (!url_patterns.MatchesURL(url))
return false;
// The referrer URL must also be whitelisted, unless the URL has the file
// scheme (there's no referrer for those URLs).
return url.SchemeIsFile() || url_patterns.MatchesURL(referrer_url);
}
bool ExtensionManagement::IsAllowedManifestType(
Manifest::Type manifest_type) const {
if (!global_settings_->has_restricted_allowed_types)
return true;
const std::vector<Manifest::Type>& allowed_types =
global_settings_->allowed_types;
return std::find(allowed_types.begin(), allowed_types.end(), manifest_type) !=
allowed_types.end();
}
APIPermissionSet ExtensionManagement::GetBlockedAPIPermissions(
const Extension* extension) const {
// Fetch per-extension blocked permissions setting.
auto iter_id = settings_by_id_.find(extension->id());
// Fetch per-update-url blocked permissions setting.
std::string update_url;
auto iter_update_url = settings_by_update_url_.end();
if (extension->manifest()->GetString(manifest_keys::kUpdateURL,
&update_url)) {
iter_update_url = settings_by_update_url_.find(update_url);
}
if (iter_id != settings_by_id_.end() &&
iter_update_url != settings_by_update_url_.end()) {
// Blocked permissions setting are specified in both per-extension and
// per-update-url settings, try to merge them.
APIPermissionSet merged;
APIPermissionSet::Union(iter_id->second->blocked_permissions,
iter_update_url->second->blocked_permissions,
&merged);
return merged;
}
// Check whether if in one of them, setting is specified.
if (iter_id != settings_by_id_.end())
return iter_id->second->blocked_permissions;
if (iter_update_url != settings_by_update_url_.end())
return iter_update_url->second->blocked_permissions;
// Fall back to the default blocked permissions setting.
return default_settings_->blocked_permissions;
}
scoped_refptr<const PermissionSet> ExtensionManagement::GetBlockedPermissions(
const Extension* extension) const {
// Only api permissions are supported currently.
return scoped_refptr<const PermissionSet>(new PermissionSet(
GetBlockedAPIPermissions(extension), ManifestPermissionSet(),
URLPatternSet(), URLPatternSet()));
}
bool ExtensionManagement::IsPermissionSetAllowed(
const Extension* extension,
scoped_refptr<const PermissionSet> perms) const {
for (const auto& blocked_api : GetBlockedAPIPermissions(extension)) {
if (perms->HasAPIPermission(blocked_api->id()))
return false;
}
return true;
}
bool ExtensionManagement::CheckMinimumVersion(
const Extension* extension,
std::string* required_version) const {
auto iter = settings_by_id_.find(extension->id());
// If there are no minimum version required for |extension|, return true.
if (iter == settings_by_id_.end() || !iter->second->minimum_version_required)
return true;
bool meets_requirement = extension->version()->CompareTo(
*iter->second->minimum_version_required) >= 0;
// Output a human readable version string for prompting if necessary.
if (!meets_requirement && required_version)
*required_version = iter->second->minimum_version_required->GetString();
return meets_requirement;
}
void ExtensionManagement::Refresh() {
// Load all extension management settings preferences.
const base::ListValue* allowed_list_pref =
static_cast<const base::ListValue*>(LoadPreference(
pref_names::kInstallAllowList, true, base::Value::TYPE_LIST));
// Allow user to use preference to block certain extensions. Note that policy
// managed forcelist or whitelist will always override this.
const base::ListValue* denied_list_pref =
static_cast<const base::ListValue*>(LoadPreference(
pref_names::kInstallDenyList, false, base::Value::TYPE_LIST));
const base::DictionaryValue* forced_list_pref =
static_cast<const base::DictionaryValue*>(LoadPreference(
pref_names::kInstallForceList, true, base::Value::TYPE_DICTIONARY));
const base::ListValue* install_sources_pref =
static_cast<const base::ListValue*>(LoadPreference(
pref_names::kAllowedInstallSites, true, base::Value::TYPE_LIST));
const base::ListValue* allowed_types_pref =
static_cast<const base::ListValue*>(LoadPreference(
pref_names::kAllowedTypes, true, base::Value::TYPE_LIST));
const base::DictionaryValue* dict_pref =
static_cast<const base::DictionaryValue*>(
LoadPreference(pref_names::kExtensionManagement,
true,
base::Value::TYPE_DICTIONARY));
// Reset all settings.
global_settings_.reset(new internal::GlobalSettings());
settings_by_id_.clear();
default_settings_.reset(new internal::IndividualSettings());
// Parse default settings.
const base::StringValue wildcard("*");
if (denied_list_pref &&
denied_list_pref->Find(wildcard) != denied_list_pref->end()) {
default_settings_->installation_mode = INSTALLATION_BLOCKED;
}
const base::DictionaryValue* subdict = NULL;
if (dict_pref &&
dict_pref->GetDictionary(schema_constants::kWildcard, &subdict)) {
if (!default_settings_->Parse(
subdict, internal::IndividualSettings::SCOPE_DEFAULT)) {
LOG(WARNING) << "Default extension management settings parsing error.";
default_settings_->Reset();
}
// Settings from new preference have higher priority over legacy ones.
const base::ListValue* list_value = NULL;
if (subdict->GetList(schema_constants::kInstallSources, &list_value))
install_sources_pref = list_value;
if (subdict->GetList(schema_constants::kAllowedTypes, &list_value))
allowed_types_pref = list_value;
}
// Parse legacy preferences.
ExtensionId id;
if (allowed_list_pref) {
for (base::ListValue::const_iterator it = allowed_list_pref->begin();
it != allowed_list_pref->end(); ++it) {
if ((*it)->GetAsString(&id) && crx_file::id_util::IdIsValid(id))
AccessById(id)->installation_mode = INSTALLATION_ALLOWED;
}
}
if (denied_list_pref) {
for (base::ListValue::const_iterator it = denied_list_pref->begin();
it != denied_list_pref->end(); ++it) {
if ((*it)->GetAsString(&id) && crx_file::id_util::IdIsValid(id))
AccessById(id)->installation_mode = INSTALLATION_BLOCKED;
}
}
if (forced_list_pref) {
std::string update_url;
for (base::DictionaryValue::Iterator it(*forced_list_pref); !it.IsAtEnd();
it.Advance()) {
if (!crx_file::id_util::IdIsValid(it.key()))
continue;
const base::DictionaryValue* dict_value = NULL;
if (it.value().GetAsDictionary(&dict_value) &&
dict_value->GetStringWithoutPathExpansion(
ExternalProviderImpl::kExternalUpdateUrl, &update_url)) {
internal::IndividualSettings* by_id = AccessById(it.key());
by_id->installation_mode = INSTALLATION_FORCED;
by_id->update_url = update_url;
}
}
}
if (install_sources_pref) {
global_settings_->has_restricted_install_sources = true;
for (base::ListValue::const_iterator it = install_sources_pref->begin();
it != install_sources_pref->end(); ++it) {
std::string url_pattern;
if ((*it)->GetAsString(&url_pattern)) {
URLPattern entry(URLPattern::SCHEME_ALL);
if (entry.Parse(url_pattern) == URLPattern::PARSE_SUCCESS) {
global_settings_->install_sources.AddPattern(entry);
} else {
LOG(WARNING) << "Invalid URL pattern in for preference "
<< pref_names::kAllowedInstallSites << ": "
<< url_pattern << ".";
}
}
}
}
if (allowed_types_pref) {
global_settings_->has_restricted_allowed_types = true;
for (base::ListValue::const_iterator it = allowed_types_pref->begin();
it != allowed_types_pref->end(); ++it) {
int int_value;
std::string string_value;
if ((*it)->GetAsInteger(&int_value) && int_value >= 0 &&
int_value < Manifest::Type::NUM_LOAD_TYPES) {
global_settings_->allowed_types.push_back(
static_cast<Manifest::Type>(int_value));
} else if ((*it)->GetAsString(&string_value)) {
Manifest::Type manifest_type =
schema_constants::GetManifestType(string_value);
if (manifest_type != Manifest::TYPE_UNKNOWN)
global_settings_->allowed_types.push_back(manifest_type);
}
}
}
if (dict_pref) {
// Parse new extension management preference.
for (base::DictionaryValue::Iterator iter(*dict_pref); !iter.IsAtEnd();
iter.Advance()) {
if (iter.key() == schema_constants::kWildcard)
continue;
if (!iter.value().GetAsDictionary(&subdict))
continue;
if (StartsWithASCII(iter.key(), schema_constants::kUpdateUrlPrefix,
true)) {
const std::string& update_url =
iter.key().substr(strlen(schema_constants::kUpdateUrlPrefix));
if (!GURL(update_url).is_valid()) {
LOG(WARNING) << "Invalid update URL: " << update_url << ".";
continue;
}
internal::IndividualSettings* by_update_url =
AccessByUpdateUrl(update_url);
if (!by_update_url->Parse(
subdict, internal::IndividualSettings::SCOPE_UPDATE_URL)) {
settings_by_update_url_.erase(update_url);
LOG(WARNING) << "Malformed Extension Management settings for "
"extensions with update url: " << update_url << ".";
}
} else {
const std::string& extension_id = iter.key();
if (!crx_file::id_util::IdIsValid(extension_id)) {
LOG(WARNING) << "Invalid extension ID : " << extension_id << ".";
continue;
}
internal::IndividualSettings* by_id = AccessById(extension_id);
if (!by_id->Parse(subdict,
internal::IndividualSettings::SCOPE_INDIVIDUAL)) {
settings_by_id_.erase(extension_id);
LOG(WARNING) << "Malformed Extension Management settings for "
<< extension_id << ".";
}
}
}
}
}
const base::Value* ExtensionManagement::LoadPreference(
const char* pref_name,
bool force_managed,
base::Value::Type expected_type) {
if (!pref_service_)
return nullptr;
const PrefService::Preference* pref =
pref_service_->FindPreference(pref_name);
if (pref && !pref->IsDefaultValue() &&
(!force_managed || pref->IsManaged())) {
const base::Value* value = pref->GetValue();
if (value && value->IsType(expected_type))
return value;
}
return nullptr;
}
void ExtensionManagement::OnExtensionPrefChanged() {
Refresh();
NotifyExtensionManagementPrefChanged();
}
void ExtensionManagement::NotifyExtensionManagementPrefChanged() {
FOR_EACH_OBSERVER(
Observer, observer_list_, OnExtensionManagementSettingsChanged());
}
internal::IndividualSettings* ExtensionManagement::AccessById(
const ExtensionId& id) {
DCHECK(crx_file::id_util::IdIsValid(id)) << "Invalid ID: " << id;
SettingsIdMap::iterator it = settings_by_id_.find(id);
if (it == settings_by_id_.end()) {
scoped_ptr<internal::IndividualSettings> settings(
new internal::IndividualSettings(default_settings_.get()));
it = settings_by_id_.add(id, settings.Pass()).first;
}
return it->second;
}
internal::IndividualSettings* ExtensionManagement::AccessByUpdateUrl(
const std::string& update_url) {
DCHECK(GURL(update_url).is_valid()) << "Invalid update URL: " << update_url;
SettingsUpdateUrlMap::iterator it = settings_by_update_url_.find(update_url);
if (it == settings_by_update_url_.end()) {
scoped_ptr<internal::IndividualSettings> settings(
new internal::IndividualSettings(default_settings_.get()));
it = settings_by_update_url_.add(update_url, settings.Pass()).first;
}
return it->second;
}
ExtensionManagement* ExtensionManagementFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<ExtensionManagement*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
ExtensionManagementFactory* ExtensionManagementFactory::GetInstance() {
return Singleton<ExtensionManagementFactory>::get();
}
ExtensionManagementFactory::ExtensionManagementFactory()
: BrowserContextKeyedServiceFactory(
"ExtensionManagement",
BrowserContextDependencyManager::GetInstance()) {
}
ExtensionManagementFactory::~ExtensionManagementFactory() {
}
KeyedService* ExtensionManagementFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return new ExtensionManagement(
Profile::FromBrowserContext(context)->GetPrefs());
}
content::BrowserContext* ExtensionManagementFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
void ExtensionManagementFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* user_prefs) {
user_prefs->RegisterDictionaryPref(
pref_names::kExtensionManagement,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
} // namespace extensions
|