blob: eaacda8742e3d7d3a42427b644de861a35402904 [file] [log] [blame]
[email protected]b9535422012-02-09 01:47:591// Copyright (c) 2012 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]b9535422012-02-09 01:47:5911#include "content/browser/child_process_security_policy_impl.h"
[email protected]a30f7d32011-05-24 19:38:3112#include "content/common/test_url_constants.h"
[email protected]a1d29162011-10-14 17:14:0313#include "content/public/common/url_constants.h"
[email protected]c6681f32012-06-05 14:43:0114#include "content/test/test_content_browser_client.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
[email protected]c6681f32012-06-05 14:43:0124 : public content::TestContentBrowserClient {
[email protected]46fb9442011-12-09 17:57:4725 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();
[email protected]c6681f32012-06-05 14:43:0153 content::GetContentClient()->set_browser_for_testing(&test_browser_client_);
[email protected]46fb9442011-12-09 17:57:4754
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();
[email protected]c6681f32012-06-05 14:43:0162 content::GetContentClient()->set_browser_for_testing(old_browser_client_);
[email protected]46fb9442011-12-09 17:57:4763 }
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) {
[email protected]b9535422012-02-09 01:47:5976 ChildProcessSecurityPolicyImpl* p =
77 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:2978
[email protected]e0d481582009-09-15 21:06:2579 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme));
80 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme));
81 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme));
82 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme));
initial.commit09911bf2008-07-26 23:55:2983 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]039c7b0b22011-03-04 23:15:4284 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme));
85 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:2986
87 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
88 p->RegisterWebSafeScheme("registered-web-safe-scheme");
89 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:0390
91 EXPECT_FALSE(p->IsWebSafeScheme(chrome::kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:2992}
93
[email protected]f58ddcf2009-05-18 22:22:0694TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
[email protected]b9535422012-02-09 01:47:5995 ChildProcessSecurityPolicyImpl* p =
96 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:2997
[email protected]e0d481582009-09-15 21:06:2598 EXPECT_TRUE(p->IsPseudoScheme(chrome::kAboutScheme));
99 EXPECT_TRUE(p->IsPseudoScheme(chrome::kJavaScriptScheme));
100 EXPECT_TRUE(p->IsPseudoScheme(chrome::kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:29101
[email protected]419a0572011-04-18 22:21:46102 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
103 p->RegisterPseudoScheme("registered-pseudo-scheme");
104 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03105
106 EXPECT_FALSE(p->IsPseudoScheme(chrome::kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46107}
108
109TEST_F(ChildProcessSecurityPolicyTest, IsDisabledSchemeTest) {
[email protected]b9535422012-02-09 01:47:59110 ChildProcessSecurityPolicyImpl* p =
111 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]419a0572011-04-18 22:21:46112
113 EXPECT_FALSE(p->IsDisabledScheme("evil-scheme"));
114 std::set<std::string> disabled_set;
115 disabled_set.insert("evil-scheme");
116 p->RegisterDisabledSchemes(disabled_set);
117 EXPECT_TRUE(p->IsDisabledScheme("evil-scheme"));
118 EXPECT_FALSE(p->IsDisabledScheme("good-scheme"));
119
120 disabled_set.clear();
121 p->RegisterDisabledSchemes(disabled_set);
122 EXPECT_FALSE(p->IsDisabledScheme("evil-scheme"));
123 EXPECT_FALSE(p->IsDisabledScheme("good-scheme"));
initial.commit09911bf2008-07-26 23:55:29124}
125
[email protected]f58ddcf2009-05-18 22:22:06126TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59127 ChildProcessSecurityPolicyImpl* p =
128 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29129
130 p->Add(kRendererID);
131
132 // Safe
133 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
134 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
135 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
136 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
137 EXPECT_TRUE(p->CanRequestURL(kRendererID,
138 GURL("view-source:http://www.google.com/")));
[email protected]039c7b0b22011-03-04 23:15:42139 EXPECT_TRUE(p->CanRequestURL(
140 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29141
142 // Dangerous
143 EXPECT_FALSE(p->CanRequestURL(kRendererID,
144 GURL("file:///etc/passwd")));
145 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16146 GURL("chrome://foo/bar")));
initial.commit09911bf2008-07-26 23:55:29147
148 p->Remove(kRendererID);
149}
150
[email protected]f58ddcf2009-05-18 22:22:06151TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59152 ChildProcessSecurityPolicyImpl* p =
153 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29154
155 p->Add(kRendererID);
156
157 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
158 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
159 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
161
[email protected]ed3456f2009-02-26 20:24:48162 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 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
initial.commit09911bf2008-07-26 23:55:29166
167 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory")));
168 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
169 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
170
[email protected]8bf1048012012-02-08 01:22:18171 // Requests for about: pages should be denied.
172 p->GrantRequestURL(kRendererID, GURL("about:crash"));
173 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29174
[email protected]89f550b2011-06-08 18:34:03175 // These requests for chrome:// pages should be granted.
176 p->GrantRequestURL(kRendererID, GURL(chrome::kTestNewTabURL));
177 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestNewTabURL)));
178
179 p->GrantRequestURL(kRendererID, GURL(chrome::kTestHistoryURL));
180 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestHistoryURL)));
181
182 p->GrantRequestURL(kRendererID, GURL(chrome::kTestBookmarksURL));
183 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL(chrome::kTestBookmarksURL)));
184
initial.commit09911bf2008-07-26 23:55:29185 p->Remove(kRendererID);
186}
187
[email protected]f58ddcf2009-05-18 22:22:06188TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59189 ChildProcessSecurityPolicyImpl* p =
190 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29191
192 p->Add(kRendererID);
193
194 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
195 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
196 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
197
198 p->Remove(kRendererID);
199}
200
[email protected]f58ddcf2009-05-18 22:22:06201TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59202 ChildProcessSecurityPolicyImpl* p =
203 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29204
205 p->Add(kRendererID);
206
207 // Currently, "asdf" is destined for ShellExecute, so it is allowed.
208 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
209
[email protected]46fb9442011-12-09 17:57:47210 // Once we register "asdf", we default to deny.
211 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29212 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
213
214 // We can allow new schemes by adding them to the whitelist.
215 p->RegisterWebSafeScheme("asdf");
216 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
217
218 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29219 p->Remove(kRendererID);
220}
221
[email protected]f58ddcf2009-05-18 22:22:06222TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59223 ChildProcessSecurityPolicyImpl* p =
224 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29225
226 p->Add(kRendererID);
227
228 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
229 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
230 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
231
[email protected]419a0572011-04-18 22:21:46232 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
233 std::set<std::string> disabled_set;
234 disabled_set.insert("evil-scheme");
235 p->RegisterDisabledSchemes(disabled_set);
236 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com")));
237 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
238 disabled_set.clear();
239 p->RegisterDisabledSchemes(disabled_set);
240 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com")));
241 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("evil-scheme:/path")));
242
initial.commit09911bf2008-07-26 23:55:29243 // We should forget our state if we repeat a renderer id.
244 p->Remove(kRendererID);
245 p->Add(kRendererID);
246 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
247 p->Remove(kRendererID);
248}
249
[email protected]f58ddcf2009-05-18 22:22:06250TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59251 ChildProcessSecurityPolicyImpl* p =
252 ChildProcessSecurityPolicyImpl::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]dc67e1c32012-06-08 00:10:40274TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
275 ChildProcessSecurityPolicyImpl* p =
276 ChildProcessSecurityPolicyImpl::GetInstance();
277
278 p->Add(kRendererID);
279
280 GURL icon_url("file:///tmp/foo.png");
281 GURL sensitive_url("file:///etc/passwd");
282 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
283 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
284
285 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
286 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
287 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
288
289 p->GrantRequestURL(kRendererID, icon_url);
290 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
291 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
292
293 p->Remove(kRendererID);
294}
295
[email protected]e54edc32010-09-28 01:09:19296TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) {
[email protected]b9535422012-02-09 01:47:59297 ChildProcessSecurityPolicyImpl* p =
298 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29299
300 p->Add(kRendererID);
301
[email protected]e54edc32010-09-28 01:09:19302 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34303 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19304 p->GrantReadFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/passwd")));
305 EXPECT_TRUE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34306 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19307 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34308 FilePath(FILE_PATH_LITERAL("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29309
310 p->Remove(kRendererID);
311 p->Add(kRendererID);
312
[email protected]e54edc32010-09-28 01:09:19313 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34314 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
[email protected]e54edc32010-09-28 01:09:19315 EXPECT_FALSE(p->CanReadFile(kRendererID,
[email protected]561abe62009-04-06 18:08:34316 FilePath(FILE_PATH_LITERAL("/etc/shadow"))));
initial.commit09911bf2008-07-26 23:55:29317
318 p->Remove(kRendererID);
319}
320
[email protected]600ea402011-04-12 00:01:51321TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) {
[email protected]b9535422012-02-09 01:47:59322 ChildProcessSecurityPolicyImpl* p =
323 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]600ea402011-04-12 00:01:51324
325 p->Add(kRendererID);
326
327 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
328 FilePath(FILE_PATH_LITERAL("/etc/"))));
329 p->GrantReadDirectory(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")));
330 EXPECT_TRUE(p->CanReadDirectory(kRendererID,
331 FilePath(FILE_PATH_LITERAL("/etc/"))));
332 EXPECT_TRUE(p->CanReadFile(kRendererID,
333 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
334
335 p->Remove(kRendererID);
336 p->Add(kRendererID);
337
338 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
339 FilePath(FILE_PATH_LITERAL("/etc/"))));
340 EXPECT_FALSE(p->CanReadFile(kRendererID,
341 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
342
343 // Just granting read permission as a file doesn't imply reading as a
344 // directory.
345 p->GrantReadFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")));
346 EXPECT_TRUE(p->CanReadFile(kRendererID,
347 FilePath(FILE_PATH_LITERAL("/etc/passwd"))));
348 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
349 FilePath(FILE_PATH_LITERAL("/etc/"))));
350
351 p->Remove(kRendererID);
352}
353
[email protected]e54edc32010-09-28 01:09:19354TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]b9535422012-02-09 01:47:59355 ChildProcessSecurityPolicyImpl* p =
356 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19357
358 // Grant permissions for a file.
359 p->Add(kRendererID);
360 FilePath file = FilePath(FILE_PATH_LITERAL("/etc/passwd"));
361 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
362 base::PLATFORM_FILE_OPEN));
363
364 p->GrantPermissionsForFile(kRendererID, file,
365 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04366 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19367 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04368 base::PLATFORM_FILE_WRITE);
[email protected]e54edc32010-09-28 01:09:19369 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
370 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04371 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19372 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04373 base::PLATFORM_FILE_WRITE));
[email protected]e54edc32010-09-28 01:09:19374 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
375 base::PLATFORM_FILE_OPEN |
376 base::PLATFORM_FILE_READ));
377 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
378 base::PLATFORM_FILE_CREATE));
379 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
380 base::PLATFORM_FILE_CREATE |
[email protected]b2f2308d2011-05-23 22:00:04381 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19382 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04383 base::PLATFORM_FILE_WRITE));
[email protected]e54edc32010-09-28 01:09:19384 p->Remove(kRendererID);
385
386 // Grant permissions for the directory the file is in.
387 p->Add(kRendererID);
388 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
389 base::PLATFORM_FILE_OPEN));
390 p->GrantPermissionsForFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc")),
391 base::PLATFORM_FILE_OPEN |
392 base::PLATFORM_FILE_READ);
393 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
394 base::PLATFORM_FILE_OPEN));
395 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
396 base::PLATFORM_FILE_READ |
397 base::PLATFORM_FILE_WRITE));
398 p->Remove(kRendererID);
399
400 // Grant permissions for the directory the file is in (with trailing '/').
401 p->Add(kRendererID);
402 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
403 base::PLATFORM_FILE_OPEN));
404 p->GrantPermissionsForFile(kRendererID, FilePath(FILE_PATH_LITERAL("/etc/")),
405 base::PLATFORM_FILE_OPEN |
406 base::PLATFORM_FILE_READ);
407 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
408 base::PLATFORM_FILE_OPEN));
409 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
410 base::PLATFORM_FILE_READ |
411 base::PLATFORM_FILE_WRITE));
412
413 // Grant permissions for the file (should overwrite the permissions granted
414 // for the directory).
415 p->GrantPermissionsForFile(kRendererID, file, base::PLATFORM_FILE_TEMPORARY);
416 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
417 base::PLATFORM_FILE_OPEN));
418 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
419 base::PLATFORM_FILE_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34420
421 // Revoke all permissions for the file (it should inherit its permissions
422 // from the directory again).
423 p->RevokeAllPermissionsForFile(kRendererID, file);
424 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
425 base::PLATFORM_FILE_OPEN |
426 base::PLATFORM_FILE_READ));
427 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
428 base::PLATFORM_FILE_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19429 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07430
431 // Grant file permissions for the file to main thread renderer process,
432 // make sure its worker thread renderer process inherits those.
433 p->Add(kRendererID);
434 p->GrantPermissionsForFile(kRendererID, file, base::PLATFORM_FILE_OPEN |
435 base::PLATFORM_FILE_READ);
436 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, file,
437 base::PLATFORM_FILE_OPEN |
438 base::PLATFORM_FILE_READ));
439 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, file,
440 base::PLATFORM_FILE_WRITE));
441 p->AddWorker(kWorkerRendererID, kRendererID);
442 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, file,
443 base::PLATFORM_FILE_OPEN |
444 base::PLATFORM_FILE_READ));
445 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, file,
446 base::PLATFORM_FILE_WRITE));
447 p->Remove(kRendererID);
448 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, file,
449 base::PLATFORM_FILE_OPEN |
450 base::PLATFORM_FILE_READ));
451 p->Remove(kWorkerRendererID);
[email protected]e54edc32010-09-28 01:09:19452}
453
[email protected]c50008512011-02-03 01:17:27454TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59455 ChildProcessSecurityPolicyImpl* p =
456 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29457
[email protected]60e448982009-05-06 04:21:16458 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29459
460 p->Add(kRendererID);
461
[email protected]c50008512011-02-03 01:17:27462 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29463 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27464 p->GrantWebUIBindings(kRendererID);
465 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29466 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
467
468 p->Remove(kRendererID);
469}
470
[email protected]f58ddcf2009-05-18 22:22:06471TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59472 ChildProcessSecurityPolicyImpl* p =
473 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29474
475 GURL url("file:///etc/passwd");
[email protected]561abe62009-04-06 18:08:34476 FilePath file(FILE_PATH_LITERAL("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29477
478 p->Add(kRendererID);
479
480 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19481 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27482 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29483
484 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19485 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27486 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29487
488 p->Remove(kRendererID);
489
490 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55491 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
492 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29493
494 // In this case, we default to secure behavior.
495 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19496 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27497 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29498}