blob: 032978f53820da06baee1cb08c281029caf2d811 [file] [log] [blame]
[email protected]cbe04ef2011-01-11 00:13:241// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]46fb9442011-12-09 17:57:475#include <set>
initial.commit09911bf2008-07-26 23:55:296#include <string>
7
8#include "base/basictypes.h"
[email protected]561abe62009-04-06 18:08:349#include "base/file_path.h"
[email protected]e54edc32010-09-28 01:09:1910#include "base/platform_file.h"
[email protected]df8e899b2011-02-22 22:58:2211#include "content/browser/child_process_security_policy.h"
[email protected]46fb9442011-12-09 17:57:4712#include "content/browser/mock_content_browser_client.h"
[email protected]a30f7d32011-05-24 19:38:3113#include "content/common/test_url_constants.h"
[email protected]a1d29162011-10-14 17:14:0314#include "content/public/common/url_constants.h"
[email protected]46fb9442011-12-09 17:57:4715#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2916#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]46fb9442011-12-09 17:57:4718namespace {
19
20const int kRendererID = 42;
21const int kWorkerRendererID = kRendererID + 1;
22
23class ChildProcessSecurityPolicyTestBrowserClient
24 : public content::MockContentBrowserClient {
25 public:
26 ChildProcessSecurityPolicyTestBrowserClient() {}
27
28 virtual bool IsHandledURL(const GURL& url) {
29 return schemes_.find(url.scheme()) != schemes_.end();
[email protected]e3539402011-07-19 09:31:0830 }
31
[email protected]46fb9442011-12-09 17:57:4732 void ClearSchemes() {
33 schemes_.clear();
initial.commit09911bf2008-07-26 23:55:2934 }
[email protected]46fb9442011-12-09 17:57:4735
36 void AddScheme(const std::string& scheme) {
37 schemes_.insert(scheme);
38 }
39
40 private:
41 std::set<std::string> schemes_;
initial.commit09911bf2008-07-26 23:55:2942};
43
[email protected]46fb9442011-12-09 17:57:4744} // namespace
45
46class ChildProcessSecurityPolicyTest : public testing::Test {
47 public:
48 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) {
49 }
50
51 virtual void SetUp() {
52 old_browser_client_ = content::GetContentClient()->browser();
53 content::GetContentClient()->set_browser(&test_browser_client_);
54
55 // Claim to always handle chrome:// URLs because the CPSP's notion of
56 // allowing WebUI bindings is hard-wired to this particular scheme.
57 test_browser_client_.AddScheme("chrome");
58 }
59
60 virtual void TearDown() {
61 test_browser_client_.ClearSchemes();
62 content::GetContentClient()->set_browser(old_browser_client_);
63 }
64
65 protected:
66 void RegisterTestScheme(const std::string& scheme) {
67 test_browser_client_.AddScheme(scheme);
68 }
69
70 private:
71 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
72 content::ContentBrowserClient* old_browser_client_;
73};
initial.commit09911bf2008-07-26 23:55:2974
[email protected]f58ddcf2009-05-18 22:22:0675TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
76 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:2977
[email protected]e0d481582009-09-15 21:06:2578 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme));
79 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme));
80 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme));
81 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme));
initial.commit09911bf2008-07-26 23:55:2982 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]039c7b0b22011-03-04 23:15:4283 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme));
84 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:2985
86 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
87 p->RegisterWebSafeScheme("registered-web-safe-scheme");
88 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:0389
90 EXPECT_FALSE(p->IsWebSafeScheme(chrome::kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:2991}
92
[email protected]f58ddcf2009-05-18 22:22:0693TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
94 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:2995
[email protected]e0d481582009-09-15 21:06:2596 EXPECT_TRUE(p->IsPseudoScheme(chrome::kAboutScheme));
97 EXPECT_TRUE(p->IsPseudoScheme(chrome::kJavaScriptScheme));
98 EXPECT_TRUE(p->IsPseudoScheme(chrome::kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:2999
[email protected]419a0572011-04-18 22:21:46100 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
101 p->RegisterPseudoScheme("registered-pseudo-scheme");
102 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03103
104 EXPECT_FALSE(p->IsPseudoScheme(chrome::kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46105}
106
107TEST_F(ChildProcessSecurityPolicyTest, IsDisabledSchemeTest) {
108 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
109
110 EXPECT_FALSE(p->IsDisabledScheme("evil-scheme"));
111 std::set<std::string> disabled_set;
112 disabled_set.insert("evil-scheme");
113 p->RegisterDisabledSchemes(disabled_set);
114 EXPECT_TRUE(p->IsDisabledScheme("evil-scheme"));
115 EXPECT_FALSE(p->IsDisabledScheme("good-scheme"));
116
117 disabled_set.clear();
118 p->RegisterDisabledSchemes(disabled_set);
119 EXPECT_FALSE(p->IsDisabledScheme("evil-scheme"));
120 EXPECT_FALSE(p->IsDisabledScheme("good-scheme"));
initial.commit09911bf2008-07-26 23:55:29121}
122
[email protected]f58ddcf2009-05-18 22:22:06123TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
124 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29125
126 p->Add(kRendererID);
127
128 // Safe
129 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
130 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
131 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
132 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
133 EXPECT_TRUE(p->CanRequestURL(kRendererID,
134 GURL("view-source:http://www.google.com/")));
[email protected]039c7b0b22011-03-04 23:15:42135 EXPECT_TRUE(p->CanRequestURL(
136 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29137
138 // Dangerous
139 EXPECT_FALSE(p->CanRequestURL(kRendererID,
140 GURL("file:///etc/passwd")));
141 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16142 GURL("chrome://foo/bar")));
initial.commit09911bf2008-07-26 23:55:29143
144 p->Remove(kRendererID);
145}
146
[email protected]f58ddcf2009-05-18 22:22:06147TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
148 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29149
150 p->Add(kRendererID);
151
152 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
153 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
154 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
155 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
156
[email protected]ed3456f2009-02-26 20:24:48157 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory")));
158 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
159 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
160 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
initial.commit09911bf2008-07-26 23:55:29161
162 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory")));
163 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
164 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
165
[email protected]89f550b2011-06-08 18:34:03166 // These requests for about: pages should be denied.
[email protected]b3adbd02011-11-30 22:23:27167 p->GrantRequestURL(kRendererID, GURL(chrome::kTestGpuCleanURL));
168 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL(chrome::kTestGpuCleanURL)));
initial.commit09911bf2008-07-26 23:55:29169
[email protected]e0d481582009-09-15 21:06:25170 p->GrantRequestURL(kRendererID, GURL(chrome::kAboutCrashURL));
171 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL(chrome::kAboutCrashURL)));
initial.commit09911bf2008-07-26 23:55:29172
[email protected]a30f7d32011-05-24 19:38:31173 p->GrantRequestURL(kRendererID, GURL(chrome::kTestCacheURL));
174 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL(chrome::kTestCacheURL)));
initial.commit09911bf2008-07-26 23:55:29175
[email protected]a30f7d32011-05-24 19:38:31176 p->GrantRequestURL(kRendererID, GURL(chrome::kTestHangURL));
177 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL(chrome::kTestHangURL)));
initial.commit09911bf2008-07-26 23:55:29178
[email protected]89f550b2011-06-08 18:34:03179 // These requests for chrome:// pages should be granted.
180 p->GrantRequestURL(kRendererID, GURL(chrome::kTestNewTabURL));
181 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestNewTabURL)));
182
183 p->GrantRequestURL(kRendererID, GURL(chrome::kTestHistoryURL));
184 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestHistoryURL)));
185
186 p->GrantRequestURL(kRendererID, GURL(chrome::kTestBookmarksURL));
187 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestBookmarksURL)));
188
initial.commit09911bf2008-07-26 23:55:29189 p->Remove(kRendererID);
190}
191
[email protected]f58ddcf2009-05-18 22:22:06192TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
193 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29194
195 p->Add(kRendererID);
196
197 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
198 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
199 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
200
201 p->Remove(kRendererID);
202}
203
[email protected]f58ddcf2009-05-18 22:22:06204TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
205 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29206
207 p->Add(kRendererID);
208
209 // Currently, "asdf" is destined for ShellExecute, so it is allowed.
210 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
211
[email protected]46fb9442011-12-09 17:57:47212 // Once we register "asdf", we default to deny.
213 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29214 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
215
216 // We can allow new schemes by adding them to the whitelist.
217 p->RegisterWebSafeScheme("asdf");
218 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
219
220 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29221 p->Remove(kRendererID);
222}
223
[email protected]f58ddcf2009-05-18 22:22:06224TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
225 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29226
227 p->Add(kRendererID);
228
229 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
230 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
231 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
232
[email protected]419a0572011-04-18 22:21:46233 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
234 std::set<std::string> disabled_set;
235 disabled_set.insert("evil-scheme");
236 p->RegisterDisabledSchemes(disabled_set);
237 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com")));
238 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
239 disabled_set.clear();
240 p->RegisterDisabledSchemes(disabled_set);
241 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com")));
242 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
243
initial.commit09911bf2008-07-26 23:55:29244 // We should forget our state if we repeat a renderer id.
245 p->Remove(kRendererID);
246 p->Add(kRendererID);
247 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
248 p->Remove(kRendererID);
249}
250
[email protected]f58ddcf2009-05-18 22:22:06251TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
252 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29253
254 p->Add(kRendererID);
255
256 // View source is determined by the embedded scheme.
257 EXPECT_TRUE(p->CanRequestURL(kRendererID,
258 GURL("view-source:http://www.google.com/")));
259 EXPECT_FALSE(p->CanRequestURL(kRendererID,
260 GURL("view-source:file:///etc/passwd")));
261 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36262 EXPECT_FALSE(p->CanRequestURL(
263 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29264
265 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
266 // View source needs to be able to request the embedded scheme.
267 EXPECT_TRUE(p->CanRequestURL(kRendererID,
268 GURL("view-source:file:///etc/passwd")));
269 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
270
271 p->Remove(kRendererID);
272}
273
[email protected]e54edc32010-09-28 01:09:19274TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) {
[email protected]f58ddcf2009-05-18 22:22:06275 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29276
277 p->Add(kRendererID);
278
[email protected]e54edc32010-09-28 01:09:19279 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34280 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19281 p->GrantReadFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/passwd")));
282 EXPECT_TRUE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34283 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19284 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34285 FilePath(FILE_PATH_LITERAL("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29286
287 p->Remove(kRendererID);
288 p->Add(kRendererID);
289
[email protected]e54edc32010-09-28 01:09:19290 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34291 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19292 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34293 FilePath(FILE_PATH_LITERAL("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29294
295 p->Remove(kRendererID);
296}
297
[email protected]600ea402011-04-12 00:01:51298TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) {
299 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
300
301 p->Add(kRendererID);
302
303 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
304 FilePath(FILE_PATH_LITERAL("/etc/"))));
305 p->GrantReadDirectory(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")));
306 EXPECT_TRUE(p->CanReadDirectory(kRendererID,
307 FilePath(FILE_PATH_LITERAL("/etc/"))));
308 EXPECT_TRUE(p->CanReadFile(kRendererID,
309 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
310
311 p->Remove(kRendererID);
312 p->Add(kRendererID);
313
314 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
315 FilePath(FILE_PATH_LITERAL("/etc/"))));
316 EXPECT_FALSE(p->CanReadFile(kRendererID,
317 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
318
319 // Just granting read permission as a file doesn't imply reading as a
320 // directory.
321 p->GrantReadFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")));
322 EXPECT_TRUE(p->CanReadFile(kRendererID,
323 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
324 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
325 FilePath(FILE_PATH_LITERAL("/etc/"))));
326
327 p->Remove(kRendererID);
328}
329
[email protected]e54edc32010-09-28 01:09:19330TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
331 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
332
333 // Grant permissions for a file.
334 p->Add(kRendererID);
335 FilePath file = FilePath(FILE_PATH_LITERAL("/etc/passwd"));
336 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
337 base::PLATFORM_FILE_OPEN));
338
339 p->GrantPermissionsForFile(kRendererID, file,
340 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04341 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19342 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04343 base::PLATFORM_FILE_WRITE);
[email protected]e54edc32010-09-28 01:09:19344 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
345 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04346 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19347 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04348 base::PLATFORM_FILE_WRITE));
[email protected]e54edc32010-09-28 01:09:19349 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
350 base::PLATFORM_FILE_OPEN |
351 base::PLATFORM_FILE_READ));
352 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
353 base::PLATFORM_FILE_CREATE));
354 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
355 base::PLATFORM_FILE_CREATE |
[email protected]b2f2308d2011-05-23 22:00:04356 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19357 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04358 base::PLATFORM_FILE_WRITE));
[email protected]e54edc32010-09-28 01:09:19359 p->Remove(kRendererID);
360
361 // Grant permissions for the directory the file is in.
362 p->Add(kRendererID);
363 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
364 base::PLATFORM_FILE_OPEN));
365 p->GrantPermissionsForFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc")),
366 base::PLATFORM_FILE_OPEN |
367 base::PLATFORM_FILE_READ);
368 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
369 base::PLATFORM_FILE_OPEN));
370 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
371 base::PLATFORM_FILE_READ |
372 base::PLATFORM_FILE_WRITE));
373 p->Remove(kRendererID);
374
375 // Grant permissions for the directory the file is in (with trailing '/').
376 p->Add(kRendererID);
377 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
378 base::PLATFORM_FILE_OPEN));
379 p->GrantPermissionsForFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")),
380 base::PLATFORM_FILE_OPEN |
381 base::PLATFORM_FILE_READ);
382 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
383 base::PLATFORM_FILE_OPEN));
384 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
385 base::PLATFORM_FILE_READ |
386 base::PLATFORM_FILE_WRITE));
387
388 // Grant permissions for the file (should overwrite the permissions granted
389 // for the directory).
390 p->GrantPermissionsForFile(kRendererID, file, base::PLATFORM_FILE_TEMPORARY);
391 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
392 base::PLATFORM_FILE_OPEN));
393 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
394 base::PLATFORM_FILE_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34395
396 // Revoke all permissions for the file (it should inherit its permissions
397 // from the directory again).
398 p->RevokeAllPermissionsForFile(kRendererID, file);
399 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
400 base::PLATFORM_FILE_OPEN |
401 base::PLATFORM_FILE_READ));
402 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
403 base::PLATFORM_FILE_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19404 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07405
406 // Grant file permissions for the file to main thread renderer process,
407 // make sure its worker thread renderer process inherits those.
408 p->Add(kRendererID);
409 p->GrantPermissionsForFile(kRendererID, file, base::PLATFORM_FILE_OPEN |
410 base::PLATFORM_FILE_READ);
411 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
412 base::PLATFORM_FILE_OPEN |
413 base::PLATFORM_FILE_READ));
414 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
415 base::PLATFORM_FILE_WRITE));
416 p->AddWorker(kWorkerRendererID, kRendererID);
417 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, file,
418 base::PLATFORM_FILE_OPEN |
419 base::PLATFORM_FILE_READ));
420 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, file,
421 base::PLATFORM_FILE_WRITE));
422 p->Remove(kRendererID);
423 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, file,
424 base::PLATFORM_FILE_OPEN |
425 base::PLATFORM_FILE_READ));
426 p->Remove(kWorkerRendererID);
[email protected]e54edc32010-09-28 01:09:19427}
428
[email protected]c50008512011-02-03 01:17:27429TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]f58ddcf2009-05-18 22:22:06430 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29431
[email protected]60e448982009-05-06 04:21:16432 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29433
434 p->Add(kRendererID);
435
[email protected]c50008512011-02-03 01:17:27436 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29437 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27438 p->GrantWebUIBindings(kRendererID);
439 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29440 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
441
442 p->Remove(kRendererID);
443}
444
[email protected]f58ddcf2009-05-18 22:22:06445TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
446 ChildProcessSecurityPolicy* p = ChildProcessSecurityPolicy::GetInstance();
initial.commit09911bf2008-07-26 23:55:29447
448 GURL url("file:///etc/passwd");
[email protected]561abe62009-04-06 18:08:34449 FilePath file(FILE_PATH_LITERAL("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29450
451 p->Add(kRendererID);
452
453 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19454 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27455 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29456
457 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19458 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27459 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29460
461 p->Remove(kRendererID);
462
463 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55464 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
465 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29466
467 // In this case, we default to secure behavior.
468 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19469 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27470 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29471}