OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/extension_navigation_throttle.h" | 5 #include "extensions/browser/extension_navigation_throttle.h" |
6 | 6 |
7 #include "content/public/browser/browser_thread.h" | 7 #include "content/public/browser/browser_thread.h" |
8 #include "content/public/browser/navigation_handle.h" | 8 #include "content/public/browser/navigation_handle.h" |
9 #include "content/public/browser/render_frame_host.h" | 9 #include "content/public/browser/render_frame_host.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "content/public/common/url_constants.h" | 11 #include "content/public/common/url_constants.h" |
12 #include "extensions/browser/extension_registry.h" | 12 #include "extensions/browser/extension_registry.h" |
13 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
15 #include "extensions/common/extension_set.h" | 15 #include "extensions/common/extension_set.h" |
16 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" | 16 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h" |
| 17 #include "extensions/common/permissions/api_permission.h" |
| 18 #include "extensions/common/permissions/permissions_data.h" |
17 | 19 |
18 namespace extensions { | 20 namespace extensions { |
19 | 21 |
20 ExtensionNavigationThrottle::ExtensionNavigationThrottle( | 22 ExtensionNavigationThrottle::ExtensionNavigationThrottle( |
21 content::NavigationHandle* navigation_handle) | 23 content::NavigationHandle* navigation_handle) |
22 : content::NavigationThrottle(navigation_handle) {} | 24 : content::NavigationThrottle(navigation_handle) {} |
23 | 25 |
24 ExtensionNavigationThrottle::~ExtensionNavigationThrottle() {} | 26 ExtensionNavigationThrottle::~ExtensionNavigationThrottle() {} |
25 | 27 |
26 content::NavigationThrottle::ThrottleCheckResult | 28 content::NavigationThrottle::ThrottleCheckResult |
27 ExtensionNavigationThrottle::WillStartRequest() { | 29 ExtensionNavigationThrottle::WillStartRequest() { |
28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 31 GURL url(navigation_handle()->GetURL()); |
| 32 ExtensionRegistry* registry = ExtensionRegistry::Get( |
| 33 navigation_handle()->GetWebContents()->GetBrowserContext()); |
29 | 34 |
30 // This method for now enforces only web_accessible_resources for navigations. | 35 if (navigation_handle()->IsInMainFrame()) { |
31 // Top-level navigations should always be allowed. | 36 // Block top-level navigations to blob: or filesystem: URLs with extension |
32 DCHECK(!navigation_handle()->IsInMainFrame()); | 37 // origin from non-extension processes. See https://crbug.com/645028. |
| 38 bool is_nested_url = url.SchemeIsFileSystem() || url.SchemeIsBlob(); |
| 39 bool is_extension = false; |
| 40 if (registry) { |
| 41 is_extension = !!registry->enabled_extensions().GetExtensionOrAppByURL( |
| 42 navigation_handle()->GetStartingSiteInstance()->GetSiteURL()); |
| 43 } |
| 44 |
| 45 url::Origin origin(url); |
| 46 if (is_nested_url && origin.scheme() == extensions::kExtensionScheme && |
| 47 !is_extension) { |
| 48 // Relax this restriction for apps that use <webview>. See |
| 49 // https://crbug.com/652077. |
| 50 const extensions::Extension* extension = |
| 51 registry->enabled_extensions().GetByID(origin.host()); |
| 52 bool has_webview_permission = |
| 53 extension && |
| 54 extension->permissions_data()->HasAPIPermission( |
| 55 extensions::APIPermission::kWebView); |
| 56 if (!has_webview_permission) |
| 57 return content::NavigationThrottle::CANCEL; |
| 58 } |
| 59 |
| 60 return content::NavigationThrottle::PROCEED; |
| 61 } |
| 62 |
| 63 // Now enforce web_accessible_resources for navigations. Top-level navigations |
| 64 // should always be allowed. |
33 | 65 |
34 // If the navigation is not to a chrome-extension:// URL, no need to perform | 66 // If the navigation is not to a chrome-extension:// URL, no need to perform |
35 // any more checks. | 67 // any more checks. |
36 if (!navigation_handle()->GetURL().SchemeIs(extensions::kExtensionScheme)) | 68 if (!url.SchemeIs(extensions::kExtensionScheme)) |
37 return content::NavigationThrottle::PROCEED; | 69 return content::NavigationThrottle::PROCEED; |
38 | 70 |
39 // The subframe which is navigated needs to have all of its ancestors be | 71 // The subframe which is navigated needs to have all of its ancestors be |
40 // at the same origin, otherwise the resource needs to be explicitly listed | 72 // at the same origin, otherwise the resource needs to be explicitly listed |
41 // in web_accessible_resources. | 73 // in web_accessible_resources. |
42 // Since the RenderFrameHost is not known until navigation has committed, | 74 // Since the RenderFrameHost is not known until navigation has committed, |
43 // we can't get it from NavigationHandle. However, this code only cares about | 75 // we can't get it from NavigationHandle. However, this code only cares about |
44 // the ancestor chain, so find the current RenderFrameHost and use it to | 76 // the ancestor chain, so find the current RenderFrameHost and use it to |
45 // traverse up to the main frame. | 77 // traverse up to the main frame. |
46 content::RenderFrameHost* navigating_frame = nullptr; | 78 content::RenderFrameHost* navigating_frame = nullptr; |
47 for (auto* frame : navigation_handle()->GetWebContents()->GetAllFrames()) { | 79 for (auto* frame : navigation_handle()->GetWebContents()->GetAllFrames()) { |
48 if (frame->GetFrameTreeNodeId() == | 80 if (frame->GetFrameTreeNodeId() == |
49 navigation_handle()->GetFrameTreeNodeId()) { | 81 navigation_handle()->GetFrameTreeNodeId()) { |
50 navigating_frame = frame; | 82 navigating_frame = frame; |
51 break; | 83 break; |
52 } | 84 } |
53 } | 85 } |
54 DCHECK(navigating_frame); | 86 DCHECK(navigating_frame); |
55 | 87 |
56 // Traverse the chain of parent frames, checking if they are the same origin | 88 // Traverse the chain of parent frames, checking if they are the same origin |
57 // as the URL of this navigation. | 89 // as the URL of this navigation. |
58 content::RenderFrameHost* ancestor = navigating_frame->GetParent(); | 90 content::RenderFrameHost* ancestor = navigating_frame->GetParent(); |
59 bool external_ancestor = false; | 91 bool external_ancestor = false; |
60 while (ancestor) { | 92 while (ancestor) { |
61 if (ancestor->GetLastCommittedURL().GetOrigin() != | 93 if (ancestor->GetLastCommittedURL().GetOrigin() != url.GetOrigin()) { |
62 navigation_handle()->GetURL().GetOrigin()) { | |
63 // Ignore DevTools, as it is allowed to embed extension pages. | 94 // Ignore DevTools, as it is allowed to embed extension pages. |
64 if (!ancestor->GetLastCommittedURL().SchemeIs( | 95 if (!ancestor->GetLastCommittedURL().SchemeIs( |
65 content::kChromeDevToolsScheme)) { | 96 content::kChromeDevToolsScheme)) { |
66 external_ancestor = true; | 97 external_ancestor = true; |
67 break; | 98 break; |
68 } | 99 } |
69 } | 100 } |
70 ancestor = ancestor->GetParent(); | 101 ancestor = ancestor->GetParent(); |
71 } | 102 } |
72 | 103 |
73 if (!external_ancestor) | 104 if (!external_ancestor) |
74 return content::NavigationThrottle::PROCEED; | 105 return content::NavigationThrottle::PROCEED; |
75 | 106 |
76 // Since there was at least one origin different than the navigation URL, | 107 // Since there was at least one origin different than the navigation URL, |
77 // explicitly check for the resource in web_accessible_resources. | 108 // explicitly check for the resource in web_accessible_resources. |
78 std::string resource_path = navigation_handle()->GetURL().path(); | 109 std::string resource_path = url.path(); |
79 ExtensionRegistry* registry = ExtensionRegistry::Get( | |
80 navigation_handle()->GetWebContents()->GetBrowserContext()); | |
81 if (!registry) | 110 if (!registry) |
82 return content::NavigationThrottle::BLOCK_REQUEST; | 111 return content::NavigationThrottle::BLOCK_REQUEST; |
83 | 112 |
84 const extensions::Extension* extension = | 113 const extensions::Extension* extension = |
85 registry->enabled_extensions().GetByID( | 114 registry->enabled_extensions().GetByID(url.host()); |
86 navigation_handle()->GetURL().host()); | |
87 if (!extension) | 115 if (!extension) |
88 return content::NavigationThrottle::BLOCK_REQUEST; | 116 return content::NavigationThrottle::BLOCK_REQUEST; |
89 | 117 |
90 if (WebAccessibleResourcesInfo::IsResourceWebAccessible(extension, | 118 if (WebAccessibleResourcesInfo::IsResourceWebAccessible(extension, |
91 resource_path)) { | 119 resource_path)) { |
92 return content::NavigationThrottle::PROCEED; | 120 return content::NavigationThrottle::PROCEED; |
93 } | 121 } |
94 | 122 |
95 return content::NavigationThrottle::BLOCK_REQUEST; | 123 return content::NavigationThrottle::BLOCK_REQUEST; |
96 } | 124 } |
97 | 125 |
98 } // namespace extensions | 126 } // namespace extensions |
OLD | NEW |