blob: 97c1889b3d7875f5116f1af86a0ecccb3e686457 [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]b9535422012-02-09 01:47:5910#include "content/browser/child_process_security_policy_impl.h"
[email protected]a1d29162011-10-14 17:14:0311#include "content/public/common/url_constants.h"
[email protected]c6681f32012-06-05 14:43:0112#include "content/test/test_content_browser_client.h"
initial.commit09911bf2008-07-26 23:55:2913#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:5814#include "url/gurl.h"
[email protected]9f104312013-07-23 23:18:1915#include "webkit/browser/fileapi/file_permission_policy.h"
16#include "webkit/browser/fileapi/file_system_url.h"
[email protected]b78c188fa62013-07-23 18:04:4517#include "webkit/browser/fileapi/isolated_context.h"
[email protected]9f104312013-07-23 23:18:1918#include "webkit/common/fileapi/file_system_types.h"
initial.commit09911bf2008-07-26 23:55:2919
[email protected]46488322012-10-30 03:22:2020namespace content {
[email protected]46fb9442011-12-09 17:57:4721namespace {
22
23const int kRendererID = 42;
24const int kWorkerRendererID = kRendererID + 1;
25
[email protected]f0ecca4522013-01-07 21:50:5626#if defined(FILE_PATH_USES_DRIVE_LETTERS)
27#define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x)
28#else
29#define TEST_PATH(x) FILE_PATH_LITERAL(x)
30#endif
31
[email protected]46fb9442011-12-09 17:57:4732class ChildProcessSecurityPolicyTestBrowserClient
[email protected]46488322012-10-30 03:22:2033 : public TestContentBrowserClient {
[email protected]46fb9442011-12-09 17:57:4734 public:
35 ChildProcessSecurityPolicyTestBrowserClient() {}
36
[email protected]c3e35892013-02-12 02:08:0137 virtual bool IsHandledURL(const GURL& url) OVERRIDE {
[email protected]46fb9442011-12-09 17:57:4738 return schemes_.find(url.scheme()) != schemes_.end();
[email protected]e3539402011-07-19 09:31:0839 }
40
[email protected]46fb9442011-12-09 17:57:4741 void ClearSchemes() {
42 schemes_.clear();
initial.commit09911bf2008-07-26 23:55:2943 }
[email protected]46fb9442011-12-09 17:57:4744
45 void AddScheme(const std::string& scheme) {
46 schemes_.insert(scheme);
47 }
48
49 private:
50 std::set<std::string> schemes_;
initial.commit09911bf2008-07-26 23:55:2951};
52
[email protected]46fb9442011-12-09 17:57:4753} // namespace
54
55class ChildProcessSecurityPolicyTest : public testing::Test {
56 public:
57 ChildProcessSecurityPolicyTest() : old_browser_client_(NULL) {
58 }
59
60 virtual void SetUp() {
[email protected]eabbfb12013-04-05 23:28:3561 old_browser_client_ = SetBrowserClientForTesting(&test_browser_client_);
[email protected]46fb9442011-12-09 17:57:4762
63 // Claim to always handle chrome:// URLs because the CPSP's notion of
64 // allowing WebUI bindings is hard-wired to this particular scheme.
[email protected]2d9748b22014-02-11 00:17:2965 test_browser_client_.AddScheme(kChromeUIScheme);
[email protected]e0f35c92013-05-08 16:04:3466
67 // Claim to always handle file:// URLs like the browser would.
68 // net::URLRequest::IsHandledURL() no longer claims support for default
69 // protocols as this is the responsibility of the browser (which is
70 // responsible for adding the appropriate ProtocolHandler).
[email protected]cca6f392014-05-28 21:32:2671 test_browser_client_.AddScheme(url::kFileScheme);
[email protected]46fb9442011-12-09 17:57:4772 }
73
74 virtual void TearDown() {
75 test_browser_client_.ClearSchemes();
[email protected]eabbfb12013-04-05 23:28:3576 SetBrowserClientForTesting(old_browser_client_);
[email protected]46fb9442011-12-09 17:57:4777 }
78
79 protected:
80 void RegisterTestScheme(const std::string& scheme) {
81 test_browser_client_.AddScheme(scheme);
82 }
83
[email protected]bfcf1e92013-07-11 04:37:2584 void GrantPermissionsForFile(ChildProcessSecurityPolicyImpl* p,
85 int child_id,
86 const base::FilePath& file,
87 int permissions) {
88 p->GrantPermissionsForFile(child_id, file, permissions);
89 }
90
[email protected]5a65fde32013-10-22 05:15:3491 void CheckHasNoFileSystemPermission(ChildProcessSecurityPolicyImpl* p,
92 const std::string& child_id) {
93 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, child_id));
94 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, child_id));
95 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, child_id));
96 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, child_id));
97 }
98
99 void CheckHasNoFileSystemFilePermission(ChildProcessSecurityPolicyImpl* p,
100 const base::FilePath& file,
101 const fileapi::FileSystemURL& url) {
102 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
103 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
104 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
105 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
106 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
107 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
108 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
109 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
110 }
111
[email protected]46fb9442011-12-09 17:57:47112 private:
113 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
[email protected]46488322012-10-30 03:22:20114 ContentBrowserClient* old_browser_client_;
[email protected]46fb9442011-12-09 17:57:47115};
initial.commit09911bf2008-07-26 23:55:29116
[email protected]9f104312013-07-23 23:18:19117
[email protected]f58ddcf2009-05-18 22:22:06118TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
[email protected]b9535422012-02-09 01:47:59119 ChildProcessSecurityPolicyImpl* p =
120 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29121
[email protected]e8ca69c2014-05-07 15:31:19122 EXPECT_TRUE(p->IsWebSafeScheme(url::kHttpScheme));
123 EXPECT_TRUE(p->IsWebSafeScheme(url::kHttpsScheme));
[email protected]cca6f392014-05-28 21:32:26124 EXPECT_TRUE(p->IsWebSafeScheme(url::kFtpScheme));
125 EXPECT_TRUE(p->IsWebSafeScheme(url::kDataScheme));
initial.commit09911bf2008-07-26 23:55:29126 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
[email protected]cca6f392014-05-28 21:32:26127 EXPECT_TRUE(p->IsWebSafeScheme(url::kBlobScheme));
128 EXPECT_TRUE(p->IsWebSafeScheme(url::kFileSystemScheme));
initial.commit09911bf2008-07-26 23:55:29129
130 EXPECT_FALSE(p->IsWebSafeScheme("registered-web-safe-scheme"));
131 p->RegisterWebSafeScheme("registered-web-safe-scheme");
132 EXPECT_TRUE(p->IsWebSafeScheme("registered-web-safe-scheme"));
[email protected]89f550b2011-06-08 18:34:03133
[email protected]2d9748b22014-02-11 00:17:29134 EXPECT_FALSE(p->IsWebSafeScheme(kChromeUIScheme));
initial.commit09911bf2008-07-26 23:55:29135}
136
[email protected]f58ddcf2009-05-18 22:22:06137TEST_F(ChildProcessSecurityPolicyTest, IsPseudoSchemeTest) {
[email protected]b9535422012-02-09 01:47:59138 ChildProcessSecurityPolicyImpl* p =
139 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29140
[email protected]8e09c7af2014-06-10 11:46:17141 EXPECT_TRUE(p->IsPseudoScheme(url::kAboutScheme));
[email protected]cca6f392014-05-28 21:32:26142 EXPECT_TRUE(p->IsPseudoScheme(url::kJavaScriptScheme));
[email protected]dbdda5402013-05-30 22:13:48143 EXPECT_TRUE(p->IsPseudoScheme(kViewSourceScheme));
initial.commit09911bf2008-07-26 23:55:29144
[email protected]419a0572011-04-18 22:21:46145 EXPECT_FALSE(p->IsPseudoScheme("registered-pseudo-scheme"));
146 p->RegisterPseudoScheme("registered-pseudo-scheme");
147 EXPECT_TRUE(p->IsPseudoScheme("registered-pseudo-scheme"));
[email protected]89f550b2011-06-08 18:34:03148
[email protected]2d9748b22014-02-11 00:17:29149 EXPECT_FALSE(p->IsPseudoScheme(kChromeUIScheme));
[email protected]419a0572011-04-18 22:21:46150}
151
[email protected]f58ddcf2009-05-18 22:22:06152TEST_F(ChildProcessSecurityPolicyTest, StandardSchemesTest) {
[email protected]b9535422012-02-09 01:47:59153 ChildProcessSecurityPolicyImpl* p =
154 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29155
156 p->Add(kRendererID);
157
158 // Safe
159 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("http://www.google.com/")));
160 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("https://www.paypal.com/")));
161 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("ftp://ftp.gnu.org/")));
162 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("data:text/html,<b>Hi</b>")));
163 EXPECT_TRUE(p->CanRequestURL(kRendererID,
164 GURL("view-source:http://www.google.com/")));
[email protected]039c7b0b22011-03-04 23:15:42165 EXPECT_TRUE(p->CanRequestURL(
166 kRendererID, GURL("filesystem:http://localhost/temporary/a.gif")));
initial.commit09911bf2008-07-26 23:55:29167
168 // Dangerous
169 EXPECT_FALSE(p->CanRequestURL(kRendererID,
170 GURL("file:///etc/passwd")));
171 EXPECT_FALSE(p->CanRequestURL(kRendererID,
[email protected]60e448982009-05-06 04:21:16172 GURL("chrome://foo/bar")));
initial.commit09911bf2008-07-26 23:55:29173
174 p->Remove(kRendererID);
175}
176
[email protected]f58ddcf2009-05-18 22:22:06177TEST_F(ChildProcessSecurityPolicyTest, AboutTest) {
[email protected]b9535422012-02-09 01:47:59178 ChildProcessSecurityPolicyImpl* p =
179 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29180
181 p->Add(kRendererID);
182
183 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("about:blank")));
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
[email protected]ed3456f2009-02-26 20:24:48188 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:memory")));
189 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
190 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:cache")));
191 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:hang")));
initial.commit09911bf2008-07-26 23:55:29192
193 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("aBoUt:memory")));
194 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:CrASh")));
195 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("abOuT:cAChe")));
196
[email protected]8bf1048012012-02-08 01:22:18197 // Requests for about: pages should be denied.
198 p->GrantRequestURL(kRendererID, GURL("about:crash"));
199 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("about:crash")));
initial.commit09911bf2008-07-26 23:55:29200
[email protected]89f550b2011-06-08 18:34:03201 // These requests for chrome:// pages should be granted.
[email protected]e068c2d2012-10-23 16:45:18202 GURL chrome_url("chrome://foo");
203 p->GrantRequestURL(kRendererID, chrome_url);
204 EXPECT_TRUE(p->CanRequestURL(kRendererID, chrome_url));
[email protected]89f550b2011-06-08 18:34:03205
initial.commit09911bf2008-07-26 23:55:29206 p->Remove(kRendererID);
207}
208
[email protected]f58ddcf2009-05-18 22:22:06209TEST_F(ChildProcessSecurityPolicyTest, JavaScriptTest) {
[email protected]b9535422012-02-09 01:47:59210 ChildProcessSecurityPolicyImpl* p =
211 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29212
213 p->Add(kRendererID);
214
215 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
216 p->GrantRequestURL(kRendererID, GURL("javascript:alert('xss')"));
217 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("javascript:alert('xss')")));
218
219 p->Remove(kRendererID);
220}
221
[email protected]f58ddcf2009-05-18 22:22:06222TEST_F(ChildProcessSecurityPolicyTest, RegisterWebSafeSchemeTest) {
[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 // Currently, "asdf" is destined for ShellExecute, so it is allowed.
229 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
230
[email protected]46fb9442011-12-09 17:57:47231 // Once we register "asdf", we default to deny.
232 RegisterTestScheme("asdf");
initial.commit09911bf2008-07-26 23:55:29233 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
234
235 // We can allow new schemes by adding them to the whitelist.
236 p->RegisterWebSafeScheme("asdf");
237 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("asdf:rockers")));
238
239 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29240 p->Remove(kRendererID);
241}
242
[email protected]f58ddcf2009-05-18 22:22:06243TEST_F(ChildProcessSecurityPolicyTest, CanServiceCommandsTest) {
[email protected]b9535422012-02-09 01:47:59244 ChildProcessSecurityPolicyImpl* p =
245 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29246
247 p->Add(kRendererID);
248
249 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
250 p->GrantRequestURL(kRendererID, GURL("file:///etc/passwd"));
251 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
252
253 // We should forget our state if we repeat a renderer id.
254 p->Remove(kRendererID);
255 p->Add(kRendererID);
256 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
257 p->Remove(kRendererID);
258}
259
[email protected]f58ddcf2009-05-18 22:22:06260TEST_F(ChildProcessSecurityPolicyTest, ViewSource) {
[email protected]b9535422012-02-09 01:47:59261 ChildProcessSecurityPolicyImpl* p =
262 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29263
264 p->Add(kRendererID);
265
266 // View source is determined by the embedded scheme.
267 EXPECT_TRUE(p->CanRequestURL(kRendererID,
268 GURL("view-source:http://www.google.com/")));
269 EXPECT_FALSE(p->CanRequestURL(kRendererID,
270 GURL("view-source:file:///etc/passwd")));
271 EXPECT_FALSE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
[email protected]690d0a9172010-01-06 00:19:36272 EXPECT_FALSE(p->CanRequestURL(
273 kRendererID, GURL("view-source:view-source:http://www.google.com/")));
initial.commit09911bf2008-07-26 23:55:29274
275 p->GrantRequestURL(kRendererID, GURL("view-source:file:///etc/passwd"));
276 // View source needs to be able to request the embedded scheme.
277 EXPECT_TRUE(p->CanRequestURL(kRendererID,
278 GURL("view-source:file:///etc/passwd")));
279 EXPECT_TRUE(p->CanRequestURL(kRendererID, GURL("file:///etc/passwd")));
280
281 p->Remove(kRendererID);
282}
283
[email protected]dc67e1c32012-06-08 00:10:40284TEST_F(ChildProcessSecurityPolicyTest, SpecificFile) {
285 ChildProcessSecurityPolicyImpl* p =
286 ChildProcessSecurityPolicyImpl::GetInstance();
287
288 p->Add(kRendererID);
289
290 GURL icon_url("file:///tmp/foo.png");
291 GURL sensitive_url("file:///etc/passwd");
292 EXPECT_FALSE(p->CanRequestURL(kRendererID, icon_url));
293 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
294
295 p->GrantRequestSpecificFileURL(kRendererID, icon_url);
296 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
297 EXPECT_FALSE(p->CanRequestURL(kRendererID, sensitive_url));
298
299 p->GrantRequestURL(kRendererID, icon_url);
300 EXPECT_TRUE(p->CanRequestURL(kRendererID, icon_url));
301 EXPECT_TRUE(p->CanRequestURL(kRendererID, sensitive_url));
302
303 p->Remove(kRendererID);
304}
305
[email protected]b78c188fa62013-07-23 18:04:45306TEST_F(ChildProcessSecurityPolicyTest, FileSystemGrantsTest) {
307 ChildProcessSecurityPolicyImpl* p =
308 ChildProcessSecurityPolicyImpl::GetInstance();
309
310 p->Add(kRendererID);
311 std::string read_id = fileapi::IsolatedContext::GetInstance()->
312 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
313 "read_filesystem",
314 base::FilePath());
315 std::string read_write_id = fileapi::IsolatedContext::GetInstance()->
316 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
317 "read_write_filesystem",
318 base::FilePath());
319 std::string copy_into_id = fileapi::IsolatedContext::GetInstance()->
320 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
321 "copy_into_filesystem",
322 base::FilePath());
[email protected]5a65fde32013-10-22 05:15:34323 std::string delete_from_id = fileapi::IsolatedContext::GetInstance()->
324 RegisterFileSystemForVirtualPath(fileapi::kFileSystemTypeTest,
325 "delete_from_filesystem",
326 base::FilePath());
[email protected]b78c188fa62013-07-23 18:04:45327
328 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34329 CheckHasNoFileSystemPermission(p, read_id);
330 CheckHasNoFileSystemPermission(p, read_write_id);
331 CheckHasNoFileSystemPermission(p, copy_into_id);
332 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45333
334 // Testing varying combinations of grants and checks.
335 p->GrantReadFileSystem(kRendererID, read_id);
336 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_id));
337 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, read_id));
338 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_id));
[email protected]5a65fde32013-10-22 05:15:34339 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_id));
[email protected]b78c188fa62013-07-23 18:04:45340
341 p->GrantReadFileSystem(kRendererID, read_write_id);
342 p->GrantWriteFileSystem(kRendererID, read_write_id);
343 EXPECT_TRUE(p->CanReadFileSystem(kRendererID, read_write_id));
344 EXPECT_TRUE(p->CanReadWriteFileSystem(kRendererID, read_write_id));
345 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, read_write_id));
[email protected]5a65fde32013-10-22 05:15:34346 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, read_write_id));
[email protected]b78c188fa62013-07-23 18:04:45347
348 p->GrantCopyIntoFileSystem(kRendererID, copy_into_id);
349 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, copy_into_id));
350 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id));
351 EXPECT_TRUE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id));
[email protected]5a65fde32013-10-22 05:15:34352 EXPECT_FALSE(p->CanDeleteFromFileSystem(kRendererID, copy_into_id));
353
354 p->GrantDeleteFromFileSystem(kRendererID, delete_from_id);
355 EXPECT_FALSE(p->CanReadFileSystem(kRendererID, delete_from_id));
356 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, delete_from_id));
357 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, delete_from_id));
358 EXPECT_TRUE(p->CanDeleteFromFileSystem(kRendererID, delete_from_id));
[email protected]b78c188fa62013-07-23 18:04:45359
360 // Test revoke permissions on renderer ID removal.
361 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34362 CheckHasNoFileSystemPermission(p, read_id);
363 CheckHasNoFileSystemPermission(p, read_write_id);
364 CheckHasNoFileSystemPermission(p, copy_into_id);
365 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45366
367 // Test having no permissions upon re-adding same renderer ID.
368 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34369 CheckHasNoFileSystemPermission(p, read_id);
370 CheckHasNoFileSystemPermission(p, read_write_id);
371 CheckHasNoFileSystemPermission(p, copy_into_id);
372 CheckHasNoFileSystemPermission(p, delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45373
374 // Cleanup.
375 p->Remove(kRendererID);
376 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_id);
377 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id);
378 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id);
[email protected]5a65fde32013-10-22 05:15:34379 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(delete_from_id);
[email protected]b78c188fa62013-07-23 18:04:45380}
381
[email protected]9f104312013-07-23 23:18:19382TEST_F(ChildProcessSecurityPolicyTest, FilePermissionGrantingAndRevoking) {
[email protected]b9535422012-02-09 01:47:59383 ChildProcessSecurityPolicyImpl* p =
384 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29385
[email protected]9f104312013-07-23 23:18:19386 p->RegisterFileSystemPermissionPolicy(
387 fileapi::kFileSystemTypeTest,
388 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION);
389
initial.commit09911bf2008-07-26 23:55:29390 p->Add(kRendererID);
[email protected]9f104312013-07-23 23:18:19391 base::FilePath file(TEST_PATH("/dir/testfile"));
392 file = file.NormalizePathSeparators();
393 fileapi::FileSystemURL url = fileapi::FileSystemURL::CreateForTest(
394 GURL("http://foo/"), fileapi::kFileSystemTypeTest, file);
initial.commit09911bf2008-07-26 23:55:29395
[email protected]9f104312013-07-23 23:18:19396 // Test initially having no permissions.
[email protected]5a65fde32013-10-22 05:15:34397 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29398
[email protected]9f104312013-07-23 23:18:19399 // Testing every combination of permissions granting and revoking.
400 p->GrantReadFile(kRendererID, file);
401 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53402 EXPECT_FALSE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19403 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
404 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
405 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53406 EXPECT_FALSE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34407 EXPECT_FALSE(p->CanCopyIntoFileSystemFile(kRendererID, url));
408 EXPECT_FALSE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19409 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34410 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19411
412 p->GrantCreateReadWriteFile(kRendererID, file);
413 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53414 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19415 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
416 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
417 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53418 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34419 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
420 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
[email protected]9f104312013-07-23 23:18:19421 p->RevokeAllPermissionsForFile(kRendererID, file);
[email protected]5a65fde32013-10-22 05:15:34422 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19423
424 // Test revoke permissions on renderer ID removal.
425 p->GrantCreateReadWriteFile(kRendererID, file);
426 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]d4c797f2013-09-26 08:18:53427 EXPECT_TRUE(p->CanCreateReadWriteFile(kRendererID, file));
[email protected]9f104312013-07-23 23:18:19428 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
429 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
430 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
[email protected]d4c797f2013-09-26 08:18:53431 EXPECT_TRUE(p->CanCreateReadWriteFileSystemFile(kRendererID, url));
[email protected]5a65fde32013-10-22 05:15:34432 EXPECT_TRUE(p->CanCopyIntoFileSystemFile(kRendererID, url));
433 EXPECT_TRUE(p->CanDeleteFileSystemFile(kRendererID, url));
initial.commit09911bf2008-07-26 23:55:29434 p->Remove(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34435 CheckHasNoFileSystemFilePermission(p, file, url);
[email protected]9f104312013-07-23 23:18:19436
437 // Test having no permissions upon re-adding same renderer ID.
initial.commit09911bf2008-07-26 23:55:29438 p->Add(kRendererID);
[email protected]5a65fde32013-10-22 05:15:34439 CheckHasNoFileSystemFilePermission(p, file, url);
initial.commit09911bf2008-07-26 23:55:29440
[email protected]9f104312013-07-23 23:18:19441 // Cleanup.
initial.commit09911bf2008-07-26 23:55:29442 p->Remove(kRendererID);
443}
444
[email protected]e54edc32010-09-28 01:09:19445TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) {
[email protected]c42de732013-02-16 06:26:31446 base::FilePath granted_file = base::FilePath(TEST_PATH("/home/joe"));
447 base::FilePath sibling_file = base::FilePath(TEST_PATH("/home/bob"));
448 base::FilePath child_file = base::FilePath(TEST_PATH("/home/joe/file"));
449 base::FilePath parent_file = base::FilePath(TEST_PATH("/home"));
450 base::FilePath parent_slash_file = base::FilePath(TEST_PATH("/home/"));
451 base::FilePath child_traversal1 =
452 base::FilePath(TEST_PATH("/home/joe/././file"));
453 base::FilePath child_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56454 TEST_PATH("/home/joe/file/../otherfile"));
[email protected]2dec8ec2013-02-07 19:20:34455 base::FilePath evil_traversal1 =
[email protected]023ad6ab2013-02-17 05:07:23456 base::FilePath(TEST_PATH("/home/joe/../../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31457 base::FilePath evil_traversal2 = base::FilePath(
[email protected]f0ecca4522013-01-07 21:50:56458 TEST_PATH("/home/joe/./.././../etc/passwd"));
[email protected]c42de732013-02-16 06:26:31459 base::FilePath self_traversal =
460 base::FilePath(TEST_PATH("/home/joe/../joe/file"));
461 base::FilePath relative_file = base::FilePath(FILE_PATH_LITERAL("home/joe"));
[email protected]80838412012-11-20 01:53:59462
[email protected]b9535422012-02-09 01:47:59463 ChildProcessSecurityPolicyImpl* p =
464 ChildProcessSecurityPolicyImpl::GetInstance();
[email protected]e54edc32010-09-28 01:09:19465
466 // Grant permissions for a file.
467 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59468 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41469 base::File::FLAG_OPEN));
[email protected]e54edc32010-09-28 01:09:19470
[email protected]bfcf1e92013-07-11 04:37:25471 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41472 base::File::FLAG_OPEN |
473 base::File::FLAG_OPEN_TRUNCATED |
474 base::File::FLAG_READ |
475 base::File::FLAG_WRITE);
[email protected]80838412012-11-20 01:53:59476 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41477 base::File::FLAG_OPEN |
478 base::File::FLAG_OPEN_TRUNCATED |
479 base::File::FLAG_READ |
480 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59481 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41482 base::File::FLAG_OPEN |
483 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59484 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41485 base::File::FLAG_CREATE));
[email protected]f0ecca4522013-01-07 21:50:56486 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, 0));
[email protected]80838412012-11-20 01:53:59487 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41488 base::File::FLAG_CREATE |
489 base::File::FLAG_OPEN_TRUNCATED |
490 base::File::FLAG_READ |
491 base::File::FLAG_WRITE));
[email protected]80838412012-11-20 01:53:59492 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, sibling_file,
[email protected]2c288ed2014-06-05 22:07:41493 base::File::FLAG_OPEN |
494 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59495 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41496 base::File::FLAG_OPEN |
497 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59498 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_file,
[email protected]2c288ed2014-06-05 22:07:41499 base::File::FLAG_OPEN |
500 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59501 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal1,
[email protected]2c288ed2014-06-05 22:07:41502 base::File::FLAG_OPEN |
503 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59504 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, child_traversal2,
[email protected]2c288ed2014-06-05 22:07:41505 base::File::FLAG_OPEN |
506 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59507 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal1,
[email protected]2c288ed2014-06-05 22:07:41508 base::File::FLAG_OPEN |
509 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59510 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, evil_traversal2,
[email protected]2c288ed2014-06-05 22:07:41511 base::File::FLAG_OPEN |
512 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59513 // CPSP doesn't allow this case for the sake of simplicity.
514 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, self_traversal,
[email protected]2c288ed2014-06-05 22:07:41515 base::File::FLAG_OPEN |
516 base::File::FLAG_READ));
[email protected]e54edc32010-09-28 01:09:19517 p->Remove(kRendererID);
518
519 // Grant permissions for the directory the file is in.
520 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59521 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41522 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25523 GrantPermissionsForFile(p, kRendererID, parent_file,
[email protected]2c288ed2014-06-05 22:07:41524 base::File::FLAG_OPEN |
525 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59526 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41527 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59528 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41529 base::File::FLAG_READ |
530 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19531 p->Remove(kRendererID);
532
533 // Grant permissions for the directory the file is in (with trailing '/').
534 p->Add(kRendererID);
[email protected]80838412012-11-20 01:53:59535 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41536 base::File::FLAG_OPEN));
[email protected]bfcf1e92013-07-11 04:37:25537 GrantPermissionsForFile(p, kRendererID, parent_slash_file,
[email protected]2c288ed2014-06-05 22:07:41538 base::File::FLAG_OPEN |
539 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59540 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41541 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59542 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41543 base::File::FLAG_READ |
544 base::File::FLAG_WRITE));
[email protected]e54edc32010-09-28 01:09:19545
546 // Grant permissions for the file (should overwrite the permissions granted
547 // for the directory).
[email protected]bfcf1e92013-07-11 04:37:25548 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41549 base::File::FLAG_TEMPORARY);
[email protected]80838412012-11-20 01:53:59550 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41551 base::File::FLAG_OPEN));
[email protected]80838412012-11-20 01:53:59552 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41553 base::File::FLAG_TEMPORARY));
[email protected]77930fe2010-10-01 22:45:34554
555 // Revoke all permissions for the file (it should inherit its permissions
556 // from the directory again).
[email protected]80838412012-11-20 01:53:59557 p->RevokeAllPermissionsForFile(kRendererID, granted_file);
558 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41559 base::File::FLAG_OPEN |
560 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59561 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41562 base::File::FLAG_TEMPORARY));
[email protected]e54edc32010-09-28 01:09:19563 p->Remove(kRendererID);
[email protected]cee64fd32011-05-02 18:59:07564
565 // Grant file permissions for the file to main thread renderer process,
566 // make sure its worker thread renderer process inherits those.
567 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25568 GrantPermissionsForFile(p, kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41569 base::File::FLAG_OPEN |
570 base::File::FLAG_READ);
[email protected]80838412012-11-20 01:53:59571 EXPECT_TRUE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41572 base::File::FLAG_OPEN |
573 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59574 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41575 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07576 p->AddWorker(kWorkerRendererID, kRendererID);
[email protected]80838412012-11-20 01:53:59577 EXPECT_TRUE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41578 base::File::FLAG_OPEN |
579 base::File::FLAG_READ));
[email protected]80838412012-11-20 01:53:59580 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41581 base::File::FLAG_WRITE));
[email protected]cee64fd32011-05-02 18:59:07582 p->Remove(kRendererID);
[email protected]80838412012-11-20 01:53:59583 EXPECT_FALSE(p->HasPermissionsForFile(kWorkerRendererID, granted_file,
[email protected]2c288ed2014-06-05 22:07:41584 base::File::FLAG_OPEN |
585 base::File::FLAG_READ));
[email protected]cee64fd32011-05-02 18:59:07586 p->Remove(kWorkerRendererID);
[email protected]f0ecca4522013-01-07 21:50:56587
588 p->Add(kRendererID);
[email protected]bfcf1e92013-07-11 04:37:25589 GrantPermissionsForFile(p, kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41590 base::File::FLAG_OPEN);
[email protected]f0ecca4522013-01-07 21:50:56591 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, relative_file,
[email protected]2c288ed2014-06-05 22:07:41592 base::File::FLAG_OPEN));
[email protected]f0ecca4522013-01-07 21:50:56593 p->Remove(kRendererID);
[email protected]e54edc32010-09-28 01:09:19594}
595
[email protected]c50008512011-02-03 01:17:27596TEST_F(ChildProcessSecurityPolicyTest, CanServiceWebUIBindings) {
[email protected]b9535422012-02-09 01:47:59597 ChildProcessSecurityPolicyImpl* p =
598 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29599
[email protected]60e448982009-05-06 04:21:16600 GURL url("chrome://thumb/http://www.google.com/");
initial.commit09911bf2008-07-26 23:55:29601
602 p->Add(kRendererID);
603
[email protected]c50008512011-02-03 01:17:27604 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29605 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]c50008512011-02-03 01:17:27606 p->GrantWebUIBindings(kRendererID);
607 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29608 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
609
610 p->Remove(kRendererID);
611}
612
[email protected]f58ddcf2009-05-18 22:22:06613TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) {
[email protected]b9535422012-02-09 01:47:59614 ChildProcessSecurityPolicyImpl* p =
615 ChildProcessSecurityPolicyImpl::GetInstance();
initial.commit09911bf2008-07-26 23:55:29616
617 GURL url("file:///etc/passwd");
[email protected]2dec8ec2013-02-07 19:20:34618 base::FilePath file(TEST_PATH("/etc/passwd"));
initial.commit09911bf2008-07-26 23:55:29619
620 p->Add(kRendererID);
621
622 p->GrantRequestURL(kRendererID, url);
[email protected]e54edc32010-09-28 01:09:19623 p->GrantReadFile(kRendererID, file);
[email protected]c50008512011-02-03 01:17:27624 p->GrantWebUIBindings(kRendererID);
initial.commit09911bf2008-07-26 23:55:29625
626 EXPECT_TRUE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19627 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27628 EXPECT_TRUE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29629
630 p->Remove(kRendererID);
631
632 // Renderers are added and removed on the UI thread, but the policy can be
[email protected]580522632009-08-17 21:55:55633 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
634 // prepared to answer policy questions about renderers who no longer exist.
initial.commit09911bf2008-07-26 23:55:29635
636 // In this case, we default to secure behavior.
637 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
[email protected]e54edc32010-09-28 01:09:19638 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
[email protected]c50008512011-02-03 01:17:27639 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
initial.commit09911bf2008-07-26 23:55:29640}
[email protected]46488322012-10-30 03:22:20641
642} // namespace content