permissions_manager.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2021 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. #ifndef EXTENSIONS_BROWSER_PERMISSIONS_MANAGER_H_
  5. #define EXTENSIONS_BROWSER_PERMISSIONS_MANAGER_H_
  6. #include <set>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "components/keyed_service/core/keyed_service.h"
  11. #include "extensions/common/extension_id.h"
  12. #include "url/origin.h"
  13. class BrowserContextKeyedServiceFactory;
  14. namespace content {
  15. class BrowserContext;
  16. }
  17. namespace user_prefs {
  18. class PrefRegistrySyncable;
  19. }
  20. namespace extensions {
  21. class ExtensionPrefs;
  22. class Extension;
  23. class PermissionSet;
  24. // Class for managing user-scoped extension permissions.
  25. // Includes blocking all extensions from running on a site and automatically
  26. // running all extensions on a site.
  27. class PermissionsManager : public KeyedService {
  28. public:
  29. // A struct storing the user-specified settings that apply to all extensions,
  30. // past, present, or future.
  31. // We use url::Origin here (rather than URLPatternSet) because permission
  32. // grants (and restrictions) are only meaningful at an origin level. It's not
  33. // possible to, say, block an extension from running on google.com/maps while
  34. // still allowing it to run on google.com/search.
  35. // Note: Policy extensions and component extensions can bypass these
  36. // settings.
  37. struct UserPermissionsSettings {
  38. UserPermissionsSettings();
  39. ~UserPermissionsSettings();
  40. UserPermissionsSettings(const UserPermissionsSettings& other) = delete;
  41. UserPermissionsSettings& operator=(UserPermissionsSettings& other) = delete;
  42. // Sites the user has blocked all extensions from running on.
  43. std::set<url::Origin> restricted_sites;
  44. // Sites the user has allowed all extensions to run on.
  45. std::set<url::Origin> permitted_sites;
  46. };
  47. struct ExtensionSiteAccess {
  48. // The extension has access to the current domain.
  49. bool has_site_access = false;
  50. // The extension requested access to the current domain, but it was
  51. // withheld.
  52. bool withheld_site_access = false;
  53. // The extension has access to all sites (or a pattern sufficiently broad
  54. // as to be functionally similar, such as https://*.com/*). Note that since
  55. // this includes "broad" patterns, this may be true even if
  56. // `has_site_access` is false.
  57. bool has_all_sites_access = false;
  58. // The extension wants access to all sites (or a pattern sufficiently broad
  59. // as to be functionally similar, such as https://*.com/*). Note that since
  60. // this includes "broad" patterns, this may be true even if
  61. // `withheld_site_access` is false.
  62. bool withheld_all_sites_access = false;
  63. };
  64. // The user's site setting for a given site.
  65. enum class UserSiteSetting {
  66. // All extensions that request access are granted access in the site.
  67. kGrantAllExtensions,
  68. // All extensions that request access have withheld access in the site.
  69. kBlockAllExtensions,
  70. // Each extension that requests access can have its site access customized
  71. // in the site.
  72. kCustomizeByExtension,
  73. };
  74. enum class UpdateReason {
  75. // Permissions were added to the extension.
  76. kAdded,
  77. // Permissions were removed from the extension.
  78. kRemoved,
  79. // Policy that affects permissions was updated.
  80. kPolicy,
  81. };
  82. class Observer {
  83. public:
  84. virtual void OnUserPermissionsSettingsChanged(
  85. const UserPermissionsSettings& settings) {}
  86. virtual void OnExtensionPermissionsUpdated(const Extension& extension,
  87. const PermissionSet& permissions,
  88. UpdateReason reason) {}
  89. };
  90. explicit PermissionsManager(content::BrowserContext* browser_context);
  91. ~PermissionsManager() override;
  92. PermissionsManager(const PermissionsManager&) = delete;
  93. const PermissionsManager& operator=(const PermissionsManager&) = delete;
  94. // Retrieves the PermissionsManager for a given `browser_context`.
  95. static PermissionsManager* Get(content::BrowserContext* browser_context);
  96. // Retrieves the factory instance for the PermissionsManager.
  97. static BrowserContextKeyedServiceFactory* GetFactory();
  98. // Registers the user preference that stores user permissions.
  99. static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
  100. // Updates the user site settings for the given `origin` to be
  101. // `site_settings`.
  102. void UpdateUserSiteSetting(const url::Origin& origin,
  103. PermissionsManager::UserSiteSetting site_setting);
  104. // Adds `origin` to the list of sites the user has blocked all
  105. // extensions from running on. If `origin` is in permitted_sites, it will
  106. // remove it from such list.
  107. void AddUserRestrictedSite(const url::Origin& origin);
  108. // Removes `origin` from the list of sites the user has blocked all
  109. // extensions from running on and notifies observers.
  110. void RemoveUserRestrictedSite(const url::Origin& origin);
  111. // Adds `origin` to the list of sites the user has allowed all
  112. // extensions to run on. If `origin` is in restricted_sites, it will remove it
  113. // from such list.
  114. void AddUserPermittedSite(const url::Origin& origin);
  115. // Removes `origin` from the list of sites the user has allowed all
  116. // extensions to run on and notifies observers.
  117. void RemoveUserPermittedSite(const url::Origin& origin);
  118. // Returns the user's permission settings.
  119. const UserPermissionsSettings& GetUserPermissionsSettings() const;
  120. // Returns the user's site setting for `origin`.
  121. UserSiteSetting GetUserSiteSetting(const url::Origin& origin) const;
  122. // Returns the current access level for the extension on the specified `url`.
  123. ExtensionSiteAccess GetSiteAccess(const Extension& extension,
  124. const GURL& url) const;
  125. // Returns whether Chrome has withheld host permissions from the extension.
  126. bool HasWithheldHostPermissions(const ExtensionId& extension_id) const;
  127. // Returns the effective list of runtime-granted permissions for a given
  128. // `extension` from its prefs. ExtensionPrefs doesn't store the valid schemes
  129. // for URLPatterns, which results in the chrome:-scheme being included for
  130. // <all_urls> when retrieving it directly from the prefs; this then causes
  131. // CHECKs to fail when validating that permissions being revoked are present
  132. // (see https://crbug.com/930062).
  133. // Returns null if there are no stored runtime-granted permissions.
  134. // TODO(https://crbug.com/931881): ExtensionPrefs should return
  135. // properly-bounded permissions.
  136. std::unique_ptr<PermissionSet> GetRuntimePermissionsFromPrefs(
  137. const Extension& extension) const;
  138. // Returns the set of permissions that the `extension` wants to have active at
  139. // this time. This does *not* take into account user-granted or runtime-
  140. // withheld permissions.
  141. std::unique_ptr<PermissionSet> GetBoundedExtensionDesiredPermissions(
  142. const Extension& extension) const;
  143. // Returns the set of permissions that should be granted to the given
  144. // `extension` according to the runtime-granted permissions and current
  145. // preferences, omitting host permissions if the extension supports it and
  146. // the user has withheld permissions.
  147. std::unique_ptr<PermissionSet> GetEffectivePermissionsToGrant(
  148. const Extension& extension,
  149. const PermissionSet& desired_permissions) const;
  150. // Notifies `observers_` that the permissions have been updated for an
  151. // extension.
  152. void NotifyExtensionPermissionsUpdated(const Extension& extension,
  153. const PermissionSet& permissions,
  154. UpdateReason reason);
  155. // Adds or removes observers.
  156. void AddObserver(Observer* observer);
  157. void RemoveObserver(Observer* observer);
  158. private:
  159. // Called whenever `user_permissions_` have changed.
  160. void OnUserPermissionsSettingsChanged();
  161. // Removes `origin` from the list of sites the user has allowed all
  162. // extensions to run on and saves the change to `extension_prefs_`. Returns if
  163. // the site has been removed.
  164. bool RemovePermittedSiteAndUpdatePrefs(const url::Origin& origin);
  165. // Removes `origin` from the list of sites the user has blocked all
  166. // extensions from running on and saves the change to `extension_prefs_`.
  167. // Returns if the site has been removed.
  168. bool RemoveRestrictedSiteAndUpdatePrefs(const url::Origin& origin);
  169. // Updates the given `extension` with the new `user_permitted_set` of sites
  170. // all extensions are allowed to run on. Note that this only updates the
  171. // permissions in the browser; updates must then be sent separately to the
  172. // renderer and network service.
  173. void UpdatePermissionsWithUserSettings(
  174. const Extension& extension,
  175. const PermissionSet& user_permitted_set);
  176. // Notifies observers of a permissions change.
  177. void NotifyObserversOfChange();
  178. base::ObserverList<Observer>::Unchecked observers_;
  179. // The associated browser context.
  180. const raw_ptr<content::BrowserContext> browser_context_;
  181. const raw_ptr<ExtensionPrefs> extension_prefs_;
  182. UserPermissionsSettings user_permissions_;
  183. base::WeakPtrFactory<PermissionsManager> weak_factory_{this};
  184. };
  185. } // namespace extensions
  186. #endif // EXTENSIONS_BROWSER_PERMISSIONS_MANAGER_H_