PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/content/browser/child_process_security_policy_impl.h

https://gitlab.com/jonnialva90/iridium-browser
C Header | 255 lines | 131 code | 49 blank | 75 comment | 0 complexity | 9d6aba0587a4602dabcacf052cee452c MD5 | raw file
  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
  3. // found in the LICENSE file.
  4. #ifndef CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
  5. #define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/gtest_prod_util.h"
  11. #include "base/memory/singleton.h"
  12. #include "base/synchronization/lock.h"
  13. #include "content/public/browser/child_process_security_policy.h"
  14. #include "content/public/common/resource_type.h"
  15. #include "storage/common/fileapi/file_system_types.h"
  16. class GURL;
  17. namespace base {
  18. class FilePath;
  19. }
  20. namespace storage {
  21. class FileSystemURL;
  22. }
  23. namespace content {
  24. class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
  25. : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) {
  26. public:
  27. // Object can only be created through GetInstance() so the constructor is
  28. // private.
  29. ~ChildProcessSecurityPolicyImpl() override;
  30. static ChildProcessSecurityPolicyImpl* GetInstance();
  31. // ChildProcessSecurityPolicy implementation.
  32. void RegisterWebSafeScheme(const std::string& scheme) override;
  33. bool IsWebSafeScheme(const std::string& scheme) override;
  34. void GrantReadFile(int child_id, const base::FilePath& file) override;
  35. void GrantCreateReadWriteFile(int child_id,
  36. const base::FilePath& file) override;
  37. void GrantCopyInto(int child_id, const base::FilePath& dir) override;
  38. void GrantDeleteFrom(int child_id, const base::FilePath& dir) override;
  39. void GrantReadFileSystem(int child_id,
  40. const std::string& filesystem_id) override;
  41. void GrantWriteFileSystem(int child_id,
  42. const std::string& filesystem_id) override;
  43. void GrantCreateFileForFileSystem(int child_id,
  44. const std::string& filesystem_id) override;
  45. void GrantCreateReadWriteFileSystem(
  46. int child_id,
  47. const std::string& filesystem_id) override;
  48. void GrantCopyIntoFileSystem(int child_id,
  49. const std::string& filesystem_id) override;
  50. void GrantDeleteFromFileSystem(int child_id,
  51. const std::string& filesystem_id) override;
  52. void GrantOrigin(int child_id, const url::Origin& origin) override;
  53. void GrantScheme(int child_id, const std::string& scheme) override;
  54. bool CanReadFile(int child_id, const base::FilePath& file) override;
  55. bool CanCreateReadWriteFile(int child_id,
  56. const base::FilePath& file) override;
  57. bool CanReadFileSystem(int child_id,
  58. const std::string& filesystem_id) override;
  59. bool CanReadWriteFileSystem(int child_id,
  60. const std::string& filesystem_id) override;
  61. bool CanCopyIntoFileSystem(int child_id,
  62. const std::string& filesystem_id) override;
  63. bool CanDeleteFromFileSystem(int child_id,
  64. const std::string& filesystem_id) override;
  65. bool HasWebUIBindings(int child_id) override;
  66. void GrantSendMidiSysExMessage(int child_id) override;
  67. bool CanAccessDataForOrigin(int child_id, const GURL& url) override;
  68. // Pseudo schemes are treated differently than other schemes because they
  69. // cannot be requested like normal URLs. There is no mechanism for revoking
  70. // pseudo schemes.
  71. void RegisterPseudoScheme(const std::string& scheme);
  72. // Returns true iff |scheme| has been registered as pseudo scheme.
  73. bool IsPseudoScheme(const std::string& scheme);
  74. // Upon creation, child processes should register themselves by calling this
  75. // this method exactly once.
  76. void Add(int child_id);
  77. // Upon creation, worker thread child processes should register themselves by
  78. // calling this this method exactly once. Workers that are not shared will
  79. // inherit permissions from their parent renderer process identified with
  80. // |main_render_process_id|.
  81. void AddWorker(int worker_child_id, int main_render_process_id);
  82. // Upon destruction, child processess should unregister themselves by caling
  83. // this method exactly once.
  84. void Remove(int child_id);
  85. // Whenever the browser processes commands the child process to request a URL,
  86. // it should call this method to grant the child process the capability to
  87. // request the URL, along with permission to request all URLs of the same
  88. // scheme.
  89. void GrantRequestURL(int child_id, const GURL& url);
  90. // Whenever the browser process drops a file icon on a tab, it should call
  91. // this method to grant the child process the capability to request this one
  92. // file:// URL, but not all urls of the file:// scheme.
  93. void GrantRequestSpecificFileURL(int child_id, const GURL& url);
  94. // Revokes all permissions granted to the given file.
  95. void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
  96. // Grant the child process the ability to use Web UI Bindings.
  97. void GrantWebUIBindings(int child_id);
  98. // Grant the child process the ability to read raw cookies.
  99. void GrantReadRawCookies(int child_id);
  100. // Revoke read raw cookies permission.
  101. void RevokeReadRawCookies(int child_id);
  102. // Before servicing a child process's request for a URL, the browser should
  103. // call this method to determine whether the process has the capability to
  104. // request the URL.
  105. bool CanRequestURL(int child_id, const GURL& url);
  106. // Whether the process is allowed to commit a document from the given URL.
  107. // This is more restrictive than CanRequestURL, since CanRequestURL allows
  108. // requests that might lead to cross-process navigations or external protocol
  109. // handlers.
  110. bool CanCommitURL(int child_id, const GURL& url);
  111. // Explicit permissions checks for FileSystemURL specified files.
  112. bool CanReadFileSystemFile(int child_id, const storage::FileSystemURL& url);
  113. bool CanWriteFileSystemFile(int child_id, const storage::FileSystemURL& url);
  114. bool CanCreateFileSystemFile(int child_id, const storage::FileSystemURL& url);
  115. bool CanCreateReadWriteFileSystemFile(int child_id,
  116. const storage::FileSystemURL& url);
  117. bool CanCopyIntoFileSystemFile(int child_id,
  118. const storage::FileSystemURL& url);
  119. bool CanDeleteFileSystemFile(int child_id, const storage::FileSystemURL& url);
  120. // Returns true if the specified child_id has been granted ReadRawCookies.
  121. bool CanReadRawCookies(int child_id);
  122. // Sets the process as only permitted to use and see the cookies for the
  123. // given origin.
  124. // Origin lock is applied only if the --site-per-process flag is used.
  125. void LockToOrigin(int child_id, const GURL& gurl);
  126. // Register FileSystem type and permission policy which should be used
  127. // for the type. The |policy| must be a bitwise-or'd value of
  128. // storage::FilePermissionPolicy.
  129. void RegisterFileSystemPermissionPolicy(storage::FileSystemType type,
  130. int policy);
  131. // Returns true if sending system exclusive messages is allowed.
  132. bool CanSendMidiSysExMessage(int child_id);
  133. private:
  134. friend class ChildProcessSecurityPolicyInProcessBrowserTest;
  135. friend class ChildProcessSecurityPolicyTest;
  136. FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
  137. NoLeak);
  138. FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
  139. class SecurityState;
  140. typedef std::set<std::string> SchemeSet;
  141. typedef std::map<int, SecurityState*> SecurityStateMap;
  142. typedef std::map<int, int> WorkerToMainProcessMap;
  143. typedef std::map<storage::FileSystemType, int> FileSystemPermissionPolicyMap;
  144. // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
  145. ChildProcessSecurityPolicyImpl();
  146. friend struct base::DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
  147. // Adds child process during registration.
  148. void AddChild(int child_id);
  149. // Determines if certain permissions were granted for a file to given child
  150. // process. |permissions| is an internally defined bit-set.
  151. bool ChildProcessHasPermissionsForFile(int child_id,
  152. const base::FilePath& file,
  153. int permissions);
  154. // Grant a particular permission set for a file. |permissions| is an
  155. // internally defined bit-set.
  156. void GrantPermissionsForFile(int child_id,
  157. const base::FilePath& file,
  158. int permissions);
  159. // Grants access permission to the given isolated file system
  160. // identified by |filesystem_id|. See comments for
  161. // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
  162. void GrantPermissionsForFileSystem(
  163. int child_id,
  164. const std::string& filesystem_id,
  165. int permission);
  166. // Determines if certain permissions were granted for a file. |permissions|
  167. // is an internally defined bit-set. If |child_id| is a worker process,
  168. // this returns true if either the worker process or its parent renderer
  169. // has permissions for the file.
  170. bool HasPermissionsForFile(int child_id,
  171. const base::FilePath& file,
  172. int permissions);
  173. // Determines if certain permissions were granted for a file in FileSystem
  174. // API. |permissions| is an internally defined bit-set.
  175. bool HasPermissionsForFileSystemFile(int child_id,
  176. const storage::FileSystemURL& url,
  177. int permissions);
  178. // Determines if certain permissions were granted for a file system.
  179. // |permissions| is an internally defined bit-set.
  180. bool HasPermissionsForFileSystem(
  181. int child_id,
  182. const std::string& filesystem_id,
  183. int permission);
  184. // You must acquire this lock before reading or writing any members of this
  185. // class. You must not block while holding this lock.
  186. base::Lock lock_;
  187. // These schemes are white-listed for all child processes. This set is
  188. // protected by |lock_|.
  189. SchemeSet web_safe_schemes_;
  190. // These schemes do not actually represent retrievable URLs. For example,
  191. // the the URLs in the "about" scheme are aliases to other URLs. This set is
  192. // protected by |lock_|.
  193. SchemeSet pseudo_schemes_;
  194. // This map holds a SecurityState for each child process. The key for the
  195. // map is the ID of the ChildProcessHost. The SecurityState objects are
  196. // owned by this object and are protected by |lock_|. References to them must
  197. // not escape this class.
  198. SecurityStateMap security_state_;
  199. // This maps keeps the record of which js worker thread child process
  200. // corresponds to which main js thread child process.
  201. WorkerToMainProcessMap worker_map_;
  202. FileSystemPermissionPolicyMap file_system_policy_map_;
  203. DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
  204. };
  205. } // namespace content
  206. #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_