Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #include "extensions/browser/extension_navigation_throttle.h" | |
| 6 | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 #include "content/public/browser/navigation_handle.h" | |
| 9 #include "content/public/browser/render_frame_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 #include "extensions/browser/extension_registry.h" | |
| 12 #include "extensions/common/constants.h" | |
| 13 #include "extensions/common/extension.h" | |
| 14 #include "extensions/common/extension_set.h" | |
| 15 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 ExtensionNavigationThrottle::ExtensionNavigationThrottle( | |
| 20 content::NavigationHandle* navigation_handle) | |
| 21 : content::NavigationThrottle(navigation_handle) {} | |
| 22 | |
| 23 ExtensionNavigationThrottle::~ExtensionNavigationThrottle() {} | |
| 24 | |
| 25 content::NavigationThrottle::ThrottleCheckResult | |
| 26 ExtensionNavigationThrottle::WillStartRequest() { | |
| 27 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 28 | |
| 29 // This method for now enforces only web_accessible_resources for navigations. | |
| 30 // Top-level navigations should always be allowed. | |
| 31 DCHECK(!navigation_handle()->IsInMainFrame()); | |
| 32 | |
| 33 // If the navigation is not to a chrome-extension:// URL, no need to perform | |
| 34 // any more checks. | |
| 35 if (!navigation_handle()->GetURL().SchemeIs(extensions::kExtensionScheme)) | |
| 36 return content::NavigationThrottle::PROCEED; | |
| 37 | |
| 38 // The subframe which is navigated needs to have all of its ancestors be | |
| 39 // at the same origin, otherwise the resource needs to be explicitly listed | |
| 40 // in web_accessible_resources. | |
| 41 // Since the RenderFrameHost is not known until navigation has committed, | |
| 42 // we can't get it from NavigationHandle. However, this code only cares about | |
| 43 // the ancestor chain, so find the current RenderFrameHost and use it to | |
| 44 // traverse up to the main frame. | |
| 45 std::vector<content::RenderFrameHost*> frames = | |
| 46 navigation_handle()->GetWebContents()->GetAllFrames(); | |
|
Devlin
2016/06/03 22:22:45
nit: I'd probably just inline this on line 47:
for
nasko
2016/06/06 17:40:44
It isn't rare, but you don't know which RenderFram
| |
| 47 content::RenderFrameHost* navigating_frame = nullptr; | |
| 48 for (auto frame : frames) { | |
| 49 if (frame->GetFrameTreeNodeId() == | |
| 50 navigation_handle()->GetFrameTreeNodeId()) { | |
| 51 navigating_frame = frame; | |
| 52 break; | |
| 53 } | |
| 54 } | |
| 55 DCHECK(navigating_frame); | |
| 56 | |
| 57 // Traverse the chain of parent frames, checking if they are the same origin | |
| 58 // as the URL of this navigation. | |
| 59 content::RenderFrameHost* ancestor = navigating_frame->GetParent(); | |
| 60 bool external_ancestor = false; | |
| 61 while (ancestor) { | |
| 62 if (ancestor->GetLastCommittedURL().GetOrigin() != | |
| 63 navigation_handle()->GetURL().GetOrigin()) { | |
| 64 // Ignore DevTools, as it is allowed to embed extension pages. | |
| 65 if (!ancestor->GetLastCommittedURL().SchemeIs("chrome-devtools")) { | |
|
Devlin
2016/06/03 22:22:45
nit: content::kChromeDevToolsScheme
nasko
2016/06/06 17:40:44
Thanks! Already done, but didn't upload.
| |
| 66 external_ancestor = true; | |
| 67 break; | |
| 68 } | |
| 69 } | |
| 70 ancestor = ancestor->GetParent(); | |
| 71 } | |
| 72 | |
| 73 if (!external_ancestor) | |
| 74 return content::NavigationThrottle::PROCEED; | |
| 75 | |
| 76 // Since there was at least one origin different than the navigation URL, | |
| 77 // explicitly check for the resource in web_accessible_resources. | |
| 78 std::string resource_path = navigation_handle()->GetURL().path(); | |
| 79 extensions::ExtensionRegistry* registry = extensions::ExtensionRegistry::Get( | |
|
Devlin
2016/06/03 22:22:45
no extensions:: prefix needed
nasko
2016/06/06 17:40:44
Done.
| |
| 80 navigation_handle()->GetWebContents()->GetBrowserContext()); | |
| 81 const extensions::Extension* extension = | |
| 82 registry->enabled_extensions().GetByID( | |
| 83 navigation_handle()->GetURL().host()); | |
| 84 | |
| 85 if (extensions::WebAccessibleResourcesInfo::IsResourceWebAccessible( | |
| 86 extension, resource_path)) { | |
| 87 return content::NavigationThrottle::PROCEED; | |
| 88 } | |
| 89 | |
| 90 return content::NavigationThrottle::BLOCK_REQUEST; | |
| 91 } | |
| 92 | |
| 93 } // namespace extensions | |
| OLD | NEW |