Chromium Code Reviews| 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" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 // launchers. | 40 // launchers. |
| 41 std::string resource_root_relative_path = | 41 std::string resource_root_relative_path = |
| 42 resource_url.path().empty() ? "" : resource_url.path().substr(1); | 42 resource_url.path().empty() ? "" : resource_url.path().substr(1); |
| 43 if (extension->is_hosted_app() && | 43 if (extension->is_hosted_app() && |
| 44 !extension->icons().ContainsPath(resource_root_relative_path)) { | 44 !extension->icons().ContainsPath(resource_root_relative_path)) { |
| 45 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 45 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " |
| 46 << "hosted app."; | 46 << "hosted app."; |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Disallow loading of extension resources which are not explicitely listed | 50 GURL frame_url = frame->document().url(); |
| 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(); | |
| 57 | 51 |
| 58 // Exceptions are: | 52 // Loading a resource into an empty iframe should check against the iframe's |
|
Aaron Boodman
2012/07/31 09:50:10
How about:
// In the case of loading a frame, fra
| |
| 59 // - empty origin (needed for some edge cases when we have empty origins) | 53 // parent's URL, not `about:blank`. We don't have a good mechanism to |
| 60 bool is_empty_origin = frame_url.is_empty(); | 54 // determine if we're actually in an iframe, so checking the parent whenever |
| 61 // - extensions requesting their own resources (frame_url check is for | 55 // we're loading into an `about:blank` frame_url is the best we can do. |
| 62 // images, page_url check is for iframes) | 56 if (frame_url == GURL(chrome::kAboutBlankURL) && frame->parent()) |
| 63 bool is_own_resource = frame_url.GetOrigin() == extension->url() || | 57 frame_url = frame->parent()->document().url(); |
| 64 page_url.GetOrigin() == extension->url(); | |
| 65 // - devtools (chrome-extension:// URLs are loaded into frames of devtools | |
| 66 // to support the devtools extension APIs) | |
| 67 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && | |
| 68 !extension->devtools_url().is_empty(); | |
| 69 | 58 |
| 70 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { | 59 // Disallow loading of extension resources when one of the following |
| 60 // conditions holds: | |
| 61 // | |
| 62 // 1. The resource is not explicitly listed as a web accessible resource (and | |
| 63 // this check isn't disabled via a command-line flag). | |
| 64 bool is_resource_web_accessible = | |
| 65 extension->IsResourceWebAccessible(resource_url.path()) || | |
| 66 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 67 switches::kDisableExtensionsResourceWhitelist); | |
| 68 | |
| 69 // 2. The resource is loaded into a context for which the extension has no | |
| 70 // permission (e.g. resources from an extension with host permissions for | |
| 71 // `evil.com` shouldn't be loaded into `example.com`). | |
| 72 bool is_access_permitted = | |
|
Aaron Boodman
2012/07/31 09:50:10
is_access_permitted is too general to be meaningfu
| |
| 73 extension->GetEffectiveHostPermissions().MatchesURL(frame_url); | |
| 74 | |
| 75 // Exceptions are made for the following cases for both of the above: | |
| 76 // | |
| 77 // 1. Empty origins (needed for some edge cases when we have empty origins). | |
| 78 bool is_empty_origin = frame_url.is_empty(); | |
| 79 | |
| 80 // 2. Extensions requesting their own resources. | |
| 81 bool is_own_resource = frame_url.GetOrigin() == extension->url(); | |
| 82 | |
| 83 // 3. Devtools (chrome-extension:// URLs are loaded into frames of devtools | |
| 84 // to support the devtools extension APIs). | |
| 85 bool is_dev_tools = frame_url.SchemeIs(chrome::kChromeDevToolsScheme) && | |
| 86 !extension->devtools_url().is_empty(); | |
| 87 | |
| 88 // Exceptions are made to the host permission restriction for the following | |
| 89 // cases. | |
| 90 // | |
| 91 // 4. `data:` origins. | |
| 92 bool is_data_origin = frame_url.SchemeIs(chrome::kDataScheme); | |
| 93 | |
| 94 // 5. `chrome-extension:` origins. | |
| 95 bool is_extension_origin = frame_url.SchemeIs(chrome::kExtensionScheme); | |
| 96 | |
| 97 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { | |
|
Aaron Boodman
2012/07/31 09:50:10
I think the commentary makes this harder to read.
| |
| 98 if (!is_resource_web_accessible) { | |
| 71 std::string message = base::StringPrintf( | 99 std::string message = base::StringPrintf( |
| 72 "Denying load of %s. Resources must be listed in the " | 100 "Denying load of %s. Resources must be listed in the " |
| 73 "web_accessible_resources manifest key in order to be loaded by " | 101 "web_accessible_resources manifest key in order to be loaded by " |
| 74 "pages outside the extension.", | 102 "pages outside the extension.", |
| 75 resource_url.spec().c_str()); | 103 resource_url.spec().c_str()); |
| 76 frame->addMessageToConsole( | 104 frame->addMessageToConsole( |
| 77 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 105 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
| 78 WebKit::WebString::fromUTF8(message))); | 106 WebKit::WebString::fromUTF8(message))); |
| 79 return false; | 107 return false; |
| 80 } | 108 } |
| 109 | |
| 110 if (!is_access_permitted && !is_extension_origin && !is_data_origin) { | |
| 111 std::string message = base::StringPrintf( | |
| 112 "Denying load of %s. An extension's resources can only be loaded " | |
| 113 "into a page for which the extension has explicit host permissions.", | |
| 114 resource_url.spec().c_str()); | |
| 115 frame->addMessageToConsole( | |
| 116 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | |
| 117 WebKit::WebString::fromUTF8(message))); | |
| 118 return false; | |
| 119 } | |
| 81 } | 120 } |
| 82 | 121 |
| 83 return true; | 122 return true; |
| 84 } | 123 } |
| 85 | 124 |
| 86 // static | 125 // static |
| 87 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( | 126 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( |
| 88 const GURL& resource_url, | 127 const GURL& resource_url, |
| 89 WebKit::WebFrame* frame) { | 128 WebKit::WebFrame* frame) { |
| 90 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); | 129 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); |
| 91 | 130 |
| 92 GURL frame_url = frame->document().url(); | 131 GURL frame_url = frame->document().url(); |
| 93 if (!frame_url.is_empty() && | 132 if (!frame_url.is_empty() && |
| 94 !frame_url.SchemeIs(chrome::kExtensionScheme)) { | 133 !frame_url.SchemeIs(chrome::kExtensionScheme)) { |
| 95 std::string message = base::StringPrintf( | 134 std::string message = base::StringPrintf( |
| 96 "Denying load of %s. chrome-extension-resources:// can only be " | 135 "Denying load of %s. chrome-extension-resources:// can only be " |
| 97 "loaded from extensions.", | 136 "loaded from extensions.", |
| 98 resource_url.spec().c_str()); | 137 resource_url.spec().c_str()); |
| 99 frame->addMessageToConsole( | 138 frame->addMessageToConsole( |
| 100 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 139 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
| 101 WebKit::WebString::fromUTF8(message))); | 140 WebKit::WebString::fromUTF8(message))); |
| 102 return false; | 141 return false; |
| 103 } | 142 } |
| 104 | 143 |
| 105 return true; | 144 return true; |
| 106 } | 145 } |
| 107 | 146 |
| 108 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { | 147 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { |
| 109 } | 148 } |
| OLD | NEW |