| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser/child_process_security_policy.h" | 5 #include "chrome/browser/child_process_security_policy.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/common/bindings_policy.h" |
| 11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 13 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 14 | 15 |
| 15 // The SecurityState class is used to maintain per-renderer security state | 16 // The SecurityState class is used to maintain per-renderer security state |
| 16 // information. | 17 // information. |
| 17 class ChildProcessSecurityPolicy::SecurityState { | 18 class ChildProcessSecurityPolicy::SecurityState { |
| 18 public: | 19 public: |
| 19 SecurityState() : has_dom_ui_bindings_(false) { } | 20 SecurityState() : enabled_bindings_(0) { } |
| 20 ~SecurityState() { | 21 ~SecurityState() { |
| 21 scheme_policy_.clear(); | 22 scheme_policy_.clear(); |
| 22 } | 23 } |
| 23 | 24 |
| 24 // Grant permission to request URLs with the specified scheme. | 25 // Grant permission to request URLs with the specified scheme. |
| 25 void GrantScheme(const std::string& scheme) { | 26 void GrantScheme(const std::string& scheme) { |
| 26 scheme_policy_[scheme] = true; | 27 scheme_policy_[scheme] = true; |
| 27 } | 28 } |
| 28 | 29 |
| 29 // Revoke permission to request URLs with the specified scheme. | 30 // Revoke permission to request URLs with the specified scheme. |
| 30 void RevokeScheme(const std::string& scheme) { | 31 void RevokeScheme(const std::string& scheme) { |
| 31 scheme_policy_[scheme] = false; | 32 scheme_policy_[scheme] = false; |
| 32 } | 33 } |
| 33 | 34 |
| 34 // Grant permission to upload the specified file to the web. | 35 // Grant permission to upload the specified file to the web. |
| 35 void GrantUploadFile(const FilePath& file) { | 36 void GrantUploadFile(const FilePath& file) { |
| 36 uploadable_files_.insert(file); | 37 uploadable_files_.insert(file); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void GrantDOMUIBindings() { | 40 void GrantBindings(int bindings) { |
| 40 has_dom_ui_bindings_ = true; | 41 enabled_bindings_ |= bindings; |
| 41 } | 42 } |
| 42 | 43 |
| 43 // Determine whether permission has been granted to request url. | 44 // Determine whether permission has been granted to request url. |
| 44 // Schemes that have not been granted default to being denied. | 45 // Schemes that have not been granted default to being denied. |
| 45 bool CanRequestURL(const GURL& url) { | 46 bool CanRequestURL(const GURL& url) { |
| 46 SchemeMap::const_iterator judgment(scheme_policy_.find(url.scheme())); | 47 SchemeMap::const_iterator judgment(scheme_policy_.find(url.scheme())); |
| 47 | 48 |
| 48 if (judgment == scheme_policy_.end()) | 49 if (judgment == scheme_policy_.end()) |
| 49 return false; // Unmentioned schemes are disallowed. | 50 return false; // Unmentioned schemes are disallowed. |
| 50 | 51 |
| 51 return judgment->second; | 52 return judgment->second; |
| 52 } | 53 } |
| 53 | 54 |
| 54 // Determine whether permission has been granted to upload file. | 55 // Determine whether permission has been granted to upload file. |
| 55 // Files that have not been granted default to being denied. | 56 // Files that have not been granted default to being denied. |
| 56 bool CanUploadFile(const FilePath& file) { | 57 bool CanUploadFile(const FilePath& file) { |
| 57 return uploadable_files_.find(file) != uploadable_files_.end(); | 58 return uploadable_files_.find(file) != uploadable_files_.end(); |
| 58 } | 59 } |
| 59 | 60 |
| 60 bool has_dom_ui_bindings() const { return has_dom_ui_bindings_; } | 61 bool has_dom_ui_bindings() const { |
| 62 return BindingsPolicy::is_dom_ui_enabled(enabled_bindings_); |
| 63 } |
| 64 |
| 65 bool has_extension_bindings() const { |
| 66 return BindingsPolicy::is_extension_enabled(enabled_bindings_); |
| 67 } |
| 61 | 68 |
| 62 private: | 69 private: |
| 63 typedef std::map<std::string, bool> SchemeMap; | 70 typedef std::map<std::string, bool> SchemeMap; |
| 64 typedef std::set<FilePath> FileSet; | 71 typedef std::set<FilePath> FileSet; |
| 65 | 72 |
| 66 // Maps URL schemes to whether permission has been granted or revoked: | 73 // Maps URL schemes to whether permission has been granted or revoked: |
| 67 // |true| means the scheme has been granted. | 74 // |true| means the scheme has been granted. |
| 68 // |false| means the scheme has been revoked. | 75 // |false| means the scheme has been revoked. |
| 69 // If a scheme is not present in the map, then it has never been granted | 76 // If a scheme is not present in the map, then it has never been granted |
| 70 // or revoked. | 77 // or revoked. |
| 71 SchemeMap scheme_policy_; | 78 SchemeMap scheme_policy_; |
| 72 | 79 |
| 73 // The set of files the renderer is permited to upload to the web. | 80 // The set of files the renderer is permited to upload to the web. |
| 74 FileSet uploadable_files_; | 81 FileSet uploadable_files_; |
| 75 | 82 |
| 76 bool has_dom_ui_bindings_; | 83 int enabled_bindings_; |
| 77 | 84 |
| 78 DISALLOW_COPY_AND_ASSIGN(SecurityState); | 85 DISALLOW_COPY_AND_ASSIGN(SecurityState); |
| 79 }; | 86 }; |
| 80 | 87 |
| 81 ChildProcessSecurityPolicy::ChildProcessSecurityPolicy() { | 88 ChildProcessSecurityPolicy::ChildProcessSecurityPolicy() { |
| 82 // We know about these schemes and believe them to be safe. | 89 // We know about these schemes and believe them to be safe. |
| 83 RegisterWebSafeScheme(chrome::kHttpScheme); | 90 RegisterWebSafeScheme(chrome::kHttpScheme); |
| 84 RegisterWebSafeScheme(chrome::kHttpsScheme); | 91 RegisterWebSafeScheme(chrome::kHttpsScheme); |
| 85 RegisterWebSafeScheme(chrome::kFtpScheme); | 92 RegisterWebSafeScheme(chrome::kFtpScheme); |
| 86 RegisterWebSafeScheme(chrome::kDataScheme); | 93 RegisterWebSafeScheme(chrome::kDataScheme); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 state->second->GrantScheme(chrome::kChromeUIScheme); | 218 state->second->GrantScheme(chrome::kChromeUIScheme); |
| 212 } | 219 } |
| 213 | 220 |
| 214 void ChildProcessSecurityPolicy::GrantDOMUIBindings(int renderer_id) { | 221 void ChildProcessSecurityPolicy::GrantDOMUIBindings(int renderer_id) { |
| 215 AutoLock lock(lock_); | 222 AutoLock lock(lock_); |
| 216 | 223 |
| 217 SecurityStateMap::iterator state = security_state_.find(renderer_id); | 224 SecurityStateMap::iterator state = security_state_.find(renderer_id); |
| 218 if (state == security_state_.end()) | 225 if (state == security_state_.end()) |
| 219 return; | 226 return; |
| 220 | 227 |
| 221 state->second->GrantDOMUIBindings(); | 228 state->second->GrantBindings(BindingsPolicy::DOM_UI); |
| 222 | 229 |
| 223 // DOM UI bindings need the ability to request chrome: URLs. | 230 // DOM UI bindings need the ability to request chrome: URLs. |
| 224 state->second->GrantScheme(chrome::kChromeUIScheme); | 231 state->second->GrantScheme(chrome::kChromeUIScheme); |
| 225 | 232 |
| 226 // DOM UI pages can contain links to file:// URLs. | 233 // DOM UI pages can contain links to file:// URLs. |
| 227 state->second->GrantScheme(chrome::kFileScheme); | 234 state->second->GrantScheme(chrome::kFileScheme); |
| 228 } | 235 } |
| 229 | 236 |
| 237 void ChildProcessSecurityPolicy::GrantExtensionBindings(int renderer_id) { |
| 238 AutoLock lock(lock_); |
| 239 |
| 240 SecurityStateMap::iterator state = security_state_.find(renderer_id); |
| 241 if (state == security_state_.end()) |
| 242 return; |
| 243 |
| 244 state->second->GrantBindings(BindingsPolicy::EXTENSION); |
| 245 } |
| 246 |
| 230 bool ChildProcessSecurityPolicy::CanRequestURL(int renderer_id, const GURL& url)
{ | 247 bool ChildProcessSecurityPolicy::CanRequestURL(int renderer_id, const GURL& url)
{ |
| 231 if (!url.is_valid()) | 248 if (!url.is_valid()) |
| 232 return false; // Can't request invalid URLs. | 249 return false; // Can't request invalid URLs. |
| 233 | 250 |
| 234 if (IsWebSafeScheme(url.scheme())) | 251 if (IsWebSafeScheme(url.scheme())) |
| 235 return true; // The scheme has been white-listed for every renderer. | 252 return true; // The scheme has been white-listed for every renderer. |
| 236 | 253 |
| 237 if (IsPseudoScheme(url.scheme())) { | 254 if (IsPseudoScheme(url.scheme())) { |
| 238 // There are a number of special cases for pseudo schemes. | 255 // There are a number of special cases for pseudo schemes. |
| 239 | 256 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 | 298 |
| 282 bool ChildProcessSecurityPolicy::HasDOMUIBindings(int renderer_id) { | 299 bool ChildProcessSecurityPolicy::HasDOMUIBindings(int renderer_id) { |
| 283 AutoLock lock(lock_); | 300 AutoLock lock(lock_); |
| 284 | 301 |
| 285 SecurityStateMap::iterator state = security_state_.find(renderer_id); | 302 SecurityStateMap::iterator state = security_state_.find(renderer_id); |
| 286 if (state == security_state_.end()) | 303 if (state == security_state_.end()) |
| 287 return false; | 304 return false; |
| 288 | 305 |
| 289 return state->second->has_dom_ui_bindings(); | 306 return state->second->has_dom_ui_bindings(); |
| 290 } | 307 } |
| 308 |
| 309 bool ChildProcessSecurityPolicy::HasExtensionBindings(int renderer_id) { |
| 310 AutoLock lock(lock_); |
| 311 |
| 312 SecurityStateMap::iterator state = security_state_.find(renderer_id); |
| 313 if (state == security_state_.end()) |
| 314 return false; |
| 315 |
| 316 return state->second->has_extension_bindings(); |
| 317 } |
| OLD | NEW |