Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/renderer/extensions/extension_resource_request_policy.cc

Issue 10828067: Extension resources should only load in contexts the extension has permission to access. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Line endings. How I hate them. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
10 #include "chrome/common/chrome_switches.h" 11 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/url_constants.h" 12 #include "chrome/common/url_constants.h"
12 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_set.h" 14 #include "chrome/common/extensions/extension_set.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
(...skipping 20 matching lines...) Expand all
40 // launchers. 41 // launchers.
41 std::string resource_root_relative_path = 42 std::string resource_root_relative_path =
42 resource_url.path().empty() ? "" : resource_url.path().substr(1); 43 resource_url.path().empty() ? "" : resource_url.path().substr(1);
43 if (extension->is_hosted_app() && 44 if (extension->is_hosted_app() &&
44 !extension->icons().ContainsPath(resource_root_relative_path)) { 45 !extension->icons().ContainsPath(resource_root_relative_path)) {
45 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " 46 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from "
46 << "hosted app."; 47 << "hosted app.";
47 return false; 48 return false;
48 } 49 }
49 50
50 // Disallow loading of extension resources which are not explicitely listed 51 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 52
58 // Exceptions are: 53 // In the case of loading a frame, frame* points to the frame being loaded,
59 // - empty origin (needed for some edge cases when we have empty origins) 54 // not the containing frame. This means that frame->document().url() ends up
60 bool is_empty_origin = frame_url.is_empty(); 55 // not being useful to us.
61 // - extensions requesting their own resources (frame_url check is for 56 //
62 // images, page_url check is for iframes) 57 // WebKit doesn't currently pass us enough information to know when we're a
63 bool is_own_resource = frame_url.GetOrigin() == extension->url() || 58 // frame, so we hack it by checking for 'about:blank', which should only
64 page_url.GetOrigin() == extension->url(); 59 // happen in this situation.
65 // - devtools (chrome-extension:// URLs are loaded into frames of devtools 60 //
66 // to support the devtools extension APIs) 61 // TODO(aa): Fix WebKit to pass the context of the load: crbug.com/139788.
67 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && 62 if (frame_url == GURL(chrome::kAboutBlankURL) && frame->parent())
68 !extension->devtools_url().is_empty(); 63 frame_url = frame->parent()->document().url();
69 64
70 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { 65 bool extension_has_access_to_frame =
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) {
71 std::string message = base::StringPrintf( 89 std::string message = base::StringPrintf(
72 "Denying load of %s. Resources must be listed in the " 90 "Denying load of %s. Resources must be listed in the "
73 "web_accessible_resources manifest key in order to be loaded by " 91 "web_accessible_resources manifest key in order to be loaded by "
74 "pages outside the extension.", 92 "pages outside the extension.",
75 resource_url.spec().c_str()); 93 resource_url.spec().c_str());
76 frame->addMessageToConsole( 94 frame->addMessageToConsole(
77 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, 95 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError,
78 WebKit::WebString::fromUTF8(message))); 96 WebKit::WebString::fromUTF8(message)));
79 return false; 97 return false;
80 } 98 }
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 }
81 } 111 }
82 112
83 return true; 113 return true;
84 } 114 }
85 115
86 // static 116 // static
87 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( 117 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme(
88 const GURL& resource_url, 118 const GURL& resource_url,
89 WebKit::WebFrame* frame) { 119 WebKit::WebFrame* frame) {
90 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); 120 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme));
91 121
92 GURL frame_url = frame->document().url(); 122 GURL frame_url = frame->document().url();
93 if (!frame_url.is_empty() && 123 if (!frame_url.is_empty() &&
94 !frame_url.SchemeIs(chrome::kExtensionScheme)) { 124 !frame_url.SchemeIs(chrome::kExtensionScheme)) {
95 std::string message = base::StringPrintf( 125 std::string message = base::StringPrintf(
96 "Denying load of %s. chrome-extension-resources:// can only be " 126 "Denying load of %s. chrome-extension-resources:// can only be "
97 "loaded from extensions.", 127 "loaded from extensions.",
98 resource_url.spec().c_str()); 128 resource_url.spec().c_str());
99 frame->addMessageToConsole( 129 frame->addMessageToConsole(
100 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, 130 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError,
101 WebKit::WebString::fromUTF8(message))); 131 WebKit::WebString::fromUTF8(message)));
102 return false; 132 return false;
103 } 133 }
104 134
105 return true; 135 return true;
106 } 136 }
107 137
108 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { 138 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() {
109 } 139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698