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
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/resource_request_policy.h"
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/common/extensions/chrome_manifest_url_handlers.h"
10 #include "chrome/common/url_constants.h"
11 #include "extensions/common/constants.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/manifest_constants.h"
14 #include "extensions/common/manifest_handlers/icons_handler.h"
15 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
16 #include "extensions/common/manifest_handlers/webview_info.h"
17 #include "extensions/renderer/dispatcher.h"
18 #include "extensions/renderer/renderer_extension_registry.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "ui/base/page_transition_types.h"
26 namespace extensions
{
28 ResourceRequestPolicy::ResourceRequestPolicy(Dispatcher
* dispatcher
)
29 : dispatcher_(dispatcher
) {}
31 // This method does a security check whether chrome-extension:// URLs can be
32 // requested by the renderer. Since this is in an untrusted process, the browser
33 // has a similar check to enforce the policy, in case this process is exploited.
34 // If you are changing this function, ensure equivalent checks are added to
35 // extension_protocols.cc's AllowExtensionResourceLoad.
36 bool ResourceRequestPolicy::CanRequestResource(
37 const GURL
& resource_url
,
38 blink::WebFrame
* frame
,
39 ui::PageTransition transition_type
) {
40 CHECK(resource_url
.SchemeIs(kExtensionScheme
));
42 const Extension
* extension
=
43 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(resource_url
);
45 // Allow the load in the case of a non-existent extension. We'll just get a
46 // 404 from the browser process.
50 // Disallow loading of packaged resources for hosted apps. We don't allow
51 // hybrid hosted/packaged apps. The one exception is access to icons, since
52 // some extensions want to be able to do things like create their own
54 std::string resource_root_relative_path
=
55 resource_url
.path().empty() ? std::string()
56 : resource_url
.path().substr(1);
57 if (extension
->is_hosted_app() &&
58 !IconsInfo::GetIcons(extension
)
59 .ContainsPath(resource_root_relative_path
)) {
60 LOG(ERROR
) << "Denying load of " << resource_url
.spec() << " from "
65 // Disallow loading of extension resources which are not explicitly listed
66 // as web or WebView accessible if the manifest version is 2 or greater.
67 if (!WebAccessibleResourcesInfo::IsResourceWebAccessible(
68 extension
, resource_url
.path()) &&
69 !WebviewInfo::IsResourceWebviewAccessible(
70 extension
, dispatcher_
->webview_partition_id(),
71 resource_url
.path())) {
72 GURL frame_url
= frame
->document().url();
74 // The page_origin may be GURL("null") for unique origins like data URLs,
75 // but this is ok for the checks below. We only care if it matches the
76 // current extension or has a devtools scheme.
77 GURL page_origin
= GURL(frame
->top()->securityOrigin().toString());
80 // - empty origin (needed for some edge cases when we have empty origins)
81 bool is_empty_origin
= frame_url
.is_empty();
82 // - extensions requesting their own resources (frame_url check is for
83 // images, page_url check is for iframes)
84 bool is_own_resource
= frame_url
.GetOrigin() == extension
->url() ||
85 page_origin
== extension
->url();
86 // - devtools (chrome-extension:// URLs are loaded into frames of devtools
87 // to support the devtools extension APIs)
89 page_origin
.SchemeIs(content::kChromeDevToolsScheme
) &&
90 !chrome_manifest_urls::GetDevToolsPage(extension
).is_empty();
91 bool transition_allowed
=
92 !ui::PageTransitionIsWebTriggerable(transition_type
);
93 // - unreachable web page error page (to allow showing the icon of the
94 // unreachable app on this page)
95 bool is_error_page
= frame_url
== GURL(content::kUnreachableWebDataURL
);
97 if (!is_empty_origin
&& !is_own_resource
&&
98 !is_dev_tools
&& !transition_allowed
&& !is_error_page
) {
99 std::string message
= base::StringPrintf(
100 "Denying load of %s. Resources must be listed in the "
101 "web_accessible_resources manifest key in order to be loaded by "
102 "pages outside the extension.",
103 resource_url
.spec().c_str());
104 frame
->addMessageToConsole(
105 blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError
,
106 blink::WebString::fromUTF8(message
)));
114 bool ResourceRequestPolicy::CanRequestExtensionResourceScheme(
115 const GURL
& resource_url
,
116 blink::WebFrame
* frame
) {
117 CHECK(resource_url
.SchemeIs(kExtensionResourceScheme
));
119 GURL frame_url
= frame
->document().url();
120 if (!frame_url
.is_empty() && !frame_url
.SchemeIs(kExtensionScheme
)) {
121 std::string message
= base::StringPrintf(
122 "Denying load of %s. chrome-extension-resources:// can only be "
123 "loaded from extensions.",
124 resource_url
.spec().c_str());
125 frame
->addMessageToConsole(
126 blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError
,
127 blink::WebString::fromUTF8(message
)));
134 } // namespace extensions