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" |
| 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 "content/public/common/page_transition_types.h" | |
| 14 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 19 | 20 |
| 20 using extensions::Extension; | 21 using extensions::Extension; |
| 21 | 22 |
| 22 // static | 23 // static |
| 23 bool ExtensionResourceRequestPolicy::CanRequestResource( | 24 bool ExtensionResourceRequestPolicy::CanRequestResource( |
| 24 const GURL& resource_url, | 25 const GURL& resource_url, |
| 25 WebKit::WebFrame* frame, | 26 WebKit::WebFrame* frame, |
| 27 content::PageTransition transition_type, | |
| 26 const ExtensionSet* loaded_extensions) { | 28 const ExtensionSet* loaded_extensions) { |
| 27 CHECK(resource_url.SchemeIs(chrome::kExtensionScheme)); | 29 CHECK(resource_url.SchemeIs(chrome::kExtensionScheme)); |
| 28 | 30 |
| 29 const Extension* extension = | 31 const Extension* extension = |
| 30 loaded_extensions->GetExtensionOrAppByURL(ExtensionURLInfo(resource_url)); | 32 loaded_extensions->GetExtensionOrAppByURL(ExtensionURLInfo(resource_url)); |
| 31 if (!extension) { | 33 if (!extension) { |
| 32 // Allow the load in the case of a non-existent extension. We'll just get a | 34 // Allow the load in the case of a non-existent extension. We'll just get a |
| 33 // 404 from the browser process. | 35 // 404 from the browser process. |
| 34 return true; | 36 return true; |
| 35 } | 37 } |
| 36 | 38 |
| 37 // Disallow loading of packaged resources for hosted apps. We don't allow | 39 // Disallow loading of packaged resources for hosted apps. We don't allow |
| 38 // hybrid hosted/packaged apps. The one exception is access to icons, since | 40 // hybrid hosted/packaged apps. The one exception is access to icons, since |
| 39 // some extensions want to be able to do things like create their own | 41 // some extensions want to be able to do things like create their own |
| 40 // launchers. | 42 // launchers. |
| 41 std::string resource_root_relative_path = | 43 std::string resource_root_relative_path = |
| 42 resource_url.path().empty() ? "" : resource_url.path().substr(1); | 44 resource_url.path().empty() ? "" : resource_url.path().substr(1); |
| 43 if (extension->is_hosted_app() && | 45 if (extension->is_hosted_app() && |
| 44 !extension->icons().ContainsPath(resource_root_relative_path)) { | 46 !extension->icons().ContainsPath(resource_root_relative_path)) { |
| 45 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 47 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " |
| 46 << "hosted app."; | 48 << "hosted app."; |
| 47 return false; | 49 return false; |
| 48 } | 50 } |
| 49 | 51 |
| 50 // Disallow loading of extension resources which are not explicitely listed | 52 // Disallow loading of extension resources which are not explicitely listed |
|
Charlie Reis
2012/08/02 18:39:26
nit: explicitly (as long as we're cleaning stuff u
| |
| 51 // as web accessible if the manifest version is 2 or greater. | 53 // as web accessible if the manifest version is 2 or greater. |
| 52 if (!extension->IsResourceWebAccessible(resource_url.path()) && | 54 if (!extension->IsResourceWebAccessible(resource_url.path()) && |
| 53 !CommandLine::ForCurrentProcess()->HasSwitch( | 55 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 54 switches::kDisableExtensionsResourceWhitelist)) { | 56 switches::kDisableExtensionsResourceWhitelist)) { |
| 55 GURL frame_url = frame->document().url(); | 57 GURL frame_url = frame->document().url(); |
| 56 GURL page_url = frame->top()->document().url(); | 58 GURL page_url = frame->top()->document().url(); |
| 57 | 59 |
| 58 // Exceptions are: | |
| 59 // - empty origin (needed for some edge cases when we have empty origins) | |
| 60 bool is_empty_origin = frame_url.is_empty(); | |
| 61 // - extensions requesting their own resources (frame_url check is for | 60 // - extensions requesting their own resources (frame_url check is for |
| 62 // images, page_url check is for iframes) | 61 // images, page_url check is for iframes) |
| 63 bool is_own_resource = frame_url.GetOrigin() == extension->url() || | 62 bool is_own_resource = frame_url.GetOrigin() == extension->url() || |
| 64 page_url.GetOrigin() == extension->url(); | 63 page_url.GetOrigin() == extension->url(); |
| 65 // - devtools (chrome-extension:// URLs are loaded into frames of devtools | 64 // - devtools (chrome-extension:// URLs are loaded into frames of devtools |
| 66 // to support the devtools extension APIs) | 65 // to support the devtools extension APIs) |
| 67 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && | 66 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && |
| 68 !extension->devtools_url().is_empty(); | 67 !extension->devtools_url().is_empty(); |
| 68 // In all other cases we forbid certain types of transitions. | |
|
Charlie Reis
2012/08/02 18:39:26
I don't quite follow this, with all the exceptions
Cris Neckar
2012/08/02 19:08:26
Yes the intent was to ensure that certain types of
| |
| 69 content::PageTransition base_transition = | |
| 70 PageTransitionStripQualifier(transition_type); | |
| 71 bool transition_allowed = | |
| 72 base_transition != content::PAGE_TRANSITION_LINK && | |
| 73 base_transition != content::PAGE_TRANSITION_AUTO_SUBFRAME && | |
| 74 base_transition != content::PAGE_TRANSITION_MANUAL_SUBFRAME && | |
| 75 base_transition != content::PAGE_TRANSITION_FORM_SUBMIT; | |
| 69 | 76 |
| 70 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { | 77 if (!is_own_resource && !is_dev_tools && !transition_allowed) { |
| 71 std::string message = base::StringPrintf( | 78 std::string message = base::StringPrintf( |
| 72 "Denying load of %s. Resources must be listed in the " | 79 "Denying load of %s. Resources must be listed in the " |
| 73 "web_accessible_resources manifest key in order to be loaded by " | 80 "web_accessible_resources manifest key in order to be loaded by " |
| 74 "pages outside the extension.", | 81 "pages outside the extension.", |
| 75 resource_url.spec().c_str()); | 82 resource_url.spec().c_str()); |
| 76 frame->addMessageToConsole( | 83 frame->addMessageToConsole( |
| 77 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 84 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
| 78 WebKit::WebString::fromUTF8(message))); | 85 WebKit::WebString::fromUTF8(message))); |
| 79 return false; | 86 return false; |
| 80 } | 87 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 100 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, | 107 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, |
| 101 WebKit::WebString::fromUTF8(message))); | 108 WebKit::WebString::fromUTF8(message))); |
| 102 return false; | 109 return false; |
| 103 } | 110 } |
| 104 | 111 |
| 105 return true; | 112 return true; |
| 106 } | 113 } |
| 107 | 114 |
| 108 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { | 115 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { |
| 109 } | 116 } |
| OLD | NEW |