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 "content/public/common/resource_type.h"
19 #include "storage/common/fileapi/file_system_types.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 ~ChildProcessSecurityPolicyImpl() override
;
40 static ChildProcessSecurityPolicyImpl
* GetInstance();
42 // ChildProcessSecurityPolicy implementation.
43 void RegisterWebSafeScheme(const std::string
& scheme
) override
;
44 bool IsWebSafeScheme(const std::string
& scheme
) override
;
45 void GrantReadFile(int child_id
, const base::FilePath
& file
) override
;
46 void GrantCreateReadWriteFile(int child_id
,
47 const base::FilePath
& file
) override
;
48 void GrantCopyInto(int child_id
, const base::FilePath
& dir
) override
;
49 void GrantDeleteFrom(int child_id
, const base::FilePath
& dir
) override
;
50 void GrantReadFileSystem(int child_id
,
51 const std::string
& filesystem_id
) override
;
52 void GrantWriteFileSystem(int child_id
,
53 const std::string
& filesystem_id
) override
;
54 void GrantCreateFileForFileSystem(int child_id
,
55 const std::string
& filesystem_id
) override
;
56 void GrantCreateReadWriteFileSystem(
58 const std::string
& filesystem_id
) override
;
59 void GrantCopyIntoFileSystem(int child_id
,
60 const std::string
& filesystem_id
) override
;
61 void GrantDeleteFromFileSystem(int child_id
,
62 const std::string
& filesystem_id
) override
;
63 void GrantScheme(int child_id
, const std::string
& scheme
) override
;
64 bool CanReadFile(int child_id
, const base::FilePath
& file
) override
;
65 bool CanCreateReadWriteFile(int child_id
,
66 const base::FilePath
& file
) override
;
67 bool CanReadFileSystem(int child_id
,
68 const std::string
& filesystem_id
) override
;
69 bool CanReadWriteFileSystem(int child_id
,
70 const std::string
& filesystem_id
) override
;
71 bool CanCopyIntoFileSystem(int child_id
,
72 const std::string
& filesystem_id
) override
;
73 bool CanDeleteFromFileSystem(int child_id
,
74 const std::string
& filesystem_id
) override
;
75 bool HasWebUIBindings(int child_id
) override
;
76 void GrantSendMidiSysExMessage(int child_id
) override
;
77 bool CanAccessDataForOrigin(int child_id
, const GURL
& url
) override
;
79 // Pseudo schemes are treated differently than other schemes because they
80 // cannot be requested like normal URLs. There is no mechanism for revoking
82 void RegisterPseudoScheme(const std::string
& scheme
);
84 // Returns true iff |scheme| has been registered as pseudo scheme.
85 bool IsPseudoScheme(const std::string
& scheme
);
87 // Upon creation, child processes should register themselves by calling this
88 // this method exactly once.
89 void Add(int child_id
);
91 // Upon creation, worker thread child processes should register themselves by
92 // calling this this method exactly once. Workers that are not shared will
93 // inherit permissions from their parent renderer process identified with
94 // |main_render_process_id|.
95 void AddWorker(int worker_child_id
, int main_render_process_id
);
97 // Upon destruction, child processess should unregister themselves by caling
98 // this method exactly once.
99 void Remove(int child_id
);
101 // Whenever the browser processes commands the child process to request a URL,
102 // it should call this method to grant the child process the capability to
103 // request the URL, along with permission to request all URLs of the same
105 void GrantRequestURL(int child_id
, const GURL
& url
);
107 // Whenever the browser process drops a file icon on a tab, it should call
108 // this method to grant the child process the capability to request this one
109 // file:// URL, but not all urls of the file:// scheme.
110 void GrantRequestSpecificFileURL(int child_id
, const GURL
& url
);
112 // Revokes all permissions granted to the given file.
113 void RevokeAllPermissionsForFile(int child_id
, const base::FilePath
& file
);
115 // Grant the child process the ability to use Web UI Bindings.
116 void GrantWebUIBindings(int child_id
);
118 // Grant the child process the ability to read raw cookies.
119 void GrantReadRawCookies(int child_id
);
121 // Revoke read raw cookies permission.
122 void RevokeReadRawCookies(int child_id
);
124 // Before servicing a child process's request for a URL, the browser should
125 // call this method to determine whether the process has the capability to
127 bool CanRequestURL(int child_id
, const GURL
& url
);
129 // Explicit permissions checks for FileSystemURL specified files.
130 bool CanReadFileSystemFile(int child_id
, const storage::FileSystemURL
& url
);
131 bool CanWriteFileSystemFile(int child_id
, const storage::FileSystemURL
& url
);
132 bool CanCreateFileSystemFile(int child_id
, const storage::FileSystemURL
& url
);
133 bool CanCreateReadWriteFileSystemFile(int child_id
,
134 const storage::FileSystemURL
& url
);
135 bool CanCopyIntoFileSystemFile(int child_id
,
136 const storage::FileSystemURL
& url
);
137 bool CanDeleteFileSystemFile(int child_id
, const storage::FileSystemURL
& url
);
139 // Returns true if the specified child_id has been granted ReadRawCookies.
140 bool CanReadRawCookies(int child_id
);
142 // Sets the process as only permitted to use and see the cookies for the
144 // Origin lock is applied only if the --site-per-process flag is used.
145 void LockToOrigin(int child_id
, const GURL
& gurl
);
147 // Register FileSystem type and permission policy which should be used
148 // for the type. The |policy| must be a bitwise-or'd value of
149 // storage::FilePermissionPolicy.
150 void RegisterFileSystemPermissionPolicy(storage::FileSystemType type
,
153 // Returns true if sending system exclusive messages is allowed.
154 bool CanSendMidiSysExMessage(int child_id
);
157 friend class ChildProcessSecurityPolicyInProcessBrowserTest
;
158 friend class ChildProcessSecurityPolicyTest
;
159 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest
,
161 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyTest
, FilePermissions
);
165 typedef std::set
<std::string
> SchemeSet
;
166 typedef std::map
<int, SecurityState
*> SecurityStateMap
;
167 typedef std::map
<int, int> WorkerToMainProcessMap
;
168 typedef std::map
<storage::FileSystemType
, int> FileSystemPermissionPolicyMap
;
170 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
171 ChildProcessSecurityPolicyImpl();
172 friend struct DefaultSingletonTraits
<ChildProcessSecurityPolicyImpl
>;
174 // Adds child process during registration.
175 void AddChild(int child_id
);
177 // Determines if certain permissions were granted for a file to given child
178 // process. |permissions| is an internally defined bit-set.
179 bool ChildProcessHasPermissionsForFile(int child_id
,
180 const base::FilePath
& file
,
183 // Grant a particular permission set for a file. |permissions| is an
184 // internally defined bit-set.
185 void GrantPermissionsForFile(int child_id
,
186 const base::FilePath
& file
,
189 // Grants access permission to the given isolated file system
190 // identified by |filesystem_id|. See comments for
191 // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
192 void GrantPermissionsForFileSystem(
194 const std::string
& filesystem_id
,
197 // Determines if certain permissions were granted for a file. |permissions|
198 // is an internally defined bit-set. If |child_id| is a worker process,
199 // this returns true if either the worker process or its parent renderer
200 // has permissions for the file.
201 bool HasPermissionsForFile(int child_id
,
202 const base::FilePath
& file
,
205 // Determines if certain permissions were granted for a file in FileSystem
206 // API. |permissions| is an internally defined bit-set.
207 bool HasPermissionsForFileSystemFile(int child_id
,
208 const storage::FileSystemURL
& url
,
211 // Determines if certain permissions were granted for a file system.
212 // |permissions| is an internally defined bit-set.
213 bool HasPermissionsForFileSystem(
215 const std::string
& filesystem_id
,
218 // You must acquire this lock before reading or writing any members of this
219 // class. You must not block while holding this lock.
222 // These schemes are white-listed for all child processes. This set is
223 // protected by |lock_|.
224 SchemeSet web_safe_schemes_
;
226 // These schemes do not actually represent retrievable URLs. For example,
227 // the the URLs in the "about" scheme are aliases to other URLs. This set is
228 // protected by |lock_|.
229 SchemeSet pseudo_schemes_
;
231 // This map holds a SecurityState for each child process. The key for the
232 // map is the ID of the ChildProcessHost. The SecurityState objects are
233 // owned by this object and are protected by |lock_|. References to them must
234 // not escape this class.
235 SecurityStateMap security_state_
;
237 // This maps keeps the record of which js worker thread child process
238 // corresponds to which main js thread child process.
239 WorkerToMainProcessMap worker_map_
;
241 FileSystemPermissionPolicyMap file_system_policy_map_
;
243 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl
);
246 } // namespace content
248 #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_