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.
5 #ifndef CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
6 #define CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/singleton.h"
16 #include "base/synchronization/lock.h"
17 #include "content/public/browser/child_process_security_policy.h"
18 #include "webkit/common/fileapi/file_system_types.h"
19 #include "webkit/common/resource_type.h"
33 class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
34 : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy) {
36 // Object can only be created through GetInstance() so the constructor is
38 virtual ~ChildProcessSecurityPolicyImpl();
40 static ChildProcessSecurityPolicyImpl* GetInstance();
42 // ChildProcessSecurityPolicy implementation.
43 virtual void RegisterWebSafeScheme(const std::string& scheme) OVERRIDE;
44 virtual bool IsWebSafeScheme(const std::string& scheme) OVERRIDE;
45 virtual void GrantReadFile(int child_id, const base::FilePath& file) OVERRIDE;
46 virtual void GrantCreateReadWriteFile(int child_id,
47 const base::FilePath& file) OVERRIDE;
48 virtual void GrantReadFileSystem(
50 const std::string& filesystem_id) OVERRIDE;
51 virtual void GrantWriteFileSystem(
53 const std::string& filesystem_id) OVERRIDE;
54 virtual void GrantCreateFileForFileSystem(
56 const std::string& filesystem_id) OVERRIDE;
57 virtual void GrantCreateReadWriteFileSystem(
59 const std::string& filesystem_id) OVERRIDE;
60 virtual void GrantCopyIntoFileSystem(
62 const std::string& filesystem_id) OVERRIDE;
63 virtual void GrantDeleteFromFileSystem(
65 const std::string& filesystem_id) OVERRIDE;
66 virtual void GrantScheme(int child_id, const std::string& scheme) OVERRIDE;
67 virtual bool CanReadFile(int child_id, const base::FilePath& file) OVERRIDE;
68 virtual bool CanCreateReadWriteFile(int child_id,
69 const base::FilePath& file) OVERRIDE;
70 virtual bool CanReadFileSystem(int child_id,
71 const std::string& filesystem_id) OVERRIDE;
72 virtual bool CanReadWriteFileSystem(
74 const std::string& filesystem_id) OVERRIDE;
75 virtual bool CanCopyIntoFileSystem(int child_id,
76 const std::string& filesystem_id) OVERRIDE;
77 virtual bool CanDeleteFromFileSystem(
79 const std::string& filesystem_id) OVERRIDE;
81 // Pseudo schemes are treated differently than other schemes because they
82 // cannot be requested like normal URLs. There is no mechanism for revoking
84 void RegisterPseudoScheme(const std::string& scheme);
86 // Returns true iff |scheme| has been registered as pseudo scheme.
87 bool IsPseudoScheme(const std::string& scheme);
89 // Upon creation, child processes should register themselves by calling this
90 // this method exactly once.
91 void Add(int child_id);
93 // Upon creation, worker thread child processes should register themselves by
94 // calling this this method exactly once. Workers that are not shared will
95 // inherit permissions from their parent renderer process identified with
96 // |main_render_process_id|.
97 void AddWorker(int worker_child_id, int main_render_process_id);
99 // Upon destruction, child processess should unregister themselves by caling
100 // this method exactly once.
101 void Remove(int child_id);
103 // Whenever the browser processes commands the child process to request a URL,
104 // it should call this method to grant the child process the capability to
105 // request the URL, along with permission to request all URLs of the same
107 void GrantRequestURL(int child_id, const GURL& url);
109 // Whenever the browser process drops a file icon on a tab, it should call
110 // this method to grant the child process the capability to request this one
111 // file:// URL, but not all urls of the file:// scheme.
112 void GrantRequestSpecificFileURL(int child_id, const GURL& url);
114 // Revokes all permissions granted to the given file.
115 void RevokeAllPermissionsForFile(int child_id, const base::FilePath& file);
117 // Grant the child process the ability to use Web UI Bindings.
118 void GrantWebUIBindings(int child_id);
120 // Grant the child process the ability to read raw cookies.
121 void GrantReadRawCookies(int child_id);
123 // Revoke read raw cookies permission.
124 void RevokeReadRawCookies(int child_id);
126 // Grants permission to send system exclusive message to any MIDI devices.
127 void GrantSendMidiSysExMessage(int child_id);
129 // Before servicing a child process's request for a URL, the browser should
130 // call this method to determine whether the process has the capability to
132 bool CanRequestURL(int child_id, const GURL& url);
134 // Returns true if the process is permitted to load pages from
135 // the given origin in main frames or subframes.
136 // Only might return false if --site-per-process flag is used.
137 bool CanLoadPage(int child_id,
139 ResourceType::Type resource_type);
141 // Explicit permissions checks for FileSystemURL specified files.
142 bool CanReadFileSystemFile(int child_id, const fileapi::FileSystemURL& url);
143 bool CanWriteFileSystemFile(int child_id, const fileapi::FileSystemURL& url);
144 bool CanCreateFileSystemFile(int child_id, const fileapi::FileSystemURL& url);
145 bool CanCreateReadWriteFileSystemFile(int child_id,
146 const fileapi::FileSystemURL& url);
147 bool CanCopyIntoFileSystemFile(int child_id,
148 const fileapi::FileSystemURL& url);
149 bool CanDeleteFileSystemFile(int child_id,
150 const fileapi::FileSystemURL& url);
152 // Returns true if the specified child_id has been granted WebUIBindings.
153 // The browser should check this property before assuming the child process is
154 // allowed to use WebUIBindings.
155 bool HasWebUIBindings(int child_id);
157 // Returns true if the specified child_id has been granted ReadRawCookies.
158 bool CanReadRawCookies(int child_id);
160 // Returns true if the process is permitted to read and modify the cookies for
161 // the given origin. Does not affect cookies attached to or set by network
163 // Only might return false if the very experimental
164 // --enable-strict-site-isolation or --site-per-process flags are used.
165 bool CanAccessCookiesForOrigin(int child_id, const GURL& gurl);
167 // Returns true if the process is permitted to attach cookies to (or have
168 // cookies set by) network requests.
169 // Only might return false if the very experimental
170 // --enable-strict-site-isolation or --site-per-process flags are used.
171 bool CanSendCookiesForOrigin(int child_id, const GURL& gurl);
173 // Sets the process as only permitted to use and see the cookies for the
175 // Only used if the very experimental --enable-strict-site-isolation or
176 // --site-per-process flags are used.
177 void LockToOrigin(int child_id, const GURL& gurl);
179 // Register FileSystem type and permission policy which should be used
180 // for the type. The |policy| must be a bitwise-or'd value of
181 // fileapi::FilePermissionPolicy.
182 void RegisterFileSystemPermissionPolicy(
183 fileapi::FileSystemType type,
186 // Returns true if sending system exclusive messages is allowed.
187 bool CanSendMidiSysExMessage(int child_id);
190 friend class ChildProcessSecurityPolicyInProcessBrowserTest;
191 friend class ChildProcessSecurityPolicyTest;
192 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest,
194 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest, FilePermissions);
198 typedef std::set<std::string> SchemeSet;
199 typedef std::map<int, SecurityState*> SecurityStateMap;
200 typedef std::map<int, int> WorkerToMainProcessMap;
201 typedef std::map<fileapi::FileSystemType, int> FileSystemPermissionPolicyMap;
203 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
204 ChildProcessSecurityPolicyImpl();
205 friend struct DefaultSingletonTraits<ChildProcessSecurityPolicyImpl>;
207 // Adds child process during registration.
208 void AddChild(int child_id);
210 // Determines if certain permissions were granted for a file to given child
211 // process. |permissions| is an internally defined bit-set.
212 bool ChildProcessHasPermissionsForFile(int child_id,
213 const base::FilePath& file,
216 // Grant a particular permission set for a file. |permissions| is an
217 // internally defined bit-set.
218 void GrantPermissionsForFile(int child_id,
219 const base::FilePath& file,
222 // Grants access permission to the given isolated file system
223 // identified by |filesystem_id|. See comments for
224 // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
225 void GrantPermissionsForFileSystem(
227 const std::string& filesystem_id,
230 // Determines if certain permissions were granted for a file. |permissions|
231 // is an internally defined bit-set. If |child_id| is a worker process,
232 // this returns true if either the worker process or its parent renderer
233 // has permissions for the file.
234 bool HasPermissionsForFile(int child_id,
235 const base::FilePath& file,
238 // Determines if certain permissions were granted for a file in FileSystem
239 // API. |permissions| is an internally defined bit-set.
240 bool HasPermissionsForFileSystemFile(int child_id,
241 const fileapi::FileSystemURL& url,
244 // Determines if certain permissions were granted for a file system.
245 // |permissions| is an internally defined bit-set.
246 bool HasPermissionsForFileSystem(
248 const std::string& filesystem_id,
251 // You must acquire this lock before reading or writing any members of this
252 // class. You must not block while holding this lock.
255 // These schemes are white-listed for all child processes. This set is
256 // protected by |lock_|.
257 SchemeSet web_safe_schemes_;
259 // These schemes do not actually represent retrievable URLs. For example,
260 // the the URLs in the "about" scheme are aliases to other URLs. This set is
261 // protected by |lock_|.
262 SchemeSet pseudo_schemes_;
264 // This map holds a SecurityState for each child process. The key for the
265 // map is the ID of the ChildProcessHost. The SecurityState objects are
266 // owned by this object and are protected by |lock_|. References to them must
267 // not escape this class.
268 SecurityStateMap security_state_;
270 // This maps keeps the record of which js worker thread child process
271 // corresponds to which main js thread child process.
272 WorkerToMainProcessMap worker_map_;
274 FileSystemPermissionPolicyMap file_system_policy_map_;
276 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl);
279 } // namespace content
281 #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_