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

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: 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 "chrome/common/chrome_switches.h" 10 #include "chrome/common/chrome_switches.h"
(...skipping 29 matching lines...) Expand all
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. 51 GURL page_url = frame->top()->document().url();
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 // Disallow loading of extension resources when one of the following
59 // - empty origin (needed for some edge cases when we have empty origins) 54 // conditions holds:
60 bool is_empty_origin = frame_url.is_empty(); 55 //
61 // - extensions requesting their own resources (frame_url check is for 56 // 1. The resource is not explicitly listed as a web accessible resource (and
62 // images, page_url check is for iframes) 57 // the extension is manifest version 2+, and this check isn't disabled via
63 bool is_own_resource = frame_url.GetOrigin() == extension->url() || 58 // a command-line flag).
64 page_url.GetOrigin() == extension->url(); 59 bool is_resource_web_accessible =
65 // - devtools (chrome-extension:// URLs are loaded into frames of devtools 60 extension->IsResourceWebAccessible(resource_url.path()) ||
Aaron Boodman 2012/07/30 14:28:26 The parenthetical comment about manifest v2+ is ha
Mike West 2012/07/31 09:25:06 Done.
66 // to support the devtools extension APIs) 61 CommandLine::ForCurrentProcess()->HasSwitch(
67 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) && 62 switches::kDisableExtensionsResourceWhitelist);
68 !extension->devtools_url().is_empty();
69 63
70 if (!is_empty_origin && !is_own_resource && !is_dev_tools) { 64 // 2. The resource is loaded into a context for which the extension has no
65 // permission (e.g. resources from an extension with host permissions for
66 // `evil.com` shouldn't be loaded into `example.com`).
67 bool is_access_permitted =
68 extension->GetEffectiveHostPermissions().MatchesURL(frame_url) ||
69 extension->GetEffectiveHostPermissions().MatchesURL(page_url);
Aaron Boodman 2012/07/30 14:28:26 I don't think you should check page_url here.
Aaron Boodman 2012/07/30 14:28:26 Can you test whether this seems to work with decla
Mike West 2012/07/31 09:25:06 I think this is covered by the existing content_sc
70
71 // Exceptions are made for the following cases for both of the above:
72 //
73 // 1. Empty origins (needed for some edge cases when we have empty origins).
74 bool is_empty_origin = frame_url.is_empty();
75
76 // 2. Extensions requesting their own resources (frame_url check is for
77 // images, page_url check is for iframes).
78 bool is_own_resource = frame_url.GetOrigin() == extension->url() ||
Aaron Boodman 2012/07/30 14:28:26 This looks wrong, but you didn't add it. Imagine t
Mike West 2012/07/31 09:25:06 As discussed, I've changed this logic to the follo
79 page_url.GetOrigin() == extension->url();
80
81 // 3. Devtools (chrome-extension:// URLs are loaded into frames of devtools
82 // to support the devtools extension APIs).
83 bool is_dev_tools = page_url.SchemeIs(chrome::kChromeDevToolsScheme) &&
84 !extension->devtools_url().is_empty();
85
86 // Exceptions are made to the host permission restriction for the following
87 // cases.
88 //
89 // 4. `data:` origins.
90 bool is_data_origin = frame_url.SchemeIs(chrome::kDataScheme) ||
91 page_url.SchemeIs(chrome::kDataScheme);
Aaron Boodman 2012/07/30 14:28:26 Again, don't think we should be checking page_url.
Mike West 2012/07/31 09:25:06 Done.
92
93 // 5. `chrome-extension:` origins.
94 bool is_extension_origin = frame_url.SchemeIs(chrome::kExtensionScheme) ||
95 page_url.SchemeIs(chrome::kExtensionScheme);
96
97 if (!is_empty_origin && !is_own_resource && !is_dev_tools) {
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_url.spec().c_str(),
116 page_url.spec().c_str());
117 frame->addMessageToConsole(
118 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError,
119 WebKit::WebString::fromUTF8(message)));
120 return false;
121 }
81 } 122 }
82 123
83 return true; 124 return true;
84 } 125 }
85 126
86 // static 127 // static
87 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme( 128 bool ExtensionResourceRequestPolicy::CanRequestExtensionResourceScheme(
88 const GURL& resource_url, 129 const GURL& resource_url,
89 WebKit::WebFrame* frame) { 130 WebKit::WebFrame* frame) {
90 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme)); 131 CHECK(resource_url.SchemeIs(chrome::kExtensionResourceScheme));
91 132
92 GURL frame_url = frame->document().url(); 133 GURL frame_url = frame->document().url();
93 if (!frame_url.is_empty() && 134 if (!frame_url.is_empty() &&
94 !frame_url.SchemeIs(chrome::kExtensionScheme)) { 135 !frame_url.SchemeIs(chrome::kExtensionScheme)) {
95 std::string message = base::StringPrintf( 136 std::string message = base::StringPrintf(
96 "Denying load of %s. chrome-extension-resources:// can only be " 137 "Denying load of %s. chrome-extension-resources:// can only be "
97 "loaded from extensions.", 138 "loaded from extensions.",
98 resource_url.spec().c_str()); 139 resource_url.spec().c_str());
99 frame->addMessageToConsole( 140 frame->addMessageToConsole(
100 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError, 141 WebKit::WebConsoleMessage(WebKit::WebConsoleMessage::LevelError,
101 WebKit::WebString::fromUTF8(message))); 142 WebKit::WebString::fromUTF8(message)));
102 return false; 143 return false;
103 } 144 }
104 145
105 return true; 146 return true;
106 } 147 }
107 148
108 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { 149 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() {
109 } 150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698