OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/common/url_constants.h" | 8 #include "chrome/common/url_constants.h" |
9 #include "chrome/common/extensions/extension.h" | 9 #include "chrome/common/extensions/extension.h" |
10 #include "chrome/common/extensions/extension_set.h" | 10 #include "chrome/common/extensions/extension_set.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
12 | 12 |
13 // static | 13 // static |
14 bool ExtensionResourceRequestPolicy::CanRequestResource( | 14 bool ExtensionResourceRequestPolicy::CanRequestResource( |
15 const GURL& resource_url, | 15 const GURL& resource_url, |
16 const GURL& frame_url, | 16 const GURL& requesting_origin, |
17 const ExtensionSet* loaded_extensions) { | 17 const ExtensionSet* loaded_extensions) { |
18 CHECK(resource_url.SchemeIs(chrome::kExtensionScheme)); | 18 CHECK(resource_url.SchemeIs(chrome::kExtensionScheme)); |
19 | 19 |
| 20 const Extension* extension = loaded_extensions->GetByURL(resource_url); |
| 21 if (!extension) { |
| 22 LOG(ERROR) << "Denying load of " << resource_url.spec() << " for unloaded " |
| 23 << "extension."; |
| 24 return false; |
| 25 } |
| 26 |
20 // chrome:// URLs are always allowed to load chrome-extension:// resources. | 27 // chrome:// URLs are always allowed to load chrome-extension:// resources. |
21 // The app launcher in the NTP uses this feature, as does dev tools. | 28 // The app launcher in the NTP uses this feature, as does dev tools. |
22 if (frame_url.SchemeIs(chrome::kChromeDevToolsScheme) || | 29 if (requesting_origin.SchemeIs(chrome::kChromeDevToolsScheme) || |
23 frame_url.SchemeIs(chrome::kChromeUIScheme)) | 30 requesting_origin.SchemeIs(chrome::kChromeUIScheme)) |
24 return true; | 31 return true; |
25 | 32 |
26 // Disallow loading of packaged resources for hosted apps. We don't allow | 33 // Disallow loading of packaged resources for hosted apps. We don't allow |
27 // hybrid hosted/packaged apps. The one exception is access to icons, since | 34 // hybrid hosted/packaged apps. The one exception is access to icons, since |
28 // some extensions want to be able to do things like create their own | 35 // some extensions want to be able to do things like create their own |
29 // launchers. | 36 // launchers. |
30 const Extension* extension = loaded_extensions->GetByURL(resource_url); | |
31 std::string resource_root_relative_path = | 37 std::string resource_root_relative_path = |
32 resource_url.path().empty() ? "" : resource_url.path().substr(1); | 38 resource_url.path().empty() ? "" : resource_url.path().substr(1); |
33 if (extension && extension->is_hosted_app() && | 39 if (extension->is_hosted_app() && |
34 !extension->icons().ContainsPath(resource_root_relative_path)) { | 40 !extension->icons().ContainsPath(resource_root_relative_path)) { |
35 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 41 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " |
36 << "hosted app."; | 42 << "hosted app."; |
37 return false; | 43 return false; |
38 } | 44 } |
39 | 45 |
40 // Otherwise, pages are allowed to load resources from extensions if the | 46 // Otherwise, pages are allowed to load resources from extensions if the |
41 // extension has host permissions to (and therefore could be running script | 47 // extension has host permissions to (and therefore could be running script |
42 // in, which might need access to the extension resources). | 48 // in, which might need access to the extension resources). |
43 // | 49 // |
44 // Exceptions are: | 50 // Exceptions are: |
45 // - empty origin (needed for some edge cases when we have empty origins) | 51 // - empty origin (needed for some edge cases when we have empty origins) |
46 // - chrome-extension:// (for legacy reasons -- some extensions interop) | 52 // - chrome-extension:// (for legacy reasons -- some extensions interop) |
47 // - data: (basic HTML notifications use data URLs internally) | 53 // - data: (basic HTML notifications use data URLs internally) |
48 if (frame_url.is_empty() || | 54 if (requesting_origin.is_empty() || |
49 frame_url.SchemeIs(chrome::kExtensionScheme) | | 55 requesting_origin.SchemeIs(chrome::kExtensionScheme) | |
50 frame_url.SchemeIs(chrome::kDataScheme)) { | 56 requesting_origin.SchemeIs(chrome::kDataScheme)) { |
51 return true; | 57 return true; |
52 } else { | 58 } else { |
53 if (extension->GetEffectiveHostPermissions().ContainsURL(frame_url)) { | 59 if (extension->GetEffectiveHostPermissions().ContainsURL( |
| 60 requesting_origin)) { |
54 return true; | 61 return true; |
55 } else { | 62 } else { |
56 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " | 63 LOG(ERROR) << "Denying load of " << resource_url.spec() << " from " |
57 << frame_url.spec() << " because the extension does not have " | 64 << requesting_origin.spec() |
58 << "access to the requesting page."; | 65 << " because the extension does not have access to the" |
| 66 << " requesting page."; |
59 return false; | 67 return false; |
60 } | 68 } |
61 } | 69 } |
62 } | 70 } |
63 | 71 |
64 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { | 72 ExtensionResourceRequestPolicy::ExtensionResourceRequestPolicy() { |
65 } | 73 } |
OLD | NEW |