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

Side by Side Diff: chrome/browser/extensions/extension_service_unittest.cc

Issue 595363002: Add policy controlled permission block list for extensions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ext-fix
Patch Set: more minor format fix Created 6 years, 2 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/at_exit.h" 9 #include "base/at_exit.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "chrome/browser/extensions/external_install_error.h" 48 #include "chrome/browser/extensions/external_install_error.h"
49 #include "chrome/browser/extensions/external_install_manager.h" 49 #include "chrome/browser/extensions/external_install_manager.h"
50 #include "chrome/browser/extensions/external_policy_loader.h" 50 #include "chrome/browser/extensions/external_policy_loader.h"
51 #include "chrome/browser/extensions/external_pref_loader.h" 51 #include "chrome/browser/extensions/external_pref_loader.h"
52 #include "chrome/browser/extensions/external_provider_impl.h" 52 #include "chrome/browser/extensions/external_provider_impl.h"
53 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h" 53 #include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
54 #include "chrome/browser/extensions/installed_loader.h" 54 #include "chrome/browser/extensions/installed_loader.h"
55 #include "chrome/browser/extensions/pack_extension_job.h" 55 #include "chrome/browser/extensions/pack_extension_job.h"
56 #include "chrome/browser/extensions/pending_extension_info.h" 56 #include "chrome/browser/extensions/pending_extension_info.h"
57 #include "chrome/browser/extensions/pending_extension_manager.h" 57 #include "chrome/browser/extensions/pending_extension_manager.h"
58 #include "chrome/browser/extensions/permissions_updater.h"
58 #include "chrome/browser/extensions/test_blacklist.h" 59 #include "chrome/browser/extensions/test_blacklist.h"
59 #include "chrome/browser/extensions/test_extension_system.h" 60 #include "chrome/browser/extensions/test_extension_system.h"
60 #include "chrome/browser/extensions/unpacked_installer.h" 61 #include "chrome/browser/extensions/unpacked_installer.h"
61 #include "chrome/browser/extensions/updater/extension_updater.h" 62 #include "chrome/browser/extensions/updater/extension_updater.h"
62 #include "chrome/browser/prefs/pref_service_syncable.h" 63 #include "chrome/browser/prefs/pref_service_syncable.h"
63 #include "chrome/browser/sync/profile_sync_service.h" 64 #include "chrome/browser/sync/profile_sync_service.h"
64 #include "chrome/browser/sync/profile_sync_service_factory.h" 65 #include "chrome/browser/sync/profile_sync_service_factory.h"
65 #include "chrome/common/chrome_constants.h" 66 #include "chrome/common/chrome_constants.h"
66 #include "chrome/common/chrome_switches.h" 67 #include "chrome/common/chrome_switches.h"
67 #include "chrome/common/extensions/api/plugins/plugins_handler.h" 68 #include "chrome/common/extensions/api/plugins/plugins_handler.h"
(...skipping 25 matching lines...) Expand all
93 #include "extensions/browser/test_management_policy.h" 94 #include "extensions/browser/test_management_policy.h"
94 #include "extensions/browser/uninstall_reason.h" 95 #include "extensions/browser/uninstall_reason.h"
95 #include "extensions/common/constants.h" 96 #include "extensions/common/constants.h"
96 #include "extensions/common/extension.h" 97 #include "extensions/common/extension.h"
97 #include "extensions/common/extension_builder.h" 98 #include "extensions/common/extension_builder.h"
98 #include "extensions/common/extension_l10n_util.h" 99 #include "extensions/common/extension_l10n_util.h"
99 #include "extensions/common/extension_resource.h" 100 #include "extensions/common/extension_resource.h"
100 #include "extensions/common/feature_switch.h" 101 #include "extensions/common/feature_switch.h"
101 #include "extensions/common/manifest_constants.h" 102 #include "extensions/common/manifest_constants.h"
102 #include "extensions/common/manifest_handlers/background_info.h" 103 #include "extensions/common/manifest_handlers/background_info.h"
104 #include "extensions/common/manifest_handlers/permissions_parser.h"
103 #include "extensions/common/permissions/permission_set.h" 105 #include "extensions/common/permissions/permission_set.h"
104 #include "extensions/common/permissions/permissions_data.h" 106 #include "extensions/common/permissions/permissions_data.h"
105 #include "extensions/common/switches.h" 107 #include "extensions/common/switches.h"
106 #include "extensions/common/url_pattern.h" 108 #include "extensions/common/url_pattern.h"
107 #include "extensions/common/value_builder.h" 109 #include "extensions/common/value_builder.h"
108 #include "gpu/config/gpu_info.h" 110 #include "gpu/config/gpu_info.h"
109 #include "grit/browser_resources.h" 111 #include "grit/browser_resources.h"
110 #include "net/cookies/canonical_cookie.h" 112 #include "net/cookies/canonical_cookie.h"
111 #include "net/cookies/cookie_monster.h" 113 #include "net/cookies/cookie_monster.h"
112 #include "net/cookies/cookie_options.h" 114 #include "net/cookies/cookie_options.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 const char all_zero[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 184 const char all_zero[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
183 const char good2048[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh"; 185 const char good2048[] = "nmgjhmhbleinmjpbdhgajfjkbijcmgbh";
184 const char good_crx[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 186 const char good_crx[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
185 const char hosted_app[] = "kbmnembihfiondgfjekmnmcbddelicoi"; 187 const char hosted_app[] = "kbmnembihfiondgfjekmnmcbddelicoi";
186 const char page_action[] = "obcimlgaoabeegjmmpldobjndiealpln"; 188 const char page_action[] = "obcimlgaoabeegjmmpldobjndiealpln";
187 const char theme_crx[] = "iamefpfkojoapidjnbafmgkgncegbkad"; 189 const char theme_crx[] = "iamefpfkojoapidjnbafmgkgncegbkad";
188 const char theme2_crx[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 190 const char theme2_crx[] = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
189 const char permissions_crx[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin"; 191 const char permissions_crx[] = "eagpmdpfmaekmmcejjbmjoecnejeiiin";
190 const char unpacked[] = "cbcdidchbppangcjoddlpdjlenngjldk"; 192 const char unpacked[] = "cbcdidchbppangcjoddlpdjlenngjldk";
191 const char updates_from_webstore[] = "akjooamlhcgeopfifcmlggaebeocgokj"; 193 const char updates_from_webstore[] = "akjooamlhcgeopfifcmlggaebeocgokj";
194 const char permissions_blocklist[] = "noffkehfcaggllbcojjbopcmlhcnhcdn";
192 195
193 struct ExtensionsOrder { 196 struct ExtensionsOrder {
194 bool operator()(const scoped_refptr<const Extension>& a, 197 bool operator()(const scoped_refptr<const Extension>& a,
195 const scoped_refptr<const Extension>& b) { 198 const scoped_refptr<const Extension>& b) {
196 return a->name() < b->name(); 199 return a->name() < b->name();
197 } 200 }
198 }; 201 };
199 202
200 static std::vector<base::string16> GetErrors() { 203 static std::vector<base::string16> GetErrors() {
201 const std::vector<base::string16>* errors = 204 const std::vector<base::string16>* errors =
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 " \"id\": 1,\n" 768 " \"id\": 1,\n"
766 " \"features\": [\"webgl\"]\n" 769 " \"features\": [\"webgl\"]\n"
767 " }\n" 770 " }\n"
768 " ]\n" 771 " ]\n"
769 "}"; 772 "}";
770 gpu::GPUInfo gpu_info; 773 gpu::GPUInfo gpu_info;
771 content::GpuDataManager::GetInstance()->InitializeForTesting( 774 content::GpuDataManager::GetInstance()->InitializeForTesting(
772 json_blacklist, gpu_info); 775 json_blacklist, gpu_info);
773 } 776 }
774 777
778 // Grants all optional permissions stated in manifest to active permission
779 // set for extension |id|.
780 void GrantAllOptionalPermissions(std::string id) {
Joao da Silva 2014/10/15 14:39:26 const std::string&
binjin 2014/10/16 18:13:58 Done.
781 const Extension* extension = service()->GetInstalledExtension(id);
782 scoped_refptr<const PermissionSet> all_optional_permissions =
783 extensions::PermissionsParser::GetOptionalPermissions(extension);
784 extensions::PermissionsUpdater perms_updater(profile());
785 perms_updater.AddPermissions(extension, all_optional_permissions.get());
786 }
787
775 // Helper method to set up a WindowedNotificationObserver to wait for a 788 // Helper method to set up a WindowedNotificationObserver to wait for a
776 // specific CrxInstaller to finish if we don't know the value of the 789 // specific CrxInstaller to finish if we don't know the value of the
777 // |installer| yet. 790 // |installer| yet.
778 static bool IsCrxInstallerDone(extensions::CrxInstaller** installer, 791 static bool IsCrxInstallerDone(extensions::CrxInstaller** installer,
779 const content::NotificationSource& source, 792 const content::NotificationSource& source,
780 const content::NotificationDetails& details) { 793 const content::NotificationDetails& details) {
781 return content::Source<extensions::CrxInstaller>(source).ptr() == 794 return content::Source<extensions::CrxInstaller>(source).ptr() ==
782 *installer; 795 *installer;
783 } 796 }
784 797
(...skipping 3059 matching lines...) Expand 10 before | Expand all | Expand 10 after
3844 extensions::TestManagementPolicyProvider provider( 3857 extensions::TestManagementPolicyProvider provider(
3845 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED); 3858 extensions::TestManagementPolicyProvider::MUST_REMAIN_ENABLED);
3846 GetManagementPolicy()->RegisterProvider(&provider); 3859 GetManagementPolicy()->RegisterProvider(&provider);
3847 3860
3848 // Reinstall the extension. 3861 // Reinstall the extension.
3849 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED); 3862 InstallCRX(data_dir().AppendASCII("good.crx"), INSTALL_UPDATED);
3850 EXPECT_EQ(1u, registry()->enabled_extensions().size()); 3863 EXPECT_EQ(1u, registry()->enabled_extensions().size());
3851 EXPECT_EQ(0u, registry()->disabled_extensions().size()); 3864 EXPECT_EQ(0u, registry()->disabled_extensions().size());
3852 } 3865 }
3853 3866
3867 // Tests that extensions with conflicting required permissions by enterprise
3868 // policy cannot be installed.
3869 TEST_F(ExtensionServiceTest, PolicyBlockedPermissionNewExtensionInstall) {
3870 InitializeEmptyExtensionServiceWithTestingPrefs();
3871 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
3872
3873 {
3874 // Update policy to block one of the required permissions of target.
3875 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3876 pref.AddBlockedPermission("*", "tabs");
3877 }
3878
3879 // The extension should be failed to install.
3880 PackAndInstallCRX(path, INSTALL_FAILED);
3881
3882 {
3883 // Update policy to block one of the optional permissions instead.
3884 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3885 pref.ClearBlockedPermissions("*");
3886 pref.AddBlockedPermission("*", "history");
3887 }
3888
3889 // The extension should failed to install.
Joao da Silva 2014/10/15 14:39:26 Should succeed?
binjin 2014/10/16 18:13:58 Done.
3890 PackAndInstallCRX(path, INSTALL_NEW);
3891 }
3892
3893 // Tests that extension supposed to be force installed but with conflicting
3894 // required permissions cannot be installed.
3895 TEST_F(ExtensionServiceTest, PolicyBlockedPermissionConflictsWithForceInstall) {
3896 InitializeEmptyExtensionServiceWithTestingPrefs();
3897
3898 // Pack the crx file.
3899 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
3900 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
3901 base::ScopedTempDir temp_dir;
3902 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
3903 base::FilePath crx_path = temp_dir.path().AppendASCII("temp.crx");
3904
3905 PackCRX(path, pem_path, crx_path);
Joao da Silva 2014/10/15 14:39:26 Assign this to a "const Extension* extension" and
binjin 2014/10/16 18:13:58 PackCRX() only create an crx_file and returns noth
3906
3907 {
3908 // Block one of the required permissions.
3909 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3910 pref.AddBlockedPermission("*", "tabs");
3911 }
3912
3913 // Use MockExtensionProvider to simulate force installing extension.
3914 MockExtensionProvider* provider =
3915 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
3916 AddMockExternalProvider(provider);
3917 provider->UpdateOrAddExtension(permissions_blocklist, "1.0", crx_path);
3918
3919 {
3920 // Attempts to force install this extension.
3921 content::WindowedNotificationObserver observer(
3922 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
3923 content::NotificationService::AllSources());
3924 service()->CheckForExternalUpdates();
3925 observer.Wait();
3926 }
3927
3928 // The extension should not be installed.
3929 ASSERT_FALSE(service()->GetInstalledExtension(permissions_blocklist));
3930
3931 // Remove this extension from pending extension manager as we would like to
3932 // give another attempt later.
3933 service()->pending_extension_manager()->Remove(permissions_blocklist);
3934
3935 {
3936 // Clears the permission block list.
3937 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3938 pref.ClearBlockedPermissions("*");
3939 }
3940
3941 {
3942 // Attempts to force install this extension again.
3943 content::WindowedNotificationObserver observer(
3944 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
3945 content::NotificationService::AllSources());
3946 service()->CheckForExternalUpdates();
3947 observer.Wait();
3948 }
3949
3950 const Extension* installed =
3951 service()->GetInstalledExtension(permissions_blocklist);
3952 ASSERT_TRUE(installed);
3953 EXPECT_EQ(installed->location(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
3954 }
3955
3956 // Tests that newer versions of an extension with conflicting required
3957 // permissions by enterprise policy cannot be updated to.
3958 TEST_F(ExtensionServiceTest, PolicyBlockedPermissionExtensionUpdate) {
3959 InitializeEmptyExtensionServiceWithTestingPrefs();
3960
3961 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
3962 base::FilePath path2 = data_dir().AppendASCII("permissions_blocklist2");
3963 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
3964
3965 // Install 'permissions_blocklist'.
3966 const Extension* installed = PackAndInstallCRX(path, pem_path, INSTALL_NEW);
3967 EXPECT_EQ(installed->id(), permissions_blocklist);
3968
3969 {
3970 // Block one of the required permissions of 'permissions_blocklist2'.
3971 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
3972 pref.AddBlockedPermission("*", "downloads");
3973 }
3974
3975 // Install 'permissions_blocklist' again, should be updated.
3976 const Extension* updated = PackAndInstallCRX(path, pem_path, INSTALL_UPDATED);
3977 EXPECT_EQ(updated->id(), permissions_blocklist);
3978
3979 std::string old_version = updated->VersionString();
3980
3981 // Attempts to update to 'permissions_blocklist2' should fail.
3982 PackAndInstallCRX(path2, pem_path, INSTALL_FAILED);
3983
3984 // Verify that the old version is still enabled.
3985 updated = service()->GetExtensionById(permissions_blocklist, false);
3986 ASSERT_TRUE(updated);
3987 EXPECT_EQ(old_version, updated->VersionString());
3988 }
3989
3990 // Tests that policy update with additional permissions blocked revoke
3991 // conflicting granted optional permissions and unload extensions with
3992 // conflicting required permissions, including the force installed ones.
3993 TEST_F(ExtensionServiceTest, PolicyBlockedPermissionPolicyUpdate) {
3994 InitializeEmptyExtensionServiceWithTestingPrefs();
3995
3996 base::FilePath path = data_dir().AppendASCII("permissions_blocklist");
3997 base::FilePath path2 = data_dir().AppendASCII("permissions_blocklist2");
3998 base::FilePath pem_path = data_dir().AppendASCII("permissions_blocklist.pem");
3999
4000 // Pack the crx file.
4001 base::ScopedTempDir temp_dir;
4002 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
4003 base::FilePath crx_path = temp_dir.path().AppendASCII("temp.crx");
4004
4005 PackCRX(path2, pem_path, crx_path);
Joao da Silva 2014/10/15 14:39:26 Check the ID of the returned Extension*
binjin 2014/10/16 18:13:58 Same as above.
4006
4007 // Install two arbitary extensions with specified manifest.
4008 std::string ext1 = PackAndInstallCRX(path, INSTALL_NEW)->id();
4009 std::string ext2 = PackAndInstallCRX(path2, INSTALL_NEW)->id();
4010 ASSERT_NE(ext1, permissions_blocklist);
4011 ASSERT_NE(ext2, permissions_blocklist);
4012 ASSERT_NE(ext1, ext2);
4013
4014 // Force install another extension with known id and same manifest as 'ext2'.
4015 std::string ext2_forced = permissions_blocklist;
4016 MockExtensionProvider* provider =
4017 new MockExtensionProvider(service(), Manifest::EXTERNAL_POLICY_DOWNLOAD);
4018 AddMockExternalProvider(provider);
4019 provider->UpdateOrAddExtension(ext2_forced, "2.0", crx_path);
4020
4021 content::WindowedNotificationObserver observer(
4022 extensions::NOTIFICATION_CRX_INSTALLER_DONE,
4023 content::NotificationService::AllSources());
4024 service()->CheckForExternalUpdates();
4025 observer.Wait();
4026
4027 extensions::ExtensionRegistry* registry =
4028 extensions::ExtensionRegistry::Get(profile());
4029
4030 // Verify all three extensions are installed and enabled.
4031 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext1));
4032 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext2));
4033 ASSERT_TRUE(registry->enabled_extensions().GetByID(ext2_forced));
4034
4035 // Grant all optional permissions to each extension.
4036 GrantAllOptionalPermissions(ext1);
4037 GrantAllOptionalPermissions(ext2);
4038 GrantAllOptionalPermissions(ext2_forced);
4039
4040 EXPECT_TRUE(ExtensionPrefs::Get(profile())
4041 ->GetActivePermissions(ext1)
4042 ->HasAPIPermission(extensions::APIPermission::kDownloads));
4043
4044 // Set policy to block 'downloads' permission.
4045 {
4046 ManagementPrefUpdater pref(profile_->GetTestingPrefService());
4047 pref.AddBlockedPermission("*", "downloads");
4048 }
4049
4050 base::RunLoop().RunUntilIdle();
4051
4052 // 'ext1' should still be enabled, but with 'downloads' permission revoked.
4053 EXPECT_TRUE(registry->enabled_extensions().GetByID(ext1));
4054 EXPECT_FALSE(ExtensionPrefs::Get(profile())
4055 ->GetActivePermissions(ext1)
4056 ->HasAPIPermission(extensions::APIPermission::kDownloads));
4057
4058 // 'ext2' should be disabled because one of its required permissions is
4059 // blocked.
4060 EXPECT_FALSE(registry->enabled_extensions().GetByID(ext2));
4061
4062 // 'ext2_forced' should be handled the same as 'ext2'
4063 EXPECT_FALSE(registry->enabled_extensions().GetByID(ext2_forced));
4064 }
4065
3854 // Flaky on windows; http://crbug.com/309833 4066 // Flaky on windows; http://crbug.com/309833
3855 #if defined(OS_WIN) 4067 #if defined(OS_WIN)
3856 #define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAut oAcknowledgement 4068 #define MAYBE_ExternalExtensionAutoAcknowledgement DISABLED_ExternalExtensionAut oAcknowledgement
3857 #else 4069 #else
3858 #define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowle dgement 4070 #define MAYBE_ExternalExtensionAutoAcknowledgement ExternalExtensionAutoAcknowle dgement
3859 #endif 4071 #endif
3860 TEST_F(ExtensionServiceTest, MAYBE_ExternalExtensionAutoAcknowledgement) { 4072 TEST_F(ExtensionServiceTest, MAYBE_ExternalExtensionAutoAcknowledgement) {
3861 InitializeEmptyExtensionService(); 4073 InitializeEmptyExtensionService();
3862 service()->set_extensions_enabled(true); 4074 service()->set_extensions_enabled(true);
3863 4075
(...skipping 3274 matching lines...) Expand 10 before | Expand all | Expand 10 after
7138 7350
7139 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED, 7351 service()->Observe(chrome::NOTIFICATION_PROFILE_DESTRUCTION_STARTED,
7140 content::Source<Profile>(profile()), 7352 content::Source<Profile>(profile()),
7141 content::NotificationService::NoDetails()); 7353 content::NotificationService::NoDetails());
7142 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_); 7354 EXPECT_EQ(UnloadedExtensionInfo::REASON_PROFILE_SHUTDOWN, unloaded_reason_);
7143 EXPECT_EQ(0u, registry()->enabled_extensions().size()); 7355 EXPECT_EQ(0u, registry()->enabled_extensions().size());
7144 EXPECT_EQ(0u, registry()->disabled_extensions().size()); 7356 EXPECT_EQ(0u, registry()->disabled_extensions().size());
7145 EXPECT_EQ(0u, registry()->terminated_extensions().size()); 7357 EXPECT_EQ(0u, registry()->terminated_extensions().size());
7146 EXPECT_EQ(0u, registry()->blacklisted_extensions().size()); 7358 EXPECT_EQ(0u, registry()->blacklisted_extensions().size());
7147 } 7359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698