| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/permissions/permissions_data.h" | 5 #include "extensions/common/permissions/permissions_data.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/lazy_instance.h" | |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "content/public/common/url_constants.h" | 12 #include "content/public/common/url_constants.h" |
| 14 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/error_utils.h" | 14 #include "extensions/common/error_utils.h" |
| 16 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 17 #include "extensions/common/extensions_client.h" | 16 #include "extensions/common/extensions_client.h" |
| 18 #include "extensions/common/manifest.h" | 17 #include "extensions/common/manifest.h" |
| 19 #include "extensions/common/manifest_constants.h" | 18 #include "extensions/common/manifest_constants.h" |
| 20 #include "extensions/common/manifest_handlers/permissions_parser.h" | 19 #include "extensions/common/manifest_handlers/permissions_parser.h" |
| 21 #include "extensions/common/permissions/api_permission.h" | 20 #include "extensions/common/permissions/api_permission.h" |
| 22 #include "extensions/common/permissions/permission_message_provider.h" | 21 #include "extensions/common/permissions/permission_message_provider.h" |
| 23 #include "extensions/common/switches.h" | 22 #include "extensions/common/switches.h" |
| 24 #include "extensions/common/url_pattern_set.h" | 23 #include "extensions/common/url_pattern_set.h" |
| 25 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 26 #include "url/url_constants.h" | 25 #include "url/url_constants.h" |
| 27 | 26 |
| 28 namespace extensions { | 27 namespace extensions { |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 PermissionsData::PolicyDelegate* g_policy_delegate = nullptr; | 31 PermissionsData::PolicyDelegate* g_policy_delegate = nullptr; |
| 33 | 32 |
| 34 struct DefaultRuntimePolicy { | |
| 35 URLPatternSet blocked_hosts; | |
| 36 URLPatternSet allowed_hosts; | |
| 37 }; | |
| 38 | |
| 39 // URLs an extension can't interact with. An extension can override these | |
| 40 // settings by declaring its own list of blocked and allowed hosts using | |
| 41 // policy_blocked_hosts and policy_allowed_hosts. | |
| 42 base::LazyInstance<DefaultRuntimePolicy>::Leaky default_runtime_policy = | |
| 43 LAZY_INSTANCE_INITIALIZER; | |
| 44 | |
| 45 class AutoLockOnValidThread { | 33 class AutoLockOnValidThread { |
| 46 public: | 34 public: |
| 47 AutoLockOnValidThread(base::Lock& lock, base::ThreadChecker* thread_checker) | 35 AutoLockOnValidThread(base::Lock& lock, base::ThreadChecker* thread_checker) |
| 48 : auto_lock_(lock) { | 36 : auto_lock_(lock) { |
| 49 DCHECK(!thread_checker || thread_checker->CalledOnValidThread()); | 37 DCHECK(!thread_checker || thread_checker->CalledOnValidThread()); |
| 50 } | 38 } |
| 51 | 39 |
| 52 private: | 40 private: |
| 53 base::AutoLock auto_lock_; | 41 base::AutoLock auto_lock_; |
| 54 | 42 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 if (extension && document_url.SchemeIs(kExtensionScheme) && | 120 if (extension && document_url.SchemeIs(kExtensionScheme) && |
| 133 document_url.host() != extension->id() && !allow_on_chrome_urls) { | 121 document_url.host() != extension->id() && !allow_on_chrome_urls) { |
| 134 if (error) | 122 if (error) |
| 135 *error = manifest_errors::kCannotAccessExtensionUrl; | 123 *error = manifest_errors::kCannotAccessExtensionUrl; |
| 136 return true; | 124 return true; |
| 137 } | 125 } |
| 138 | 126 |
| 139 return false; | 127 return false; |
| 140 } | 128 } |
| 141 | 129 |
| 142 bool PermissionsData::UsesDefaultPolicyHostRestrictions() const { | |
| 143 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | |
| 144 return uses_default_policy_host_restrictions; | |
| 145 } | |
| 146 | |
| 147 const URLPatternSet& PermissionsData::default_policy_blocked_hosts() { | |
| 148 return default_runtime_policy.Get().blocked_hosts; | |
| 149 } | |
| 150 | |
| 151 const URLPatternSet& PermissionsData::default_policy_allowed_hosts() { | |
| 152 return default_runtime_policy.Get().allowed_hosts; | |
| 153 } | |
| 154 | |
| 155 const URLPatternSet PermissionsData::policy_blocked_hosts() const { | |
| 156 base::AutoLock auto_lock(runtime_lock_); | |
| 157 return PolicyBlockedHostsUnsafe(); | |
| 158 } | |
| 159 | |
| 160 const URLPatternSet& PermissionsData::PolicyBlockedHostsUnsafe() const { | |
| 161 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | |
| 162 if (uses_default_policy_host_restrictions) | |
| 163 return default_policy_blocked_hosts(); | |
| 164 runtime_lock_.AssertAcquired(); | |
| 165 return policy_blocked_hosts_unsafe_; | |
| 166 } | |
| 167 | |
| 168 const URLPatternSet PermissionsData::policy_allowed_hosts() const { | |
| 169 base::AutoLock auto_lock(runtime_lock_); | |
| 170 return PolicyAllowedHostsUnsafe(); | |
| 171 } | |
| 172 | |
| 173 const URLPatternSet& PermissionsData::PolicyAllowedHostsUnsafe() const { | |
| 174 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | |
| 175 if (uses_default_policy_host_restrictions) | |
| 176 return default_policy_allowed_hosts(); | |
| 177 runtime_lock_.AssertAcquired(); | |
| 178 return policy_allowed_hosts_unsafe_; | |
| 179 } | |
| 180 | |
| 181 void PermissionsData::BindToCurrentThread() const { | 130 void PermissionsData::BindToCurrentThread() const { |
| 182 DCHECK(!thread_checker_); | 131 DCHECK(!thread_checker_); |
| 183 thread_checker_.reset(new base::ThreadChecker()); | 132 thread_checker_.reset(new base::ThreadChecker()); |
| 184 } | 133 } |
| 185 | 134 |
| 186 void PermissionsData::SetPermissions( | 135 void PermissionsData::SetPermissions( |
| 187 std::unique_ptr<const PermissionSet> active, | 136 std::unique_ptr<const PermissionSet> active, |
| 188 std::unique_ptr<const PermissionSet> withheld) const { | 137 std::unique_ptr<const PermissionSet> withheld) const { |
| 189 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); | 138 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); |
| 190 active_permissions_unsafe_ = std::move(active); | 139 active_permissions_unsafe_ = std::move(active); |
| 191 withheld_permissions_unsafe_ = std::move(withheld); | 140 withheld_permissions_unsafe_ = std::move(withheld); |
| 192 } | 141 } |
| 193 | 142 |
| 194 void PermissionsData::SetPolicyHostRestrictions( | |
| 195 const URLPatternSet& runtime_blocked_hosts, | |
| 196 const URLPatternSet& runtime_allowed_hosts) const { | |
| 197 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); | |
| 198 policy_blocked_hosts_unsafe_ = runtime_blocked_hosts; | |
| 199 policy_allowed_hosts_unsafe_ = runtime_allowed_hosts; | |
| 200 uses_default_policy_host_restrictions = false; | |
| 201 } | |
| 202 | |
| 203 void PermissionsData::SetUsesDefaultHostRestrictions() const { | |
| 204 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); | |
| 205 uses_default_policy_host_restrictions = true; | |
| 206 } | |
| 207 | |
| 208 // static | |
| 209 void PermissionsData::SetDefaultPolicyHostRestrictions( | |
| 210 const URLPatternSet& default_runtime_blocked_hosts, | |
| 211 const URLPatternSet& default_runtime_allowed_hosts) { | |
| 212 default_runtime_policy.Get().blocked_hosts = default_runtime_blocked_hosts; | |
| 213 default_runtime_policy.Get().allowed_hosts = default_runtime_allowed_hosts; | |
| 214 } | |
| 215 | |
| 216 void PermissionsData::SetActivePermissions( | 143 void PermissionsData::SetActivePermissions( |
| 217 std::unique_ptr<const PermissionSet> active) const { | 144 std::unique_ptr<const PermissionSet> active) const { |
| 218 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); | 145 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); |
| 219 active_permissions_unsafe_ = std::move(active); | 146 active_permissions_unsafe_ = std::move(active); |
| 220 } | 147 } |
| 221 | 148 |
| 222 void PermissionsData::UpdateTabSpecificPermissions( | 149 void PermissionsData::UpdateTabSpecificPermissions( |
| 223 int tab_id, | 150 int tab_id, |
| 224 const PermissionSet& permissions) const { | 151 const PermissionSet& permissions) const { |
| 225 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); | 152 AutoLockOnValidThread lock(runtime_lock_, thread_checker_.get()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 URLPatternSet PermissionsData::GetEffectiveHostPermissions() const { | 201 URLPatternSet PermissionsData::GetEffectiveHostPermissions() const { |
| 275 base::AutoLock auto_lock(runtime_lock_); | 202 base::AutoLock auto_lock(runtime_lock_); |
| 276 URLPatternSet effective_hosts = active_permissions_unsafe_->effective_hosts(); | 203 URLPatternSet effective_hosts = active_permissions_unsafe_->effective_hosts(); |
| 277 for (const auto& val : tab_specific_permissions_) | 204 for (const auto& val : tab_specific_permissions_) |
| 278 effective_hosts.AddPatterns(val.second->effective_hosts()); | 205 effective_hosts.AddPatterns(val.second->effective_hosts()); |
| 279 return effective_hosts; | 206 return effective_hosts; |
| 280 } | 207 } |
| 281 | 208 |
| 282 bool PermissionsData::HasHostPermission(const GURL& url) const { | 209 bool PermissionsData::HasHostPermission(const GURL& url) const { |
| 283 base::AutoLock auto_lock(runtime_lock_); | 210 base::AutoLock auto_lock(runtime_lock_); |
| 284 return active_permissions_unsafe_->HasExplicitAccessToOrigin(url) && | 211 return active_permissions_unsafe_->HasExplicitAccessToOrigin(url); |
| 285 !IsRuntimeBlockedHost(url); | |
| 286 } | 212 } |
| 287 | 213 |
| 288 bool PermissionsData::HasEffectiveAccessToAllHosts() const { | 214 bool PermissionsData::HasEffectiveAccessToAllHosts() const { |
| 289 base::AutoLock auto_lock(runtime_lock_); | 215 base::AutoLock auto_lock(runtime_lock_); |
| 290 return active_permissions_unsafe_->HasEffectiveAccessToAllHosts(); | 216 return active_permissions_unsafe_->HasEffectiveAccessToAllHosts(); |
| 291 } | 217 } |
| 292 | 218 |
| 293 PermissionMessages PermissionsData::GetPermissionMessages() const { | 219 PermissionMessages PermissionsData::GetPermissionMessages() const { |
| 294 base::AutoLock auto_lock(runtime_lock_); | 220 base::AutoLock auto_lock(runtime_lock_); |
| 295 return PermissionMessageProvider::Get()->GetPermissionMessages( | 221 return PermissionMessageProvider::Get()->GetPermissionMessages( |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 if (tab_id >= 0) { | 320 if (tab_id >= 0) { |
| 395 const PermissionSet* tab_permissions = GetTabSpecificPermissions(tab_id); | 321 const PermissionSet* tab_permissions = GetTabSpecificPermissions(tab_id); |
| 396 if (tab_permissions && | 322 if (tab_permissions && |
| 397 tab_permissions->explicit_hosts().MatchesSecurityOrigin(url)) { | 323 tab_permissions->explicit_hosts().MatchesSecurityOrigin(url)) { |
| 398 return true; | 324 return true; |
| 399 } | 325 } |
| 400 } | 326 } |
| 401 return false; | 327 return false; |
| 402 } | 328 } |
| 403 | 329 |
| 404 bool PermissionsData::IsRuntimeBlockedHost(const GURL& url) const { | |
| 405 runtime_lock_.AssertAcquired(); | |
| 406 return PolicyBlockedHostsUnsafe().MatchesURL(url) && | |
| 407 !PolicyAllowedHostsUnsafe().MatchesURL(url); | |
| 408 } | |
| 409 | |
| 410 PermissionsData::AccessType PermissionsData::CanRunOnPage( | 330 PermissionsData::AccessType PermissionsData::CanRunOnPage( |
| 411 const Extension* extension, | 331 const Extension* extension, |
| 412 const GURL& document_url, | 332 const GURL& document_url, |
| 413 int tab_id, | 333 int tab_id, |
| 414 const URLPatternSet& permitted_url_patterns, | 334 const URLPatternSet& permitted_url_patterns, |
| 415 const URLPatternSet& withheld_url_patterns, | 335 const URLPatternSet& withheld_url_patterns, |
| 416 std::string* error) const { | 336 std::string* error) const { |
| 417 runtime_lock_.AssertAcquired(); | 337 runtime_lock_.AssertAcquired(); |
| 418 if (g_policy_delegate && !g_policy_delegate->CanExecuteScriptOnPage( | 338 if (g_policy_delegate && |
| 419 extension, document_url, tab_id, error)) | 339 !g_policy_delegate->CanExecuteScriptOnPage(extension, document_url, |
| 420 return ACCESS_DENIED; | 340 tab_id, error)) { |
| 421 | |
| 422 if (extension->location() != Manifest::COMPONENT && | |
| 423 extension->permissions_data()->IsRuntimeBlockedHost(document_url)) { | |
| 424 if (error) | |
| 425 *error = extension_misc::kPolicyBlockedScripting; | |
| 426 return ACCESS_DENIED; | 341 return ACCESS_DENIED; |
| 427 } | 342 } |
| 428 | 343 |
| 429 if (IsRestrictedUrl(document_url, extension, error)) | 344 if (IsRestrictedUrl(document_url, extension, error)) |
| 430 return ACCESS_DENIED; | 345 return ACCESS_DENIED; |
| 431 | 346 |
| 432 if (HasTabSpecificPermissionToExecuteScript(tab_id, document_url)) | 347 if (HasTabSpecificPermissionToExecuteScript(tab_id, document_url)) |
| 433 return ACCESS_ALLOWED; | 348 return ACCESS_ALLOWED; |
| 434 | 349 |
| 435 if (permitted_url_patterns.MatchesURL(document_url)) | 350 if (permitted_url_patterns.MatchesURL(document_url)) |
| 436 return ACCESS_ALLOWED; | 351 return ACCESS_ALLOWED; |
| 437 | 352 |
| 438 if (withheld_url_patterns.MatchesURL(document_url)) | 353 if (withheld_url_patterns.MatchesURL(document_url)) |
| 439 return ACCESS_WITHHELD; | 354 return ACCESS_WITHHELD; |
| 440 | 355 |
| 441 if (error) { | 356 if (error) { |
| 442 if (extension->permissions_data()->active_permissions().HasAPIPermission( | 357 if (extension->permissions_data()->active_permissions().HasAPIPermission( |
| 443 APIPermission::kTab)) { | 358 APIPermission::kTab)) { |
| 444 *error = ErrorUtils::FormatErrorMessage( | 359 *error = ErrorUtils::FormatErrorMessage( |
| 445 manifest_errors::kCannotAccessPageWithUrl, document_url.spec()); | 360 manifest_errors::kCannotAccessPageWithUrl, document_url.spec()); |
| 446 } else { | 361 } else { |
| 447 *error = manifest_errors::kCannotAccessPage; | 362 *error = manifest_errors::kCannotAccessPage; |
| 448 } | 363 } |
| 449 } | 364 } |
| 450 | 365 |
| 451 return ACCESS_DENIED; | 366 return ACCESS_DENIED; |
| 452 } | 367 } |
| 453 | 368 |
| 454 } // namespace extensions | 369 } // namespace extensions |
| OLD | NEW |