Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / renderer / extensions / resource_request_policy.cc
blob339acfadf295adfc0855cdce1088d8beb8abefde
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_handlers/icons_handler.h"
14 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
15 #include "extensions/renderer/renderer_extension_registry.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
18 #include "third_party/WebKit/public/web/WebDocument.h"
19 #include "third_party/WebKit/public/web/WebFrame.h"
20 #include "ui/base/page_transition_types.h"
21 #include "url/gurl.h"
23 namespace extensions {
25 // This method does a security check whether chrome-extension:// URLs can be
26 // requested by the renderer. Since this is in an untrusted process, the browser
27 // has a similar check to enforce the policy, in case this process is exploited.
28 // If you are changing this function, ensure equivalent checks are added to
29 // extension_protocols.cc's AllowExtensionResourceLoad.
31 // static
32 bool ResourceRequestPolicy::CanRequestResource(
33 const GURL& resource_url,
34 blink::WebFrame* frame,
35 ui::PageTransition transition_type) {
36 CHECK(resource_url.SchemeIs(extensions::kExtensionScheme));
38 const Extension* extension =
39 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(resource_url);
40 if (!extension) {
41 // Allow the load in the case of a non-existent extension. We'll just get a
42 // 404 from the browser process.
43 return true;
46 // Disallow loading of packaged resources for hosted apps. We don't allow
47 // hybrid hosted/packaged apps. The one exception is access to icons, since
48 // some extensions want to be able to do things like create their own
49 // launchers.
50 std::string resource_root_relative_path =
51 resource_url.path().empty() ? std::string()
52 : resource_url.path().substr(1);
53 if (extension->is_hosted_app() &&
54 !IconsInfo::GetIcons(extension)
55 .ContainsPath(resource_root_relative_path)) {
56 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from "
57 << "hosted app.";
58 return false;
61 // Disallow loading of extension resources which are not explicitly listed
62 // as web accessible if the manifest version is 2 or greater.
63 if (!WebAccessibleResourcesInfo::IsResourceWebAccessible(
64 extension, resource_url.path())) {
65 GURL frame_url = frame->document().url();
67 // The page_origin may be GURL("null") for unique origins like data URLs,
68 // but this is ok for the checks below. We only care if it matches the
69 // current extension or has a devtools scheme.
70 GURL page_origin = GURL(frame->top()->securityOrigin().toString());
72 // Exceptions are:
73 // - empty origin (needed for some edge cases when we have empty origins)
74 bool is_empty_origin = frame_url.is_empty();
75 // - extensions requesting their own resources (frame_url check is for
76 // images, page_url check is for iframes)
77 bool is_own_resource = frame_url.GetOrigin() == extension->url() ||
78 page_origin == extension->url();
79 // - devtools (chrome-extension:// URLs are loaded into frames of devtools
80 // to support the devtools extension APIs)
81 bool is_dev_tools =
82 page_origin.SchemeIs(content::kChromeDevToolsScheme) &&
83 !chrome_manifest_urls::GetDevToolsPage(extension).is_empty();
84 bool transition_allowed =
85 !ui::PageTransitionIsWebTriggerable(transition_type);
86 // - unreachable web page error page (to allow showing the icon of the
87 // unreachable app on this page)
88 bool is_error_page = frame_url == GURL(content::kUnreachableWebDataURL);
90 if (!is_empty_origin && !is_own_resource &&
91 !is_dev_tools && !transition_allowed && !is_error_page) {
92 std::string message = base::StringPrintf(
93 "Denying load of %s. Resources must be listed in the "
94 "web_accessible_resources manifest key in order to be loaded by "
95 "pages outside the extension.",
96 resource_url.spec().c_str());
97 frame->addMessageToConsole(
98 blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
99 blink::WebString::fromUTF8(message)));
100 return false;
104 return true;
107 // static
108 bool ResourceRequestPolicy::CanRequestExtensionResourceScheme(
109 const GURL& resource_url,
110 blink::WebFrame* frame) {
111 CHECK(resource_url.SchemeIs(extensions::kExtensionResourceScheme));
113 GURL frame_url = frame->document().url();
114 if (!frame_url.is_empty() &&
115 !frame_url.SchemeIs(extensions::kExtensionScheme)) {
116 std::string message = base::StringPrintf(
117 "Denying load of %s. chrome-extension-resources:// can only be "
118 "loaded from extensions.",
119 resource_url.spec().c_str());
120 frame->addMessageToConsole(
121 blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
122 blink::WebString::fromUTF8(message)));
123 return false;
126 return true;
129 ResourceRequestPolicy::ResourceRequestPolicy() {
132 } // namespace extensions