blob: eb1fcc78a0bcdf2ad32c0602355f385b0af8aec8 [file] [log] [blame]
[email protected]ae7bf342014-08-06 18:03:471// Copyright 2014 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
dcheng4af48582016-04-19 00:29:355#include <memory>
[email protected]ae7bf342014-08-06 18:03:476#include <string>
vabr9984ea62017-04-10 11:33:497#include <utility>
[email protected]ae7bf342014-08-06 18:03:478
blundell6e85b7c2015-09-29 12:33:359#include "base/base_switches.h"
[email protected]ae7bf342014-08-06 18:03:4710#include "base/command_line.h"
[email protected]ae7bf342014-08-06 18:03:4711#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0412#include "base/files/file_util.h"
[email protected]ae7bf342014-08-06 18:03:4713#include "base/json/json_file_value_serializer.h"
a-v-y38416862016-12-08 08:40:1314#include "base/json/json_reader.h"
[email protected]ae7bf342014-08-06 18:03:4715#include "base/metrics/histogram_base.h"
16#include "base/metrics/histogram_samples.h"
17#include "base/metrics/statistics_recorder.h"
18#include "base/path_service.h"
[email protected]ae7bf342014-08-06 18:03:4719#include "base/strings/string_number_conversions.h"
20#include "base/strings/string_util.h"
a-v-y38416862016-12-08 08:40:1321#include "base/strings/utf_string_conversions.h"
[email protected]ae7bf342014-08-06 18:03:4722#include "base/values.h"
23#include "build/build_config.h"
24#include "chrome/browser/extensions/extension_browsertest.h"
25#include "chrome/browser/extensions/extension_service.h"
26#include "chrome/browser/prefs/chrome_pref_service_factory.h"
27#include "chrome/browser/prefs/profile_pref_store_manager.h"
28#include "chrome/browser/prefs/session_startup_pref.h"
29#include "chrome/browser/profiles/profile.h"
30#include "chrome/browser/ui/browser.h"
31#include "chrome/common/chrome_constants.h"
32#include "chrome/common/chrome_paths.h"
33#include "chrome/common/pref_names.h"
34#include "chrome/test/base/testing_profile.h"
35#include "components/search_engines/default_search_manager.h"
a-v-y38416862016-12-08 08:40:1336#include "components/search_engines/template_url_data.h"
Lei Zhangfd5e6f0a2017-11-28 18:24:2937#include "content/public/test/test_launcher.h"
[email protected]ae7bf342014-08-06 18:03:4738#include "extensions/browser/pref_names.h"
39#include "extensions/common/extension.h"
sammcfb473a872017-04-03 01:56:5940#include "services/preferences/public/cpp/tracked/tracked_preference_histogram_names.h"
[email protected]ae7bf342014-08-06 18:03:4741
42#if defined(OS_CHROMEOS)
Steven Bennetts38e9bde22019-01-03 17:31:1043#include "chromeos/constants/chromeos_switches.h"
[email protected]ae7bf342014-08-06 18:03:4744#endif
45
proberge269fd092016-10-04 22:13:4146#if defined(OS_WIN)
gab36b85522017-05-24 19:30:4547#include "base/win/registry.h"
48#include "chrome/install_static/install_util.h"
proberge269fd092016-10-04 22:13:4149#endif
50
[email protected]ae7bf342014-08-06 18:03:4751namespace {
52
53// Extension ID of chrome/test/data/extensions/good.crx
54const char kGoodCrxId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
55
56// Explicit expectations from the caller of GetTrackedPrefHistogramCount(). This
57// enables detailed reporting of the culprit on failure.
58enum AllowedBuckets {
59 // Allow no samples in any buckets.
60 ALLOW_NONE = -1,
61 // Any integer between BEGIN_ALLOW_SINGLE_BUCKET and END_ALLOW_SINGLE_BUCKET
62 // indicates that only this specific bucket is allowed to have a sample.
63 BEGIN_ALLOW_SINGLE_BUCKET = 0,
64 END_ALLOW_SINGLE_BUCKET = 100,
65 // Allow any buckets (no extra verifications performed).
66 ALLOW_ANY
67};
68
proberge269fd092016-10-04 22:13:4169#if defined(OS_WIN)
70base::string16 GetRegistryPathForTestProfile() {
gab36b85522017-05-24 19:30:4571 // Cleanup follow-up to http://crbug.com/721245 for the previous location of
72 // this test key which had similar problems (to a lesser extent). It's
73 // redundant but harmless to have multiple callers hit this on the same
74 // machine. TODO(gab): remove this mid-june 2017.
75 base::win::RegKey key;
76 if (key.Open(HKEY_CURRENT_USER, L"SOFTWARE\\Chromium\\PrefHashBrowserTest",
77 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
78 LONG result = key.DeleteKey(L"");
79 EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
80 }
81
proberge269fd092016-10-04 22:13:4182 base::FilePath profile_dir;
Avi Drissman9098f9002018-05-04 00:11:5283 EXPECT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
gab36b85522017-05-24 19:30:4584
85 // Use a location under the real PreferenceMACs path so that the backup
86 // cleanup logic in ChromeTestLauncherDelegate::PreSharding() for interrupted
87 // tests covers this test key as well.
88 return install_static::GetRegistryPath() +
89 L"\\PreferenceMACs\\PrefHashBrowserTest\\" +
proberge269fd092016-10-04 22:13:4190 profile_dir.BaseName().value();
91}
92#endif
93
[email protected]ae7bf342014-08-06 18:03:4794// Returns the number of times |histogram_name| was reported so far; adding the
95// results of the first 100 buckets (there are only ~19 reporting IDs as of this
96// writing; varies depending on the platform). |allowed_buckets| hints at extra
97// requirements verified in this method (see AllowedBuckets for details).
98int GetTrackedPrefHistogramCount(const char* histogram_name,
proberge269fd092016-10-04 22:13:4199 const char* histogram_suffix,
[email protected]ae7bf342014-08-06 18:03:47100 int allowed_buckets) {
proberge269fd092016-10-04 22:13:41101 std::string full_histogram_name(histogram_name);
102 if (*histogram_suffix)
103 full_histogram_name.append(".").append(histogram_suffix);
[email protected]ae7bf342014-08-06 18:03:47104 const base::HistogramBase* histogram =
proberge269fd092016-10-04 22:13:41105 base::StatisticsRecorder::FindHistogram(full_histogram_name);
[email protected]ae7bf342014-08-06 18:03:47106 if (!histogram)
107 return 0;
108
dcheng4af48582016-04-19 00:29:35109 std::unique_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
[email protected]ae7bf342014-08-06 18:03:47110 int sum = 0;
111 for (int i = 0; i < 100; ++i) {
112 int count_for_id = samples->GetCount(i);
113 EXPECT_GE(count_for_id, 0);
114 sum += count_for_id;
115
116 if (allowed_buckets == ALLOW_NONE ||
117 (allowed_buckets != ALLOW_ANY && i != allowed_buckets)) {
118 EXPECT_EQ(0, count_for_id) << "Unexpected reporting_id: " << i;
119 }
120 }
121 return sum;
122}
123
proberge269fd092016-10-04 22:13:41124// Helper function to call GetTrackedPrefHistogramCount with no external
125// validation suffix.
126int GetTrackedPrefHistogramCount(const char* histogram_name,
127 int allowed_buckets) {
128 return GetTrackedPrefHistogramCount(histogram_name, "", allowed_buckets);
129}
130
dcheng4af48582016-04-19 00:29:35131std::unique_ptr<base::DictionaryValue> ReadPrefsDictionary(
[email protected]ae7bf342014-08-06 18:03:47132 const base::FilePath& pref_file) {
prashhir54a994502015-03-05 09:30:57133 JSONFileValueDeserializer deserializer(pref_file);
134 int error_code = JSONFileValueDeserializer::JSON_NO_ERROR;
[email protected]ae7bf342014-08-06 18:03:47135 std::string error_str;
dcheng4af48582016-04-19 00:29:35136 std::unique_ptr<base::Value> prefs =
olli.raulaba045252015-10-16 06:16:40137 deserializer.Deserialize(&error_code, &error_str);
prashhir54a994502015-03-05 09:30:57138 if (!prefs || error_code != JSONFileValueDeserializer::JSON_NO_ERROR) {
[email protected]ae7bf342014-08-06 18:03:47139 ADD_FAILURE() << "Error #" << error_code << ": " << error_str;
dcheng4af48582016-04-19 00:29:35140 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47141 }
jdoerrie1f536b22017-10-23 17:15:11142 if (!prefs->is_dict()) {
[email protected]ae7bf342014-08-06 18:03:47143 ADD_FAILURE();
dcheng4af48582016-04-19 00:29:35144 return std::unique_ptr<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47145 }
dcheng4af48582016-04-19 00:29:35146 return std::unique_ptr<base::DictionaryValue>(
[email protected]ae7bf342014-08-06 18:03:47147 static_cast<base::DictionaryValue*>(prefs.release()));
148}
149
proberge269fd092016-10-04 22:13:41150// Returns whether external validation is supported on the platform through
151// storing MACs in the registry.
152bool SupportsRegistryValidation() {
153#if defined(OS_WIN)
154 return true;
155#else
156 return false;
157#endif
158}
159
Victor Costan0e45f27a2019-02-02 00:13:59160#define PREF_HASH_BROWSER_TEST(fixture, test_name) \
161 IN_PROC_BROWSER_TEST_P(fixture, PRE_##test_name) { SetupPreferences(); } \
162 IN_PROC_BROWSER_TEST_P(fixture, test_name) { VerifyReactionToPrefAttack(); } \
163 INSTANTIATE_TEST_SUITE_P( \
164 fixture##Instance, fixture, \
165 testing::Values( \
166 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement, \
167 chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways, \
168 chrome_prefs::internals:: \
169 kSettingsEnforcementGroupEnforceAlwaysWithDSE, \
170 chrome_prefs::internals:: \
Nico Weber42c88d62019-02-11 03:09:25171 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE))
[email protected]ae7bf342014-08-06 18:03:47172
173// A base fixture designed such that implementations do two things:
174// 1) Override all three pure-virtual methods below to setup, attack, and
175// verify preferenes throughout the tests provided by this fixture.
176// 2) Instantiate their test via the PREF_HASH_BROWSER_TEST macro above.
177// Based on top of ExtensionBrowserTest to allow easy interaction with the
178// ExtensionService.
179class PrefHashBrowserTestBase
Devlin Cronin836f545d2018-05-09 00:25:05180 : public extensions::ExtensionBrowserTest,
[email protected]ae7bf342014-08-06 18:03:47181 public testing::WithParamInterface<std::string> {
182 public:
183 // List of potential protection levels for this test in strict increasing
184 // order of protection levels.
185 enum SettingsProtectionLevel {
186 PROTECTION_DISABLED_ON_PLATFORM,
187 PROTECTION_DISABLED_FOR_GROUP,
188 PROTECTION_ENABLED_BASIC,
189 PROTECTION_ENABLED_DSE,
190 PROTECTION_ENABLED_EXTENSIONS,
191 // Represents the strongest level (i.e. always equivalent to the last one in
192 // terms of protection), leave this one last when adding new levels.
193 PROTECTION_ENABLED_ALL
194 };
195
196 PrefHashBrowserTestBase()
197 : protection_level_(GetProtectionLevelFromTrialGroup(GetParam())) {
198 }
199
avi556c05022014-12-22 23:31:43200 void SetUpCommandLine(base::CommandLine* command_line) override {
Devlin Cronin836f545d2018-05-09 00:25:05201 extensions::ExtensionBrowserTest::SetUpCommandLine(command_line);
[email protected]ae7bf342014-08-06 18:03:47202 EXPECT_FALSE(command_line->HasSwitch(switches::kForceFieldTrials));
203 command_line->AppendSwitchASCII(
204 switches::kForceFieldTrials,
205 std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
206 "/" + GetParam() + "/");
207#if defined(OS_CHROMEOS)
208 command_line->AppendSwitch(
209 chromeos::switches::kIgnoreUserProfileMappingForTests);
210#endif
211 }
212
dcheng8f4b8622014-10-23 16:37:48213 bool SetUpUserDataDirectory() override {
[email protected]ae7bf342014-08-06 18:03:47214 // Do the normal setup in the PRE test and attack preferences in the main
215 // test.
Lei Zhangfd5e6f0a2017-11-28 18:24:29216 if (content::IsPreTest())
Devlin Cronin836f545d2018-05-09 00:25:05217 return extensions::ExtensionBrowserTest::SetUpUserDataDirectory();
[email protected]ae7bf342014-08-06 18:03:47218
219#if defined(OS_CHROMEOS)
220 // For some reason, the Preferences file does not exist in the location
221 // below on Chrome OS. Since protection is disabled on Chrome OS, it's okay
222 // to simply not attack preferences at all (and still assert that no
223 // hardening related histogram kicked in in VerifyReactionToPrefAttack()).
224 // TODO(gab): Figure out why there is no Preferences file in this location
225 // on Chrome OS (and re-enable the section disabled for OS_CHROMEOS further
226 // below).
227 EXPECT_EQ(PROTECTION_DISABLED_ON_PLATFORM, protection_level_);
228 return true;
229#endif
230
231 base::FilePath profile_dir;
Avi Drissman9098f9002018-05-04 00:11:52232 EXPECT_TRUE(base::PathService::Get(chrome::DIR_USER_DATA, &profile_dir));
[email protected]ae7bf342014-08-06 18:03:47233 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
234
235 // Sanity check that old protected pref file is never present in modern
236 // Chromes.
237 EXPECT_FALSE(base::PathExists(
sammcf420c16e2017-03-16 02:11:13238 profile_dir.Append(FILE_PATH_LITERAL("Protected Preferences"))));
[email protected]ae7bf342014-08-06 18:03:47239
240 // Read the preferences from disk.
241
242 const base::FilePath unprotected_pref_file =
243 profile_dir.Append(chrome::kPreferencesFilename);
244 EXPECT_TRUE(base::PathExists(unprotected_pref_file));
245
246 const base::FilePath protected_pref_file =
247 profile_dir.Append(chrome::kSecurePreferencesFilename);
248 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
249 base::PathExists(protected_pref_file));
250
dcheng4af48582016-04-19 00:29:35251 std::unique_ptr<base::DictionaryValue> unprotected_preferences(
[email protected]ae7bf342014-08-06 18:03:47252 ReadPrefsDictionary(unprotected_pref_file));
253 if (!unprotected_preferences)
254 return false;
255
dcheng4af48582016-04-19 00:29:35256 std::unique_ptr<base::DictionaryValue> protected_preferences;
[email protected]ae7bf342014-08-06 18:03:47257 if (protection_level_ > PROTECTION_DISABLED_ON_PLATFORM) {
258 protected_preferences = ReadPrefsDictionary(protected_pref_file);
259 if (!protected_preferences)
260 return false;
261 }
262
263 // Let the underlying test modify the preferences.
264 AttackPreferencesOnDisk(unprotected_preferences.get(),
265 protected_preferences.get());
266
267 // Write the modified preferences back to disk.
268
269 JSONFileValueSerializer unprotected_prefs_serializer(unprotected_pref_file);
270 EXPECT_TRUE(
271 unprotected_prefs_serializer.Serialize(*unprotected_preferences));
272
273 if (protected_preferences) {
274 JSONFileValueSerializer protected_prefs_serializer(protected_pref_file);
275 EXPECT_TRUE(protected_prefs_serializer.Serialize(*protected_preferences));
276 }
277
278 return true;
279 }
280
gabbcdefd0cc2015-02-17 18:12:40281 void SetUpInProcessBrowserTestFixture() override {
Devlin Cronin836f545d2018-05-09 00:25:05282 extensions::ExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
gabbcdefd0cc2015-02-17 18:12:40283
284 // Bots are on a domain, turn off the domain check for settings hardening in
285 // order to be able to test all SettingsEnforcement groups.
286 chrome_prefs::DisableDomainCheckForTesting();
proberge269fd092016-10-04 22:13:41287
288#if defined(OS_WIN)
289 // Avoid polluting prefs for the user and the bots by writing to a specific
290 // testing registry path.
291 registry_key_for_external_validation_ = GetRegistryPathForTestProfile();
292 ProfilePrefStoreManager::SetPreferenceValidationRegistryPathForTesting(
293 &registry_key_for_external_validation_);
294
295 // Keys should be unique, but to avoid flakes in the long run make sure an
296 // identical test key wasn't left behind by a previous test.
Lei Zhangfd5e6f0a2017-11-28 18:24:29297 if (content::IsPreTest()) {
proberge269fd092016-10-04 22:13:41298 base::win::RegKey key;
299 if (key.Open(HKEY_CURRENT_USER,
300 registry_key_for_external_validation_.c_str(),
301 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
302 LONG result = key.DeleteKey(L"");
303 ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
304 }
305 }
306#endif
307 }
308
309 void TearDown() override {
310#if defined(OS_WIN)
311 // When done, delete the Registry key to avoid polluting the registry.
Lei Zhangfd5e6f0a2017-11-28 18:24:29312 if (!content::IsPreTest()) {
proberge269fd092016-10-04 22:13:41313 base::string16 registry_key = GetRegistryPathForTestProfile();
314 base::win::RegKey key;
315 if (key.Open(HKEY_CURRENT_USER, registry_key.c_str(),
316 KEY_SET_VALUE | KEY_WOW64_32KEY) == ERROR_SUCCESS) {
317 LONG result = key.DeleteKey(L"");
318 ASSERT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
319 }
320 }
321#endif
Devlin Cronin836f545d2018-05-09 00:25:05322 extensions::ExtensionBrowserTest::TearDown();
gabbcdefd0cc2015-02-17 18:12:40323 }
324
[email protected]ae7bf342014-08-06 18:03:47325 // In the PRE_ test, find the number of tracked preferences that were
326 // initialized and save it to a file to be read back in the main test and used
327 // as the total number of tracked preferences.
dcheng8f4b8622014-10-23 16:37:48328 void SetUpOnMainThread() override {
Devlin Cronin836f545d2018-05-09 00:25:05329 extensions::ExtensionBrowserTest::SetUpOnMainThread();
[email protected]ae7bf342014-08-06 18:03:47330
331 // File in which the PRE_ test will save the number of tracked preferences
332 // on this platform.
333 const char kNumTrackedPrefFilename[] = "NumTrackedPrefs";
334
335 base::FilePath num_tracked_prefs_file;
336 ASSERT_TRUE(
Avi Drissman9098f9002018-05-04 00:11:52337 base::PathService::Get(chrome::DIR_USER_DATA, &num_tracked_prefs_file));
[email protected]ae7bf342014-08-06 18:03:47338 num_tracked_prefs_file =
339 num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename);
340
Lei Zhangfd5e6f0a2017-11-28 18:24:29341 if (content::IsPreTest()) {
[email protected]ae7bf342014-08-06 18:03:47342 num_tracked_prefs_ = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43343 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47344 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
345 num_tracked_prefs_ > 0);
346
gab342aa6182014-10-02 21:59:00347 // Split tracked prefs are reported as Unchanged not as NullInitialized
[email protected]ae7bf342014-08-06 18:03:47348 // when an empty dictionary is encountered on first run (this should only
349 // hit for pref #5 in the current design).
350 int num_split_tracked_prefs = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43351 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
352 BEGIN_ALLOW_SINGLE_BUCKET + 5);
[email protected]ae7bf342014-08-06 18:03:47353 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
354 num_split_tracked_prefs);
355
proberge269fd092016-10-04 22:13:41356 if (SupportsRegistryValidation()) {
357 // Same checks as above, but for the registry.
358 num_tracked_prefs_ = GetTrackedPrefHistogramCount(
359 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
360 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
361 ALLOW_ANY);
362 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
363 num_tracked_prefs_ > 0);
364
365 int num_split_tracked_prefs = GetTrackedPrefHistogramCount(
366 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
367 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
368 BEGIN_ALLOW_SINGLE_BUCKET + 5);
369 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
370 num_split_tracked_prefs);
371 }
372
[email protected]ae7bf342014-08-06 18:03:47373 num_tracked_prefs_ += num_split_tracked_prefs;
374
Raul Tambrefff51b752019-02-04 13:09:47375 std::string num_tracked_prefs_str =
376 base::NumberToString(num_tracked_prefs_);
[email protected]ae7bf342014-08-06 18:03:47377 EXPECT_EQ(static_cast<int>(num_tracked_prefs_str.size()),
378 base::WriteFile(num_tracked_prefs_file,
379 num_tracked_prefs_str.c_str(),
380 num_tracked_prefs_str.size()));
381 } else {
382 std::string num_tracked_prefs_str;
383 EXPECT_TRUE(base::ReadFileToString(num_tracked_prefs_file,
384 &num_tracked_prefs_str));
385 EXPECT_TRUE(
386 base::StringToInt(num_tracked_prefs_str, &num_tracked_prefs_));
387 }
388 }
389
390 protected:
391 // Called from the PRE_ test's body. Overrides should use it to setup
392 // preferences through Chrome.
393 virtual void SetupPreferences() = 0;
394
395 // Called prior to the main test launching its browser. Overrides should use
396 // it to attack preferences. |(un)protected_preferences| represent the state
397 // on disk prior to launching the main test, they can be modified by this
398 // method and modifications will be flushed back to disk before launching the
399 // main test. |unprotected_preferences| is never NULL, |protected_preferences|
400 // may be NULL if in PROTECTION_DISABLED_ON_PLATFORM mode.
401 virtual void AttackPreferencesOnDisk(
402 base::DictionaryValue* unprotected_preferences,
403 base::DictionaryValue* protected_preferences) = 0;
404
405 // Called from the body of the main test. Overrides should use it to verify
406 // that the browser had the desired reaction when faced when the attack
407 // orchestrated in AttackPreferencesOnDisk().
408 virtual void VerifyReactionToPrefAttack() = 0;
409
410 int num_tracked_prefs() const { return num_tracked_prefs_; }
411
412 const SettingsProtectionLevel protection_level_;
413
414 private:
[email protected]ae7bf342014-08-06 18:03:47415 SettingsProtectionLevel GetProtectionLevelFromTrialGroup(
416 const std::string& trial_group) {
417 if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking)
418 return PROTECTION_DISABLED_ON_PLATFORM;
419
420// Protection levels can't be adjusted via --force-fieldtrials in official
421// builds.
422#if defined(OFFICIAL_BUILD)
423
proberge2562e6f2017-04-10 17:46:57424#if defined(OS_WIN) || defined(OS_MACOSX)
425 // The strongest mode is enforced on Windows and MacOS in the absence of a
426 // field trial.
[email protected]ae7bf342014-08-06 18:03:47427 return PROTECTION_ENABLED_ALL;
428#else
429 return PROTECTION_DISABLED_FOR_GROUP;
Lei Zhangfd5e6f0a2017-11-28 18:24:29430#endif // defined(OS_WIN) || defined(OS_MACOSX)
[email protected]ae7bf342014-08-06 18:03:47431
432#else // defined(OFFICIAL_BUILD)
433
434 using namespace chrome_prefs::internals;
Lei Zhangfd5e6f0a2017-11-28 18:24:29435 if (trial_group == kSettingsEnforcementGroupNoEnforcement)
[email protected]ae7bf342014-08-06 18:03:47436 return PROTECTION_DISABLED_FOR_GROUP;
Lei Zhangfd5e6f0a2017-11-28 18:24:29437 if (trial_group == kSettingsEnforcementGroupEnforceAlways)
[email protected]ae7bf342014-08-06 18:03:47438 return PROTECTION_ENABLED_BASIC;
Lei Zhangfd5e6f0a2017-11-28 18:24:29439 if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE)
[email protected]ae7bf342014-08-06 18:03:47440 return PROTECTION_ENABLED_DSE;
Lei Zhangfd5e6f0a2017-11-28 18:24:29441 if (trial_group ==
442 kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
[email protected]ae7bf342014-08-06 18:03:47443 return PROTECTION_ENABLED_EXTENSIONS;
[email protected]ae7bf342014-08-06 18:03:47444 }
Lei Zhangfd5e6f0a2017-11-28 18:24:29445 ADD_FAILURE();
446 return static_cast<SettingsProtectionLevel>(-1);
[email protected]ae7bf342014-08-06 18:03:47447#endif // defined(OFFICIAL_BUILD)
[email protected]ae7bf342014-08-06 18:03:47448 }
449
450 int num_tracked_prefs_;
proberge269fd092016-10-04 22:13:41451
452#if defined(OS_WIN)
453 base::string16 registry_key_for_external_validation_;
454#endif
[email protected]ae7bf342014-08-06 18:03:47455};
456
457} // namespace
458
459// Verifies that nothing is reset when nothing is tampered with.
460// Also sanity checks that the expected preferences files are in place.
461class PrefHashBrowserTestUnchangedDefault : public PrefHashBrowserTestBase {
462 public:
dcheng8f4b8622014-10-23 16:37:48463 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47464 // Default Chrome setup.
465 }
466
dcheng8f4b8622014-10-23 16:37:48467 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47468 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17469 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47470 // No attack.
471 }
472
dcheng8f4b8622014-10-23 16:37:48473 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47474 // Expect all prefs to be reported as Unchanged with no resets.
deepak.m113481c82015-12-16 05:21:43475 EXPECT_EQ(
476 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
477 ? num_tracked_prefs()
478 : 0,
479 GetTrackedPrefHistogramCount(
480 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
481 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
482 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
483 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47484 EXPECT_EQ(0,
485 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43486 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47487
488 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47489 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43490 0, GetTrackedPrefHistogramCount(
491 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
492 EXPECT_EQ(
493 0, GetTrackedPrefHistogramCount(
494 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
495 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
496 user_prefs::tracked::kTrackedPrefHistogramInitialized,
497 ALLOW_NONE));
498 EXPECT_EQ(0,
499 GetTrackedPrefHistogramCount(
500 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
501 ALLOW_NONE));
502 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
503 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
504 ALLOW_NONE));
505 EXPECT_EQ(
506 0, GetTrackedPrefHistogramCount(
507 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
508 ALLOW_NONE));
proberge269fd092016-10-04 22:13:41509
510 if (SupportsRegistryValidation()) {
511 // Expect all prefs to be reported as Unchanged.
512 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
513 ? num_tracked_prefs()
514 : 0,
515 GetTrackedPrefHistogramCount(
516 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
517 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
518 ALLOW_ANY));
519 }
[email protected]ae7bf342014-08-06 18:03:47520 }
521};
522
gab36b85522017-05-24 19:30:45523PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedDefault, UnchangedDefault);
[email protected]ae7bf342014-08-06 18:03:47524
525// Augments PrefHashBrowserTestUnchangedDefault to confirm that nothing is reset
526// when nothing is tampered with, even if Chrome itself wrote custom prefs in
527// its last run.
528class PrefHashBrowserTestUnchangedCustom
529 : public PrefHashBrowserTestUnchangedDefault {
530 public:
dcheng8f4b8622014-10-23 16:37:48531 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47532 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
533
534 InstallExtensionWithUIAutoConfirm(
535 test_data_dir_.AppendASCII("good.crx"), 1, browser());
536 }
537
dcheng8f4b8622014-10-23 16:37:48538 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47539 // Make sure the settings written in the last run stuck.
540 EXPECT_EQ("http://example.com",
541 profile()->GetPrefs()->GetString(prefs::kHomePage));
542
543 EXPECT_TRUE(extension_service()->GetExtensionById(kGoodCrxId, false));
544
545 // Reaction should be identical to unattacked default prefs.
546 PrefHashBrowserTestUnchangedDefault::VerifyReactionToPrefAttack();
547 }
548};
549
gab36b85522017-05-24 19:30:45550PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUnchangedCustom, UnchangedCustom);
[email protected]ae7bf342014-08-06 18:03:47551
552// Verifies that cleared prefs are reported.
553class PrefHashBrowserTestClearedAtomic : public PrefHashBrowserTestBase {
554 public:
dcheng8f4b8622014-10-23 16:37:48555 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47556 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
557 }
558
dcheng8f4b8622014-10-23 16:37:48559 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47560 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17561 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47562 base::DictionaryValue* selected_prefs =
563 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
564 : unprotected_preferences;
565 // |selected_prefs| should never be NULL under the protection level picking
566 // it.
567 EXPECT_TRUE(selected_prefs);
568 EXPECT_TRUE(selected_prefs->Remove(prefs::kHomePage, NULL));
569 }
570
dcheng8f4b8622014-10-23 16:37:48571 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47572 // The clearance of homepage should have been noticed (as pref #2 being
573 // cleared), but shouldn't have triggered a reset (as there is nothing we
574 // can do when the pref is already gone).
575 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47576 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43577 user_prefs::tracked::kTrackedPrefHistogramCleared,
578 BEGIN_ALLOW_SINGLE_BUCKET + 2));
579 EXPECT_EQ(
580 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
581 ? num_tracked_prefs() - 1
582 : 0,
583 GetTrackedPrefHistogramCount(
584 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
585 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
586 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
587 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47588 EXPECT_EQ(0,
589 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43590 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47591
592 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47593 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43594 0, GetTrackedPrefHistogramCount(
595 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
596 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
597 user_prefs::tracked::kTrackedPrefHistogramInitialized,
598 ALLOW_NONE));
599 EXPECT_EQ(0,
600 GetTrackedPrefHistogramCount(
601 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
602 ALLOW_NONE));
603 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
604 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
605 ALLOW_NONE));
606 EXPECT_EQ(
607 0, GetTrackedPrefHistogramCount(
608 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
609 ALLOW_NONE));
proberge269fd092016-10-04 22:13:41610
611 if (SupportsRegistryValidation()) {
612 // Expect homepage clearance to have been noticed by registry validation.
613 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
614 GetTrackedPrefHistogramCount(
615 user_prefs::tracked::kTrackedPrefHistogramCleared,
616 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
617 BEGIN_ALLOW_SINGLE_BUCKET + 2));
618 }
[email protected]ae7bf342014-08-06 18:03:47619 }
620};
621
gab36b85522017-05-24 19:30:45622PREF_HASH_BROWSER_TEST(PrefHashBrowserTestClearedAtomic, ClearedAtomic);
[email protected]ae7bf342014-08-06 18:03:47623
624// Verifies that clearing the MACs results in untrusted Initialized pings for
625// non-null protected prefs.
626class PrefHashBrowserTestUntrustedInitialized : public PrefHashBrowserTestBase {
627 public:
dcheng8f4b8622014-10-23 16:37:48628 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47629 // Explicitly set the DSE (it's otherwise NULL by default, preventing
630 // thorough testing of the PROTECTION_ENABLED_DSE level).
631 DefaultSearchManager default_search_manager(
632 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
633 DefaultSearchManager::Source dse_source =
634 static_cast<DefaultSearchManager::Source>(-1);
635
636 const TemplateURLData* default_template_url_data =
637 default_search_manager.GetDefaultSearchEngine(&dse_source);
638 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source);
639
640 default_search_manager.SetUserSelectedDefaultSearchEngine(
641 *default_template_url_data);
642
643 default_search_manager.GetDefaultSearchEngine(&dse_source);
644 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source);
645
646 // Also explicitly set an atomic pref that falls under
647 // PROTECTION_ENABLED_BASIC.
648 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
649 SessionStartupPref::URLS);
650 }
651
dcheng8f4b8622014-10-23 16:37:48652 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47653 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17654 base::DictionaryValue* protected_preferences) override {
proberge574d7d92016-08-01 20:14:11655 unprotected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47656 if (protected_preferences)
proberge574d7d92016-08-01 20:14:11657 protected_preferences->Remove("protection.macs", NULL);
[email protected]ae7bf342014-08-06 18:03:47658 }
659
dcheng8f4b8622014-10-23 16:37:48660 void VerifyReactionToPrefAttack() override {
gab342aa6182014-10-02 21:59:00661 // Preferences that are NULL by default will be NullInitialized.
[email protected]ae7bf342014-08-06 18:03:47662 int num_null_values = GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43663 user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
[email protected]ae7bf342014-08-06 18:03:47664 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
665 num_null_values > 0);
666 if (num_null_values > 0) {
667 // This test requires that at least 3 prefs be non-null (extensions, DSE,
668 // and 1 atomic pref explictly set for this test above).
gab342aa6182014-10-02 21:59:00669 EXPECT_GE(num_tracked_prefs() - num_null_values, 3);
[email protected]ae7bf342014-08-06 18:03:47670 }
671
672 // Expect all non-null prefs to be reported as Initialized (with
673 // accompanying resets or wanted resets based on the current protection
674 // level).
deepak.m113481c82015-12-16 05:21:43675 EXPECT_EQ(
676 num_tracked_prefs() - num_null_values,
677 GetTrackedPrefHistogramCount(
678 user_prefs::tracked::kTrackedPrefHistogramInitialized, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47679
680 int num_protected_prefs = 0;
681 // A switch statement falling through each protection level in decreasing
682 // levels of protection to add expectations for each level which augments
683 // the previous one.
684 switch (protection_level_) {
685 case PROTECTION_ENABLED_ALL:
[email protected]ae7bf342014-08-06 18:03:47686 case PROTECTION_ENABLED_EXTENSIONS:
687 ++num_protected_prefs;
Nico Weber1c64f9a2018-01-30 12:00:48688 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47689 case PROTECTION_ENABLED_DSE:
690 ++num_protected_prefs;
Nico Weber1c64f9a2018-01-30 12:00:48691 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47692 case PROTECTION_ENABLED_BASIC:
693 num_protected_prefs += num_tracked_prefs() - num_null_values - 2;
Nico Weber1c64f9a2018-01-30 12:00:48694 FALLTHROUGH;
[email protected]ae7bf342014-08-06 18:03:47695 case PROTECTION_DISABLED_FOR_GROUP:
[email protected]ae7bf342014-08-06 18:03:47696 case PROTECTION_DISABLED_ON_PLATFORM:
697 // No protection.
698 break;
699 }
700
deepak.m113481c82015-12-16 05:21:43701 EXPECT_EQ(
702 num_tracked_prefs() - num_null_values - num_protected_prefs,
703 GetTrackedPrefHistogramCount(
704 user_prefs::tracked::kTrackedPrefHistogramWantedReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47705 EXPECT_EQ(num_protected_prefs,
deepak.m113481c82015-12-16 05:21:43706 GetTrackedPrefHistogramCount(
707 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_ANY));
[email protected]ae7bf342014-08-06 18:03:47708
709 // Explicitly verify the result of reported resets.
710
711 DefaultSearchManager default_search_manager(
712 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
713 DefaultSearchManager::Source dse_source =
714 static_cast<DefaultSearchManager::Source>(-1);
715 default_search_manager.GetDefaultSearchEngine(&dse_source);
716 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_DSE
717 ? DefaultSearchManager::FROM_USER
718 : DefaultSearchManager::FROM_FALLBACK,
719 dse_source);
720
721 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_BASIC,
722 profile()->GetPrefs()->GetInteger(prefs::kRestoreOnStartup) ==
723 SessionStartupPref::URLS);
724
725 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:43726 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
727 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
728 ALLOW_NONE));
[email protected]ae7bf342014-08-06 18:03:47729 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43730 0, GetTrackedPrefHistogramCount(
731 user_prefs::tracked::kTrackedPrefHistogramChanged, ALLOW_NONE));
732 EXPECT_EQ(
733 0, GetTrackedPrefHistogramCount(
734 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
735 EXPECT_EQ(
736 0, GetTrackedPrefHistogramCount(
737 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
738 ALLOW_NONE));
proberge269fd092016-10-04 22:13:41739
740 if (SupportsRegistryValidation()) {
741 // The MACs have been cleared but the preferences have not been tampered.
742 // The registry should report all prefs as unchanged.
743 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
744 ? num_tracked_prefs()
745 : 0,
746 GetTrackedPrefHistogramCount(
747 user_prefs::tracked::kTrackedPrefHistogramUnchanged,
748 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
749 ALLOW_ANY));
750 }
[email protected]ae7bf342014-08-06 18:03:47751 }
752};
753
754PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedInitialized,
gab36b85522017-05-24 19:30:45755 UntrustedInitialized);
[email protected]ae7bf342014-08-06 18:03:47756
757// Verifies that changing an atomic pref results in it being reported (and reset
758// if the protection level allows it).
759class PrefHashBrowserTestChangedAtomic : public PrefHashBrowserTestBase {
760 public:
dcheng8f4b8622014-10-23 16:37:48761 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47762 profile()->GetPrefs()->SetInteger(prefs::kRestoreOnStartup,
763 SessionStartupPref::URLS);
764
765 ListPrefUpdate update(profile()->GetPrefs(),
766 prefs::kURLsToRestoreOnStartup);
767 update->AppendString("http://example.com");
768 }
769
dcheng8f4b8622014-10-23 16:37:48770 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47771 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17772 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47773 base::DictionaryValue* selected_prefs =
774 protection_level_ >= PROTECTION_ENABLED_BASIC ? protected_preferences
775 : unprotected_preferences;
776 // |selected_prefs| should never be NULL under the protection level picking
777 // it.
778 EXPECT_TRUE(selected_prefs);
779 base::ListValue* startup_urls;
780 EXPECT_TRUE(
781 selected_prefs->GetList(prefs::kURLsToRestoreOnStartup, &startup_urls));
782 EXPECT_TRUE(startup_urls);
783 EXPECT_EQ(1U, startup_urls->GetSize());
784 startup_urls->AppendString("http://example.org");
785 }
786
dcheng8f4b8622014-10-23 16:37:48787 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47788 // Expect a single Changed event for tracked pref #4 (startup URLs).
789 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
[email protected]ae7bf342014-08-06 18:03:47790 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43791 user_prefs::tracked::kTrackedPrefHistogramChanged,
792 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47793 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43794 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
795 ? num_tracked_prefs() - 1
796 : 0,
797 GetTrackedPrefHistogramCount(
798 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
799
800 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
801 protection_level_ < PROTECTION_ENABLED_BASIC)
802 ? 1
803 : 0,
804 GetTrackedPrefHistogramCount(
805 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
806 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47807 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43808 GetTrackedPrefHistogramCount(
809 user_prefs::tracked::kTrackedPrefHistogramReset,
810 BEGIN_ALLOW_SINGLE_BUCKET + 4));
[email protected]ae7bf342014-08-06 18:03:47811
812// TODO(gab): This doesn't work on OS_CHROMEOS because we fail to attack
813// Preferences.
814#if !defined(OS_CHROMEOS)
815 // Explicitly verify the result of reported resets.
816 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_BASIC ? 0U : 2U,
817 profile()
818 ->GetPrefs()
819 ->GetList(prefs::kURLsToRestoreOnStartup)
820 ->GetSize());
821#endif
822
823 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47824 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43825 0, GetTrackedPrefHistogramCount(
826 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
827 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
828 user_prefs::tracked::kTrackedPrefHistogramInitialized,
829 ALLOW_NONE));
830 EXPECT_EQ(0,
831 GetTrackedPrefHistogramCount(
832 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
833 ALLOW_NONE));
834 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
835 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
836 ALLOW_NONE));
837 EXPECT_EQ(
838 0, GetTrackedPrefHistogramCount(
839 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
840 ALLOW_NONE));
proberge269fd092016-10-04 22:13:41841
842 if (SupportsRegistryValidation()) {
843 // Expect a single Changed event for tracked pref #4 (startup URLs).
844 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
845 GetTrackedPrefHistogramCount(
846 user_prefs::tracked::kTrackedPrefHistogramChanged,
847 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
848 BEGIN_ALLOW_SINGLE_BUCKET + 4));
849 }
[email protected]ae7bf342014-08-06 18:03:47850 }
851};
852
gab36b85522017-05-24 19:30:45853PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedAtomic, ChangedAtomic);
[email protected]ae7bf342014-08-06 18:03:47854
855// Verifies that changing or adding an entry in a split pref results in both
856// items being reported (and remove if the protection level allows it).
857class PrefHashBrowserTestChangedSplitPref : public PrefHashBrowserTestBase {
858 public:
dcheng8f4b8622014-10-23 16:37:48859 void SetupPreferences() override {
[email protected]ae7bf342014-08-06 18:03:47860 InstallExtensionWithUIAutoConfirm(
861 test_data_dir_.AppendASCII("good.crx"), 1, browser());
862 }
863
dcheng8f4b8622014-10-23 16:37:48864 void AttackPreferencesOnDisk(
[email protected]ae7bf342014-08-06 18:03:47865 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17866 base::DictionaryValue* protected_preferences) override {
[email protected]ae7bf342014-08-06 18:03:47867 base::DictionaryValue* selected_prefs =
868 protection_level_ >= PROTECTION_ENABLED_EXTENSIONS
869 ? protected_preferences
870 : unprotected_preferences;
871 // |selected_prefs| should never be NULL under the protection level picking
872 // it.
873 EXPECT_TRUE(selected_prefs);
874 base::DictionaryValue* extensions_dict;
875 EXPECT_TRUE(selected_prefs->GetDictionary(
876 extensions::pref_names::kExtensions, &extensions_dict));
877 EXPECT_TRUE(extensions_dict);
878
879 // Tamper with any installed setting for good.crx
880 base::DictionaryValue* good_crx_dict;
881 EXPECT_TRUE(extensions_dict->GetDictionary(kGoodCrxId, &good_crx_dict));
882 int good_crx_state;
883 EXPECT_TRUE(good_crx_dict->GetInteger("state", &good_crx_state));
884 EXPECT_EQ(extensions::Extension::ENABLED, good_crx_state);
885 good_crx_dict->SetInteger("state", extensions::Extension::DISABLED);
886
887 // Drop a fake extension (for the purpose of this test, dropped settings
888 // don't need to be valid extension settings).
Jeremy Romanec48d7a2018-03-01 17:35:09889 auto fake_extension = std::make_unique<base::DictionaryValue>();
[email protected]ae7bf342014-08-06 18:03:47890 fake_extension->SetString("name", "foo");
vabr9984ea62017-04-10 11:33:49891 extensions_dict->Set(std::string(32, 'a'), std::move(fake_extension));
[email protected]ae7bf342014-08-06 18:03:47892 }
893
dcheng8f4b8622014-10-23 16:37:48894 void VerifyReactionToPrefAttack() override {
[email protected]ae7bf342014-08-06 18:03:47895 // Expect a single split pref changed report with a count of 2 for tracked
896 // pref #5 (extensions).
897 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43898 GetTrackedPrefHistogramCount(
899 user_prefs::tracked::kTrackedPrefHistogramChanged,
900 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47901 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
902 GetTrackedPrefHistogramCount(
903 "Settings.TrackedSplitPreferenceChanged.extensions.settings",
904 BEGIN_ALLOW_SINGLE_BUCKET + 2));
905
906 // Everything else should have remained unchanged.
[email protected]ae7bf342014-08-06 18:03:47907 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43908 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
909 ? num_tracked_prefs() - 1
910 : 0,
911 GetTrackedPrefHistogramCount(
912 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
913
914 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
915 protection_level_ < PROTECTION_ENABLED_EXTENSIONS)
916 ? 1
917 : 0,
918 GetTrackedPrefHistogramCount(
919 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
920 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47921 EXPECT_EQ(protection_level_ >= PROTECTION_ENABLED_EXTENSIONS ? 1 : 0,
deepak.m113481c82015-12-16 05:21:43922 GetTrackedPrefHistogramCount(
923 user_prefs::tracked::kTrackedPrefHistogramReset,
924 BEGIN_ALLOW_SINGLE_BUCKET + 5));
[email protected]ae7bf342014-08-06 18:03:47925
926 EXPECT_EQ(protection_level_ < PROTECTION_ENABLED_EXTENSIONS,
927 extension_service()->GetExtensionById(kGoodCrxId, true) != NULL);
928
929 // Nothing else should have triggered.
[email protected]ae7bf342014-08-06 18:03:47930 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43931 0, GetTrackedPrefHistogramCount(
932 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
933 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
934 user_prefs::tracked::kTrackedPrefHistogramInitialized,
935 ALLOW_NONE));
936 EXPECT_EQ(0,
937 GetTrackedPrefHistogramCount(
938 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
939 ALLOW_NONE));
940 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
941 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
942 ALLOW_NONE));
943 EXPECT_EQ(
944 0, GetTrackedPrefHistogramCount(
945 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
946 ALLOW_NONE));
proberge269fd092016-10-04 22:13:41947
948 if (SupportsRegistryValidation()) {
949 // Expect that the registry validation caught the invalid MAC in split
950 // pref #5 (extensions).
951 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
952 GetTrackedPrefHistogramCount(
953 user_prefs::tracked::kTrackedPrefHistogramChanged,
954 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
955 BEGIN_ALLOW_SINGLE_BUCKET + 5));
956 }
[email protected]ae7bf342014-08-06 18:03:47957 }
958};
959
gab36b85522017-05-24 19:30:45960PREF_HASH_BROWSER_TEST(PrefHashBrowserTestChangedSplitPref, ChangedSplitPref);
gabb004a562014-09-16 12:45:22961
962// Verifies that adding a value to unprotected preferences for a key which is
963// still using the default (i.e. has no value stored in protected preferences)
964// doesn't allow that value to slip in with no valid MAC (regression test for
965// http://crbug.com/414554)
966class PrefHashBrowserTestUntrustedAdditionToPrefs
967 : public PrefHashBrowserTestBase {
968 public:
dcheng8f4b8622014-10-23 16:37:48969 void SetupPreferences() override {
gabb004a562014-09-16 12:45:22970 // Ensure there is no user-selected value for kRestoreOnStartup.
971 EXPECT_FALSE(
972 profile()->GetPrefs()->GetUserPrefValue(prefs::kRestoreOnStartup));
973 }
974
dcheng8f4b8622014-10-23 16:37:48975 void AttackPreferencesOnDisk(
gabb004a562014-09-16 12:45:22976 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:17977 base::DictionaryValue* protected_preferences) override {
gabb004a562014-09-16 12:45:22978 unprotected_preferences->SetInteger(prefs::kRestoreOnStartup,
979 SessionStartupPref::LAST);
980 }
981
dcheng8f4b8622014-10-23 16:37:48982 void VerifyReactionToPrefAttack() override {
gabb004a562014-09-16 12:45:22983 // Expect a single Changed event for tracked pref #3 (kRestoreOnStartup) if
984 // not protecting; if protection is enabled the change should be a no-op.
985 int changed_expected =
986 protection_level_ == PROTECTION_DISABLED_FOR_GROUP ? 1 : 0;
deepak.m113481c82015-12-16 05:21:43987 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
988 protection_level_ < PROTECTION_ENABLED_BASIC)
989 ? changed_expected
990 : 0,
gabb004a562014-09-16 12:45:22991 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:43992 user_prefs::tracked::kTrackedPrefHistogramChanged,
993 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:22994 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:43995 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
996 ? num_tracked_prefs() - changed_expected
997 : 0,
998 GetTrackedPrefHistogramCount(
999 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
1000
1001 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
1002 protection_level_ < PROTECTION_ENABLED_BASIC)
1003 ? 1
1004 : 0,
1005 GetTrackedPrefHistogramCount(
1006 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
1007 BEGIN_ALLOW_SINGLE_BUCKET + 3));
gabb004a562014-09-16 12:45:221008 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431009 GetTrackedPrefHistogramCount(
1010 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
gabb004a562014-09-16 12:45:221011
1012 // Nothing else should have triggered.
gabb004a562014-09-16 12:45:221013 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431014 0, GetTrackedPrefHistogramCount(
1015 user_prefs::tracked::kTrackedPrefHistogramCleared, ALLOW_NONE));
1016 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1017 user_prefs::tracked::kTrackedPrefHistogramInitialized,
1018 ALLOW_NONE));
1019 EXPECT_EQ(0,
1020 GetTrackedPrefHistogramCount(
1021 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
1022 ALLOW_NONE));
1023 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1024 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
1025 ALLOW_NONE));
1026 EXPECT_EQ(
1027 0, GetTrackedPrefHistogramCount(
1028 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
1029 ALLOW_NONE));
proberge269fd092016-10-04 22:13:411030
1031 if (SupportsRegistryValidation()) {
1032 EXPECT_EQ((protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
1033 protection_level_ < PROTECTION_ENABLED_BASIC)
1034 ? changed_expected
1035 : 0,
1036 GetTrackedPrefHistogramCount(
1037 user_prefs::tracked::kTrackedPrefHistogramChanged,
1038 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
1039 BEGIN_ALLOW_SINGLE_BUCKET + 3));
1040 }
gabb004a562014-09-16 12:45:221041 }
1042};
1043
1044PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefs,
gab36b85522017-05-24 19:30:451045 UntrustedAdditionToPrefs);
erikwrightf4e02b72014-09-17 20:25:451046
1047// Verifies that adding a value to unprotected preferences while wiping a
1048// user-selected value from protected preferences doesn't allow that value to
1049// slip in with no valid MAC (regression test for http://crbug.com/414554).
1050class PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe
1051 : public PrefHashBrowserTestBase {
1052 public:
dcheng8f4b8622014-10-23 16:37:481053 void SetupPreferences() override {
erikwrightf4e02b72014-09-17 20:25:451054 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
1055 }
1056
dcheng8f4b8622014-10-23 16:37:481057 void AttackPreferencesOnDisk(
erikwrightf4e02b72014-09-17 20:25:451058 base::DictionaryValue* unprotected_preferences,
mostynb2b52d1db2014-10-07 02:47:171059 base::DictionaryValue* protected_preferences) override {
erikwrightf4e02b72014-09-17 20:25:451060 // Set or change the value in Preferences to the attacker's choice.
1061 unprotected_preferences->SetString(prefs::kHomePage, "http://example.net");
1062 // Clear the value in Secure Preferences, if any.
1063 if (protected_preferences)
1064 protected_preferences->Remove(prefs::kHomePage, NULL);
1065 }
1066
dcheng8f4b8622014-10-23 16:37:481067 void VerifyReactionToPrefAttack() override {
erikwrightf4e02b72014-09-17 20:25:451068 // Expect a single Changed event for tracked pref #2 (kHomePage) if
1069 // not protecting; if protection is enabled the change should be a Cleared.
1070 int changed_expected =
1071 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM &&
1072 protection_level_ < PROTECTION_ENABLED_BASIC
1073 ? 1 : 0;
1074 int cleared_expected =
1075 protection_level_ >= PROTECTION_ENABLED_BASIC
1076 ? 1 : 0;
1077 EXPECT_EQ(changed_expected,
erikwrightf4e02b72014-09-17 20:25:451078 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431079 user_prefs::tracked::kTrackedPrefHistogramChanged,
1080 BEGIN_ALLOW_SINGLE_BUCKET + 2));
1081 EXPECT_EQ(cleared_expected,
1082 GetTrackedPrefHistogramCount(
1083 user_prefs::tracked::kTrackedPrefHistogramCleared,
1084 BEGIN_ALLOW_SINGLE_BUCKET + 2));
erikwrightf4e02b72014-09-17 20:25:451085 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431086 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
1087 ? num_tracked_prefs() - changed_expected - cleared_expected
1088 : 0,
1089 GetTrackedPrefHistogramCount(
1090 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
1091
1092 EXPECT_EQ(changed_expected,
1093 GetTrackedPrefHistogramCount(
1094 user_prefs::tracked::kTrackedPrefHistogramWantedReset,
1095 BEGIN_ALLOW_SINGLE_BUCKET + 2));
erikwrightf4e02b72014-09-17 20:25:451096 EXPECT_EQ(0,
deepak.m113481c82015-12-16 05:21:431097 GetTrackedPrefHistogramCount(
1098 user_prefs::tracked::kTrackedPrefHistogramReset, ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451099
1100 // Nothing else should have triggered.
deepak.m113481c82015-12-16 05:21:431101 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1102 user_prefs::tracked::kTrackedPrefHistogramInitialized,
1103 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451104 EXPECT_EQ(0,
1105 GetTrackedPrefHistogramCount(
deepak.m113481c82015-12-16 05:21:431106 user_prefs::tracked::kTrackedPrefHistogramTrustedInitialized,
1107 ALLOW_NONE));
1108 EXPECT_EQ(0, GetTrackedPrefHistogramCount(
1109 user_prefs::tracked::kTrackedPrefHistogramNullInitialized,
1110 ALLOW_NONE));
erikwrightf4e02b72014-09-17 20:25:451111 EXPECT_EQ(
deepak.m113481c82015-12-16 05:21:431112 0, GetTrackedPrefHistogramCount(
1113 user_prefs::tracked::kTrackedPrefHistogramMigratedLegacyDeviceId,
1114 ALLOW_NONE));
proberge269fd092016-10-04 22:13:411115
1116 if (SupportsRegistryValidation()) {
1117 EXPECT_EQ(changed_expected,
1118 GetTrackedPrefHistogramCount(
1119 user_prefs::tracked::kTrackedPrefHistogramChanged,
1120 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
1121 BEGIN_ALLOW_SINGLE_BUCKET + 2));
1122 EXPECT_EQ(cleared_expected,
1123 GetTrackedPrefHistogramCount(
1124 user_prefs::tracked::kTrackedPrefHistogramCleared,
1125 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
1126 BEGIN_ALLOW_SINGLE_BUCKET + 2));
1127 }
erikwrightf4e02b72014-09-17 20:25:451128 }
1129};
1130
1131PREF_HASH_BROWSER_TEST(PrefHashBrowserTestUntrustedAdditionToPrefsAfterWipe,
gab36b85522017-05-24 19:30:451132 UntrustedAdditionToPrefsAfterWipe);
proberge269fd092016-10-04 22:13:411133
1134#if defined(OS_WIN)
1135class PrefHashBrowserTestRegistryValidationFailure
1136 : public PrefHashBrowserTestBase {
1137 public:
1138 void SetupPreferences() override {
1139 profile()->GetPrefs()->SetString(prefs::kHomePage, "http://example.com");
1140 }
1141
1142 void AttackPreferencesOnDisk(
1143 base::DictionaryValue* unprotected_preferences,
1144 base::DictionaryValue* protected_preferences) override {
1145 base::string16 registry_key =
1146 GetRegistryPathForTestProfile() + L"\\PreferenceMACs\\Default";
1147 base::win::RegKey key;
1148 ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, registry_key.c_str(),
1149 KEY_SET_VALUE | KEY_WOW64_32KEY));
1150 // An incorrect hash should still have the correct size.
1151 ASSERT_EQ(ERROR_SUCCESS,
1152 key.WriteValue(L"homepage", base::string16(64, 'A').c_str()));
1153 }
1154
1155 void VerifyReactionToPrefAttack() override {
1156 EXPECT_EQ(
1157 protection_level_ > PROTECTION_DISABLED_ON_PLATFORM
1158 ? num_tracked_prefs()
1159 : 0,
1160 GetTrackedPrefHistogramCount(
1161 user_prefs::tracked::kTrackedPrefHistogramUnchanged, ALLOW_ANY));
1162
1163 if (SupportsRegistryValidation()) {
1164 // Expect that the registry validation caught the invalid MAC for pref #2
1165 // (homepage).
1166 EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM ? 1 : 0,
1167 GetTrackedPrefHistogramCount(
1168 user_prefs::tracked::kTrackedPrefHistogramChanged,
1169 user_prefs::tracked::kTrackedPrefRegistryValidationSuffix,
1170 BEGIN_ALLOW_SINGLE_BUCKET + 2));
1171 }
1172 }
1173};
1174
1175PREF_HASH_BROWSER_TEST(PrefHashBrowserTestRegistryValidationFailure,
gab36b85522017-05-24 19:30:451176 RegistryValidationFailure);
proberge269fd092016-10-04 22:13:411177#endif
a-v-y38416862016-12-08 08:40:131178
1179// Verifies that all preferences related to choice of default search engine are
1180// protected.
1181class PrefHashBrowserTestDefaultSearch : public PrefHashBrowserTestBase {
1182 public:
1183 void SetupPreferences() override {
1184 // Set user selected default search engine.
1185 DefaultSearchManager default_search_manager(
1186 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
1187 DefaultSearchManager::Source dse_source =
1188 static_cast<DefaultSearchManager::Source>(-1);
1189
1190 TemplateURLData user_dse;
1191 user_dse.SetKeyword(base::UTF8ToUTF16("userkeyword"));
1192 user_dse.SetShortName(base::UTF8ToUTF16("username"));
1193 user_dse.SetURL("http://user_default_engine/search?q=good_user_query");
1194 default_search_manager.SetUserSelectedDefaultSearchEngine(user_dse);
1195
1196 const TemplateURLData* current_dse =
1197 default_search_manager.GetDefaultSearchEngine(&dse_source);
1198 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source);
1199 EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("userkeyword"));
1200 EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("username"));
1201 EXPECT_EQ(current_dse->url(),
1202 "http://user_default_engine/search?q=good_user_query");
1203 }
1204
1205 void AttackPreferencesOnDisk(
1206 base::DictionaryValue* unprotected_preferences,
1207 base::DictionaryValue* protected_preferences) override {
1208 static constexpr char default_search_provider_data[] = R"(
1209 {
1210 "default_search_provider_data" : {
1211 "template_url_data" : {
1212 "keyword" : "badkeyword",
1213 "short_name" : "badname",
1214 "url" : "http://bad_default_engine/search?q=dirty_user_query"
1215 }
1216 }
1217 })";
1218 static constexpr char search_provider_overrides[] = R"(
1219 {
1220 "search_provider_overrides" : [
1221 {
1222 "keyword" : "badkeyword",
1223 "name" : "badname",
1224 "search_url" : "http://bad_default_engine/search?q=dirty_user_query",
1225 "encoding" : "utf-8",
1226 "id" : 1
1227 }, {
1228 "keyword" : "badkeyword2",
1229 "name" : "badname2",
1230 "search_url" : "http://bad_default_engine2/search?q=dirty_user_query",
1231 "encoding" : "utf-8",
1232 "id" : 2
1233 }
1234 ]
1235 })";
a-v-y38416862016-12-08 08:40:131236
1237 // Try to override default search in all three of available preferences.
1238 auto attack1 = base::DictionaryValue::From(
Lei Zhang582ecd12019-02-13 20:28:541239 base::JSONReader::ReadDeprecated(default_search_provider_data));
a-v-y38416862016-12-08 08:40:131240 auto attack2 = base::DictionaryValue::From(
Lei Zhang582ecd12019-02-13 20:28:541241 base::JSONReader::ReadDeprecated(search_provider_overrides));
a-v-y38416862016-12-08 08:40:131242 unprotected_preferences->MergeDictionary(attack1.get());
1243 unprotected_preferences->MergeDictionary(attack2.get());
a-v-y38416862016-12-08 08:40:131244 if (protected_preferences) {
1245 // Override here, too.
1246 protected_preferences->MergeDictionary(attack1.get());
1247 protected_preferences->MergeDictionary(attack2.get());
a-v-y38416862016-12-08 08:40:131248 }
1249 }
1250
1251 void VerifyReactionToPrefAttack() override {
1252 DefaultSearchManager default_search_manager(
1253 profile()->GetPrefs(), DefaultSearchManager::ObserverCallback());
1254 DefaultSearchManager::Source dse_source =
1255 static_cast<DefaultSearchManager::Source>(-1);
1256
1257 const TemplateURLData* current_dse =
1258 default_search_manager.GetDefaultSearchEngine(&dse_source);
1259
1260 if (protection_level_ < PROTECTION_ENABLED_DSE) {
1261// This doesn't work on OS_CHROMEOS because we fail to attack Preferences.
1262#if !defined(OS_CHROMEOS)
1263 // Attack is successful.
1264 EXPECT_EQ(DefaultSearchManager::FROM_USER, dse_source);
1265 EXPECT_EQ(current_dse->keyword(), base::UTF8ToUTF16("badkeyword"));
1266 EXPECT_EQ(current_dse->short_name(), base::UTF8ToUTF16("badname"));
1267 EXPECT_EQ(current_dse->url(),
1268 "http://bad_default_engine/search?q=dirty_user_query");
1269#endif
1270 } else {
1271 // Attack fails.
1272 EXPECT_EQ(DefaultSearchManager::FROM_FALLBACK, dse_source);
1273 EXPECT_NE(current_dse->keyword(), base::UTF8ToUTF16("badkeyword"));
1274 EXPECT_NE(current_dse->short_name(), base::UTF8ToUTF16("badname"));
1275 EXPECT_NE(current_dse->url(),
1276 "http://bad_default_engine/search?q=dirty_user_query");
1277 }
1278 }
1279};
1280
gab36b85522017-05-24 19:30:451281PREF_HASH_BROWSER_TEST(PrefHashBrowserTestDefaultSearch, SearchProtected);