blob: f91af9eb7df58d80f9a4eeed7e46015bb8ce4ff7 [file] [log] [blame]
initial.commit09911bf2008-07-26 23:55:291// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef CHROME_BROWSER_RENDERER_SECURITY_POLICY_H__
31#define CHROME_BROWSER_RENDERER_SECURITY_POLICY_H__
32
33#include <string>
34#include <map>
35#include <set>
36
37#include "base/basictypes.h"
38#include "base/lock.h"
39#include "base/singleton.h"
[email protected]46072d42008-07-28 14:49:3540
41class GURL;
initial.commit09911bf2008-07-26 23:55:2942
43// The RendererSecurityPolicy class is used to grant and revoke security
44// capabilities for renderers. For example, it restricts whether a renderer
45// is permmitted to loaded file:// URLs based on whether the renderer has ever
46// been commanded to load file:// URLs by the browser.
47//
48// RendererSecurityPolicy is a singleton that may be used on any thread.
49//
50class RendererSecurityPolicy {
51 public:
52 // There is one global RendererSecurityPolicy object for the entire browser
53 // processes. The object returned by this method may be accessed on any
54 // thread.
55 static RendererSecurityPolicy* GetInstance();
56
57 // Web-safe schemes can be requested by any renderer. Once a web-safe scheme
58 // has been registered, any renderer processes can request URLs with that
59 // scheme. There is no mechanism for revoking web-safe schemes.
60 void RegisterWebSafeScheme(const std::string& scheme);
61
62 // Returns true iff |scheme| has been registered as a web-safe scheme.
63 bool IsWebSafeScheme(const std::string& scheme);
64
65 // Pseudo schemes are treated differently than other schemes because they
66 // cannot be requested like normal URLs. There is no mechanism for revoking
67 // pseudo schemes.
68 void RegisterPseudoScheme(const std::string& scheme);
69
70 // Returns true iff |scheme| has been registered as pseudo scheme.
71 bool IsPseudoScheme(const std::string& scheme);
72
73 // Upon creation, render processes should register themselves by calling this
74 // this method exactly once.
75 void Add(int renderer_id);
76
77 // Upon destruction, render processess should unregister themselves by caling
78 // this method exactly once.
79 void Remove(int renderer_id);
80
81 // Whenever the browser processes commands the renderer to request a URL, it
82 // should call this method to grant the renderer process the capability to
83 // request the URL.
84 void GrantRequestURL(int renderer_id, const GURL& url);
85
86 // Whenever the user picks a file from a <input type="file"> element, the
87 // browser should call this function to grant the renderer the capability to
88 // upload the file to the web.
89 void GrantUploadFile(int renderer_id, const std::wstring& file);
90
91 // Whenever the browser processes commands the renderer to run web inspector,
92 // it should call this method to grant the renderer process the capability to
93 // run the inspector.
94 void GrantInspectElement(int renderer_id);
95
96 // Grant this renderer the ability to use DOM UI Bindings.
97 void GrantDOMUIBindings(int renderer_id);
98
99 // Before servicing a renderer's request for a URL, the browser should call
100 // this method to determine whether the renderer has the capability to
101 // request the URL.
102 bool CanRequestURL(int renderer_id, const GURL& url);
103
104 // Before servicing a renderer's request to upload a file to the web, the
105 // browser should call this method to determine whether the renderer has the
106 // capability to upload the requested file.
107 bool CanUploadFile(int renderer_id, const std::wstring& file);
108
109 // Returns true of the specified renderer_id has been granted DOMUIBindings.
110 // The browser should check this property before assuming the renderer is
111 // allowed to use DOMUIBindings.
112 bool HasDOMUIBindings(int renderer_id);
113
114 private:
115 class SecurityState;
116
117 typedef std::set<std::string> SchemeSet;
118 typedef std::map<int, SecurityState*> SecurityStateMap;
119
120 // Obtain an instance of RendererSecurityPolicy via GetInstance().
121 RendererSecurityPolicy();
122 friend DefaultSingletonTraits<RendererSecurityPolicy>;
123
124 // You must acquire this lock before reading or writing any members of this
125 // class. You must not block while holding this lock.
126 Lock lock_;
127
128 // These schemes are white-listed for all renderers. This set is protected
129 // by |lock_|.
130 SchemeSet web_safe_schemes_;
131
132 // These schemes do not actually represent retrievable URLs. For example,
133 // the the URLs in the "about" scheme are aliases to other URLs. This set is
134 // protected by |lock_|.
135 SchemeSet pseudo_schemes_;
136
137 // This map holds a SecurityState for each renderer process. The key for the
138 // map is the ID of the RenderProcessHost. The SecurityState objects are
139 // owned by this object and are protected by |lock_|. References to them must
140 // not escape this class.
141 SecurityStateMap security_state_;
142
143 DISALLOW_EVIL_CONSTRUCTORS(RendererSecurityPolicy);
144};
145
[email protected]46072d42008-07-28 14:49:35146#endif // CHROME_BROWSER_RENDERER_SECURITY_POLICY_H__