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
|
// Copyright 2013 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/policy_handlers.h"
#include <stddef.h>
#include <utility>
#include "base/logging.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/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.h"
#include "url/gurl.h"
namespace extensions {
// ExtensionListPolicyHandler implementation -----------------------------------
ExtensionListPolicyHandler::ExtensionListPolicyHandler(const char* policy_name,
const char* pref_path,
bool allow_wildcards)
: policy::TypeCheckingPolicyHandler(policy_name, base::Value::Type::LIST),
pref_path_(pref_path),
allow_wildcards_(allow_wildcards) {}
ExtensionListPolicyHandler::~ExtensionListPolicyHandler() {}
bool ExtensionListPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
return CheckAndGetList(policies, errors, NULL);
}
void ExtensionListPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
std::unique_ptr<base::ListValue> list;
policy::PolicyErrorMap errors;
if (CheckAndGetList(policies, &errors, &list) && list)
prefs->SetValue(pref_path(), std::move(list));
}
const char* ExtensionListPolicyHandler::pref_path() const {
return pref_path_;
}
bool ExtensionListPolicyHandler::CheckAndGetList(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors,
std::unique_ptr<base::ListValue>* extension_ids) {
if (extension_ids)
extension_ids->reset();
const base::Value* value = NULL;
if (!CheckAndGetValue(policies, errors, &value))
return false;
if (!value)
return true;
const base::ListValue* list_value = NULL;
if (!value->GetAsList(&list_value)) {
NOTREACHED();
return false;
}
// Filter the list, rejecting any invalid extension IDs.
std::unique_ptr<base::ListValue> filtered_list(new base::ListValue());
for (base::ListValue::const_iterator entry(list_value->begin());
entry != list_value->end(); ++entry) {
std::string id;
if (!(*entry)->GetAsString(&id)) {
errors->AddError(policy_name(), entry - list_value->begin(),
IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING));
continue;
}
if (!(allow_wildcards_ && id == "*") && !crx_file::id_util::IdIsValid(id)) {
errors->AddError(policy_name(),
entry - list_value->begin(),
IDS_POLICY_VALUE_FORMAT_ERROR);
continue;
}
filtered_list->AppendString(id);
}
if (extension_ids)
*extension_ids = std::move(filtered_list);
return true;
}
// ExtensionInstallForcelistPolicyHandler implementation -----------------------
ExtensionInstallForcelistPolicyHandler::ExtensionInstallForcelistPolicyHandler()
: policy::TypeCheckingPolicyHandler(policy::key::kExtensionInstallForcelist,
base::Value::Type::LIST) {}
ExtensionInstallForcelistPolicyHandler::
~ExtensionInstallForcelistPolicyHandler() {}
bool ExtensionInstallForcelistPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
const base::Value* value;
return CheckAndGetValue(policies, errors, &value) &&
ParseList(value, NULL, errors);
}
void ExtensionInstallForcelistPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
const base::Value* value = NULL;
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
if (CheckAndGetValue(policies, NULL, &value) &&
value &&
ParseList(value, dict.get(), NULL)) {
prefs->SetValue(pref_names::kInstallForceList, std::move(dict));
}
}
bool ExtensionInstallForcelistPolicyHandler::ParseList(
const base::Value* policy_value,
base::DictionaryValue* extension_dict,
policy::PolicyErrorMap* errors) {
if (!policy_value)
return true;
const base::ListValue* policy_list_value = NULL;
if (!policy_value->GetAsList(&policy_list_value)) {
// This should have been caught in CheckPolicySettings.
NOTREACHED();
return false;
}
for (base::ListValue::const_iterator entry(policy_list_value->begin());
entry != policy_list_value->end(); ++entry) {
std::string entry_string;
if (!(*entry)->GetAsString(&entry_string)) {
if (errors) {
errors->AddError(policy_name(), entry - policy_list_value->begin(),
IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING));
}
continue;
}
// Each string item of the list has the following form:
// <extension_id>;<update_url>
// Note: The update URL might also contain semicolons.
size_t pos = entry_string.find(';');
if (pos == std::string::npos) {
if (errors) {
errors->AddError(policy_name(),
entry - policy_list_value->begin(),
IDS_POLICY_VALUE_FORMAT_ERROR);
}
continue;
}
std::string extension_id = entry_string.substr(0, pos);
std::string update_url = entry_string.substr(pos+1);
if (!crx_file::id_util::IdIsValid(extension_id) ||
!GURL(update_url).is_valid()) {
if (errors) {
errors->AddError(policy_name(),
entry - policy_list_value->begin(),
IDS_POLICY_VALUE_FORMAT_ERROR);
}
continue;
}
if (extension_dict) {
extensions::ExternalPolicyLoader::AddExtension(
extension_dict, extension_id, update_url);
}
}
return true;
}
// 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() {}
bool ExtensionURLPatternListPolicyHandler::CheckPolicySettings(
const policy::PolicyMap& policies,
policy::PolicyErrorMap* errors) {
const base::Value* value = NULL;
if (!CheckAndGetValue(policies, errors, &value))
return false;
if (!value)
return true;
const base::ListValue* list_value = NULL;
if (!value->GetAsList(&list_value)) {
NOTREACHED();
return false;
}
// Check that the list contains valid URLPattern strings only.
for (base::ListValue::const_iterator entry(list_value->begin());
entry != list_value->end(); ++entry) {
std::string url_pattern_string;
if (!(*entry)->GetAsString(&url_pattern_string)) {
errors->AddError(policy_name(), entry - list_value->begin(),
IDS_POLICY_TYPE_ERROR,
base::Value::GetTypeName(base::Value::Type::STRING));
return false;
}
URLPattern pattern(URLPattern::SCHEME_ALL);
if (pattern.Parse(url_pattern_string) != URLPattern::PARSE_SUCCESS) {
errors->AddError(policy_name(),
entry - list_value->begin(),
IDS_POLICY_VALUE_FORMAT_ERROR);
return false;
}
}
return true;
}
void ExtensionURLPatternListPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
if (!pref_path_)
return;
const base::Value* value = policies.GetValue(policy_name());
if (value)
prefs->SetValue(pref_path_, value->CreateDeepCopy());
}
// 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() {
}
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;
// |policy_value| is expected to conform to the defined schema. But it's
// not strictly valid since there are additional restrictions.
const base::DictionaryValue* dict_value = NULL;
DCHECK(policy_value->IsType(base::Value::Type::DICTIONARY));
policy_value->GetAsDictionary(&dict_value);
for (base::DictionaryValue::Iterator it(*dict_value); !it.IsAtEnd();
it.Advance()) {
DCHECK(it.key() == schema_constants::kWildcard ||
crx_file::id_util::IdIsValid(it.key()));
DCHECK(it.value().IsType(base::Value::Type::DICTIONARY));
// Extracts sub dictionary.
const base::DictionaryValue* sub_dict = NULL;
it.value().GetAsDictionary(&sub_dict);
std::string installation_mode;
if (sub_dict->GetString(schema_constants::kInstallationMode,
&installation_mode)) {
if (installation_mode == schema_constants::kForceInstalled ||
installation_mode == schema_constants::kNormalInstalled) {
DCHECK(it.key() != schema_constants::kWildcard);
// Verifies that 'update_url' is specified for 'force_installed' and
// 'normal_installed' mode.
std::string update_url;
if (!sub_dict->GetString(schema_constants::kUpdateUrl, &update_url) ||
update_url.empty()) {
errors->AddError(policy_name(),
it.key() + "." + schema_constants::kUpdateUrl,
IDS_POLICY_NOT_SPECIFIED_ERROR);
return false;
}
// Verifies that update URL is valid.
if (!GURL(update_url).is_valid()) {
errors->AddError(
policy_name(), IDS_POLICY_INVALID_UPDATE_URL_ERROR, it.key());
return false;
}
}
}
}
return true;
}
void ExtensionSettingsPolicyHandler::ApplyPolicySettings(
const policy::PolicyMap& policies,
PrefValueMap* prefs) {
std::unique_ptr<base::Value> policy_value;
if (!CheckAndGetValue(policies, NULL, &policy_value) || !policy_value)
return;
prefs->SetValue(pref_names::kExtensionManagement, std::move(policy_value));
}
} // namespace extensions
|