Chromium Code Reviews| Index: chrome/renderer/extensions/resource_request_policy.cc |
| diff --git a/chrome/renderer/extensions/resource_request_policy.cc b/chrome/renderer/extensions/resource_request_policy.cc |
| index 0519a63fe5363c77ad5c83e97caa4cfbc6fca418..3c8b58f23a0bd5bc0b487b4f96b1c20c3c04fc8d 100644 |
| --- a/chrome/renderer/extensions/resource_request_policy.cc |
| +++ b/chrome/renderer/extensions/resource_request_policy.cc |
| @@ -30,6 +30,28 @@ namespace extensions { |
| ResourceRequestPolicy::ResourceRequestPolicy(Dispatcher* dispatcher) |
| : dispatcher_(dispatcher) {} |
| +ResourceRequestPolicy::~ResourceRequestPolicy() = default; |
| + |
| +void ResourceRequestPolicy::OnExtensionAdded(const Extension& extension) { |
| + if (WebAccessibleResourcesInfo::HasWebAccessibleResources(&extension) || |
| + // Extensions below manifest version 2 had all resources accessible by |
| + // default. |
| + // TODO(devlin): Two things - first, we might not have any v1 extensions |
| + // anymore; second, this should maybe be included in |
| + // HasWebAccessibleResources(). |
| + extension.manifest_version() < 2 || |
| + WebviewInfo::HasWebviewAccessibleResources( |
| + extension, dispatcher_->webview_partition_id()) || |
| + // Hosted app icons are accessible. |
| + (extension.is_hosted_app() && !IconsInfo::GetIcons(&extension).empty())) { |
|
nasko
2017/06/30 18:08:10
I see this call scattered a few places already. Sh
Devlin
2017/07/06 00:40:39
Most places check a specific path to see if it's r
|
| + web_accessible_ids_.insert(extension.id()); |
| + } |
| +} |
| + |
| +void ResourceRequestPolicy::OnExtensionRemoved( |
| + const ExtensionId& extension_id) { |
| + web_accessible_ids_.erase(extension_id); |
| +} |
| // This method does a security check whether chrome-extension:// URLs can be |
| // requested by the renderer. Since this is in an untrusted process, the browser |
| @@ -42,11 +64,50 @@ bool ResourceRequestPolicy::CanRequestResource( |
| ui::PageTransition transition_type) { |
| CHECK(resource_url.SchemeIs(kExtensionScheme)); |
| + GURL frame_url = frame->GetDocument().Url(); |
| + |
| + // The page_origin may be GURL("null") for unique origins like data URLs, |
| + // but this is ok for the checks below. We only care if it matches the |
| + // current extension or has a devtools scheme. |
| + GURL page_origin = url::Origin(frame->Top()->GetSecurityOrigin()).GetURL(); |
|
nasko
2017/06/30 18:08:09
Ok, I realize this is move only operation here. Ho
Devlin
2017/07/06 00:40:38
Yes, we probably should. I tried this (see PS4),
alexmos
2017/07/06 18:20:06
(Pitching in here since Nasko's OOO for a while)
Devlin
2017/07/06 19:14:56
Acknowledged; will leave the TODO and submit as-is
|
| + |
| + GURL extension_origin = resource_url.GetOrigin(); |
| + |
| + // We always allow loads in the following cases, regardless of web accessible |
| + // resources: |
| + |
| + // Empty origin (needed for some edge cases when we have empty origins). |
|
nasko
2017/06/30 18:08:09
Empty origins or empty URLs?
Devlin
2017/07/06 00:40:39
This was copy-pasted, but code implies url, so cha
|
| + if (frame_url.is_empty()) |
| + return true; |
|
nasko
2017/06/30 18:08:09
In general, if you want timing neutral code, early
Devlin
2017/07/06 00:40:38
Except if we return true, it's because the navigat
|
| + |
| + // Extensions requesting their own resources (frame_url check is for images, |
| + // page_url check is for iframes) |
| + if (frame_url.GetOrigin() == extension_origin || |
|
alexmos
2017/06/30 21:10:17
I know you're not changing this logic... but why d
Devlin
2017/07/06 00:40:38
Sorry, you lost me at the sandboxed frame part - c
alexmos
2017/07/06 18:20:06
If a frame is sandboxed via <iframe sandbox>, the
Devlin
2017/07/06 19:14:56
Thanks for the explanation! I've expanded the TOD
|
| + page_origin == extension_origin) { |
| + return true; |
| + } |
| + |
| + if (!ui::PageTransitionIsWebTriggerable(transition_type)) |
| + return true; |
| + |
| + // Unreachable web page error page (to allow showing the icon of the |
| + // unreachable app on this page). |
| + if (frame_url == content::kUnreachableWebDataURL) |
| + return true; |
| + |
| + bool is_dev_tools = page_origin.SchemeIs(content::kChromeDevToolsScheme); |
| + if (!is_dev_tools && !web_accessible_ids_.count(extension_origin.host())) { |
|
alexmos
2017/06/30 21:10:17
nit: no { needed
alexmos
2017/06/30 21:10:18
Maybe add a comment about why you needed to add th
Devlin
2017/07/06 00:40:38
Done.
Devlin
2017/07/06 00:40:39
Done.
alexmos
2017/07/06 18:20:06
Was it added somewhere where I missed it? :) I wa
Devlin
2017/07/06 19:14:56
Whoops - it *was* added (https://codereview.chromi
|
| + return false; |
|
alexmos
2017/06/30 21:10:18
Just to clarify: does this help for an extension t
Devlin
2017/07/06 00:40:39
If an extension has a web accessible resource, it
alexmos
2017/07/06 18:20:06
Acknowledged.
|
| + } |
| + |
| const Extension* extension = |
| RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(resource_url); |
| - if (!extension) { |
| - // Allow the load in the case of a non-existent extension. We'll just get a |
| - // 404 from the browser process. |
| + DCHECK(extension); |
|
alexmos
2017/06/30 21:10:17
I'm curious if it's possible to hit this when |is_
Devlin
2017/07/06 00:40:38
Umm... I'd hope not, but I'm not 100% sure. I've
alexmos
2017/07/06 18:20:06
Acknowledged.
|
| + |
| + // Devtools (chrome-extension:// URLs are loaded into frames of devtools to |
| + // support the devtools extension APIs). |
| + if (is_dev_tools && |
| + !chrome_manifest_urls::GetDevToolsPage(extension).is_empty()) { |
| return true; |
| } |
| @@ -72,43 +133,15 @@ bool ResourceRequestPolicy::CanRequestResource( |
| !WebviewInfo::IsResourceWebviewAccessible( |
| extension, dispatcher_->webview_partition_id(), |
| resource_url.path())) { |
| - GURL frame_url = frame->GetDocument().Url(); |
| - |
| - // The page_origin may be GURL("null") for unique origins like data URLs, |
| - // but this is ok for the checks below. We only care if it matches the |
| - // current extension or has a devtools scheme. |
| - GURL page_origin = url::Origin(frame->Top()->GetSecurityOrigin()).GetURL(); |
| - |
| - // Exceptions are: |
| - // - empty origin (needed for some edge cases when we have empty origins) |
| - bool is_empty_origin = frame_url.is_empty(); |
| - // - extensions requesting their own resources (frame_url check is for |
| - // images, page_url check is for iframes) |
| - bool is_own_resource = frame_url.GetOrigin() == extension->url() || |
| - page_origin == extension->url(); |
| - // - devtools (chrome-extension:// URLs are loaded into frames of devtools |
| - // to support the devtools extension APIs) |
| - bool is_dev_tools = |
| - page_origin.SchemeIs(content::kChromeDevToolsScheme) && |
| - !chrome_manifest_urls::GetDevToolsPage(extension).is_empty(); |
| - bool transition_allowed = |
| - !ui::PageTransitionIsWebTriggerable(transition_type); |
| - // - unreachable web page error page (to allow showing the icon of the |
| - // unreachable app on this page) |
| - bool is_error_page = frame_url == content::kUnreachableWebDataURL; |
| - |
| - if (!is_empty_origin && !is_own_resource && |
| - !is_dev_tools && !transition_allowed && !is_error_page) { |
| - std::string message = base::StringPrintf( |
| - "Denying load of %s. Resources must be listed in the " |
| - "web_accessible_resources manifest key in order to be loaded by " |
| - "pages outside the extension.", |
| - resource_url.spec().c_str()); |
| - frame->AddMessageToConsole( |
| - blink::WebConsoleMessage(blink::WebConsoleMessage::kLevelError, |
| - blink::WebString::FromUTF8(message))); |
| - return false; |
| - } |
| + std::string message = base::StringPrintf( |
| + "Denying load of %s. Resources must be listed in the " |
| + "web_accessible_resources manifest key in order to be loaded by " |
| + "pages outside the extension.", |
| + resource_url.spec().c_str()); |
| + frame->AddMessageToConsole( |
| + blink::WebConsoleMessage(blink::WebConsoleMessage::kLevelError, |
| + blink::WebString::FromUTF8(message))); |
| + return false; |
| } |
| return true; |