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
|
// Copyright 2013 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/extensions/policy_handlers.h"
#include <stddef.h>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_management_constants.h"
#include "chrome/browser/extensions/external_policy_loader.h"
#include "components/crx_file/id_util.h"
#include "components/policy/core/browser/configuration_policy_handler.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/schema.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/strings/grit/components_strings.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/url_pattern.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_WIN)
#include "base/enterprise_util.h"
#endif
namespace extensions {
namespace {
// Returns true if extensions_ids contains a list of valid extension ids,
// divided by comma.
bool IsValidIdList(const std::string& extension_ids) {
std::vector<std::string_view> ids = base::SplitStringPiece(
extension_ids, ",", base::WhitespaceHandling::TRIM_WHITESPACE,
base::SplitResult::SPLIT_WANT_NONEMPTY);
if (ids.size() == 0) {
return false;
}
for (const auto& id : ids) {
if (!crx_file::id_util::IdIsValid(std::string(id))) {
return false;
}
}
return true;
}
// Returns true if URL is valid and uses one of the supported schemes.
bool IsValidUpdateUrl(const std::string& update_url) {
GURL update_gurl(update_url);
if (!update_gurl.is_valid()) {
return false;
}
return update_gurl.SchemeIsHTTPOrHTTPS() || update_gurl.SchemeIsFile();
}
} // namespace
// ExtensionListPolicyHandler implementation -----------------------------------
ExtensionListPolicyHandler::ExtensionListPolicyHandler(const char* policy_name,
const char* pref_path,
bool allow_wildcards)
: policy::ListPolicyHandler(policy_name, base::Value::Type::STRING),
pref_path_(pref_path),
allow_wildcards_(allow_wildcards) {}
ExtensionListPolicyHandler::~ExtensionListPolicyHandler() = default;
bool ExtensionListPolicyHandler::CheckListEntry(const base::Value& value) {
const std::string& str = value.GetString();
if (allow_wildcards_ && str == "*") {
return true;
}
// Make sure str is an extension id.
return crx_file::id_util::IdIsValid(str);
}
void ExtensionListPolicyHandler::ApplyList(base::Value::List filtered_list,
PrefValueMap* prefs) {
prefs->SetValue(pref_path_, base::Value(std::move(filtered_list)));
}
// ExtensionInstallForceListPolicyHandler implementation -----------------------
ExtensionInstallForceListPolicyHandler::ExtensionInstallForceListPolicyHandler()
: policy::TypeCheckingPolicyHandler(policy::key::kExtensionInstallForcelist,
base::Value::Type::LIST) {}
bool ExtensionInstallForceListPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
const base::Value* value;
return CheckAndGetValue(policies, errors, &value) &&
ParseList(value, nullptr, errors);
}
void ExtensionInstallForceListPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
auto dict = GetPolicyDict(policies);
if (dict.has_value()) {
prefs->SetValue(pref_names::kInstallForceList,
base::Value(std::move(dict).value()));
}
}
bool ExtensionInstallForceListPolicyHandler::ParseList(
const base::Value* policy_value,
base::Value::Dict* extension_dict,
policy::PolicyErrorMap* errors) {
if (!policy_value) {
return true;
}
if (!policy_value->is_list()) {
// This should have been caught in CheckPolicySettings.
NOTREACHED();
}
int index = -1;
for (const auto& entry : policy_value->GetList()) {
++index;
if (!entry.is_string()) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING),
policy::PolicyErrorPath{index});
}
continue;
}
std::string entry_string = entry.GetString();
// Each string item of the list should be of one of the following forms:
// * <extension_id>
// * <extension_id>;<update_url>
// Note: The update URL might also contain semicolons.
std::string extension_id;
std::string update_url;
size_t pos = entry_string.find(';');
if (pos == std::string::npos) {
extension_id = entry_string;
update_url = extension_urls::kChromeWebstoreUpdateURL;
} else {
extension_id = entry_string.substr(0, pos);
update_url = entry_string.substr(pos + 1);
}
if (!crx_file::id_util::IdIsValid(extension_id)) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_EXTENSION_ID_ERROR,
policy::PolicyErrorPath{index});
}
continue;
}
// Check that url is valid and uses one of the supported schemes.
if (!IsValidUpdateUrl(update_url)) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_UPDATE_URL_ERROR,
extension_id, policy::PolicyErrorPath{index});
}
continue;
}
if (extension_dict) {
ExternalPolicyLoader::AddExtension(*extension_dict, extension_id,
update_url);
}
}
return true;
}
std::optional<base::Value::Dict>
ExtensionInstallForceListPolicyHandler::GetPolicyDict(
const policy::PolicyMap& policies) {
const base::Value* value = nullptr;
base::Value::Dict dict;
if (CheckAndGetValue(policies, nullptr, &value) && value &&
ParseList(value, &dict, nullptr)) {
return dict;
}
return std::nullopt;
}
// ExtensionInstallBlockListPolicyHandler implementation ----------------------
ExtensionInstallBlockListPolicyHandler::ExtensionInstallBlockListPolicyHandler()
: list_handler_(policy::key::kExtensionInstallBlocklist,
pref_names::kInstallDenyList,
/*allow_wildcards*/ true) {}
ExtensionInstallBlockListPolicyHandler::
~ExtensionInstallBlockListPolicyHandler() = default;
bool ExtensionInstallBlockListPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
return list_handler_.CheckPolicySettings(policies, errors);
}
void ExtensionInstallBlockListPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
list_handler_.ApplyPolicySettings(policies, prefs);
}
// TODO(crbug.com/394876083): Support more extension policy handlers on desktop
// Android.
#if !BUILDFLAG(IS_ANDROID)
// ExtensionURLPatternListPolicyHandler implementation -------------------------
ExtensionURLPatternListPolicyHandler::ExtensionURLPatternListPolicyHandler(
const char* policy_name,
const char* pref_path)
: policy::TypeCheckingPolicyHandler(policy_name, base::Value::Type::LIST),
pref_path_(pref_path) {}
ExtensionURLPatternListPolicyHandler::~ExtensionURLPatternListPolicyHandler() =
default;
bool ExtensionURLPatternListPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
const base::Value* value = nullptr;
if (!CheckAndGetValue(policies, errors, &value)) {
return false;
}
if (!value) {
return true;
}
if (!value->is_list()) {
NOTREACHED();
}
// Check that the list contains valid URLPattern strings only.
int index = 0;
for (const auto& entry : value->GetList()) {
if (!entry.is_string()) {
errors->AddError(policy_name(), IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING),
policy::PolicyErrorPath{index});
return false;
}
std::string url_pattern_string = entry.GetString();
URLPattern pattern(URLPattern::SCHEME_ALL);
if (pattern.Parse(url_pattern_string) !=
URLPattern::ParseResult::kSuccess) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_URL_ERROR,
policy::PolicyErrorPath{index});
return false;
}
++index;
}
return true;
}
void ExtensionURLPatternListPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
if (!pref_path_) {
return;
}
// It is safe to use `GetValueUnsafe()` as multiple policy types are handled.
const base::Value* value = policies.GetValueUnsafe(policy_name());
if (value) {
prefs->SetValue(pref_path_, value->Clone());
}
}
#endif // !BUILDFLAG(IS_ANDROID)
// ExtensionSettingsPolicyHandler implementation ------------------------------
ExtensionSettingsPolicyHandler::ExtensionSettingsPolicyHandler(
const policy::Schema& chrome_schema)
: policy::SchemaValidatingPolicyHandler(
policy::key::kExtensionSettings,
chrome_schema.GetKnownProperty(policy::key::kExtensionSettings),
policy::SCHEMA_ALLOW_UNKNOWN) {}
ExtensionSettingsPolicyHandler::~ExtensionSettingsPolicyHandler() = default;
void ExtensionSettingsPolicyHandler::SanitizePolicySettings(
base::Value* policy_value,
policy::PolicyErrorMap* errors) {
DCHECK(policy_value);
// |policy_value| is expected to conform to the defined schema. But it's
// not strictly valid since there are additional restrictions.
DCHECK(policy_value->is_dict());
// Dictionary entries with any invalid setting get removed at the end. We
// can't mutate the dict while iterating, so store them here.
std::unordered_set<std::string> invalid_keys;
// Check each entry, populating |invalid_keys| and |errors|.
for (const auto [extension_ids, policy] : policy_value->GetDict()) {
DCHECK(extension_ids == schema_constants::kWildcard ||
IsValidIdList(extension_ids));
DCHECK(policy.is_dict());
// Extracts sub dictionary.
const base::Value::Dict& sub_dict = policy.GetDict();
const std::string* installation_mode =
sub_dict.FindString(schema_constants::kInstallationMode);
if (installation_mode) {
if (*installation_mode == schema_constants::kForceInstalled ||
*installation_mode == schema_constants::kNormalInstalled) {
DCHECK(extension_ids != schema_constants::kWildcard);
// Verifies that 'update_url' is specified for 'force_installed' and
// 'normal_installed' mode.
const std::string* update_url =
sub_dict.FindString(schema_constants::kUpdateUrl);
if (!update_url || update_url->empty()) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_NOT_SPECIFIED_ERROR,
policy::PolicyErrorPath{
extension_ids, schema_constants::kUpdateUrl});
}
invalid_keys.insert(extension_ids);
continue;
}
// Check that url is valid and uses one of the supported schemes.
if (!IsValidUpdateUrl(*update_url)) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_UPDATE_URL_ERROR,
extension_ids);
}
invalid_keys.insert(extension_ids);
continue;
}
}
}
// Host keys that don't support user defined paths.
const char* host_keys[] = {schema_constants::kPolicyBlockedHosts,
schema_constants::kPolicyAllowedHosts};
const int extension_scheme_mask =
URLPattern::GetValidSchemeMaskForExtensions();
for (const char* key : host_keys) {
const base::Value::List* unparsed_urls = sub_dict.FindList(key);
if (unparsed_urls != nullptr) {
for (const auto& url_value : *unparsed_urls) {
const std::string& unparsed_url = url_value.GetString();
URLPattern pattern(extension_scheme_mask);
URLPattern::ParseResult parse_result = pattern.Parse(unparsed_url);
// These keys don't support paths due to how we track the initiator
// of a webRequest and cookie security policy. We expect a valid
// pattern to return a PARSE_ERROR_EMPTY_PATH.
if (parse_result == URLPattern::ParseResult::kEmptyPath) {
// Add a wildcard path to the URL as it should match any path.
parse_result = pattern.Parse(unparsed_url + "/*");
} else if (parse_result == URLPattern::ParseResult::kSuccess) {
// The user supplied a path, notify them that this is not supported.
if (!pattern.match_all_urls()) {
if (errors) {
errors->AddError(
policy_name(), IDS_POLICY_URL_PATH_SPECIFIED_ERROR,
unparsed_url, policy::PolicyErrorPath{extension_ids, key});
}
invalid_keys.insert(extension_ids);
break;
}
}
if (parse_result != URLPattern::ParseResult::kSuccess) {
if (errors) {
errors->AddError(policy_name(), IDS_POLICY_INVALID_URL_ERROR,
policy::PolicyErrorPath{extension_ids, key});
}
invalid_keys.insert(extension_ids);
break;
}
}
}
}
const base::Value::List* runtime_blocked_hosts =
sub_dict.FindList(schema_constants::kPolicyBlockedHosts);
if (runtime_blocked_hosts != nullptr &&
runtime_blocked_hosts->size() >
schema_constants::kMaxItemsURLPatternSet) {
if (errors) {
policy::PolicyErrorPath error_path = {
extension_ids, schema_constants::kPolicyBlockedHosts};
errors->AddError(
policy_name(), IDS_POLICY_EXTENSION_SETTINGS_ORIGIN_LIMIT_WARNING,
base::NumberToString(schema_constants::kMaxItemsURLPatternSet),
error_path);
}
}
const base::Value::List* runtime_allowed_hosts =
sub_dict.FindList(schema_constants::kPolicyAllowedHosts);
if (runtime_allowed_hosts != nullptr &&
runtime_allowed_hosts->size() >
schema_constants::kMaxItemsURLPatternSet) {
if (errors) {
policy::PolicyErrorPath error_path = {
extension_ids, schema_constants::kPolicyAllowedHosts};
errors->AddError(
policy_name(), IDS_POLICY_EXTENSION_SETTINGS_ORIGIN_LIMIT_WARNING,
base::NumberToString(schema_constants::kMaxItemsURLPatternSet),
error_path);
}
}
}
// Remove |invalid_keys| from the dictionary.
for (const std::string& key : invalid_keys) {
policy_value->GetDict().Remove(key);
}
}
bool ExtensionSettingsPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
std::unique_ptr<base::Value> policy_value;
if (!CheckAndGetValue(policies, errors, &policy_value)) {
return false;
}
if (!policy_value) {
return true;
}
SanitizePolicySettings(policy_value.get(), errors);
return true;
}
void ExtensionSettingsPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
std::unique_ptr<base::Value> policy_value;
if (!CheckAndGetValue(policies, nullptr, &policy_value) || !policy_value) {
return;
}
SanitizePolicySettings(policy_value.get(), nullptr);
prefs->SetValue(pref_names::kExtensionManagement,
base::Value::FromUniquePtrValue(std::move(policy_value)));
}
} // namespace extensions
|