license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |
| 6 | #define CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <map> |
| 10 | #include <set> |
| 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/lock.h" |
| 14 | #include "base/singleton.h" |
[email protected] | 79dc62e3 | 2009-05-19 21:02:58 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest_prod.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 16 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 17 | class FilePath; |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 18 | class GURL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 20 | // The ChildProcessSecurityPolicy class is used to grant and revoke security |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | // capabilities for renderers. For example, it restricts whether a renderer |
| 22 | // is permmitted to loaded file:// URLs based on whether the renderer has ever |
| 23 | // been commanded to load file:// URLs by the browser. |
| 24 | // |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 25 | // ChildProcessSecurityPolicy is a singleton that may be used on any thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | // |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 27 | class ChildProcessSecurityPolicy { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | public: |
[email protected] | c11ffb46 | 2009-05-15 18:03:40 | [diff] [blame] | 29 | // Object can only be created through GetInstance() so the constructor is |
| 30 | // private. |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 31 | ~ChildProcessSecurityPolicy(); |
[email protected] | c11ffb46 | 2009-05-15 18:03:40 | [diff] [blame] | 32 | |
[email protected] | 58052263 | 2009-08-17 21:55:55 | [diff] [blame] | 33 | // There is one global ChildProcessSecurityPolicy object for the entire |
| 34 | // browser process. The object returned by this method may be accessed on |
| 35 | // any thread. |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 36 | static ChildProcessSecurityPolicy* GetInstance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | |
| 38 | // Web-safe schemes can be requested by any renderer. Once a web-safe scheme |
| 39 | // has been registered, any renderer processes can request URLs with that |
| 40 | // scheme. There is no mechanism for revoking web-safe schemes. |
| 41 | void RegisterWebSafeScheme(const std::string& scheme); |
| 42 | |
| 43 | // Returns true iff |scheme| has been registered as a web-safe scheme. |
| 44 | bool IsWebSafeScheme(const std::string& scheme); |
| 45 | |
| 46 | // Pseudo schemes are treated differently than other schemes because they |
| 47 | // cannot be requested like normal URLs. There is no mechanism for revoking |
| 48 | // pseudo schemes. |
| 49 | void RegisterPseudoScheme(const std::string& scheme); |
| 50 | |
| 51 | // Returns true iff |scheme| has been registered as pseudo scheme. |
| 52 | bool IsPseudoScheme(const std::string& scheme); |
| 53 | |
| 54 | // Upon creation, render processes should register themselves by calling this |
| 55 | // this method exactly once. |
| 56 | void Add(int renderer_id); |
| 57 | |
| 58 | // Upon destruction, render processess should unregister themselves by caling |
| 59 | // this method exactly once. |
| 60 | void Remove(int renderer_id); |
| 61 | |
| 62 | // Whenever the browser processes commands the renderer to request a URL, it |
| 63 | // should call this method to grant the renderer process the capability to |
| 64 | // request the URL. |
| 65 | void GrantRequestURL(int renderer_id, const GURL& url); |
| 66 | |
| 67 | // Whenever the user picks a file from a <input type="file"> element, the |
| 68 | // browser should call this function to grant the renderer the capability to |
| 69 | // upload the file to the web. |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 70 | void GrantUploadFile(int renderer_id, const FilePath& file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | |
| 72 | // Whenever the browser processes commands the renderer to run web inspector, |
| 73 | // it should call this method to grant the renderer process the capability to |
| 74 | // run the inspector. |
| 75 | void GrantInspectElement(int renderer_id); |
| 76 | |
| 77 | // Grant this renderer the ability to use DOM UI Bindings. |
| 78 | void GrantDOMUIBindings(int renderer_id); |
| 79 | |
[email protected] | 1adff06 | 2009-06-02 18:39:55 | [diff] [blame] | 80 | // Grant this renderer the ability to use extension Bindings. |
| 81 | void GrantExtensionBindings(int renderer_id); |
| 82 | |
[email protected] | 971713e | 2009-10-29 16:07:21 | [diff] [blame] | 83 | // Grant this renderer the ability to read raw cookies. |
| 84 | void GrantReadRawCookies(int renderer_id); |
| 85 | |
| 86 | // Revoke read raw cookies permission. |
| 87 | void RevokeReadRawCookies(int renderer_id); |
| 88 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | // Before servicing a renderer's request for a URL, the browser should call |
| 90 | // this method to determine whether the renderer has the capability to |
| 91 | // request the URL. |
| 92 | bool CanRequestURL(int renderer_id, const GURL& url); |
| 93 | |
| 94 | // Before servicing a renderer's request to upload a file to the web, the |
| 95 | // browser should call this method to determine whether the renderer has the |
| 96 | // capability to upload the requested file. |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 97 | bool CanUploadFile(int renderer_id, const FilePath& file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
[email protected] | 971713e | 2009-10-29 16:07:21 | [diff] [blame] | 99 | // Returns true if the specified renderer_id has been granted DOMUIBindings. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | // The browser should check this property before assuming the renderer is |
| 101 | // allowed to use DOMUIBindings. |
| 102 | bool HasDOMUIBindings(int renderer_id); |
| 103 | |
[email protected] | b7c2f25 | 2009-12-08 00:47:23 | [diff] [blame] | 104 | // Returns true if the specified renderer_id has been granted DOMUIBindings. |
| 105 | // The browser should check this property before assuming the renderer is |
| 106 | // allowed to use extension bindings. |
[email protected] | 1adff06 | 2009-06-02 18:39:55 | [diff] [blame] | 107 | bool HasExtensionBindings(int renderer_id); |
| 108 | |
[email protected] | 971713e | 2009-10-29 16:07:21 | [diff] [blame] | 109 | // Returns true if the specified renderer_id has been granted ReadRawCookies. |
| 110 | bool CanReadRawCookies(int renderer_id); |
| 111 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | private: |
[email protected] | 79dc62e3 | 2009-05-19 21:02:58 | [diff] [blame] | 113 | friend class ChildProcessSecurityPolicyInProcessBrowserTest; |
| 114 | FRIEND_TEST(ChildProcessSecurityPolicyInProcessBrowserTest, NoLeak); |
| 115 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | class SecurityState; |
| 117 | |
| 118 | typedef std::set<std::string> SchemeSet; |
| 119 | typedef std::map<int, SecurityState*> SecurityStateMap; |
| 120 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 121 | // Obtain an instance of ChildProcessSecurityPolicy via GetInstance(). |
| 122 | ChildProcessSecurityPolicy(); |
| 123 | friend struct DefaultSingletonTraits<ChildProcessSecurityPolicy>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | |
| 125 | // You must acquire this lock before reading or writing any members of this |
| 126 | // class. You must not block while holding this lock. |
| 127 | Lock lock_; |
| 128 | |
| 129 | // These schemes are white-listed for all renderers. This set is protected |
| 130 | // by |lock_|. |
| 131 | SchemeSet web_safe_schemes_; |
| 132 | |
| 133 | // These schemes do not actually represent retrievable URLs. For example, |
| 134 | // the the URLs in the "about" scheme are aliases to other URLs. This set is |
| 135 | // protected by |lock_|. |
| 136 | SchemeSet pseudo_schemes_; |
| 137 | |
| 138 | // This map holds a SecurityState for each renderer process. The key for the |
| 139 | // map is the ID of the RenderProcessHost. The SecurityState objects are |
| 140 | // owned by this object and are protected by |lock_|. References to them must |
| 141 | // not escape this class. |
| 142 | SecurityStateMap security_state_; |
| 143 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(ChildProcessSecurityPolicy); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | }; |
| 146 | |
[email protected] | f58ddcf | 2009-05-18 22:22:06 | [diff] [blame] | 147 | #endif // CHROME_BROWSER_CHILD_PROCESS_SECURITY_POLICY_H_ |