Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: content/browser/child_process_security_policy_unittest.cc

Issue 19599006: ChildProcessSecurityPolicy: Deprecate bitmask-based permissions checks for files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test on win Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set> 5 #include <set>
6 #include <string> 6 #include <string>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/platform_file.h" 10 #include "base/platform_file.h"
11 #include "content/browser/child_process_security_policy_impl.h" 11 #include "content/browser/child_process_security_policy_impl.h"
12 #include "content/public/common/url_constants.h" 12 #include "content/public/common/url_constants.h"
13 #include "content/test/test_content_browser_client.h" 13 #include "content/test/test_content_browser_client.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h" 15 #include "url/gurl.h"
16 #include "webkit/browser/fileapi/file_permission_policy.h"
17 #include "webkit/browser/fileapi/file_system_url.h"
16 #include "webkit/browser/fileapi/isolated_context.h" 18 #include "webkit/browser/fileapi/isolated_context.h"
19 #include "webkit/common/fileapi/file_system_types.h"
17 20
18 namespace content { 21 namespace content {
19 namespace { 22 namespace {
20 23
21 const int kRendererID = 42; 24 const int kRendererID = 42;
22 const int kWorkerRendererID = kRendererID + 1; 25 const int kWorkerRendererID = kRendererID + 1;
23 26
24 #if defined(FILE_PATH_USES_DRIVE_LETTERS) 27 #if defined(FILE_PATH_USES_DRIVE_LETTERS)
25 #define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x) 28 #define TEST_PATH(x) FILE_PATH_LITERAL("c:") FILE_PATH_LITERAL(x)
26 #else 29 #else
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 const base::FilePath& file, 87 const base::FilePath& file,
85 int permissions) { 88 int permissions) {
86 p->GrantPermissionsForFile(child_id, file, permissions); 89 p->GrantPermissionsForFile(child_id, file, permissions);
87 } 90 }
88 91
89 private: 92 private:
90 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_; 93 ChildProcessSecurityPolicyTestBrowserClient test_browser_client_;
91 ContentBrowserClient* old_browser_client_; 94 ContentBrowserClient* old_browser_client_;
92 }; 95 };
93 96
97
94 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) { 98 TEST_F(ChildProcessSecurityPolicyTest, IsWebSafeSchemeTest) {
95 ChildProcessSecurityPolicyImpl* p = 99 ChildProcessSecurityPolicyImpl* p =
96 ChildProcessSecurityPolicyImpl::GetInstance(); 100 ChildProcessSecurityPolicyImpl::GetInstance();
97 101
98 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme)); 102 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpScheme));
99 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme)); 103 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kHttpsScheme));
100 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme)); 104 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kFtpScheme));
101 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme)); 105 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kDataScheme));
102 EXPECT_TRUE(p->IsWebSafeScheme("feed")); 106 EXPECT_TRUE(p->IsWebSafeScheme("feed"));
103 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme)); 107 EXPECT_TRUE(p->IsWebSafeScheme(chrome::kBlobScheme));
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id)); 359 EXPECT_FALSE(p->CanReadWriteFileSystem(kRendererID, copy_into_id));
356 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id)); 360 EXPECT_FALSE(p->CanCopyIntoFileSystem(kRendererID, copy_into_id));
357 361
358 // Cleanup. 362 // Cleanup.
359 p->Remove(kRendererID); 363 p->Remove(kRendererID);
360 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_id); 364 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_id);
361 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id); 365 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(read_write_id);
362 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id); 366 fileapi::IsolatedContext::GetInstance()->RevokeFileSystem(copy_into_id);
363 } 367 }
364 368
365 TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) { 369 TEST_F(ChildProcessSecurityPolicyTest, FilePermissionGrantingAndRevoking) {
366 ChildProcessSecurityPolicyImpl* p = 370 ChildProcessSecurityPolicyImpl* p =
367 ChildProcessSecurityPolicyImpl::GetInstance(); 371 ChildProcessSecurityPolicyImpl::GetInstance();
368 372
373 p->RegisterFileSystemPermissionPolicy(
374 fileapi::kFileSystemTypeTest,
375 fileapi::FILE_PERMISSION_USE_FILE_PERMISSION);
376
369 p->Add(kRendererID); 377 p->Add(kRendererID);
378 base::FilePath file(TEST_PATH("/dir/testfile"));
379 file = file.NormalizePathSeparators();
380 fileapi::FileSystemURL url = fileapi::FileSystemURL::CreateForTest(
381 GURL("http://foo/"), fileapi::kFileSystemTypeTest, file);
370 382
371 EXPECT_FALSE(p->CanReadFile(kRendererID, 383 // Test initially having no permissions.
372 base::FilePath(TEST_PATH("/etc/passwd")))); 384 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
373 p->GrantReadFile(kRendererID, base::FilePath(TEST_PATH("/etc/passwd"))); 385 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
374 EXPECT_TRUE(p->CanReadFile(kRendererID, 386 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
375 base::FilePath(TEST_PATH("/etc/passwd")))); 387 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
376 EXPECT_FALSE(p->CanReadFile(kRendererID, 388 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
377 base::FilePath(TEST_PATH("/etc/shadow")))); 389 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
390 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
391 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
378 392
393 // Testing every combination of permissions granting and revoking.
394 p->GrantReadFile(kRendererID, file);
395 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
396 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
397 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
398 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
399 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
400 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
401 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
402 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
403 p->RevokeAllPermissionsForFile(kRendererID, file);
404 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
405 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
406 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
407 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
408 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
409 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
410 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
411 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
412
413 p->GrantCreateReadWriteFile(kRendererID, file);
414 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
415 EXPECT_TRUE(p->CanWriteFile(kRendererID, file));
416 EXPECT_TRUE(p->CanCreateFile(kRendererID, file));
417 EXPECT_TRUE(p->CanCreateWriteFile(kRendererID, file));
418 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
419 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
420 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
421 EXPECT_TRUE(p->CanCreateWriteFileSystemFile(kRendererID, url));
422 p->RevokeAllPermissionsForFile(kRendererID, file);
423 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
424 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
425 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
426 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
427 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
428 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
429 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
430 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
431
432 p->GrantCreateWriteFile(kRendererID, file);
433 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
434 EXPECT_TRUE(p->CanWriteFile(kRendererID, file));
435 EXPECT_TRUE(p->CanCreateFile(kRendererID, file));
436 EXPECT_TRUE(p->CanCreateWriteFile(kRendererID, file));
437 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
438 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
439 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
440 EXPECT_TRUE(p->CanCreateWriteFileSystemFile(kRendererID, url));
441 p->RevokeAllPermissionsForFile(kRendererID, file);
442 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
443 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
444 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
445 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
446 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
447 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
448 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
449 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
450
451 // Test revoke permissions on renderer ID removal.
452 p->GrantCreateReadWriteFile(kRendererID, file);
453 EXPECT_TRUE(p->CanReadFile(kRendererID, file));
454 EXPECT_TRUE(p->CanWriteFile(kRendererID, file));
455 EXPECT_TRUE(p->CanCreateFile(kRendererID, file));
456 EXPECT_TRUE(p->CanCreateWriteFile(kRendererID, file));
457 EXPECT_TRUE(p->CanReadFileSystemFile(kRendererID, url));
458 EXPECT_TRUE(p->CanWriteFileSystemFile(kRendererID, url));
459 EXPECT_TRUE(p->CanCreateFileSystemFile(kRendererID, url));
460 EXPECT_TRUE(p->CanCreateWriteFileSystemFile(kRendererID, url));
379 p->Remove(kRendererID); 461 p->Remove(kRendererID);
462 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
463 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
464 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
465 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
466 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
467 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
468 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
469 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
470
471 // Test having no permissions upon re-adding same renderer ID.
380 p->Add(kRendererID); 472 p->Add(kRendererID);
473 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
474 EXPECT_FALSE(p->CanWriteFile(kRendererID, file));
475 EXPECT_FALSE(p->CanCreateFile(kRendererID, file));
476 EXPECT_FALSE(p->CanCreateWriteFile(kRendererID, file));
477 EXPECT_FALSE(p->CanReadFileSystemFile(kRendererID, url));
478 EXPECT_FALSE(p->CanWriteFileSystemFile(kRendererID, url));
479 EXPECT_FALSE(p->CanCreateFileSystemFile(kRendererID, url));
480 EXPECT_FALSE(p->CanCreateWriteFileSystemFile(kRendererID, url));
381 481
382 EXPECT_FALSE(p->CanReadFile(kRendererID, 482 // Cleanup.
383 base::FilePath(TEST_PATH("/etc/passwd"))));
384 EXPECT_FALSE(p->CanReadFile(kRendererID,
385 base::FilePath(TEST_PATH("/etc/shadow"))));
386
387 p->Remove(kRendererID); 483 p->Remove(kRendererID);
388 } 484 }
389 485
390 TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) { 486 TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) {
391 ChildProcessSecurityPolicyImpl* p = 487 ChildProcessSecurityPolicyImpl* p =
392 ChildProcessSecurityPolicyImpl::GetInstance(); 488 ChildProcessSecurityPolicyImpl::GetInstance();
393 489
394 p->Add(kRendererID); 490 p->Add(kRendererID);
395 491
396 EXPECT_FALSE(p->CanReadDirectory(kRendererID, 492 EXPECT_FALSE(p->CanReadDirectory(kRendererID,
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be 708 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be
613 // prepared to answer policy questions about renderers who no longer exist. 709 // prepared to answer policy questions about renderers who no longer exist.
614 710
615 // In this case, we default to secure behavior. 711 // In this case, we default to secure behavior.
616 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); 712 EXPECT_FALSE(p->CanRequestURL(kRendererID, url));
617 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); 713 EXPECT_FALSE(p->CanReadFile(kRendererID, file));
618 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); 714 EXPECT_FALSE(p->HasWebUIBindings(kRendererID));
619 } 715 }
620 716
621 } // namespace content 717 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/child_process_security_policy_impl.cc ('k') | content/public/browser/child_process_security_policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698