• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/renderer/extensions/resource_request_policy.h"
6 
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/common/extensions/manifest_url_handler.h"
10 #include "chrome/common/url_constants.h"
11 #include "extensions/common/constants.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_set.h"
14 #include "extensions/common/manifest_handlers/icons_handler.h"
15 #include "extensions/common/manifest_handlers/web_accessible_resources_info.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"
22 
23 namespace extensions {
24 
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.
30 
31 // static
CanRequestResource(const GURL & resource_url,blink::WebFrame * frame,ui::PageTransition transition_type,const ExtensionSet * loaded_extensions)32 bool ResourceRequestPolicy::CanRequestResource(
33     const GURL& resource_url,
34     blink::WebFrame* frame,
35     ui::PageTransition transition_type,
36     const ExtensionSet* loaded_extensions) {
37   CHECK(resource_url.SchemeIs(extensions::kExtensionScheme));
38 
39   const Extension* extension =
40       loaded_extensions->GetExtensionOrAppByURL(resource_url);
41   if (!extension) {
42     // Allow the load in the case of a non-existent extension. We'll just get a
43     // 404 from the browser process.
44     return true;
45   }
46 
47   // Disallow loading of packaged resources for hosted apps. We don't allow
48   // hybrid hosted/packaged apps. The one exception is access to icons, since
49   // some extensions want to be able to do things like create their own
50   // launchers.
51   std::string resource_root_relative_path =
52       resource_url.path().empty() ? std::string()
53                                   : resource_url.path().substr(1);
54   if (extension->is_hosted_app() &&
55       !IconsInfo::GetIcons(extension)
56           .ContainsPath(resource_root_relative_path)) {
57     LOG(ERROR) << "Denying load of " << resource_url.spec() << " from "
58                << "hosted app.";
59     return false;
60   }
61 
62   // Disallow loading of extension resources which are not explicitly listed
63   // as web accessible if the manifest version is 2 or greater.
64   if (!WebAccessibleResourcesInfo::IsResourceWebAccessible(
65           extension, resource_url.path())) {
66     GURL frame_url = frame->document().url();
67     GURL page_url = frame->top()->document().url();
68 
69     // Exceptions are:
70     // - empty origin (needed for some edge cases when we have empty origins)
71     bool is_empty_origin = frame_url.is_empty();
72     // - extensions requesting their own resources (frame_url check is for
73     //     images, page_url check is for iframes)
74     bool is_own_resource = frame_url.GetOrigin() == extension->url() ||
75         page_url.GetOrigin() == extension->url();
76     // - devtools (chrome-extension:// URLs are loaded into frames of devtools
77     //     to support the devtools extension APIs)
78     bool is_dev_tools = page_url.SchemeIs(content::kChromeDevToolsScheme) &&
79                         !ManifestURL::GetDevToolsPage(extension).is_empty();
80     bool transition_allowed =
81         !ui::PageTransitionIsWebTriggerable(transition_type);
82     // - unreachable web page error page (to allow showing the icon of the
83     //   unreachable app on this page)
84     bool is_error_page = frame_url == GURL(content::kUnreachableWebDataURL);
85 
86     if (!is_empty_origin && !is_own_resource &&
87         !is_dev_tools && !transition_allowed && !is_error_page) {
88       std::string message = base::StringPrintf(
89           "Denying load of %s. Resources must be listed in the "
90           "web_accessible_resources manifest key in order to be loaded by "
91           "pages outside the extension.",
92           resource_url.spec().c_str());
93       frame->addMessageToConsole(
94           blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
95                                     blink::WebString::fromUTF8(message)));
96       return false;
97     }
98   }
99 
100   return true;
101 }
102 
103 // static
CanRequestExtensionResourceScheme(const GURL & resource_url,blink::WebFrame * frame)104 bool ResourceRequestPolicy::CanRequestExtensionResourceScheme(
105     const GURL& resource_url,
106     blink::WebFrame* frame) {
107   CHECK(resource_url.SchemeIs(extensions::kExtensionResourceScheme));
108 
109   GURL frame_url = frame->document().url();
110   if (!frame_url.is_empty() &&
111       !frame_url.SchemeIs(extensions::kExtensionScheme)) {
112     std::string message = base::StringPrintf(
113         "Denying load of %s. chrome-extension-resources:// can only be "
114         "loaded from extensions.",
115       resource_url.spec().c_str());
116     frame->addMessageToConsole(
117         blink::WebConsoleMessage(blink::WebConsoleMessage::LevelError,
118                                   blink::WebString::fromUTF8(message)));
119     return false;
120   }
121 
122   return true;
123 }
124 
ResourceRequestPolicy()125 ResourceRequestPolicy::ResourceRequestPolicy() {
126 }
127 
128 }  // namespace extensions
129