Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/omaha_attributes_handler.h" |
| 6 | |
| 7 | #include "base/test/metrics/histogram_tester.h" |
| 8 | #include "base/values.h" |
| 9 | #include "chrome/browser/extensions/extension_service.h" |
| 10 | #include "chrome/browser/extensions/extension_service_test_base.h" |
Xinghui Lu | 574a067 | 2021-07-15 19:54:49 | [diff] [blame] | 11 | #include "extensions/browser/blocklist_extension_prefs.h" |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 12 | #include "extensions/browser/disable_reason.h" |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 13 | #include "extensions/browser/extension_prefs.h" |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 14 | #include "extensions/test/extension_state_tester.h" |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Extension ids used during testing. |
| 22 | constexpr char kTestExtensionId[] = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | // Test suite to test Omaha attribute handler. |
Xinghui Lu | e6ff51b | 2021-12-29 20:07:38 | [diff] [blame] | 27 | using OmahaAttributesHandlerUnitTest = ExtensionServiceTestBase; |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 28 | |
| 29 | TEST_F(OmahaAttributesHandlerUnitTest, LogPolicyViolationUWSMetrics) { |
| 30 | base::HistogramTester histograms; |
Xinghui Lu | a1be2c0 | 2021-11-03 07:50:34 | [diff] [blame] | 31 | InitializeGoodInstalledExtensionService(); |
| 32 | service()->Init(); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 33 | base::Value attributes(base::Value::Type::DICTIONARY); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 34 | attributes.SetBoolKey("_policy_violation", true); |
| 35 | attributes.SetBoolKey("_potentially_uws", true); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 36 | |
| 37 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 38 | |
| 39 | histograms.ExpectBucketCount( |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 40 | "Extensions.ExtensionDisabledRemotely2", |
| 41 | /*sample=*/ExtensionUpdateCheckDataKey::kPotentiallyUWS, |
| 42 | /*expected_count=*/1); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 43 | histograms.ExpectBucketCount( |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 44 | "Extensions.ExtensionAddDisabledRemotelyReason2", |
| 45 | /*sample=*/ExtensionUpdateCheckDataKey::kPotentiallyUWS, |
| 46 | /*expected_count=*/1); |
| 47 | histograms.ExpectBucketCount( |
| 48 | "Extensions.ExtensionDisabledRemotely2", |
| 49 | /*sample=*/ExtensionUpdateCheckDataKey::kPolicyViolation, |
| 50 | /*expected_count=*/1); |
| 51 | histograms.ExpectBucketCount( |
| 52 | "Extensions.ExtensionAddDisabledRemotelyReason2", |
| 53 | /*sample=*/ExtensionUpdateCheckDataKey::kPolicyViolation, |
| 54 | /*expected_count=*/1); |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 55 | } |
| 56 | |
Xinghui Lu | a1be2c0 | 2021-11-03 07:50:34 | [diff] [blame] | 57 | TEST_F(OmahaAttributesHandlerUnitTest, LogMalwareMetrics) { |
| 58 | base::HistogramTester histograms; |
| 59 | InitializeGoodInstalledExtensionService(); |
| 60 | service()->Init(); |
| 61 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 62 | |
| 63 | attributes.SetBoolKey("_malware", false); |
| 64 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 65 | // The re-enabled metric should not be logged if the extension is not disabled |
| 66 | // previously. |
| 67 | histograms.ExpectBucketCount("Extensions.ExtensionReenabledRemotely", |
| 68 | /*sample=*/ExtensionUpdateCheckDataKey::kMalware, |
| 69 | /*expected_count=*/0); |
| 70 | histograms.ExpectBucketCount("Extensions.ExtensionDisabledRemotely2", |
| 71 | /*sample=*/ExtensionUpdateCheckDataKey::kNoKey, |
| 72 | /*expected_count=*/1); |
| 73 | |
| 74 | attributes.SetBoolKey("_malware", true); |
| 75 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 76 | histograms.ExpectBucketCount("Extensions.ExtensionDisabledRemotely2", |
| 77 | /*sample=*/ExtensionUpdateCheckDataKey::kMalware, |
| 78 | /*expected_count=*/1); |
| 79 | histograms.ExpectBucketCount("Extensions.ExtensionAddDisabledRemotelyReason2", |
| 80 | /*sample=*/ExtensionUpdateCheckDataKey::kMalware, |
| 81 | /*expected_count=*/1); |
| 82 | |
| 83 | attributes.SetBoolKey("_malware", false); |
| 84 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 85 | histograms.ExpectBucketCount("Extensions.ExtensionReenabledRemotely", |
| 86 | /*sample=*/ExtensionUpdateCheckDataKey::kMalware, |
| 87 | /*expected_count=*/1); |
| 88 | histograms.ExpectBucketCount("Extensions.ExtensionDisabledRemotely2", |
| 89 | /*sample=*/ExtensionUpdateCheckDataKey::kNoKey, |
| 90 | /*expected_count=*/2); |
| 91 | } |
| 92 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 93 | TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPolicyViolation) { |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 94 | base::HistogramTester histograms; |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 95 | InitializeGoodInstalledExtensionService(); |
| 96 | service()->Init(); |
| 97 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 98 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 99 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 100 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 101 | |
| 102 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 103 | attributes.SetBoolKey("_policy_violation", true); |
| 104 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 105 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 106 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 107 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 108 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 109 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 110 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 111 | prefs)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 112 | |
| 113 | // Remove extensions from greylist. |
| 114 | attributes.SetBoolKey("_policy_violation", false); |
| 115 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 116 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 117 | // The extension is re-enabled. |
| 118 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 119 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 120 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 121 | prefs)); |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 122 | histograms.ExpectBucketCount( |
| 123 | "Extensions.ExtensionReenabledRemotelyForPolicyViolation", |
| 124 | /*sample=*/1, |
| 125 | /*expected_count=*/1); |
| 126 | histograms.ExpectBucketCount( |
| 127 | "Extensions.ExtensionReenabledRemotelyForPotentiallyUWS", |
| 128 | /*sample=*/1, |
| 129 | /*expected_count=*/0); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 130 | } |
| 131 | |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 132 | TEST_F(OmahaAttributesHandlerUnitTest, DisableRemotelyForPotentiallyUws) { |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 133 | base::HistogramTester histograms; |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 134 | InitializeGoodInstalledExtensionService(); |
| 135 | service()->Init(); |
| 136 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 137 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 138 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 139 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 140 | |
| 141 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 142 | attributes.SetBoolKey("_potentially_uws", true); |
| 143 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 144 | |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 145 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 146 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 147 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 148 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 149 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 150 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 151 | |
| 152 | // Remove extensions from greylist. |
| 153 | attributes.SetBoolKey("_potentially_uws", false); |
| 154 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 155 | |
| 156 | // The extension is re-enabled. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 157 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
| 158 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 159 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 160 | prefs)); |
Xinghui Lu | 09c221f | 2021-06-12 02:43:41 | [diff] [blame] | 161 | histograms.ExpectBucketCount( |
| 162 | "Extensions.ExtensionReenabledRemotelyForPotentiallyUWS", |
| 163 | /*sample=*/1, |
| 164 | /*expected_count=*/1); |
| 165 | histograms.ExpectBucketCount( |
| 166 | "Extensions.ExtensionReenabledRemotelyForPolicyViolation", |
| 167 | /*sample=*/1, |
| 168 | /*expected_count=*/0); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | TEST_F(OmahaAttributesHandlerUnitTest, MultipleGreylistStates) { |
| 172 | InitializeGoodInstalledExtensionService(); |
| 173 | service()->Init(); |
| 174 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 175 | ExtensionStateTester state_tester(profile()); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 176 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 177 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 178 | |
| 179 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 180 | attributes.SetBoolKey("_policy_violation", true); |
| 181 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 182 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 183 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 184 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 185 | |
| 186 | // Now user enables kTestExtensionId. |
| 187 | service()->EnableExtension(kTestExtensionId); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 188 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 189 | |
| 190 | // Another greylist state is added to Omaha attribute. |
| 191 | attributes.SetBoolKey("_potentially_uws", true); |
| 192 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 193 | |
| 194 | // The extension should be disabled again. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 195 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 196 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 197 | |
| 198 | // Remove extensions from the first greylist state. |
| 199 | attributes.SetBoolKey("_policy_violation", false); |
| 200 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 201 | |
| 202 | // The extension should still be disabled, because it is still in the |
| 203 | // potentially unwanted state. |
| 204 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 205 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 206 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 207 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 208 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_CWS_POLICY_VIOLATION, |
| 209 | prefs)); |
| 210 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 211 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 212 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 213 | |
| 214 | // Remove the other greylist state. |
| 215 | attributes.SetBoolKey("_potentially_uws", false); |
| 216 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 217 | |
| 218 | // The extension is re-enabled. |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 219 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 220 | EXPECT_FALSE(blocklist_prefs::HasOmahaBlocklistState( |
| 221 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_POTENTIALLY_UNWANTED, |
| 222 | prefs)); |
Xinghui Lu | bfcb30a | 2021-05-19 20:49:16 | [diff] [blame] | 223 | } |
| 224 | |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 225 | TEST_F(OmahaAttributesHandlerUnitTest, KeepDisabledWhenMalwareRemoved) { |
| 226 | InitializeGoodInstalledExtensionService(); |
| 227 | service()->Init(); |
| 228 | |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 229 | ExtensionStateTester state_tester(profile()); |
| 230 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 231 | |
| 232 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 233 | attributes.SetBoolKey("_malware", true); |
| 234 | attributes.SetBoolKey("_policy_violation", true); |
| 235 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 236 | |
| 237 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 238 | EXPECT_TRUE(state_tester.ExpectBlocklisted(kTestExtensionId)); |
Xinghui Lu | 7e27722 | 2021-10-12 20:25:10 | [diff] [blame] | 239 | EXPECT_TRUE(blocklist_prefs::HasOmahaBlocklistState( |
| 240 | kTestExtensionId, BitMapBlocklistState::BLOCKLISTED_MALWARE, prefs)); |
| 241 | EXPECT_EQ(disable_reason::DISABLE_GREYLIST, |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 242 | prefs->GetDisableReasons(kTestExtensionId)); |
| 243 | |
| 244 | // Remove malware. |
| 245 | attributes.SetBoolKey("_malware", false); |
| 246 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 247 | |
| 248 | // The extension is not enabled because the policy violation bit is not |
Devlin Cronin | d025bf8 | 2021-05-20 19:42:27 | [diff] [blame] | 249 | // cleared, but it is no longer blocklisted (instead just disabled). |
| 250 | EXPECT_TRUE(state_tester.ExpectDisabledWithSingleReason( |
| 251 | kTestExtensionId, disable_reason::DISABLE_GREYLIST)); |
Xinghui Lu | 778ea58 | 2021-05-18 23:47:31 | [diff] [blame] | 252 | } |
| 253 | |
Xinghui Lu | 4819431 | 2022-06-01 01:30:52 | [diff] [blame] | 254 | TEST_F(OmahaAttributesHandlerUnitTest, ExtensionUninstalledBeforeNotified) { |
| 255 | InitializeGoodInstalledExtensionService(); |
| 256 | service()->Init(); |
| 257 | |
| 258 | ExtensionStateTester state_tester(profile()); |
| 259 | |
| 260 | EXPECT_TRUE(state_tester.ExpectEnabled(kTestExtensionId)); |
| 261 | |
| 262 | service()->UninstallExtension(kTestExtensionId, UNINSTALL_REASON_FOR_TESTING, |
| 263 | nullptr); |
| 264 | |
| 265 | base::Value attributes(base::Value::Type::DICTIONARY); |
| 266 | attributes.SetBoolKey("_malware", true); |
| 267 | // kTestExtensionId is already uninstalled. Performing action on it should |
| 268 | // not crash. Regression test for https://crbug.com/1305490. |
| 269 | service()->PerformActionBasedOnOmahaAttributes(kTestExtensionId, attributes); |
| 270 | } |
| 271 | |
Xinghui Lu | 259be4d | 2021-04-21 20:03:35 | [diff] [blame] | 272 | } // namespace extensions |