[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/policy_handlers.h" |
| 6 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 7 | #include <stddef.h> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 8 | #include <utility> |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 9 | |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 10 | #include "base/logging.h" |
Yann Dago | ee29190 | 2019-08-19 15:49:06 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
Owen Min | 8dfd074 | 2019-09-06 21:27:08 | [diff] [blame] | 12 | #include "base/strings/string_split.h" |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 13 | #include "build/build_config.h" |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_management_constants.h" |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 15 | #include "chrome/browser/extensions/external_policy_loader.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 16 | #include "components/crx_file/id_util.h" |
[email protected] | f6c403b | 2013-12-05 19:01:25 | [diff] [blame] | 17 | #include "components/policy/core/browser/policy_error_map.h" |
[email protected] | c4a138a | 2013-11-21 19:54:57 | [diff] [blame] | 18 | #include "components/policy/core/common/policy_map.h" |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 19 | #include "components/policy/core/common/schema.h" |
brettw | 39d6ba4 | 2016-08-24 16:56:38 | [diff] [blame] | 20 | #include "components/policy/policy_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 21 | #include "components/prefs/pref_value_map.h" |
thestig | 4a2e88e | 2016-08-27 23:23:51 | [diff] [blame] | 22 | #include "components/strings/grit/components_strings.h" |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 23 | #include "extensions/browser/pref_names.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 24 | #include "extensions/common/extension.h" |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 25 | #include "extensions/common/extension_urls.h" |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 26 | #include "url/gurl.h" |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 27 | |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 28 | #if defined(OS_WIN) |
Hector Carmona | c3565bc4 | 2019-02-01 23:31:21 | [diff] [blame] | 29 | #include "base/enterprise_util.h" |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 30 | #endif |
| 31 | |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 32 | namespace extensions { |
Owen Min | 8dfd074 | 2019-09-06 21:27:08 | [diff] [blame] | 33 | namespace { |
| 34 | // Returns true if extensions_ids contains a list of valid extension ids, |
| 35 | // divided by comma. |
| 36 | bool IsValidIdList(const std::string& extension_ids) { |
| 37 | std::vector<base::StringPiece> ids = base::SplitStringPiece( |
| 38 | extension_ids, ",", base::WhitespaceHandling::TRIM_WHITESPACE, |
| 39 | base::SplitResult::SPLIT_WANT_NONEMPTY); |
| 40 | if (ids.size() == 0) |
| 41 | return false; |
| 42 | for (const auto& id : ids) { |
| 43 | if (!crx_file::id_util::IdIsValid(id.as_string())) |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | } // namespace |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 49 | // ExtensionListPolicyHandler implementation ----------------------------------- |
| 50 | |
| 51 | ExtensionListPolicyHandler::ExtensionListPolicyHandler(const char* policy_name, |
| 52 | const char* pref_path, |
| 53 | bool allow_wildcards) |
Lutz Justen | e45e3fe | 2017-08-18 07:11:39 | [diff] [blame] | 54 | : policy::ListPolicyHandler(policy_name, base::Value::Type::STRING), |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 55 | pref_path_(pref_path), |
| 56 | allow_wildcards_(allow_wildcards) {} |
| 57 | |
| 58 | ExtensionListPolicyHandler::~ExtensionListPolicyHandler() {} |
| 59 | |
Lutz Justen | e45e3fe | 2017-08-18 07:11:39 | [diff] [blame] | 60 | bool ExtensionListPolicyHandler::CheckListEntry(const base::Value& value) { |
| 61 | const std::string& str = value.GetString(); |
| 62 | if (allow_wildcards_ && str == "*") |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 63 | return true; |
| 64 | |
Lutz Justen | e45e3fe | 2017-08-18 07:11:39 | [diff] [blame] | 65 | // Make sure str is an extension id. |
| 66 | return crx_file::id_util::IdIsValid(str); |
| 67 | } |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 68 | |
Lutz Justen | e45e3fe | 2017-08-18 07:11:39 | [diff] [blame] | 69 | void ExtensionListPolicyHandler::ApplyList( |
| 70 | std::unique_ptr<base::ListValue> filtered_list, |
| 71 | PrefValueMap* prefs) { |
Sylvain Defresne | 1787960d | 2019-01-30 21:02:10 | [diff] [blame] | 72 | DCHECK(filtered_list); |
| 73 | prefs->SetValue(pref_path_, |
| 74 | base::Value::FromUniquePtrValue(std::move(filtered_list))); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 75 | } |
| 76 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 77 | // ExtensionInstallListPolicyHandler implementation ---------------------------- |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 78 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 79 | ExtensionInstallListPolicyHandler::ExtensionInstallListPolicyHandler( |
| 80 | const char* policy_name, |
| 81 | const char* pref_name) |
| 82 | : policy::TypeCheckingPolicyHandler(policy_name, base::Value::Type::LIST), |
| 83 | pref_name_(pref_name) {} |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 84 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 85 | bool ExtensionInstallListPolicyHandler::CheckPolicySettings( |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 86 | const policy::PolicyMap& policies, |
| 87 | policy::PolicyErrorMap* errors) { |
| 88 | const base::Value* value; |
| 89 | return CheckAndGetValue(policies, errors, &value) && |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 90 | ParseList(value, nullptr, errors); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 91 | } |
| 92 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 93 | void ExtensionInstallListPolicyHandler::ApplyPolicySettings( |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 94 | const policy::PolicyMap& policies, |
| 95 | PrefValueMap* prefs) { |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 96 | const base::Value* value = nullptr; |
Sylvain Defresne | 1787960d | 2019-01-30 21:02:10 | [diff] [blame] | 97 | base::DictionaryValue dict; |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 98 | if (CheckAndGetValue(policies, nullptr, &value) && value && |
Sylvain Defresne | 1787960d | 2019-01-30 21:02:10 | [diff] [blame] | 99 | ParseList(value, &dict, nullptr)) { |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 100 | prefs->SetValue(pref_name_, std::move(dict)); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 104 | bool ExtensionInstallListPolicyHandler::ParseList( |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 105 | const base::Value* policy_value, |
| 106 | base::DictionaryValue* extension_dict, |
| 107 | policy::PolicyErrorMap* errors) { |
| 108 | if (!policy_value) |
| 109 | return true; |
| 110 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 111 | const base::ListValue* policy_list_value = nullptr; |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 112 | if (!policy_value->GetAsList(&policy_list_value)) { |
| 113 | // This should have been caught in CheckPolicySettings. |
| 114 | NOTREACHED(); |
| 115 | return false; |
| 116 | } |
| 117 | |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 118 | for (auto entry(policy_list_value->begin()); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 119 | entry != policy_list_value->end(); ++entry) { |
| 120 | std::string entry_string; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 121 | if (!entry->GetAsString(&entry_string)) { |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 122 | if (errors) { |
thestig | e7615d6c | 2016-07-19 19:43:46 | [diff] [blame] | 123 | errors->AddError(policy_name(), entry - policy_list_value->begin(), |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 124 | IDS_POLICY_TYPE_ERROR, |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 125 | base::Value::GetTypeName(base::Value::Type::STRING)); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 126 | } |
| 127 | continue; |
| 128 | } |
| 129 | |
Maksim Ivanov | eaac2ff | 2018-04-16 16:23:24 | [diff] [blame] | 130 | // Each string item of the list should be of one of the following forms: |
| 131 | // * <extension_id> |
| 132 | // * <extension_id>;<update_url> |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 133 | // Note: The update URL might also contain semicolons. |
Maksim Ivanov | eaac2ff | 2018-04-16 16:23:24 | [diff] [blame] | 134 | std::string extension_id; |
| 135 | std::string update_url; |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 136 | size_t pos = entry_string.find(';'); |
| 137 | if (pos == std::string::npos) { |
Maksim Ivanov | eaac2ff | 2018-04-16 16:23:24 | [diff] [blame] | 138 | extension_id = entry_string; |
| 139 | update_url = extension_urls::kChromeWebstoreUpdateURL; |
| 140 | } else { |
| 141 | extension_id = entry_string.substr(0, pos); |
| 142 | update_url = entry_string.substr(pos + 1); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 145 | if (!crx_file::id_util::IdIsValid(extension_id) || |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 146 | !GURL(update_url).is_valid()) { |
| 147 | if (errors) { |
| 148 | errors->AddError(policy_name(), |
| 149 | entry - policy_list_value->begin(), |
| 150 | IDS_POLICY_VALUE_FORMAT_ERROR); |
| 151 | } |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | if (extension_dict) { |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 156 | ExternalPolicyLoader::AddExtension(extension_dict, extension_id, |
| 157 | update_url); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 164 | // ExtensionInstallForcelistPolicyHandler implementation ----------------------- |
| 165 | |
| 166 | ExtensionInstallForcelistPolicyHandler::ExtensionInstallForcelistPolicyHandler() |
| 167 | : ExtensionInstallListPolicyHandler(policy::key::kExtensionInstallForcelist, |
| 168 | pref_names::kInstallForceList) {} |
| 169 | |
Alexander Hendrich | b07fd55b | 2019-04-01 09:24:37 | [diff] [blame] | 170 | // ExtensionInstallLoginScreenExtensionsPolicyHandler implementation ----------- |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 171 | |
Alexander Hendrich | b07fd55b | 2019-04-01 09:24:37 | [diff] [blame] | 172 | ExtensionInstallLoginScreenExtensionsPolicyHandler:: |
| 173 | ExtensionInstallLoginScreenExtensionsPolicyHandler() |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 174 | : ExtensionInstallListPolicyHandler( |
Alexander Hendrich | b07fd55b | 2019-04-01 09:24:37 | [diff] [blame] | 175 | policy::key::kDeviceLoginScreenExtensions, |
| 176 | pref_names::kLoginScreenExtensions) {} |
achuith | 4607f07 | 2017-03-08 11:49:13 | [diff] [blame] | 177 | |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 178 | // ExtensionURLPatternListPolicyHandler implementation ------------------------- |
| 179 | |
| 180 | ExtensionURLPatternListPolicyHandler::ExtensionURLPatternListPolicyHandler( |
| 181 | const char* policy_name, |
| 182 | const char* pref_path) |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 183 | : policy::TypeCheckingPolicyHandler(policy_name, base::Value::Type::LIST), |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 184 | pref_path_(pref_path) {} |
| 185 | |
| 186 | ExtensionURLPatternListPolicyHandler::~ExtensionURLPatternListPolicyHandler() {} |
| 187 | |
| 188 | bool ExtensionURLPatternListPolicyHandler::CheckPolicySettings( |
| 189 | const policy::PolicyMap& policies, |
| 190 | policy::PolicyErrorMap* errors) { |
| 191 | const base::Value* value = NULL; |
| 192 | if (!CheckAndGetValue(policies, errors, &value)) |
| 193 | return false; |
| 194 | |
| 195 | if (!value) |
| 196 | return true; |
| 197 | |
| 198 | const base::ListValue* list_value = NULL; |
| 199 | if (!value->GetAsList(&list_value)) { |
| 200 | NOTREACHED(); |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // Check that the list contains valid URLPattern strings only. |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 205 | for (auto entry(list_value->begin()); entry != list_value->end(); ++entry) { |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 206 | std::string url_pattern_string; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 207 | if (!entry->GetAsString(&url_pattern_string)) { |
thestig | e7615d6c | 2016-07-19 19:43:46 | [diff] [blame] | 208 | errors->AddError(policy_name(), entry - list_value->begin(), |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 209 | IDS_POLICY_TYPE_ERROR, |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 210 | base::Value::GetTypeName(base::Value::Type::STRING)); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
| 214 | URLPattern pattern(URLPattern::SCHEME_ALL); |
Devlin Cronin | bd7f2b5fa | 2018-09-05 17:41:18 | [diff] [blame] | 215 | if (pattern.Parse(url_pattern_string) != |
| 216 | URLPattern::ParseResult::kSuccess) { |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 217 | errors->AddError(policy_name(), |
| 218 | entry - list_value->begin(), |
| 219 | IDS_POLICY_VALUE_FORMAT_ERROR); |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | void ExtensionURLPatternListPolicyHandler::ApplyPolicySettings( |
| 228 | const policy::PolicyMap& policies, |
| 229 | PrefValueMap* prefs) { |
| 230 | if (!pref_path_) |
| 231 | return; |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 232 | const base::Value* value = policies.GetValue(policy_name()); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 233 | if (value) |
Sylvain Defresne | 1787960d | 2019-01-30 21:02:10 | [diff] [blame] | 234 | prefs->SetValue(pref_path_, value->Clone()); |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 235 | } |
| 236 | |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 237 | // ExtensionSettingsPolicyHandler implementation ------------------------------ |
| 238 | |
| 239 | ExtensionSettingsPolicyHandler::ExtensionSettingsPolicyHandler( |
| 240 | const policy::Schema& chrome_schema) |
| 241 | : policy::SchemaValidatingPolicyHandler( |
| 242 | policy::key::kExtensionSettings, |
| 243 | chrome_schema.GetKnownProperty(policy::key::kExtensionSettings), |
| 244 | policy::SCHEMA_ALLOW_UNKNOWN) { |
| 245 | } |
| 246 | |
| 247 | ExtensionSettingsPolicyHandler::~ExtensionSettingsPolicyHandler() { |
| 248 | } |
| 249 | |
| 250 | bool ExtensionSettingsPolicyHandler::CheckPolicySettings( |
| 251 | const policy::PolicyMap& policies, |
| 252 | policy::PolicyErrorMap* errors) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 253 | std::unique_ptr<base::Value> policy_value; |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 254 | if (!CheckAndGetValue(policies, errors, &policy_value)) |
| 255 | return false; |
| 256 | if (!policy_value) |
| 257 | return true; |
| 258 | |
| 259 | // |policy_value| is expected to conform to the defined schema. But it's |
| 260 | // not strictly valid since there are additional restrictions. |
| 261 | const base::DictionaryValue* dict_value = NULL; |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 262 | DCHECK(policy_value->is_dict()); |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 263 | policy_value->GetAsDictionary(&dict_value); |
| 264 | |
| 265 | for (base::DictionaryValue::Iterator it(*dict_value); !it.IsAtEnd(); |
| 266 | it.Advance()) { |
Owen Min | 8dfd074 | 2019-09-06 21:27:08 | [diff] [blame] | 267 | DCHECK(it.key() == schema_constants::kWildcard || IsValidIdList(it.key())); |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 268 | DCHECK(it.value().is_dict()); |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 269 | |
| 270 | // Extracts sub dictionary. |
| 271 | const base::DictionaryValue* sub_dict = NULL; |
| 272 | it.value().GetAsDictionary(&sub_dict); |
| 273 | |
| 274 | std::string installation_mode; |
| 275 | if (sub_dict->GetString(schema_constants::kInstallationMode, |
| 276 | &installation_mode)) { |
| 277 | if (installation_mode == schema_constants::kForceInstalled || |
| 278 | installation_mode == schema_constants::kNormalInstalled) { |
| 279 | DCHECK(it.key() != schema_constants::kWildcard); |
| 280 | // Verifies that 'update_url' is specified for 'force_installed' and |
| 281 | // 'normal_installed' mode. |
| 282 | std::string update_url; |
| 283 | if (!sub_dict->GetString(schema_constants::kUpdateUrl, &update_url) || |
| 284 | update_url.empty()) { |
| 285 | errors->AddError(policy_name(), |
| 286 | it.key() + "." + schema_constants::kUpdateUrl, |
| 287 | IDS_POLICY_NOT_SPECIFIED_ERROR); |
| 288 | return false; |
| 289 | } |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 290 | if (GURL(update_url).is_valid()) { |
| 291 | // Unless enterprise managed only extensions from the Chrome Webstore |
| 292 | // can be force installed. |
| 293 | #if defined(OS_WIN) |
| 294 | // We can't use IsWebstoreUpdateUrl() here since the ExtensionClient |
| 295 | // isn't set this early during startup. |
Hector Carmona | c3565bc4 | 2019-02-01 23:31:21 | [diff] [blame] | 296 | if (!base::IsMachineExternallyManaged() && |
Nick Peterson | d952cb77 | 2018-03-07 15:46:03 | [diff] [blame] | 297 | !base::LowerCaseEqualsASCII( |
| 298 | update_url, extension_urls::kChromeWebstoreUpdateURL)) { |
| 299 | errors->AddError(policy_name(), it.key(), |
| 300 | IDS_POLICY_OFF_CWS_URL_ERROR, |
| 301 | extension_urls::kChromeWebstoreUpdateURL); |
| 302 | return false; |
| 303 | } |
| 304 | #endif |
| 305 | } else { |
| 306 | // Warns about an invalid update URL. |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 307 | errors->AddError( |
| 308 | policy_name(), IDS_POLICY_INVALID_UPDATE_URL_ERROR, it.key()); |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | } |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 313 | // Host keys that don't support user defined paths. |
Devlin Cronin | 7e0f41ff | 2018-05-16 17:19:36 | [diff] [blame] | 314 | const char* host_keys[] = {schema_constants::kPolicyBlockedHosts, |
| 315 | schema_constants::kPolicyAllowedHosts}; |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 316 | const int extension_scheme_mask = |
| 317 | URLPattern::GetValidSchemeMaskForExtensions(); |
| 318 | for (const char* key : host_keys) { |
| 319 | const base::ListValue* unparsed_urls; |
| 320 | if (sub_dict->GetList(key, &unparsed_urls)) { |
| 321 | for (size_t i = 0; i < unparsed_urls->GetSize(); ++i) { |
| 322 | std::string unparsed_url; |
| 323 | unparsed_urls->GetString(i, &unparsed_url); |
| 324 | URLPattern pattern(extension_scheme_mask); |
Devlin Cronin | 35f8e37 | 2019-08-16 19:15:38 | [diff] [blame] | 325 | URLPattern::ParseResult parse_result = pattern.Parse(unparsed_url); |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 326 | // These keys don't support paths due to how we track the initiator |
| 327 | // of a webRequest and cookie security policy. We expect a valid |
| 328 | // pattern to return a PARSE_ERROR_EMPTY_PATH. |
Devlin Cronin | bd7f2b5fa | 2018-09-05 17:41:18 | [diff] [blame] | 329 | if (parse_result == URLPattern::ParseResult::kEmptyPath) { |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 330 | // Add a wildcard path to the URL as it should match any path. |
Devlin Cronin | 35f8e37 | 2019-08-16 19:15:38 | [diff] [blame] | 331 | parse_result = pattern.Parse(unparsed_url + "/*"); |
Devlin Cronin | bd7f2b5fa | 2018-09-05 17:41:18 | [diff] [blame] | 332 | } else if (parse_result == URLPattern::ParseResult::kSuccess) { |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 333 | // The user supplied a path, notify them that this is not supported. |
| 334 | if (!pattern.match_all_urls()) { |
| 335 | errors->AddError( |
| 336 | policy_name(), it.key(), |
| 337 | "The URL pattern '" + unparsed_url + "' for attribute " + |
| 338 | key + " has a path specified. Paths are not " + |
| 339 | "supported, please remove the path and try again. " + |
| 340 | "e.g. *://example.com/ => *://example.com"); |
| 341 | return false; |
| 342 | } |
| 343 | } |
Devlin Cronin | bd7f2b5fa | 2018-09-05 17:41:18 | [diff] [blame] | 344 | if (parse_result != URLPattern::ParseResult::kSuccess) { |
Nick Peterson | 6bdf582 | 2017-06-01 20:42:45 | [diff] [blame] | 345 | errors->AddError(policy_name(), it.key(), |
| 346 | "Invalid URL pattern '" + unparsed_url + |
| 347 | "' for attribute " + key); |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
Yann Dago | ee29190 | 2019-08-19 15:49:06 | [diff] [blame] | 353 | |
| 354 | const base::ListValue* runtime_blocked_hosts = nullptr; |
| 355 | if (sub_dict->GetList(schema_constants::kPolicyBlockedHosts, |
| 356 | &runtime_blocked_hosts) && |
| 357 | runtime_blocked_hosts->GetList().size() > |
| 358 | schema_constants::kMaxItemsURLPatternSet) { |
| 359 | errors->AddError( |
| 360 | policy_name(), it.key() + "." + schema_constants::kPolicyBlockedHosts, |
| 361 | IDS_POLICY_EXTENSION_SETTINGS_ORIGIN_LIMIT_WARNING, |
| 362 | base::NumberToString(schema_constants::kMaxItemsURLPatternSet)); |
| 363 | } |
| 364 | |
| 365 | const base::ListValue* runtime_allowed_hosts = nullptr; |
| 366 | if (sub_dict->GetList(schema_constants::kPolicyAllowedHosts, |
| 367 | &runtime_allowed_hosts) && |
| 368 | runtime_allowed_hosts->GetList().size() > |
| 369 | schema_constants::kMaxItemsURLPatternSet) { |
| 370 | errors->AddError( |
| 371 | policy_name(), it.key() + "." + schema_constants::kPolicyAllowedHosts, |
| 372 | IDS_POLICY_EXTENSION_SETTINGS_ORIGIN_LIMIT_WARNING, |
| 373 | base::NumberToString(schema_constants::kMaxItemsURLPatternSet)); |
| 374 | } |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | void ExtensionSettingsPolicyHandler::ApplyPolicySettings( |
| 381 | const policy::PolicyMap& policies, |
| 382 | PrefValueMap* prefs) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 383 | std::unique_ptr<base::Value> policy_value; |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 384 | if (!CheckAndGetValue(policies, NULL, &policy_value) || !policy_value) |
| 385 | return; |
Sylvain Defresne | 1787960d | 2019-01-30 21:02:10 | [diff] [blame] | 386 | prefs->SetValue(pref_names::kExtensionManagement, |
| 387 | base::Value::FromUniquePtrValue(std::move(policy_value))); |
binjin | 1e1cc33a | 2014-10-09 18:08:16 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 01253d27 | 2013-10-21 17:07:50 | [diff] [blame] | 390 | } // namespace extensions |