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
|
// Copyright (c) 2012 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/active_tab_permission_granter.h"
#include <set>
#include <utility>
#include <vector>
#include "base/no_destructor.h"
#include "chrome/browser/extensions/extension_action_runner.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/permissions/permission_set.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/user_script.h"
#include "url/gurl.h"
namespace extensions {
namespace {
using CreateMessageFunction = base::Callback<IPC::Message*(bool)>;
// Creates a new IPC message for updating tab-specific permissions.
IPC::Message* CreateUpdateMessage(const GURL& visible_url,
const std::string& extension_id,
const URLPatternSet& new_hosts,
int tab_id,
bool update_whitelist) {
return new ExtensionMsg_UpdateTabSpecificPermissions(
visible_url, extension_id, new_hosts, update_whitelist, tab_id);
}
// Creates a new IPC message for clearing tab-specific permissions.
IPC::Message* CreateClearMessage(const std::vector<std::string>& ids,
int tab_id,
bool update_whitelist) {
return new ExtensionMsg_ClearTabSpecificPermissions(
ids, update_whitelist, tab_id);
}
// Sends a message exactly once to each render process host owning one of the
// given |frame_hosts| and |tab_process|. If |tab_process| doesn't own any of
// the |frame_hosts|, it will not be signaled to update its origin whitelist.
void SendMessageToProcesses(
const std::set<content::RenderFrameHost*>& frame_hosts,
content::RenderProcessHost* tab_process,
const CreateMessageFunction& create_message) {
std::set<content::RenderProcessHost*> sent_to_hosts;
for (content::RenderFrameHost* frame_host : frame_hosts) {
content::RenderProcessHost* process_host = frame_host->GetProcess();
if (sent_to_hosts.count(process_host) == 0) {
// Extension processes have to update the origin whitelists.
process_host->Send(create_message.Run(true));
sent_to_hosts.insert(frame_host->GetProcess());
}
}
// If the tab wasn't one of those processes already updated (it likely
// wasn't), update it. Tabs don't need to update the origin whitelist.
if (sent_to_hosts.count(tab_process) == 0)
tab_process->Send(create_message.Run(false));
}
std::unique_ptr<ActiveTabPermissionGranter::Delegate>&
GetActiveTabPermissionGranterDelegateWrapper() {
static base::NoDestructor<
std::unique_ptr<ActiveTabPermissionGranter::Delegate>>
delegate_wrapper;
return *delegate_wrapper;
}
ActiveTabPermissionGranter::Delegate* GetActiveTabPermissionGranterDelegate() {
return GetActiveTabPermissionGranterDelegateWrapper().get();
}
// Returns true if activeTab is allowed to be granted to the extension. This can
// return false for platform-specific implementations.
bool ShouldGrantActiveTabOrPrompt(const Extension* extension,
content::WebContents* web_contents) {
return !GetActiveTabPermissionGranterDelegate() ||
GetActiveTabPermissionGranterDelegate()->ShouldGrantActiveTabOrPrompt(
extension, web_contents);
}
} // namespace
ActiveTabPermissionGranter::ActiveTabPermissionGranter(
content::WebContents* web_contents,
int tab_id,
Profile* profile)
: content::WebContentsObserver(web_contents),
tab_id_(tab_id),
extension_registry_observer_(this) {
extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
}
ActiveTabPermissionGranter::~ActiveTabPermissionGranter() {}
// static
void ActiveTabPermissionGranter::SetPlatformDelegate(
std::unique_ptr<Delegate> delegate) {
GetActiveTabPermissionGranterDelegateWrapper() = std::move(delegate);
}
void ActiveTabPermissionGranter::GrantIfRequested(const Extension* extension) {
if (granted_extensions_.Contains(extension->id()))
return;
APIPermissionSet new_apis;
URLPatternSet new_hosts;
const PermissionsData* permissions_data = extension->permissions_data();
// TODO(devlin): This should be GetLastCommittedURL().
GURL url = web_contents()->GetVisibleURL();
// If the extension requested the host permission to |url| but had it
// withheld, we grant it active tab-style permissions, even if it doesn't have
// the activeTab permission in the manifest. This is necessary for the
// runtime host permissions feature to work.
// Note: It's important that we check if the extension has activeTab before
// checking ShouldGrantActiveTabOrPrompt() in order to prevent
// ShouldGrantActiveTabOrPrompt() from prompting for extensions that don't
// request the activeTab permission.
if ((permissions_data->HasAPIPermission(APIPermission::kActiveTab) ||
permissions_data->withheld_permissions().effective_hosts().MatchesURL(
url)) &&
ShouldGrantActiveTabOrPrompt(extension, web_contents())) {
// Gate activeTab for file urls on extensions having explicit access to file
// urls.
int valid_schemes = UserScript::ValidUserScriptSchemes();
if (!util::AllowFileAccess(extension->id(),
web_contents()->GetBrowserContext())) {
valid_schemes &= ~URLPattern::SCHEME_FILE;
}
new_hosts.AddOrigin(valid_schemes, url.GetOrigin());
new_apis.insert(APIPermission::kTab);
}
if (permissions_data->HasAPIPermission(APIPermission::kTabCapture))
new_apis.insert(APIPermission::kTabCaptureForTab);
if (!new_apis.empty() || !new_hosts.is_empty()) {
granted_extensions_.Insert(extension);
PermissionSet new_permissions(std::move(new_apis), ManifestPermissionSet(),
new_hosts, new_hosts);
permissions_data->UpdateTabSpecificPermissions(tab_id_, new_permissions);
content::NavigationEntry* navigation_entry =
web_contents()->GetController().GetVisibleEntry();
if (navigation_entry) {
// We update all extension render views with the new tab permissions, and
// also the tab itself.
CreateMessageFunction update_message =
base::Bind(&CreateUpdateMessage, navigation_entry->GetURL(),
extension->id(), new_hosts.Clone(), tab_id_);
SendMessageToProcesses(
ProcessManager::Get(web_contents()->GetBrowserContext())
->GetRenderFrameHostsForExtension(extension->id()),
web_contents()->GetMainFrame()->GetProcess(), update_message);
// If more things ever need to know about this, we should consider making
// an observer class.
// It's important that this comes after the IPC is sent to the renderer,
// so that any tasks executing in the renderer occur after it has the
// updated permissions.
ExtensionActionRunner::GetForWebContents(web_contents())
->OnActiveTabPermissionGranted(extension);
}
}
}
void ActiveTabPermissionGranter::RevokeForTesting() {
ClearActiveExtensionsAndNotify();
}
void ActiveTabPermissionGranter::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Important: sub-frames don't get granted!
if (!navigation_handle->IsInMainFrame() ||
!navigation_handle->HasCommitted() ||
navigation_handle->IsSameDocument()) {
return;
}
// Only clear the granted permissions for cross-origin navigations.
// TODO(devlin): We likely shouldn't be using the visible entry. Instead,
// we should use WebContents::GetLastCommittedURL().
content::NavigationEntry* navigation_entry =
web_contents()->GetController().GetVisibleEntry();
if (navigation_entry && navigation_entry->GetURL().GetOrigin() ==
navigation_handle->GetPreviousURL().GetOrigin()) {
return;
}
ClearActiveExtensionsAndNotify();
}
void ActiveTabPermissionGranter::WebContentsDestroyed() {
ClearActiveExtensionsAndNotify();
}
void ActiveTabPermissionGranter::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) {
// Note: don't need to clear the permissions (nor tell the renderer about it)
// because it's being unloaded anyway.
granted_extensions_.Remove(extension->id());
}
void ActiveTabPermissionGranter::ClearActiveExtensionsAndNotify() {
if (granted_extensions_.is_empty())
return;
std::set<content::RenderFrameHost*> frame_hosts;
std::vector<std::string> extension_ids;
ProcessManager* process_manager =
ProcessManager::Get(web_contents()->GetBrowserContext());
for (const scoped_refptr<const Extension>& extension : granted_extensions_) {
extension->permissions_data()->ClearTabSpecificPermissions(tab_id_);
extension_ids.push_back(extension->id());
std::set<content::RenderFrameHost*> extension_frame_hosts =
process_manager->GetRenderFrameHostsForExtension(extension->id());
frame_hosts.insert(extension_frame_hosts.begin(),
extension_frame_hosts.end());
}
CreateMessageFunction clear_message =
base::Bind(&CreateClearMessage, extension_ids, tab_id_);
SendMessageToProcesses(
frame_hosts, web_contents()->GetMainFrame()->GetProcess(), clear_message);
granted_extensions_.Clear();
}
} // namespace extensions
|