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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "chrome/browser/extensions/extension_management.h"
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/syslog_logging.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "base/version.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/cws_info_service.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/forced_extensions/install_stage_tracker_factory.h"
#include "chrome/browser/extensions/managed_installation_mode.h"
#include "chrome/browser/extensions/managed_toolbar_pin_mode.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 "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "components/crx_file/id_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/content_switches.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_url_handlers.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"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/profiles/profile_helper.h"
#else
#include "components/enterprise/browser/reporting/common_pref_names.h"
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#include "chrome/browser/extensions/management/management_util.h"
#endif
namespace extensions {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
// Disables off-store force-installed extensions in low trust environments.
BASE_FEATURE(kDisableOffstoreForceInstalledExtensionsInLowTrustEnviroment,
"DisableOffstoreForceInstalledExtensionsInLowTrustEnviroment",
base::FEATURE_ENABLED_BY_DEFAULT);
#endif
ExtensionManagement::ExtensionManagement(Profile* profile)
: profile_(profile), pref_service_(profile_->GetPrefs()) {
TRACE_EVENT0("browser,startup",
"ExtensionManagement::ExtensionManagement::ctor");
#if BUILDFLAG(IS_CHROMEOS)
is_signin_profile_ = ash::ProfileHelper::IsSigninProfile(profile);
#endif
pref_change_registrar_.Init(pref_service_);
base::RepeatingClosure pref_change_callback = base::BindRepeating(
&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);
pref_change_registrar_.Add(prefs::kCloudExtensionRequestEnabled,
pref_change_callback);
#if !BUILDFLAG(IS_CHROMEOS)
pref_change_registrar_.Add(enterprise_reporting::kCloudReportingEnabled,
pref_change_callback);
#endif
pref_change_registrar_.Add(pref_names::kManifestV2Availability,
pref_change_callback);
pref_change_registrar_.Add(pref_names::kExtensionUnpublishedAvailability,
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();
ReportExtensionManagementInstallCreationStage(
InstallStageTracker::InstallCreationStage::
NOTIFIED_FROM_MANAGEMENT_INITIAL_CREATION_FORCED,
InstallStageTracker::InstallCreationStage::
NOTIFIED_FROM_MANAGEMENT_INITIAL_CREATION_NOT_FORCED);
providers_.push_back(
std::make_unique<StandardManagementPolicyProvider>(this, profile_.get()));
providers_.push_back(
std::make_unique<PermissionsBasedManagementPolicyProvider>(this));
}
ExtensionManagement::~ExtensionManagement() = default;
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);
}
const std::vector<std::unique_ptr<ManagementPolicy::Provider>>&
ExtensionManagement::GetProviders() const {
return providers_;
}
bool ExtensionManagement::BlocklistedByDefault() const {
return (default_settings_->installation_mode ==
ManagedInstallationMode::kBlocked ||
default_settings_->installation_mode ==
ManagedInstallationMode::kRemoved);
}
ManagedInstallationMode ExtensionManagement::GetInstallationMode(
const Extension* extension) {
const std::string* update_url =
extension->manifest()->FindStringPath(manifest_keys::kUpdateURL);
return GetInstallationMode(extension->id(),
update_url ? *update_url : std::string());
}
ManagedInstallationMode ExtensionManagement::GetInstallationMode(
const ExtensionId& extension_id,
const std::string& update_url) {
// Check per-extension installation mode setting first.
auto* setting = GetSettingsForId(extension_id);
if (setting)
return setting->installation_mode;
// Check per-update-url installation mode setting.
if (!update_url.empty()) {
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;
}
base::Value::Dict ExtensionManagement::GetForceInstallList() const {
return GetInstallListByMode(ManagedInstallationMode::kForced);
}
base::Value::Dict ExtensionManagement::GetRecommendedInstallList() const {
return GetInstallListByMode(ManagedInstallationMode::kRecommended);
}
bool ExtensionManagement::HasAllowlistedExtension() {
// TODO(rdevlin.cronin): investigate implementation correctness per
// https://crbug.com/1258180.
if (default_settings_->installation_mode !=
ManagedInstallationMode::kBlocked &&
default_settings_->installation_mode !=
ManagedInstallationMode::kRemoved) {
return true;
}
for (const auto& it : settings_by_id_) {
if (it.second->installation_mode == ManagedInstallationMode::kAllowed) {
return true;
}
}
// If there are deferred extensions try loading them.
while (!deferred_ids_.empty()) {
auto extension_id = *deferred_ids_.begin();
// This will remove the entry from |deferred_ids_|.
LoadDeferredExtensionSetting(extension_id);
DCHECK(!base::Contains(deferred_ids_, extension_id));
if (AccessById(extension_id)->installation_mode ==
ManagedInstallationMode::kAllowed) {
return true;
}
}
return false;
}
bool ExtensionManagement::IsUpdateUrlOverridden(const ExtensionId& id) {
auto* setting = GetSettingsForId(id);
// No settings explicitly specified for |id|.
return setting && setting->override_update_url;
}
GURL ExtensionManagement::GetEffectiveUpdateURL(const Extension& extension) {
if (IsUpdateUrlOverridden(extension.id())) {
DCHECK(!extension.was_installed_by_default())
<< "Update URL should not be overridden for default-installed "
"extensions!";
auto* setting = GetSettingsForId(extension.id());
DCHECK(setting);
const GURL update_url(setting->update_url);
// It's important that we never override a non-webstore update URL to be
// the webstore URL. Otherwise, a policy may inadvertently cause
// non-webstore extensions to be treated as from-webstore (including content
// verification, report abuse options, etc).
DCHECK(!extension_urls::IsWebstoreUpdateUrl(update_url))
<< "Update URL cannot be overridden to be the webstore URL!";
return update_url;
}
return ManifestURL::GetUpdateURL(&extension);
}
bool ExtensionManagement::UpdatesFromWebstore(const Extension& extension) {
const bool is_webstore_url = extension_urls::IsWebstoreUpdateUrl(
GURL(GetEffectiveUpdateURL(extension)));
if (is_webstore_url) {
DCHECK(!IsUpdateUrlOverridden(extension.id()))
<< "An extension's update URL cannot be overridden to the webstore.";
}
return is_webstore_url;
}
bool ExtensionManagement::IsInstallationExplicitlyAllowed(
const ExtensionId& id) {
auto* setting = GetSettingsForId(id);
// No settings explicitly specified for |id|.
if (setting == nullptr)
return false;
// Checks if the extension is on the automatically installed list or
// install allow-list.
ManagedInstallationMode mode = setting->installation_mode;
return mode == ManagedInstallationMode::kForced ||
mode == ManagedInstallationMode::kRecommended ||
mode == ManagedInstallationMode::kAllowed;
}
bool ExtensionManagement::IsInstallationExplicitlyBlocked(
const ExtensionId& id) {
auto* setting = GetSettingsForId(id);
// No settings explicitly specified for |id|.
if (setting == nullptr)
return false;
// Checks if the extension is listed as blocked or removed.
ManagedInstallationMode mode = setting->installation_mode;
return mode == ManagedInstallationMode::kBlocked ||
mode == ManagedInstallationMode::kRemoved;
}
bool ExtensionManagement::IsOffstoreInstallAllowed(
const GURL& url,
const GURL& referrer_url) const {
// No allowed install sites specified, disallow by default.
if (!global_settings_->install_sources.has_value())
return false;
const URLPatternSet& url_patterns = *global_settings_->install_sources;
if (!url_patterns.MatchesURL(url))
return false;
// The referrer URL must also be allowlisted, 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 std::string& extension_id) const {
#if BUILDFLAG(ENABLE_EXTENSIONS)
// If a managed theme has been set for the current profile, theme extension
// installations are not allowed.
if (manifest_type == Manifest::Type::TYPE_THEME &&
ThemeServiceFactory::GetForProfile(profile_)->UsingPolicyTheme())
return false;
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
if (!global_settings_->allowed_types.has_value())
return true;
const std::vector<Manifest::Type>& allowed_types =
*global_settings_->allowed_types;
return base::Contains(allowed_types, manifest_type);
}
bool ExtensionManagement::IsAllowedManifestVersion(
int manifest_version,
const std::string& extension_id,
Manifest::Type manifest_type) {
bool enabled_by_default =
!base::FeatureList::IsEnabled(
extensions_features::kExtensionsManifestV3Only) ||
manifest_version >= 3;
// Manifest version policy only supports normal extensions and Chrome OS login
// screen extension.
if (manifest_type != Manifest::Type::TYPE_EXTENSION &&
manifest_type != Manifest::Type::TYPE_LOGIN_SCREEN_EXTENSION) {
return enabled_by_default;
}
switch (global_settings_->manifest_v2_setting) {
case internal::GlobalSettings::ManifestV2Setting::kDefault:
return enabled_by_default;
case internal::GlobalSettings::ManifestV2Setting::kDisabled:
return manifest_version >= 3;
case internal::GlobalSettings::ManifestV2Setting::kEnabled:
return true;
case internal::GlobalSettings::ManifestV2Setting::kEnabledForForceInstalled:
auto installation_mode =
GetInstallationMode(extension_id, /*update_url=*/std::string());
return manifest_version >= 3 ||
installation_mode == ManagedInstallationMode::kForced ||
installation_mode == ManagedInstallationMode::kRecommended;
}
}
bool ExtensionManagement::IsAllowedManifestVersion(const Extension* extension) {
return IsAllowedManifestVersion(extension->manifest_version(),
extension->id(), extension->GetType());
}
bool ExtensionManagement::IsExemptFromMV2DeprecationByPolicy(
int manifest_version,
const std::string& extension_id,
Manifest::Type manifest_type) {
// This policy only affects MV2 extensions.
if (manifest_version != 2) {
return false;
}
if (manifest_type != Manifest::Type::TYPE_EXTENSION &&
manifest_type != Manifest::Type::TYPE_LOGIN_SCREEN_EXTENSION) {
return false;
}
switch (global_settings_->manifest_v2_setting) {
case internal::GlobalSettings::ManifestV2Setting::kDefault:
// Default browser behavior. Not exempt.
return false;
case internal::GlobalSettings::ManifestV2Setting::kDisabled:
// All MV2 extensions are disallowed. Not exempt.
return false;
case internal::GlobalSettings::ManifestV2Setting::kEnabled:
// All MV2 extensions are allowed. Exempt.
return true;
case internal::GlobalSettings::ManifestV2Setting::kEnabledForForceInstalled:
// Force-installed MV2 extensions are allowed. Exempt if it's a force-
// installed extension only.
auto installation_mode =
GetInstallationMode(extension_id, /*update_url=*/std::string());
return installation_mode == ManagedInstallationMode::kForced ||
installation_mode == ManagedInstallationMode::kRecommended;
}
return false;
}
bool ExtensionManagement::IsAllowedByUnpublishedAvailabilityPolicy(
const Extension* extension) {
// This policy only applies to extensions that update from CWS.
if (!UpdatesFromWebstore(*extension)) {
return true;
}
if (global_settings_->unpublished_availability_setting ==
internal::GlobalSettings::UnpublishedAvailability::kAllowUnpublished) {
return true;
}
if (!cws_info_service_) {
cws_info_service_ = CWSInfoService::Get(profile_);
}
// Return the current published status of the extension in CWS if available.
// Otherwise assume the extension is currently published and return true.
// Ignore extensions taken down for malware as they are blocklisted and
// unloaded independently of policy.
// Current publish status may not available if the policy setting just changed
// to |kDisableUnpublished|. The actual publish status will be retrieved
// by CWSInfoService separately and will trigger this same policy check.
std::optional<CWSInfoServiceInterface::CWSInfo> cws_info =
cws_info_service_->GetCWSInfo(*extension);
if (cws_info.has_value() && cws_info->is_present &&
cws_info->violation_type !=
CWSInfoServiceInterface::CWSViolationType::kMalware) {
return cws_info->is_live;
}
return true;
}
bool ExtensionManagement::IsAllowedByUnpackedDeveloperModePolicy(
const Extension& extension) {
if (!base::FeatureList::IsEnabled(
extensions_features::kExtensionDisableUnsupportedDeveloper)) {
return true;
}
if (!extension.is_extension()) {
return true;
}
if (extension.location() != mojom::ManifestLocation::kUnpacked) {
return true;
}
// Allow extensions loaded from DevTools' "Extensions.loadUnpacked" command.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableUnsafeExtensionDebugging)) {
return true;
}
bool in_developer_mode =
profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode);
return in_developer_mode;
}
bool ExtensionManagement::IsForceInstalledInLowTrustEnvironment(
const Extension& extension) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
if (GetInstallationMode(&extension) != ManagedInstallationMode::kForced) {
return false;
}
if (!Manifest::IsPolicyLocation(extension.location())) {
return false;
}
return GetHigherManagementAuthorityTrustworthiness(profile_) <
policy::ManagementAuthorityTrustworthiness::TRUSTED;
#else
return false;
#endif
}
bool ExtensionManagement::ShouldBlockForceInstalledOffstoreExtension(
const Extension& extension) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
if (!base::FeatureList::IsEnabled(
kDisableOffstoreForceInstalledExtensionsInLowTrustEnviroment)) {
return false;
}
if (extension.from_webstore() || UpdatesFromWebstore(extension)) {
return false;
}
if (GetInstallationMode(&extension) != ManagedInstallationMode::kForced) {
return false;
}
if (!Manifest::IsPolicyLocation(extension.location())) {
return false;
}
return GetHigherManagementAuthorityTrustworthiness(profile_) <
policy::ManagementAuthorityTrustworthiness::TRUSTED;
#else
return false;
#endif
}
APIPermissionSet ExtensionManagement::GetBlockedAPIPermissions(
const Extension* extension) {
const std::string* update_url =
extension->manifest()->FindStringPath(manifest_keys::kUpdateURL);
return GetBlockedAPIPermissions(extension->id(),
update_url ? *update_url : std::string());
}
APIPermissionSet ExtensionManagement::GetBlockedAPIPermissions(
const ExtensionId& extension_id,
const std::string& update_url) {
// Fetch per-extension blocked permissions setting.
auto* setting = GetSettingsForId(extension_id);
// Fetch per-update-url blocked permissions setting.
auto iter_update_url = settings_by_update_url_.end();
if (!update_url.empty())
iter_update_url = settings_by_update_url_.find(update_url);
if (setting && 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(setting->blocked_permissions,
iter_update_url->second->blocked_permissions,
&merged);
return merged;
}
// Check whether if in one of them, setting is specified.
if (setting)
return setting->blocked_permissions.Clone();
if (iter_update_url != settings_by_update_url_.end())
return iter_update_url->second->blocked_permissions.Clone();
// Fall back to the default blocked permissions setting.
return default_settings_->blocked_permissions.Clone();
}
const URLPatternSet& ExtensionManagement::GetDefaultPolicyBlockedHosts() const {
return default_settings_->policy_blocked_hosts;
}
const URLPatternSet& ExtensionManagement::GetDefaultPolicyAllowedHosts() const {
return default_settings_->policy_allowed_hosts;
}
const URLPatternSet& ExtensionManagement::GetPolicyBlockedHosts(
const Extension* extension) {
auto* setting = GetSettingsForId(extension->id());
if (setting)
return setting->policy_blocked_hosts;
return default_settings_->policy_blocked_hosts;
}
const URLPatternSet& ExtensionManagement::GetPolicyAllowedHosts(
const Extension* extension) {
auto* setting = GetSettingsForId(extension->id());
if (setting)
return setting->policy_allowed_hosts;
return default_settings_->policy_allowed_hosts;
}
bool ExtensionManagement::UsesDefaultPolicyHostRestrictions(
const Extension* extension) {
return GetSettingsForId(extension->id()) == nullptr;
}
std::unique_ptr<const PermissionSet> ExtensionManagement::GetBlockedPermissions(
const Extension* extension) {
// Only api permissions are supported currently.
return std::unique_ptr<const PermissionSet>(new PermissionSet(
GetBlockedAPIPermissions(extension), ManifestPermissionSet(),
URLPatternSet(), URLPatternSet()));
}
bool ExtensionManagement::IsPermissionSetAllowed(const Extension* extension,
const PermissionSet& perms) {
const std::string* update_url =
extension->manifest()->FindStringPath(manifest_keys::kUpdateURL);
return IsPermissionSetAllowed(
extension->id(), update_url ? *update_url : std::string(), perms);
}
bool ExtensionManagement::IsPermissionSetAllowed(
const ExtensionId& extension_id,
const std::string& update_url,
const PermissionSet& perms) {
for (const APIPermission* blocked_api :
GetBlockedAPIPermissions(extension_id, update_url)) {
if (perms.HasAPIPermission(blocked_api->id()))
return false;
}
return true;
}
const std::string ExtensionManagement::BlockedInstallMessage(
const ExtensionId& id) {
auto* setting = GetSettingsForId(id);
if (setting)
return setting->blocked_install_message;
return default_settings_->blocked_install_message;
}
ExtensionIdSet ExtensionManagement::GetForcePinnedList() const {
ExtensionIdSet force_pinned_list;
for (const auto& entry : settings_by_id_) {
if (entry.second->toolbar_pin == ManagedToolbarPinMode::kForcePinned) {
force_pinned_list.insert(entry.first);
}
}
return force_pinned_list;
}
bool ExtensionManagement::IsFileUrlNavigationAllowed(const ExtensionId& id) {
auto* setting = GetSettingsForId(id);
return setting && setting->file_url_navigation_allowed;
}
bool ExtensionManagement::CheckMinimumVersion(const Extension* extension,
std::string* required_version) {
auto* setting = GetSettingsForId(extension->id());
// If there are no minimum version required for |extension|, return true.
if (!setting || !setting->minimum_version_required)
return true;
bool meets_requirement =
extension->version().CompareTo(*setting->minimum_version_required) >= 0;
// Output a human readable version string for prompting if necessary.
if (!meets_requirement && required_version)
*required_version = setting->minimum_version_required->GetString();
return meets_requirement;
}
void ExtensionManagement::Refresh() {
TRACE_EVENT0("browser,startup", "ExtensionManagement::Refresh");
SCOPED_UMA_HISTOGRAM_TIMER("Extensions.Management_Refresh");
// Load all extension management settings preferences.
const base::Value::List* allowed_list_pref =
LoadListPreference(pref_names::kInstallAllowList, true);
// Allow user to use preference to block certain extensions. Note that policy
// managed forcelist or allowlist will always override this.
const base::Value::List* denied_list_pref =
LoadListPreference(pref_names::kInstallDenyList, false);
const base::Value::Dict* forced_list_pref =
LoadDictPreference(pref_names::kInstallForceList, true);
const base::Value::List* install_sources_pref =
LoadListPreference(pref_names::kAllowedInstallSites, true);
const base::Value::List* allowed_types_pref =
LoadListPreference(pref_names::kAllowedTypes, true);
const base::Value::Dict* dict_pref =
LoadDictPreference(pref_names::kExtensionManagement, true);
const base::Value* extension_request_pref = LoadPreference(
prefs::kCloudExtensionRequestEnabled, false, base::Value::Type::BOOLEAN);
const base::Value* manifest_v2_pref =
LoadPreference(pref_names::kManifestV2Availability,
/*force_managed=*/true, base::Value::Type::INTEGER);
const base::Value* unpublished_availability_pref =
LoadPreference(pref_names::kExtensionUnpublishedAvailability,
/*force_managed=*/true, base::Value::Type::INTEGER);
// Reset all settings.
global_settings_ = std::make_unique<internal::GlobalSettings>();
settings_by_id_.clear();
deferred_ids_.clear();
default_settings_ = std::make_unique<internal::IndividualSettings>();
// Parse default settings.
const base::Value wildcard("*");
if ((denied_list_pref && base::Contains(*denied_list_pref, wildcard)) ||
(extension_request_pref && extension_request_pref->GetBool())) {
default_settings_->installation_mode = ManagedInstallationMode::kBlocked;
}
if (const base::Value::Dict* subdict =
dict_pref ? dict_pref->FindDict(schema_constants::kWildcard)
: nullptr) {
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::Value::List* list_value =
subdict->FindList(schema_constants::kInstallSources);
if (list_value)
install_sources_pref = list_value;
list_value = subdict->FindList(schema_constants::kAllowedTypes);
if (list_value)
allowed_types_pref = list_value;
}
// Parse legacy preferences.
if (allowed_list_pref) {
for (const auto& entry : *allowed_list_pref) {
if (entry.is_string() && crx_file::id_util::IdIsValid(entry.GetString()))
AccessById(entry.GetString())->installation_mode =
ManagedInstallationMode::kAllowed;
}
}
if (denied_list_pref) {
for (const auto& entry : *denied_list_pref) {
if (entry.is_string() && crx_file::id_util::IdIsValid(entry.GetString()))
AccessById(entry.GetString())->installation_mode =
ManagedInstallationMode::kBlocked;
}
}
UpdateForcedExtensions(forced_list_pref);
if (install_sources_pref) {
global_settings_->install_sources = URLPatternSet();
for (const auto& entry : *install_sources_pref) {
if (entry.is_string()) {
std::string url_pattern = entry.GetString();
URLPattern pattern(URLPattern::SCHEME_ALL);
if (pattern.Parse(url_pattern) == URLPattern::ParseResult::kSuccess) {
global_settings_->install_sources->AddPattern(pattern);
} else {
LOG(WARNING) << "Invalid URL pattern in for preference "
<< pref_names::kAllowedInstallSites << ": "
<< url_pattern << ".";
}
}
}
}
if (allowed_types_pref) {
global_settings_->allowed_types.emplace();
for (const auto& entry : *allowed_types_pref) {
if (entry.is_int() && entry.GetInt() >= 0 &&
entry.GetInt() < Manifest::Type::NUM_LOAD_TYPES) {
global_settings_->allowed_types->push_back(
static_cast<Manifest::Type>(entry.GetInt()));
} else if (entry.is_string()) {
Manifest::Type manifest_type =
schema_constants::GetManifestType(entry.GetString());
if (manifest_type != Manifest::TYPE_UNKNOWN)
global_settings_->allowed_types->push_back(manifest_type);
}
}
}
if (manifest_v2_pref) {
global_settings_->manifest_v2_setting =
static_cast<internal::GlobalSettings::ManifestV2Setting>(
manifest_v2_pref->GetInt());
}
if (unpublished_availability_pref) {
global_settings_->unpublished_availability_setting =
static_cast<internal::GlobalSettings::UnpublishedAvailability>(
unpublished_availability_pref->GetInt());
}
if (dict_pref) {
// Parse new extension management preference.
std::unordered_set<std::string> installed_extensions;
auto* extension_prefs = ExtensionPrefs::Get(profile_);
auto extensions_info = extension_prefs->GetInstalledExtensionsInfo();
for (const auto& extension_info : extensions_info) {
installed_extensions.insert(extension_info.extension_id);
}
for (auto iter : *dict_pref) {
if (iter.first == schema_constants::kWildcard)
continue;
const base::Value::Dict* subdict = iter.second.GetIfDict();
if (!subdict)
continue;
std::optional<std::string_view> remainder =
base::RemovePrefix(iter.first, schema_constants::kUpdateUrlPrefix);
if (remainder) {
const std::string update_url(*remainder);
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 {
std::vector<std::string> extension_ids = base::SplitString(
iter.first, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const auto& extension_id : extension_ids) {
if (!crx_file::id_util::IdIsValid(extension_id)) {
SYSLOG(WARNING) << "Invalid extension ID : " << extension_id << ".";
continue;
}
auto should_defer = [&extension_id, &installed_extensions](
const base::Value::Dict& dict,
const SettingsIdMap* settings_by_id) {
// If in legacy force list, don't defer since already have an
// entry. This ensures that the entry in these settings matches
// the entry in the forcelist. Also don't defer if the extension
// is installed.
if (base::Contains(*settings_by_id, extension_id) ||
base::Contains(installed_extensions, extension_id)) {
return false;
}
auto* install_mode =
dict.FindString(schema_constants::kInstallationMode);
if (!install_mode) {
return true;
}
// Don't defer if the extension needs to be installed.
return *install_mode != schema_constants::kForceInstalled &&
*install_mode != schema_constants::kNormalInstalled;
};
if (should_defer(*subdict, &settings_by_id_)) {
deferred_ids_.insert(extension_id);
continue;
}
internal::IndividualSettings* by_id = AccessById(extension_id);
const bool included_in_forcelist =
by_id->installation_mode == ManagedInstallationMode::kForced;
if (!ParseById(extension_id, *subdict))
continue;
// If applying the ExtensionSettings policy changes installation mode
// from force-installed to anything else, the extension might not get
// installed and will get stuck in CREATED stage.
if (included_in_forcelist &&
by_id->installation_mode != ManagedInstallationMode::kForced) {
InstallStageTracker::Get(profile_)->ReportFailure(
extension_id,
InstallStageTracker::FailureReason::OVERRIDDEN_BY_SETTINGS);
}
}
}
}
}
}
bool ExtensionManagement::ParseById(const std::string& extension_id,
const base::Value::Dict& subdict) {
internal::IndividualSettings* by_id = AccessById(extension_id);
if (by_id->Parse(subdict, internal::IndividualSettings::SCOPE_INDIVIDUAL))
return true;
settings_by_id_.erase(extension_id);
InstallStageTracker::Get(profile_)->ReportFailure(
extension_id,
InstallStageTracker::FailureReason::MALFORMED_EXTENSION_SETTINGS);
SYSLOG(WARNING) << "Malformed Extension Management settings for "
<< extension_id << ".";
return false;
}
internal::IndividualSettings* ExtensionManagement::GetSettingsForId(
const std::string& extension_id) {
if (base::Contains(deferred_ids_, extension_id))
LoadDeferredExtensionSetting(extension_id);
auto iter_id = settings_by_id_.find(extension_id);
if (iter_id == settings_by_id_.end())
return nullptr;
return iter_id->second.get();
}
void ExtensionManagement::LoadDeferredExtensionSetting(
const std::string& extension_id) {
DCHECK(base::Contains(deferred_ids_, extension_id));
// No need to check again later.
deferred_ids_.erase(extension_id);
const base::Value::Dict* dict_pref =
LoadDictPreference(pref_names::kExtensionManagement, true);
bool found = false;
for (auto iter : *dict_pref) {
if (iter.first == schema_constants::kWildcard ||
base::StartsWith(iter.first, schema_constants::kUpdateUrlPrefix,
base::CompareCase::SENSITIVE)) {
continue;
}
const base::Value::Dict* subdict = iter.second.GetIfDict();
if (!subdict)
continue;
auto extension_ids = base::SplitStringPiece(
iter.first, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (base::Contains(extension_ids, extension_id)) {
// Found our settings. After parsing, continue looking for more entries.
ParseById(extension_id, *subdict);
found = true;
}
}
DCHECK(found) << "Couldn't find dictionary for extension in deferred_ids_.";
}
const base::Value* ExtensionManagement::LoadPreference(
const char* pref_name,
bool force_managed,
base::Value::Type expected_type) const {
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->type() == expected_type)
return value;
}
return nullptr;
}
const base::Value::Dict* ExtensionManagement::LoadDictPreference(
const char* pref_name,
bool force_managed) const {
const base::Value* value =
LoadPreference(pref_name, force_managed, base::Value::Type::DICT);
return value ? &value->GetDict() : nullptr;
}
const base::Value::List* ExtensionManagement::LoadListPreference(
const char* pref_name,
bool force_managed) const {
const base::Value* value =
LoadPreference(pref_name, force_managed, base::Value::Type::LIST);
return value ? &value->GetList() : nullptr;
}
void ExtensionManagement::OnExtensionPrefChanged() {
Refresh();
NotifyExtensionManagementPrefChanged();
}
void ExtensionManagement::NotifyExtensionManagementPrefChanged() {
ReportExtensionManagementInstallCreationStage(
InstallStageTracker::InstallCreationStage::NOTIFIED_FROM_MANAGEMENT,
InstallStageTracker::InstallCreationStage::
NOTIFIED_FROM_MANAGEMENT_NOT_FORCED);
for (auto& observer : observer_list_)
observer.OnExtensionManagementSettingsChanged();
}
void ExtensionManagement::ReportExtensionManagementInstallCreationStage(
InstallStageTracker::InstallCreationStage forced_stage,
InstallStageTracker::InstallCreationStage other_stage) {
InstallStageTracker* install_stage_tracker =
InstallStageTracker::Get(profile_);
for (const auto& entry : settings_by_id_) {
if (entry.second->installation_mode == ManagedInstallationMode::kForced) {
install_stage_tracker->ReportInstallCreationStage(entry.first,
forced_stage);
} else {
install_stage_tracker->ReportInstallCreationStage(entry.first,
other_stage);
}
}
}
base::Value::Dict ExtensionManagement::GetInstallListByMode(
ManagedInstallationMode installation_mode) const {
// This is only meaningful if we 've loaded the extensions for the given
// installation mode.
DCHECK(installation_mode == ManagedInstallationMode::kForced ||
installation_mode == ManagedInstallationMode::kRecommended);
base::Value::Dict extension_dict;
for (const auto& [id, settings] : settings_by_id_) {
if (settings->installation_mode == installation_mode) {
ExternalPolicyLoader::AddExtension(extension_dict, id,
settings->update_url);
}
}
return extension_dict;
}
void ExtensionManagement::UpdateForcedExtensions(
const base::Value::Dict* extension_dict) {
if (!extension_dict)
return;
InstallStageTracker* install_stage_tracker =
InstallStageTracker::Get(profile_);
for (auto it : *extension_dict) {
if (!crx_file::id_util::IdIsValid(it.first)) {
install_stage_tracker->ReportFailure(
it.first, InstallStageTracker::FailureReason::INVALID_ID);
continue;
}
const base::Value::Dict* dict_value = it.second.GetIfDict();
if (!dict_value) {
install_stage_tracker->ReportFailure(
it.first, InstallStageTracker::FailureReason::NO_UPDATE_URL);
continue;
}
const std::string* update_url =
dict_value->FindString(ExternalProviderImpl::kExternalUpdateUrl);
if (!update_url) {
install_stage_tracker->ReportFailure(
it.first, InstallStageTracker::FailureReason::NO_UPDATE_URL);
continue;
}
internal::IndividualSettings* by_id = AccessById(it.first);
by_id->installation_mode = ManagedInstallationMode::kForced;
by_id->update_url = *update_url;
install_stage_tracker->ReportInstallationStage(
it.first, InstallStageTracker::Stage::CREATED);
install_stage_tracker->ReportInstallCreationStage(
it.first,
InstallStageTracker::InstallCreationStage::CREATION_INITIATED);
}
}
internal::IndividualSettings* ExtensionManagement::AccessById(
const ExtensionId& id) {
DCHECK(crx_file::id_util::IdIsValid(id)) << "Invalid ID: " << id;
std::unique_ptr<internal::IndividualSettings>& settings = settings_by_id_[id];
if (!settings) {
settings =
std::make_unique<internal::IndividualSettings>(default_settings_.get());
}
return settings.get();
}
internal::IndividualSettings* ExtensionManagement::AccessByUpdateUrl(
const std::string& update_url) {
DCHECK(GURL(update_url).is_valid()) << "Invalid update URL: " << update_url;
std::unique_ptr<internal::IndividualSettings>& settings =
settings_by_update_url_[update_url];
if (!settings) {
settings =
std::make_unique<internal::IndividualSettings>(default_settings_.get());
}
return settings.get();
}
// static
ExtensionManagement* ExtensionManagementFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<ExtensionManagement*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
ExtensionManagementFactory* ExtensionManagementFactory::GetInstance() {
return base::Singleton<ExtensionManagementFactory>::get();
}
ExtensionManagementFactory::ExtensionManagementFactory()
: ProfileKeyedServiceFactory(
"ExtensionManagement",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kRedirectedToOriginal)
// TODO(crbug.com/40257657): Audit whether these should be
// redirected or should have their own instance.
.WithGuest(ProfileSelection::kRedirectedToOriginal)
// TODO(crbug.com/41488885): Check if this service is needed for
// Ash Internals.
.WithAshInternals(ProfileSelection::kRedirectedToOriginal)
.Build()) {
DependsOn(InstallStageTrackerFactory::GetInstance());
}
ExtensionManagementFactory::~ExtensionManagementFactory() = default;
std::unique_ptr<KeyedService>
ExtensionManagementFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
TRACE_EVENT0("browser,startup",
"ExtensionManagementFactory::BuildServiceInstanceFor");
return std::make_unique<ExtensionManagement>(
Profile::FromBrowserContext(context));
}
} // namespace extensions
|