OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/renderer/extensions/extension_resource_request_policy.h" | 5 #include "chrome/renderer/extensions/extension_resource_request_policy.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/string_util.h" | |
11 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
13 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
14 #include "chrome/common/extensions/extension_set.h" | 13 #include "chrome/common/extensions/extension_set.h" |
15 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
20 | 19 |
(...skipping 20 matching lines...) Expand all Loading... |
41 // launchers. | 40 // launchers. |
42 std::string resource_root_relative_path = | 41 std::string resource_root_relative_path = |
43 resource_url.path().empty() ? "" : resource_url.path().substr(1); | 42 resource_url.path().empty() ? "" : resource_url.path().substr(1); |
44 if (extension->is_hosted_app() && | 43 if (extension->is_hosted_app() && |
45 !extension->icons().ContainsPath(resource_root_relative_path)) { | 44 !extension->icons().ContainsPath(resource_root_relative_path)) { |
46 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 45 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " |
47 << "hosted app."; | 46 << "hosted app."; |
48 return false; | 47 return false; |
49 } | 48 } |
50 | 49 |
51 GURL frame_url = frame->document().url(); | 50 // Disallow loading of extension resources which are not explicitely listed |
| 51 // as web accessible if the manifest version is 2 or greater. |
| 52 if (!extension->IsResourceWebAccessible(resource_url.path()) && |
| 53 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 54 switches::kDisableExtensionsResourceWhitelist)) { |
| 55 GURL frame_url = frame->document().url(); |
| 56 GURL page_url = frame->top()->document().url(); |
52 | 57 |
53 // In the case of loading a frame, frame* points to the frame being loaded, | 58 // Exceptions are: |
54 // not the containing frame. This means that frame->document().url() ends up | 59 // - empty origin (needed for some edge cases when we have empty origins) |
55 // not being useful to us. | 60 bool is_empty_origin = frame_url.is_empty(); |
56 // | 61 // - extensions requesting their own resources (frame_url check is for |
57 // WebKit doesn't currently pass us enough information to know when we're a | 62 // images, page_url check is for iframes) |
58 // frame, so we hack it by checking for 'about:blank', which should only | 63 bool is_own_resource = frame_url.GetOrigin() == extension->url() || |
59 // happen in this situation. | 64 page_url.GetOrigin() == extension->url(); |
60 // | 65 // - devtools (chrome-extension:// URLs are loaded into frames of devtools |
61 // TODO(aa): Fix WebKit to pass the context of the load: crbug.com/139788. | 66 // to support the devtools extension APIs) |
62 if (frame_url == GURL(chrome::kAboutBlankURL) && frame->parent()) | 67 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && |
63 frame_url = frame->parent()->document().url(); | 68 !extension->devtools_url().is_empty(); |
64 | 69 |
65 bool extension_has_access_to_frame = | 70 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { |
66 extension->GetEffectiveHostPermissions().MatchesURL(frame_url); | |
67 bool frame_has_empty_origin = frame_url.is_empty(); | |
68 bool frame_is_data_url = frame_url.SchemeIs(chrome::kDataScheme); | |
69 bool frame_is_devtools = frame_url.SchemeIs(chrome::kChromeDevToolsScheme) && | |
70 !extension->devtools_url().is_empty(); | |
71 bool frame_is_extension = frame_url.SchemeIs(chrome::kExtensionScheme); | |
72 bool is_own_resource = frame_url.GetOrigin() == extension->url(); | |
73 bool is_resource_nacl_manifest = | |
74 extension->IsResourceNaClManifest(resource_url.path()); | |
75 bool is_resource_web_accessible = | |
76 extension->IsResourceWebAccessible(resource_url.path()) || | |
77 CommandLine::ForCurrentProcess()->HasSwitch( | |
78 switches::kDisableExtensionsResourceWhitelist); | |
79 | |
80 // Given that the goal here is to prevent malicious injection of a benign | |
81 // extension's content into a context where it might be damaging, allowing | |
82 // unvalidated "nexe" resources is low-risk. If a mechanism for synchronously | |
83 // validating that the "nexe" is a NaCl executable appears, we should use it. | |
84 bool is_resource_nexe = extension->HasNaClModules() && | |
85 EndsWith(resource_url.path(), ".nexe", true); | |
86 | |
87 if (!frame_has_empty_origin && !frame_is_devtools && !is_own_resource) { | |
88 if (!is_resource_web_accessible) { | |
89 std::string message = base::StringPrintf( | 71 std::string message = base::StringPrintf( |
90 "Denying load of %s. Resources must be listed in the " | 72 "Denying load of %s. Resources must be listed in the " |
91 "web_accessible_resources manifest key in order to be loaded by " | 73 "web_accessible_resources manifest key in order to be loaded by " |
92 "pages outside the extension.", | 74 "pages outside the extension.", |
93 resource_url.spec().c_str()); | 75 resource_url.spec().c_str()); |
94 frame->addMessageToConsole( | 76 frame->addMessageToConsole( |
95 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 77 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
96 WebKit::WebString::fromUTF8(message))); | 78 WebKit::WebString::fromUTF8(message))); |
97 return false; | 79 return false; |
98 } | 80 } |
99 | |
100 if (!extension_has_access_to_frame && !frame_is_extension && | |
101 !frame_is_data_url && !is_resource_nacl_manifest && !is_resource_nexe) { | |
102 std::string message = base::StringPrintf( | |
103 "Denying load of %s. An extension's resources can only be loaded " | |
104 "into a page for which the extension has explicit host permissions.", | |
105 resource_url.spec().c_str()); | |
106 frame->addMessageToConsole( | |
107 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | |
108 WebKit::WebString::fromUTF8(message))); | |
109 return false; | |
110 } | |
111 } | 81 } |
112 | 82 |
113 return true; | 83 return true; |
114 } | 84 } |
115 | 85 |
116 // static | 86 // static |
117 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( | 87 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( |
118 const GURL& resource_url, | 88 const GURL& resource_url, |
119 WebKit::WebFrame* frame) { | 89 WebKit::WebFrame* frame) { |
120 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); | 90 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); |
121 | 91 |
122 GURL frame_url = frame->document().url(); | 92 GURL frame_url = frame->document().url(); |
123 if (!frame_url.is_empty() && | 93 if (!frame_url.is_empty() && |
124 !frame_url.SchemeIs(chrome::kExtensionScheme)) { | 94 !frame_url.SchemeIs(chrome::kExtensionScheme)) { |
125 std::string message = base::StringPrintf( | 95 std::string message = base::StringPrintf( |
126 "Denying load of %s. chrome-extension-resources:// can only be " | 96 "Denying load of %s. chrome-extension-resources:// can only be " |
127 "loaded from extensions.", | 97 "loaded from extensions.", |
128 resource_url.spec().c_str()); | 98 resource_url.spec().c_str()); |
129 frame->addMessageToConsole( | 99 frame->addMessageToConsole( |
130 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 100 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
131 WebKit::WebString::fromUTF8(message))); | 101 WebKit::WebString::fromUTF8(message))); |
132 return false; | 102 return false; |
133 } | 103 } |
134 | 104 |
135 return true; | 105 return true; |
136 } | 106 } |
137 | 107 |
138 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { | 108 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { |
139 } | 109 } |
OLD | NEW |