Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/permissions_updater.h" | 5 #include "chrome/browser/extensions/permissions_updater.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 | 29 |
| 30 using content::RenderProcessHost; | 30 using content::RenderProcessHost; |
| 31 using extensions::permissions_api_helpers::PackPermissionSet; | 31 using extensions::permissions_api_helpers::PackPermissionSet; |
| 32 | 32 |
| 33 namespace extensions { | 33 namespace extensions { |
| 34 | 34 |
| 35 namespace permissions = api::permissions; | 35 namespace permissions = api::permissions; |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // Returns a set of single origin permissions from |active| that match | |
| 40 // |bounded_active|. This is necessary for two reasons: | |
| 41 // a) single origin active permissions can get filtered out in | |
| 42 // GetBoundedActivePermissions because they are not recognized as a subset | |
| 43 // of all-host permissions | |
| 44 // b) active permissions that do not match any manifest permissions can | |
| 45 // exist if a manifest permission is dropped | |
| 46 URLPatternSet FilterSingleOriginPermissions( | |
| 47 const URLPatternSet& active, | |
|
not at google - send to devlin
2014/08/13 22:59:17
|active| vs |bounded_active| are implementation de
gpdavis
2014/08/13 23:23:06
Can I leave the comment the same, save for swappin
| |
| 48 const URLPatternSet& bounded_active) { | |
| 49 URLPatternSet single_origin_permissions; | |
| 50 for (URLPatternSet::const_iterator iter = active.begin(); | |
| 51 iter != active.end(); | |
| 52 ++iter) { | |
| 53 if (iter->IsOrigin() && bounded_active.MatchesURL(iter->ToOrigin())) | |
|
not at google - send to devlin
2014/08/13 22:59:17
Bleh ToOrigin is kind of pointless actually, since
gpdavis
2014/08/13 23:23:06
Done.
| |
| 54 single_origin_permissions.AddPattern(*iter); | |
| 55 } | |
| 56 return single_origin_permissions; | |
| 57 } | |
| 58 | |
| 39 // Returns a PermissionSet that has the active permissions of the extension, | 59 // Returns a PermissionSet that has the active permissions of the extension, |
| 40 // bounded to its current manifest. | 60 // bounded to its current manifest. |
| 41 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( | 61 scoped_refptr<const PermissionSet> GetBoundedActivePermissions( |
| 42 const Extension* extension, ExtensionPrefs* extension_prefs) { | 62 const Extension* extension, |
| 63 const PermissionSet* active_permissions) { | |
| 43 // If the extension has used the optional permissions API, it will have a | 64 // If the extension has used the optional permissions API, it will have a |
| 44 // custom set of active permissions defined in the extension prefs. Here, | 65 // custom set of active permissions defined in the extension prefs. Here, |
| 45 // we update the extension's active permissions based on the prefs. | 66 // we update the extension's active permissions based on the prefs. |
| 46 scoped_refptr<const PermissionSet> active_permissions = | |
| 47 extension_prefs->GetActivePermissions(extension->id()); | |
| 48 if (!active_permissions) | 67 if (!active_permissions) |
| 49 return extension->permissions_data()->active_permissions(); | 68 return extension->permissions_data()->active_permissions(); |
| 50 | 69 |
| 51 scoped_refptr<const PermissionSet> required_permissions = | 70 scoped_refptr<const PermissionSet> required_permissions = |
| 52 PermissionsParser::GetRequiredPermissions(extension); | 71 PermissionsParser::GetRequiredPermissions(extension); |
| 53 | 72 |
| 54 // We restrict the active permissions to be within the bounds defined in the | 73 // We restrict the active permissions to be within the bounds defined in the |
| 55 // extension's manifest. | 74 // extension's manifest. |
| 56 // a) active permissions must be a subset of optional + default permissions | 75 // a) active permissions must be a subset of optional + default permissions |
| 57 // b) active permissions must contains all default permissions | 76 // b) active permissions must contains all default permissions |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 if (!Manifest::IsUnpackedLocation(extension->location()) && | 156 if (!Manifest::IsUnpackedLocation(extension->location()) && |
| 138 extension->location() != Manifest::INTERNAL) | 157 extension->location() != Manifest::INTERNAL) |
| 139 return; | 158 return; |
| 140 | 159 |
| 141 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( | 160 ExtensionPrefs::Get(browser_context_)->AddGrantedPermissions( |
| 142 extension->id(), | 161 extension->id(), |
| 143 extension->permissions_data()->active_permissions().get()); | 162 extension->permissions_data()->active_permissions().get()); |
| 144 } | 163 } |
| 145 | 164 |
| 146 void PermissionsUpdater::InitializePermissions(const Extension* extension) { | 165 void PermissionsUpdater::InitializePermissions(const Extension* extension) { |
| 166 scoped_refptr<const PermissionSet> active_permissions = | |
| 167 ExtensionPrefs::Get(browser_context_) | |
| 168 ->GetActivePermissions(extension->id()); | |
| 147 scoped_refptr<const PermissionSet> bounded_active = | 169 scoped_refptr<const PermissionSet> bounded_active = |
| 148 GetBoundedActivePermissions(extension, | 170 GetBoundedActivePermissions(extension, active_permissions.get()); |
| 149 ExtensionPrefs::Get(browser_context_)); | |
| 150 | 171 |
| 151 // We withhold permissions iff the switch to do so is enabled, the extension | 172 // We withhold permissions iff the switch to do so is enabled, the extension |
| 152 // shows up in chrome:extensions (so the user can grant withheld permissions), | 173 // shows up in chrome:extensions (so the user can grant withheld permissions), |
| 153 // the extension is not part of chrome or corporate policy, and also not on | 174 // the extension is not part of chrome or corporate policy, and also not on |
| 154 // the scripting whitelist. Additionally, we don't withhold if the extension | 175 // the scripting whitelist. Additionally, we don't withhold if the extension |
| 155 // has the preference to allow scripting on all urls. | 176 // has the preference to allow scripting on all urls. |
| 156 bool should_withhold_permissions = | 177 bool should_withhold_permissions = |
| 157 FeatureSwitch::scripts_require_action()->IsEnabled() && | 178 FeatureSwitch::scripts_require_action()->IsEnabled() && |
| 158 extension->ShouldDisplayInExtensionSettings() && | 179 extension->ShouldDisplayInExtensionSettings() && |
| 159 !Manifest::IsPolicyLocation(extension->location()) && | 180 !Manifest::IsPolicyLocation(extension->location()) && |
| 160 !Manifest::IsComponentLocation(extension->location()) && | 181 !Manifest::IsComponentLocation(extension->location()) && |
| 161 !PermissionsData::CanExecuteScriptEverywhere(extension) && | 182 !PermissionsData::CanExecuteScriptEverywhere(extension) && |
| 162 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); | 183 !util::AllowedScriptingOnAllUrls(extension->id(), browser_context_); |
| 163 | 184 |
| 164 URLPatternSet granted_explicit_hosts; | 185 URLPatternSet granted_explicit_hosts; |
| 165 URLPatternSet withheld_explicit_hosts; | 186 URLPatternSet withheld_explicit_hosts; |
| 166 SegregateUrlPermissions(bounded_active->explicit_hosts(), | 187 SegregateUrlPermissions(bounded_active->explicit_hosts(), |
| 167 should_withhold_permissions, | 188 should_withhold_permissions, |
| 168 &granted_explicit_hosts, | 189 &granted_explicit_hosts, |
| 169 &withheld_explicit_hosts); | 190 &withheld_explicit_hosts); |
| 170 | 191 |
| 171 URLPatternSet granted_scriptable_hosts; | 192 URLPatternSet granted_scriptable_hosts; |
| 172 URLPatternSet withheld_scriptable_hosts; | 193 URLPatternSet withheld_scriptable_hosts; |
| 173 SegregateUrlPermissions(bounded_active->scriptable_hosts(), | 194 SegregateUrlPermissions(bounded_active->scriptable_hosts(), |
| 174 should_withhold_permissions, | 195 should_withhold_permissions, |
| 175 &granted_scriptable_hosts, | 196 &granted_scriptable_hosts, |
| 176 &withheld_scriptable_hosts); | 197 &withheld_scriptable_hosts); |
| 177 | 198 |
| 199 // After withholding permissions, add back any origins to the active set that | |
| 200 // may have been lost during the set operations that would have dropped them. | |
| 201 // For example, the union of <all_urls> and <"example.com"> is <all_urls>, so | |
| 202 // we may lose "example.com". However, "example.com" is important once | |
| 203 // <all_urls> is stripped during withholding. | |
| 204 if (active_permissions) { | |
| 205 granted_explicit_hosts.AddPatterns( | |
| 206 FilterSingleOriginPermissions(active_permissions->explicit_hosts(), | |
| 207 bounded_active->explicit_hosts())); | |
| 208 granted_scriptable_hosts.AddPatterns( | |
| 209 FilterSingleOriginPermissions(active_permissions->scriptable_hosts(), | |
| 210 bounded_active->scriptable_hosts())); | |
| 211 } | |
| 212 | |
| 178 bounded_active = new PermissionSet(bounded_active->apis(), | 213 bounded_active = new PermissionSet(bounded_active->apis(), |
| 179 bounded_active->manifest_permissions(), | 214 bounded_active->manifest_permissions(), |
| 180 granted_explicit_hosts, | 215 granted_explicit_hosts, |
| 181 granted_scriptable_hosts); | 216 granted_scriptable_hosts); |
| 182 | 217 |
| 183 scoped_refptr<const PermissionSet> withheld = | 218 scoped_refptr<const PermissionSet> withheld = |
| 184 new PermissionSet(APIPermissionSet(), | 219 new PermissionSet(APIPermissionSet(), |
| 185 ManifestPermissionSet(), | 220 ManifestPermissionSet(), |
| 186 withheld_explicit_hosts, | 221 withheld_explicit_hosts, |
| 187 withheld_scriptable_hosts); | 222 withheld_scriptable_hosts); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 Profile::FromBrowserContext(host->GetBrowserContext()))) { | 356 Profile::FromBrowserContext(host->GetBrowserContext()))) { |
| 322 host->Send(new ExtensionMsg_UpdatePermissions(params)); | 357 host->Send(new ExtensionMsg_UpdatePermissions(params)); |
| 323 } | 358 } |
| 324 } | 359 } |
| 325 | 360 |
| 326 // Trigger the onAdded and onRemoved events in the extension. | 361 // Trigger the onAdded and onRemoved events in the extension. |
| 327 DispatchEvent(extension->id(), event_name, changed); | 362 DispatchEvent(extension->id(), event_name, changed); |
| 328 } | 363 } |
| 329 | 364 |
| 330 } // namespace extensions | 365 } // namespace extensions |
| OLD | NEW |