blob: 670848fa48f96b9dea8189bce7ed876881563d57 [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]57999812013-02-24 05:40:529#include "base/files/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]a1d29162011-10-14 17:14:0312#include "content/public/common/url_constants.h"
[email protected]c6681f32012-06-05 14:43:0113#include "content/test/test_content_browser_client.h"
initial.commit09911bf2008-07-26 23:55:2914#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:5815#include "url/gurl.h"
[email protected]9f104312013-07-23 23:18:1916#include "webkit/browser/fileapi/file_permission_policy.h"
17#include "webkit/browser/fileapi/file_system_url.h"
[email protected]b78c188fa62013-07-23 18:04:4518#include "webkit/browser/fileapi/isolated_context.h"
[email protected]9f104312013-07-23 23:18:1919#include "webkit/common/fileapi/file_system_types.h"
initial.commit09911bf2008-07-26 23:55:2920
[email protected]46488322012-10-30 03:22:2021namespace content {
[email protected]46fb9442011-12-09 17:57:4722namespace {
23
24const int kRendererID = 42;
25const int kWorkerRendererID = kRendererID + 1;
26
[email protected]f0ecca4522013-01-07 21:50:5627#if defined(FILE_PATH_USES_DRIVE_LETTERS)
28#define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x)
29#else
30#define TEST_PATH(x) FILE_PATH_LITERAL(x)
31#endif
32
[email protected]46fb9442011-12-09 17:57:4733class ChildProcessSecurityPolicyTestBrowserClient
[email protected]46488322012-10-30 03:22:2034 : public TestContentBrowserClient {
[email protected]46fb9442011-12-09 17:57:4735 public:
36 ChildProcessSecurityPolicyTestBrowserClient() {}
37
[email protected]c3e35892013-02-12 02:08:0138 virtual bool IsHandledURL(const GURL& url) OVERRIDE {
[email protected]46fb9442011-12-09 17:57:4739 return schemes_.find(url.scheme()) != schemes_.end();
[email protected]e3539402011-07-19 09:31:0840 }
41
[email protected]46fb9442011-12-09 17:57:4742 void ClearSchemes() {
43 schemes_.clear();
initial.commit09911bf2008-07-26 23:55:2944 }
[email protected]46fb9442011-12-09 17:57:4745
46 void AddScheme(const std::string& scheme) {
47 schemes_.insert(scheme);
48 }
49
50 private:
51 std::set<std::string> schemes_;
initial.commit09911bf2008-07-26 23:55:2952};
53
[email protected]46fb9442011-12-09 17:57:4754} // namespace
55
56class ChildProcessSecurityPolicyTest : public testing::Test {
57 public:
58 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) {
59 }
60
61 virtual void SetUp() {
[email protected]eabbfb12013-04-05 23:28:3562 old_browser_client_ = SetBrowserClientForTesting(&test_browser_client_);
[email protected]46fb9442011-12-09 17:57:4763
64 // Claim to always handle chrome:// URLs because the CPSP's notion of
65 // allowing WebUI bindings is hard-wired to this particular scheme.
[email protected]e0f35c92013-05-08 16:04:3466 test_browser_client_.AddScheme(chrome::kChromeUIScheme);
67
68 // Claim to always handle file:// URLs like the browser would.
69 // net::URLRequest::IsHandledURL() no longer claims support for default
70 // protocols as this is the responsibility of the browser (which is
71 // responsible for adding the appropriate ProtocolHandler).
72 test_browser_client_.AddScheme(chrome::kFileScheme);
[email protected]46fb9442011-12-09 17:57:4773 }
74
75 virtual void TearDown() {
76 test_browser_client_.ClearSchemes();
[email protected]eabbfb12013-04-05 23:28:3577 SetBrowserClientForTesting(old_browser_client_);
[email protected]46fb9442011-12-09 17:57:4778 }
79
80 protected:
81 void RegisterTestScheme(const std::string& scheme) {
82 test_browser_client_.AddScheme(scheme);
83 }
84
[email protected]bfcf1e92013-07-11 04:37:2585 void GrantPermissionsForFile(ChildProcessSecurityPolicyImpl* p,
86 int child_id,
87 const base::FilePath& file,
88 int permissions) {
89 p->GrantPermissionsForFile(child_id, file, permissions);
90 }
91
[email protected]5a65fde32013-10-22 05:15:3492 void CheckHasNoFileSystemPermission(ChildProcessSecurityPolicyImpl* p,
93 const std::string& child_id) {
94 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, child_id));
95 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, child_id));
96 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, child_id));
97 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, child_id));
98 }
99
100 void CheckHasNoFileSystemFilePermission(ChildProcessSecurityPolicyImpl* p,
101 const base::FilePath& file,
102 const fileapi::FileSystemURL& url) {
103 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
104 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
105 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
106 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
107 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
108 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
109 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
110 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
111 }
112
[email protected]46fb9442011-12-09 17:57:47113 private:
114 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
[email protected]46488322012-10-30 03:22:20115 ContentBrowserClient* old_browser_client_;
[email protected]46fb9442011-12-09 17:57:47116};
initial.commit09911bf2008-07-26 23:55:29117
[email protected]9f104312013-07-23 23:18:19118
[email protected]f58ddcf2009-05-18 22:22:06119TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59120 ChildProcessSecurityPolicyImpl* p =
121 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29122
[email protected]e9a82042013-09-15 03:38:29123 EXPECT_TRUE(p->IsWebSafeScheme(kHttpScheme));
[email protected]4654bfe2013-08-26 03:36:58124 EXPECT_TRUE(p->IsWebSafeScheme(kHttpsScheme));
[email protected]e0d481582009-09-15 21:06:25125 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme));
126 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme));
initial.commit09911bf2008-07-26 23:55:29127 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]039c7b0b22011-03-04 23:15:42128 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme));
129 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:29130
131 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
132 p->RegisterWebSafeScheme("registered-web-safe-scheme");
133 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:03134
135 EXPECT_FALSE(p->IsWebSafeScheme(chrome::kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:29136}
137
[email protected]f58ddcf2009-05-18 22:22:06138TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
[email protected]b9535422012-02-09 01:47:59139 ChildProcessSecurityPolicyImpl* p =
140 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29141
[email protected]e0d481582009-09-15 21:06:25142 EXPECT_TRUE(p->IsPseudoScheme(chrome::kAboutScheme));
[email protected]44b05812013-08-19 07:59:35143 EXPECT_TRUE(p->IsPseudoScheme(kJavaScriptScheme));
[email protected]dbdda5402013-05-30 22:13:48144 EXPECT_TRUE(p->IsPseudoScheme(kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:29145
[email protected]419a0572011-04-18 22:21:46146 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
147 p->RegisterPseudoScheme("registered-pseudo-scheme");
148 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03149
150 EXPECT_FALSE(p->IsPseudoScheme(chrome::kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46151}
152
[email protected]f58ddcf2009-05-18 22:22:06153TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59154 ChildProcessSecurityPolicyImpl* p =
155 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29156
157 p->Add(kRendererID);
158
159 // Safe
160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
163 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
164 EXPECT_TRUE(p->CanRequestURL(kRendererID,
165 GURL("view-source:http://www.google.com/")));
[email protected]039c7b0b22011-03-04 23:15:42166 EXPECT_TRUE(p->CanRequestURL(
167 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29168
169 // Dangerous
170 EXPECT_FALSE(p->CanRequestURL(kRendererID,
171 GURL("file:///etc/passwd")));
172 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16173 GURL("chrome://foo/bar")));
initial.commit09911bf2008-07-26 23:55:29174
175 p->Remove(kRendererID);
176}
177
[email protected]f58ddcf2009-05-18 22:22:06178TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59179 ChildProcessSecurityPolicyImpl* p =
180 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29181
182 p->Add(kRendererID);
183
184 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
185 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:BlAnK")));
186 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:BlAnK")));
187 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("aBouT:blank")));
188
[email protected]ed3456f2009-02-26 20:24:48189 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory")));
190 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
191 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
192 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
initial.commit09911bf2008-07-26 23:55:29193
194 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory")));
195 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
196 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
197
[email protected]8bf1048012012-02-08 01:22:18198 // Requests for about: pages should be denied.
199 p->GrantRequestURL(kRendererID, GURL("about:crash"));
200 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29201
[email protected]89f550b2011-06-08 18:34:03202 // These requests for chrome:// pages should be granted.
[email protected]e068c2d2012-10-23 16:45:18203 GURL chrome_url("chrome://foo");
204 p->GrantRequestURL(kRendererID, chrome_url);
205 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url));
[email protected]89f550b2011-06-08 18:34:03206
initial.commit09911bf2008-07-26 23:55:29207 p->Remove(kRendererID);
208}
209
[email protected]f58ddcf2009-05-18 22:22:06210TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59211 ChildProcessSecurityPolicyImpl* p =
212 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29213
214 p->Add(kRendererID);
215
216 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
217 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
218 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
219
220 p->Remove(kRendererID);
221}
222
[email protected]f58ddcf2009-05-18 22:22:06223TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59224 ChildProcessSecurityPolicyImpl* p =
225 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29226
227 p->Add(kRendererID);
228
229 // Currently, "asdf" is destined for ShellExecute, so it is allowed.
230 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
231
[email protected]46fb9442011-12-09 17:57:47232 // Once we register "asdf", we default to deny.
233 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29234 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
235
236 // We can allow new schemes by adding them to the whitelist.
237 p->RegisterWebSafeScheme("asdf");
238 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
239
240 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29241 p->Remove(kRendererID);
242}
243
[email protected]f58ddcf2009-05-18 22:22:06244TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59245 ChildProcessSecurityPolicyImpl* p =
246 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29247
248 p->Add(kRendererID);
249
250 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
251 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
252 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
253
254 // We should forget our state if we repeat a renderer id.
255 p->Remove(kRendererID);
256 p->Add(kRendererID);
257 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
258 p->Remove(kRendererID);
259}
260
[email protected]f58ddcf2009-05-18 22:22:06261TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59262 ChildProcessSecurityPolicyImpl* p =
263 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29264
265 p->Add(kRendererID);
266
267 // View source is determined by the embedded scheme.
268 EXPECT_TRUE(p->CanRequestURL(kRendererID,
269 GURL("view-source:http://www.google.com/")));
270 EXPECT_FALSE(p->CanRequestURL(kRendererID,
271 GURL("view-source:file:///etc/passwd")));
272 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36273 EXPECT_FALSE(p->CanRequestURL(
274 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29275
276 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
277 // View source needs to be able to request the embedded scheme.
278 EXPECT_TRUE(p->CanRequestURL(kRendererID,
279 GURL("view-source:file:///etc/passwd")));
280 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
281
282 p->Remove(kRendererID);
283}
284
[email protected]dc67e1c32012-06-08 00:10:40285TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
286 ChildProcessSecurityPolicyImpl* p =
287 ChildProcessSecurityPolicyImpl::GetInstance();
288
289 p->Add(kRendererID);
290
291 GURL icon_url("file:///tmp/foo.png");
292 GURL sensitive_url("file:///etc/passwd");
293 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
294 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
295
296 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
297 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
298 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
299
300 p->GrantRequestURL(kRendererID, icon_url);
301 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
302 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
303
304 p->Remove(kRendererID);
305}
306
[email protected]b78c188fa62013-07-23 18:04:45307TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) {
308 ChildProcessSecurityPolicyImpl* p =
309 ChildProcessSecurityPolicyImpl::GetInstance();
310
311 p->Add(kRendererID);
312 std::string read_id = fileapi::IsolatedContext::GetInstance()->
313 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
314 "read_filesystem",
315 base::FilePath());
316 std::string read_write_id = fileapi::IsolatedContext::GetInstance()->
317 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
318 "read_write_filesystem",
319 base::FilePath());
320 std::string copy_into_id = fileapi::IsolatedContext::GetInstance()->
321 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
322 "copy_into_filesystem",
323 base::FilePath());
[email protected]5a65fde32013-10-22 05:15:34324 std::string delete_from_id = fileapi::IsolatedContext::GetInstance()->
325 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
326 "delete_from_filesystem",
327 base::FilePath());
[email protected]b78c188fa62013-07-23 18:04:45328
329 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34330 CheckHasNoFileSystemPermission(p, read_id);
331 CheckHasNoFileSystemPermission(p, read_write_id);
332 CheckHasNoFileSystemPermission(p, copy_into_id);
333 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45334
335 // Testing varying combinations of grants and checks.
336 p->GrantReadFileSystem(kRendererID, read_id);
337 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_id));
338 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, read_id));
339 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_id));
[email protected]5a65fde32013-10-22 05:15:34340 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_id));
[email protected]b78c188fa62013-07-23 18:04:45341
342 p->GrantReadFileSystem(kRendererID, read_write_id);
343 p->GrantWriteFileSystem(kRendererID, read_write_id);
344 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_write_id));
345 EXPECT_TRUE(p->CanReadWriteFileSystem(kRendererID, read_write_id));
346 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_write_id));
[email protected]5a65fde32013-10-22 05:15:34347 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_write_id));
[email protected]b78c188fa62013-07-23 18:04:45348
349 p->GrantCopyIntoFileSystem(kRendererID, copy_into_id);
350 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, copy_into_id));
351 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id));
352 EXPECT_TRUE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id));
[email protected]5a65fde32013-10-22 05:15:34353 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, copy_into_id));
354
355 p->GrantDeleteFromFileSystem(kRendererID, delete_from_id);
356 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, delete_from_id));
357 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, delete_from_id));
358 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, delete_from_id));
359 EXPECT_TRUE(p->CanDeleteFromFileSystem(kRendererID, delete_from_id));
[email protected]b78c188fa62013-07-23 18:04:45360
361 // Test revoke permissions on renderer ID removal.
362 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34363 CheckHasNoFileSystemPermission(p, read_id);
364 CheckHasNoFileSystemPermission(p, read_write_id);
365 CheckHasNoFileSystemPermission(p, copy_into_id);
366 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45367
368 // Test having no permissions upon re-adding same renderer ID.
369 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34370 CheckHasNoFileSystemPermission(p, read_id);
371 CheckHasNoFileSystemPermission(p, read_write_id);
372 CheckHasNoFileSystemPermission(p, copy_into_id);
373 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45374
375 // Cleanup.
376 p->Remove(kRendererID);
377 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_id);
378 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id);
379 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id);
[email protected]5a65fde32013-10-22 05:15:34380 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45381}
382
[email protected]9f104312013-07-23 23:18:19383TEST_F(ChildProcessSecurityPolicyTest, FilePermissionGrantingAndRevoking) {
[email protected]b9535422012-02-09 01:47:59384 ChildProcessSecurityPolicyImpl* p =
385 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29386
[email protected]9f104312013-07-23 23:18:19387 p->RegisterFileSystemPermissionPolicy(
388 fileapi::kFileSystemTypeTest,
389 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION);
390
initial.commit09911bf2008-07-26 23:55:29391 p->Add(kRendererID);
[email protected]9f104312013-07-23 23:18:19392 base::FilePath file(TEST_PATH("/dir/testfile"));
393 file = file.NormalizePathSeparators();
394 fileapi::FileSystemURL url = fileapi::FileSystemURL::CreateForTest(
395 GURL("http://foo/"), fileapi::kFileSystemTypeTest, file);
initial.commit09911bf2008-07-26 23:55:29396
[email protected]9f104312013-07-23 23:18:19397 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34398 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29399
[email protected]9f104312013-07-23 23:18:19400 // Testing every combination of permissions granting and revoking.
401 p->GrantReadFile(kRendererID, file);
402 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53403 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19404 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
405 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
406 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53407 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34408 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
409 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19410 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34411 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19412
413 p->GrantCreateReadWriteFile(kRendererID, file);
414 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53415 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19416 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
417 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
418 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53419 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34420 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
421 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19422 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34423 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19424
425 // Test revoke permissions on renderer ID removal.
426 p->GrantCreateReadWriteFile(kRendererID, file);
427 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53428 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19429 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
430 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
431 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53432 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34433 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
434 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
initial.commit09911bf2008-07-26 23:55:29435 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34436 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19437
438 // Test having no permissions upon re-adding same renderer ID.
initial.commit09911bf2008-07-26 23:55:29439 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34440 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29441
[email protected]9f104312013-07-23 23:18:19442 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29443 p->Remove(kRendererID);
444}
445
[email protected]e54edc32010-09-28 01:09:19446TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]c42de732013-02-16 06:26:31447 base::FilePath granted_file = base::FilePath(TEST_PATH("/home/joe"));
448 base::FilePath sibling_file = base::FilePath(TEST_PATH("/home/bob"));
449 base::FilePath child_file = base::FilePath(TEST_PATH("/home/joe/file"));
450 base::FilePath parent_file = base::FilePath(TEST_PATH("/home"));
451 base::FilePath parent_slash_file = base::FilePath(TEST_PATH("/home/"));
452 base::FilePath child_traversal1 =
453 base::FilePath(TEST_PATH("/home/joe/././file"));
454 base::FilePath child_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56455 TEST_PATH("/home/joe/file/../otherfile"));
[email protected]2dec8ec2013-02-07 19:20:34456 base::FilePath evil_traversal1 =
[email protected]023ad6ab2013-02-17 05:07:23457 base::FilePath(TEST_PATH("/home/joe/../../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31458 base::FilePath evil_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56459 TEST_PATH("/home/joe/./.././../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31460 base::FilePath self_traversal =
461 base::FilePath(TEST_PATH("/home/joe/../joe/file"));
462 base::FilePath relative_file = base::FilePath(FILE_PATH_LITERAL("home/joe"));
[email protected]80838412012-11-20 01:53:59463
[email protected]b9535422012-02-09 01:47:59464 ChildProcessSecurityPolicyImpl* p =
465 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19466
467 // Grant permissions for a file.
468 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59469 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19470 base::PLATFORM_FILE_OPEN));
471
[email protected]bfcf1e92013-07-11 04:37:25472 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19473 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04474 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19475 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04476 base::PLATFORM_FILE_WRITE);
[email protected]80838412012-11-20 01:53:59477 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19478 base::PLATFORM_FILE_OPEN |
[email protected]b2f2308d2011-05-23 22:00:04479 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19480 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04481 base::PLATFORM_FILE_WRITE));
[email protected]80838412012-11-20 01:53:59482 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19483 base::PLATFORM_FILE_OPEN |
484 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59485 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19486 base::PLATFORM_FILE_CREATE));
[email protected]f0ecca4522013-01-07 21:50:56487 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, 0));
[email protected]80838412012-11-20 01:53:59488 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19489 base::PLATFORM_FILE_CREATE |
[email protected]b2f2308d2011-05-23 22:00:04490 base::PLATFORM_FILE_OPEN_TRUNCATED |
[email protected]e54edc32010-09-28 01:09:19491 base::PLATFORM_FILE_READ |
[email protected]b2f2308d2011-05-23 22:00:04492 base::PLATFORM_FILE_WRITE));
[email protected]80838412012-11-20 01:53:59493 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, sibling_file,
494 base::PLATFORM_FILE_OPEN |
495 base::PLATFORM_FILE_READ));
496 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, parent_file,
497 base::PLATFORM_FILE_OPEN |
498 base::PLATFORM_FILE_READ));
499 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_file,
500 base::PLATFORM_FILE_OPEN |
501 base::PLATFORM_FILE_READ));
502 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal1,
503 base::PLATFORM_FILE_OPEN |
504 base::PLATFORM_FILE_READ));
505 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal2,
506 base::PLATFORM_FILE_OPEN |
507 base::PLATFORM_FILE_READ));
508 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal1,
509 base::PLATFORM_FILE_OPEN |
510 base::PLATFORM_FILE_READ));
511 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal2,
512 base::PLATFORM_FILE_OPEN |
513 base::PLATFORM_FILE_READ));
514 // CPSP doesn't allow this case for the sake of simplicity.
515 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, self_traversal,
516 base::PLATFORM_FILE_OPEN |
517 base::PLATFORM_FILE_READ));
[email protected]e54edc32010-09-28 01:09:19518 p->Remove(kRendererID);
519
520 // Grant permissions for the directory the file is in.
521 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59522 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19523 base::PLATFORM_FILE_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25524 GrantPermissionsForFile(p, kRendererID, parent_file,
[email protected]e54edc32010-09-28 01:09:19525 base::PLATFORM_FILE_OPEN |
526 base::PLATFORM_FILE_READ);
[email protected]80838412012-11-20 01:53:59527 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19528 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59529 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19530 base::PLATFORM_FILE_READ |
531 base::PLATFORM_FILE_WRITE));
532 p->Remove(kRendererID);
533
534 // Grant permissions for the directory the file is in (with trailing '/').
535 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59536 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19537 base::PLATFORM_FILE_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25538 GrantPermissionsForFile(p, kRendererID, parent_slash_file,
[email protected]e54edc32010-09-28 01:09:19539 base::PLATFORM_FILE_OPEN |
540 base::PLATFORM_FILE_READ);
[email protected]80838412012-11-20 01:53:59541 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19542 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59543 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19544 base::PLATFORM_FILE_READ |
545 base::PLATFORM_FILE_WRITE));
546
547 // Grant permissions for the file (should overwrite the permissions granted
548 // for the directory).
[email protected]bfcf1e92013-07-11 04:37:25549 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]80838412012-11-20 01:53:59550 base::PLATFORM_FILE_TEMPORARY);
551 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19552 base::PLATFORM_FILE_OPEN));
[email protected]80838412012-11-20 01:53:59553 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]e54edc32010-09-28 01:09:19554 base::PLATFORM_FILE_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34555
556 // Revoke all permissions for the file (it should inherit its permissions
557 // from the directory again).
[email protected]80838412012-11-20 01:53:59558 p->RevokeAllPermissionsForFile(kRendererID, granted_file);
559 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]77930fe2010-10-01 22:45:34560 base::PLATFORM_FILE_OPEN |
561 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59562 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]77930fe2010-10-01 22:45:34563 base::PLATFORM_FILE_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19564 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07565
566 // Grant file permissions for the file to main thread renderer process,
567 // make sure its worker thread renderer process inherits those.
568 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25569 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]80838412012-11-20 01:53:59570 base::PLATFORM_FILE_OPEN |
571 base::PLATFORM_FILE_READ);
572 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07573 base::PLATFORM_FILE_OPEN |
574 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59575 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07576 base::PLATFORM_FILE_WRITE));
577 p->AddWorker(kWorkerRendererID, kRendererID);
[email protected]80838412012-11-20 01:53:59578 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07579 base::PLATFORM_FILE_OPEN |
580 base::PLATFORM_FILE_READ));
[email protected]80838412012-11-20 01:53:59581 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07582 base::PLATFORM_FILE_WRITE));
583 p->Remove(kRendererID);
[email protected]80838412012-11-20 01:53:59584 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]cee64fd32011-05-02 18:59:07585 base::PLATFORM_FILE_OPEN |
586 base::PLATFORM_FILE_READ));
587 p->Remove(kWorkerRendererID);
[email protected]f0ecca4522013-01-07 21:50:56588
589 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25590 GrantPermissionsForFile(p, kRendererID, relative_file,
[email protected]f0ecca4522013-01-07 21:50:56591 base::PLATFORM_FILE_OPEN);
592 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, relative_file,
593 base::PLATFORM_FILE_OPEN));
594 p->Remove(kRendererID);
[email protected]e54edc32010-09-28 01:09:19595}
596
[email protected]c50008512011-02-03 01:17:27597TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59598 ChildProcessSecurityPolicyImpl* p =
599 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29600
[email protected]60e448982009-05-06 04:21:16601 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29602
603 p->Add(kRendererID);
604
[email protected]c50008512011-02-03 01:17:27605 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29606 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27607 p->GrantWebUIBindings(kRendererID);
608 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29609 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
610
611 p->Remove(kRendererID);
612}
613
[email protected]f58ddcf2009-05-18 22:22:06614TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59615 ChildProcessSecurityPolicyImpl* p =
616 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29617
618 GURL url("file:///etc/passwd");
[email protected]2dec8ec2013-02-07 19:20:34619 base::FilePath file(TEST_PATH("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29620
621 p->Add(kRendererID);
622
623 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19624 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27625 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29626
627 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19628 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27629 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29630
631 p->Remove(kRendererID);
632
633 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55634 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
635 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29636
637 // In this case, we default to secure behavior.
638 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19639 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27640 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29641}
[email protected]46488322012-10-30 03:22:20642
643} // namespace content