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 "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
12 #include "chrome/common/extensions/extension.h" | 12 #include "chrome/common/extensions/extension.h" |
13 #include "chrome/common/extensions/extension_set.h" | 13 #include "chrome/common/extensions/extension_set.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
19 | 19 |
20 // static | 20 // static |
21 bool ExtensionResourceRequestPolicy::CanRequestResource( | 21 bool ExtensionResourceRequestPolicy::CanRequestResource( |
abarth-chromium
2012/04/13 17:11:24
This function is getting very complicated. Are th
Peng
2012/04/17 13:52:05
This function's input & output highly depend on en
| |
22 const GURL& resource_url, | 22 const GURL& resource_url, |
23 WebKit::WebFrame* frame, | 23 WebKit::WebFrame* frame, |
24 const ExtensionSet* loaded_extensions) { | 24 const ExtensionSet* loaded_extensions) { |
25 CHECK(resource_url.SchemeIs(chrome::kExtensionScheme)); | 25 if (resource_url.SchemeIs(chrome::kExtensionScheme)) { |
26 const Extension* extension = | |
27 loaded_extensions->GetExtensionOrAppByURL( | |
28 ExtensionURLInfo(resource_url)); | |
29 if (!extension) { | |
30 // Allow the load in the case of a non-existent extension. We'll just get | |
31 // a 404 from the browser process. | |
32 return true; | |
33 } | |
26 | 34 |
27 const Extension* extension = | 35 // Disallow loading of packaged resources for hosted apps. We don't allow |
28 loaded_extensions->GetExtensionOrAppByURL(ExtensionURLInfo(resource_url)); | 36 // hybrid hosted/packaged apps. The one exception is access to icons, since |
29 if (!extension) { | 37 // some extensions want to be able to do things like create their own |
30 // Allow the load in the case of a non-existent extension. We'll just get a | 38 // launchers. |
31 // 404 from the browser process. | 39 std::string resource_root_relative_path = |
40 resource_url.path().empty() ? "" : resource_url.path().substr(1); | |
41 if (extension->is_hosted_app() && | |
42 !extension->icons().ContainsPath(resource_root_relative_path)) { | |
43 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | |
44 "hosted app."; | |
45 return false; | |
46 } | |
47 | |
48 // Disallow loading of extension resources that are not explicitely listed | |
49 // as web accessible if the manifest version is 2 or greater. | |
50 | |
51 GURL frame_url = frame->document().url(); | |
52 GURL page_url = frame->top()->document().url(); | |
53 // Exceptions are: | |
54 // - empty origin (needed for some edge cases when we have empty origins) | |
55 // - chrome-extension:// (for legacy reasons - some extensions interoperate) | |
56 // - devtools (chrome-extension:// URLs are loaded into frames of devtools | |
57 // to support the devtools extension APIs) | |
58 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
59 switches::kDisableExtensionsResourceWhitelist) && | |
60 !frame_url.is_empty() && | |
61 !frame_url.SchemeIs(chrome::kExtensionScheme) && | |
62 !(page_url.SchemeIs(chrome::kChromeDevToolsScheme) && | |
63 !extension->devtools_url().is_empty()) && | |
64 !extension->IsResourceWebAccessible(resource_url.path())) { | |
65 std::string message = base::StringPrintf( | |
66 "Denying load of %s. Resources must be listed in the " | |
67 "web_accessible_resources manifest key in order to be loaded by web " | |
68 "pages.", | |
69 resource_url.spec().c_str()); | |
70 frame->addMessageToConsole( | |
71 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | |
72 WebKit::WebString::fromUTF8(message))); | |
73 return false; | |
74 } | |
75 | |
32 return true; | 76 return true; |
33 } | 77 } |
34 | 78 |
35 // Disallow loading of packaged resources for hosted apps. We don't allow | 79 if (resource_url.SchemeIs(chrome::kExtensionResourceScheme)) { |
36 // hybrid hosted/packaged apps. The one exception is access to icons, since | 80 GURL frame_url = frame->document().url(); |
37 // some extensions want to be able to do things like create their own | 81 GURL page_url = frame->top()->document().url(); |
abarth-chromium
2012/04/13 17:11:24
It looks like the page_url variable is unused. Pe
Peng
2012/04/17 13:52:05
Done.
| |
38 // launchers. | 82 if (!frame_url.is_empty() && |
39 std::string resource_root_relative_path = | 83 !frame_url.SchemeIs(chrome::kExtensionScheme) && |
40 resource_url.path().empty() ? "" : resource_url.path().substr(1); | 84 !frame_url.SchemeIs(chrome::kExtensionResourceScheme)) { |
41 if (extension->is_hosted_app() && | 85 std::string message = base::StringPrintf( |
42 !extension->icons().ContainsPath(resource_root_relative_path)) { | 86 "Denying load of %s. chrome-extension-resources:// can only be " |
43 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 87 "loaded from extensions.", |
44 << "hosted app."; | |
45 return false; | |
46 } | |
47 | |
48 // Disallow loading of extension resources which are not explicitely listed | |
49 // as web accessible if the manifest version is 2 or greater. | |
50 | |
51 GURL frame_url = frame->document().url(); | |
52 GURL page_url = frame->top()->document().url(); | |
53 // Exceptions are: | |
54 // - empty origin (needed for some edge cases when we have empty origins) | |
55 // - chrome-extension:// (for legacy reasons -- some extensions interop) | |
56 // - devtools (chrome-extension:// URLs are loaded into frames of devtools | |
57 // to support the devtools extension APIs) | |
58 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
59 switches::kDisableExtensionsResourceWhitelist) && | |
60 !frame_url.is_empty() && | |
61 !frame_url.SchemeIs(chrome::kExtensionScheme) && | |
62 !(page_url.SchemeIs(chrome::kChromeDevToolsScheme) && | |
63 !extension->devtools_url().is_empty()) && | |
64 !extension->IsResourceWebAccessible(resource_url.path())) { | |
65 std::string message = base::StringPrintf( | |
66 "Denying load of %s. Resources must be listed in the " | |
67 "web_accessible_resources manifest key in order to be loaded by web " | |
68 "pages.", | |
69 resource_url.spec().c_str()); | 88 resource_url.spec().c_str()); |
70 frame->addMessageToConsole( | 89 frame->addMessageToConsole( |
71 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 90 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
72 WebKit::WebString::fromUTF8(message))); | 91 WebKit::WebString::fromUTF8(message))); |
73 return false; | 92 return false; |
93 } | |
94 return true; | |
74 } | 95 } |
75 | 96 |
76 return true; | 97 return true; |
77 } | 98 } |
78 | 99 |
79 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { | 100 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { |
80 } | 101 } |
OLD | NEW |