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/glue/resource_type.h"
25 class CONTENT_EXPORT ChildProcessSecurityPolicyImpl
26 : NON_EXPORTED_BASE(public ChildProcessSecurityPolicy
) {
28 // Object can only be created through GetInstance() so the constructor is
30 virtual ~ChildProcessSecurityPolicyImpl();
32 static ChildProcessSecurityPolicyImpl
* GetInstance();
34 // ChildProcessSecurityPolicy implementation.
35 virtual void RegisterWebSafeScheme(const std::string
& scheme
) OVERRIDE
;
36 virtual bool IsWebSafeScheme(const std::string
& scheme
) OVERRIDE
;
37 virtual void RegisterDisabledSchemes(const std::set
<std::string
>& schemes
)
39 virtual void GrantPermissionsForFile(int child_id
,
41 int permissions
) OVERRIDE
;
42 virtual void GrantReadFile(int child_id
, const FilePath
& file
) OVERRIDE
;
43 virtual void GrantReadFileSystem(
45 const std::string
& filesystem_id
) OVERRIDE
;
46 virtual void GrantWriteFileSystem(
48 const std::string
& filesystem_id
) OVERRIDE
;
49 virtual void GrantCreateFileForFileSystem(
51 const std::string
& filesystem_id
) OVERRIDE
;
52 virtual void GrantScheme(int child_id
, const std::string
& scheme
) OVERRIDE
;
53 virtual bool CanReadFile(int child_id
, const FilePath
& file
) OVERRIDE
;
54 virtual bool CanReadFileSystem(int child_id
,
55 const std::string
& filesystem_id
) OVERRIDE
;
56 virtual bool CanReadWriteFileSystem(
58 const std::string
& filesystem_id
) OVERRIDE
;
60 // Pseudo schemes are treated differently than other schemes because they
61 // cannot be requested like normal URLs. There is no mechanism for revoking
63 void RegisterPseudoScheme(const std::string
& scheme
);
65 // Returns true iff |scheme| has been registered as pseudo scheme.
66 bool IsPseudoScheme(const std::string
& scheme
);
68 // Returns true iff |scheme| is listed as a disabled scheme.
69 bool IsDisabledScheme(const std::string
& scheme
);
71 // Upon creation, child processes should register themselves by calling this
72 // this method exactly once.
73 void Add(int child_id
);
75 // Upon creation, worker thread child processes should register themselves by
76 // calling this this method exactly once. Workers that are not shared will
77 // inherit permissions from their parent renderer process identified with
78 // |main_render_process_id|.
79 void AddWorker(int worker_child_id
, int main_render_process_id
);
81 // Upon destruction, child processess should unregister themselves by caling
82 // this method exactly once.
83 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
89 void GrantRequestURL(int child_id
, const GURL
& url
);
91 // Whenever the browser process drops a file icon on a tab, it should call
92 // this method to grant the child process the capability to request this one
93 // file:// URL, but not all urls of the file:// scheme.
94 void GrantRequestSpecificFileURL(int child_id
, const GURL
& url
);
96 // Grants the child process permission to enumerate all the files in
97 // this directory and read those files.
98 void GrantReadDirectory(int child_id
, const FilePath
& directory
);
100 // Revokes all permissions granted to the given file.
101 void RevokeAllPermissionsForFile(int child_id
, const FilePath
& file
);
103 // Grant the child process the ability to use Web UI Bindings.
104 void GrantWebUIBindings(int child_id
);
106 // Grant the child process the ability to read raw cookies.
107 void GrantReadRawCookies(int child_id
);
109 // Revoke read raw cookies permission.
110 void RevokeReadRawCookies(int child_id
);
112 // Before servicing a child process's request for a URL, the browser should
113 // call this method to determine whether the process has the capability to
115 bool CanRequestURL(int child_id
, const GURL
& url
);
117 // Returns true if the process is permitted to load pages from
118 // the given origin in main frames or subframes.
119 // Only might return false if --site-per-process flag is used.
120 bool CanLoadPage(int child_id
,
122 ResourceType::Type resource_type
);
124 // Before servicing a child process's request to enumerate a directory
125 // the browser should call this method to check for the capability.
126 bool CanReadDirectory(int child_id
, const FilePath
& directory
);
128 // Determines if certain permissions were granted for a file. |permissions|
129 // must be a bit-set of base::PlatformFileFlags.
130 bool HasPermissionsForFile(int child_id
,
131 const FilePath
& file
,
134 // Returns true if the specified child_id has been granted WebUIBindings.
135 // The browser should check this property before assuming the child process is
136 // allowed to use WebUIBindings.
137 bool HasWebUIBindings(int child_id
);
139 // Returns true if the specified child_id has been granted ReadRawCookies.
140 bool CanReadRawCookies(int child_id
);
142 // Returns true if the process is permitted to read and modify the cookies for
143 // the given origin. Does not affect cookies attached to or set by network
145 // Only might return false if the very experimental
146 // --enable-strict-site-isolation or --site-per-process flags are used.
147 bool CanAccessCookiesForOrigin(int child_id
, const GURL
& gurl
);
149 // Returns true if the process is permitted to attach cookies to (or have
150 // cookies set by) network requests.
151 // Only might return false if the very experimental
152 // --enable-strict-site-isolation or --site-per-process flags are used.
153 bool CanSendCookiesForOrigin(int child_id
, const GURL
& gurl
);
155 // Sets the process as only permitted to use and see the cookies for the
157 // Only used if the very experimental --enable-strict-site-isolation or
158 // --site-per-process flags are used.
159 void LockToOrigin(int child_id
, const GURL
& gurl
);
161 // Grants access permission to the given isolated file system
162 // identified by |filesystem_id|. See comments for
163 // ChildProcessSecurityPolicy::GrantReadFileSystem() for more details.
164 void GrantPermissionsForFileSystem(
166 const std::string
& filesystem_id
,
169 // Determines if certain permissions were granted for a file fystem.
170 // |permissions| must be a bit-set of base::PlatformFileFlags.
171 bool HasPermissionsForFileSystem(
173 const std::string
& filesystem_id
,
177 friend class ChildProcessSecurityPolicyInProcessBrowserTest
;
178 FRIEND_TEST_ALL_PREFIXES(ChildProcessSecurityPolicyInProcessBrowserTest
,
183 typedef std::set
<std::string
> SchemeSet
;
184 typedef std::map
<int, SecurityState
*> SecurityStateMap
;
185 typedef std::map
<int, int> WorkerToMainProcessMap
;
187 // Obtain an instance of ChildProcessSecurityPolicyImpl via GetInstance().
188 ChildProcessSecurityPolicyImpl();
189 friend struct DefaultSingletonTraits
<ChildProcessSecurityPolicyImpl
>;
191 // Adds child process during registration.
192 void AddChild(int child_id
);
194 // Determines if certain permissions were granted for a file to given child
195 // process. |permissions| must be a bit-set of base::PlatformFileFlags.
196 bool ChildProcessHasPermissionsForFile(int child_id
,
197 const FilePath
& file
,
200 // You must acquire this lock before reading or writing any members of this
201 // class. You must not block while holding this lock.
204 // These schemes are white-listed for all child processes. This set is
205 // protected by |lock_|.
206 SchemeSet web_safe_schemes_
;
208 // These schemes do not actually represent retrievable URLs. For example,
209 // the the URLs in the "about" scheme are aliases to other URLs. This set is
210 // protected by |lock_|.
211 SchemeSet pseudo_schemes_
;
213 // These schemes are disabled by policy, and child processes are always
214 // denied permission to request them. This overrides |web_safe_schemes_|.
215 // This set is protected by |lock_|.
216 SchemeSet disabled_schemes_
;
218 // This map holds a SecurityState for each child process. The key for the
219 // map is the ID of the ChildProcessHost. The SecurityState objects are
220 // owned by this object and are protected by |lock_|. References to them must
221 // not escape this class.
222 SecurityStateMap security_state_
;
224 // This maps keeps the record of which js worker thread child process
225 // corresponds to which main js thread child process.
226 WorkerToMainProcessMap worker_map_
;
228 DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicyImpl
);
231 } // namespace content
233 #endif // CONTENT_BROWSER_CHILD_PROCESS_SECURITY_POLICY_IMPL_H_