OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/password_manager/login_database.h" | 5 #include "chrome/browser/password_manager/login_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
| 10 #include "base/command_line.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "chrome/common/chrome_switches.h" |
| 21 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
18 #include "sql/statement.h" | 22 #include "sql/statement.h" |
19 #include "sql/transaction.h" | 23 #include "sql/transaction.h" |
20 | 24 |
21 using content::PasswordForm; | 25 using content::PasswordForm; |
22 | 26 |
23 static const int kCurrentVersionNumber = 3; | 27 static const int kCurrentVersionNumber = 3; |
24 static const int kCompatibleVersionNumber = 1; | 28 static const int kCompatibleVersionNumber = 1; |
25 | 29 |
26 namespace { | 30 namespace { |
27 | 31 |
(...skipping 10 matching lines...) Expand all Loading... |
38 COLUMN_SSL_VALID, | 42 COLUMN_SSL_VALID, |
39 COLUMN_PREFERRED, | 43 COLUMN_PREFERRED, |
40 COLUMN_DATE_CREATED, | 44 COLUMN_DATE_CREATED, |
41 COLUMN_BLACKLISTED_BY_USER, | 45 COLUMN_BLACKLISTED_BY_USER, |
42 COLUMN_SCHEME, | 46 COLUMN_SCHEME, |
43 COLUMN_PASSWORD_TYPE, | 47 COLUMN_PASSWORD_TYPE, |
44 COLUMN_POSSIBLE_USERNAMES, | 48 COLUMN_POSSIBLE_USERNAMES, |
45 COLUMN_TIMES_USED | 49 COLUMN_TIMES_USED |
46 }; | 50 }; |
47 | 51 |
| 52 std::string GetRegistryControlledDomain(const GURL& signon_realm) { |
| 53 return net::registry_controlled_domains::GetDomainAndRegistry( |
| 54 signon_realm, |
| 55 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 56 } |
| 57 |
| 58 std::string GetRegistryControlledDomain(const std::string& signon_realm_str) { |
| 59 GURL signon_realm(signon_realm_str); |
| 60 return net::registry_controlled_domains::GetDomainAndRegistry( |
| 61 signon_realm, |
| 62 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 63 } |
| 64 |
| 65 bool RegistryControlledDomainMatches(const scoped_ptr<PasswordForm>& found, |
| 66 const PasswordForm current) { |
| 67 const std::string found_registry_controlled_domain = |
| 68 GetRegistryControlledDomain(found->signon_realm); |
| 69 const std::string form_registry_controlled_domain = |
| 70 GetRegistryControlledDomain(current.signon_realm); |
| 71 return found_registry_controlled_domain == form_registry_controlled_domain; |
| 72 } |
| 73 |
| 74 bool SchemeMatches(const scoped_ptr<PasswordForm>& found, |
| 75 const PasswordForm current) { |
| 76 const std::string found_scheme = GURL(found->signon_realm).scheme(); |
| 77 const std::string form_scheme = GURL(current.signon_realm).scheme(); |
| 78 return found_scheme == form_scheme; |
| 79 } |
| 80 |
| 81 bool PortMatches(const scoped_ptr<PasswordForm>& found, |
| 82 const PasswordForm current) { |
| 83 const std::string found_port = GURL(found->signon_realm).port(); |
| 84 const std::string form_port = GURL(current.signon_realm).port(); |
| 85 return found_port == form_port; |
| 86 } |
| 87 |
48 } // namespace | 88 } // namespace |
49 | 89 |
50 LoginDatabase::LoginDatabase() { | 90 LoginDatabase::LoginDatabase() : public_suffix_domain_matching_(false) { |
51 } | 91 } |
52 | 92 |
53 LoginDatabase::~LoginDatabase() { | 93 LoginDatabase::~LoginDatabase() { |
54 } | 94 } |
55 | 95 |
56 bool LoginDatabase::Init(const base::FilePath& db_path) { | 96 bool LoginDatabase::Init(const base::FilePath& db_path) { |
57 // Set pragmas for a small, private database (based on WebDatabase). | 97 // Set pragmas for a small, private database (based on WebDatabase). |
58 db_.set_page_size(2048); | 98 db_.set_page_size(2048); |
59 db_.set_cache_size(32); | 99 db_.set_cache_size(32); |
60 db_.set_exclusive_locking(); | 100 db_.set_exclusive_locking(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 if (!MigrateOldVersionsAsNeeded()) { | 133 if (!MigrateOldVersionsAsNeeded()) { |
94 LOG(WARNING) << "Unable to migrate database"; | 134 LOG(WARNING) << "Unable to migrate database"; |
95 db_.Close(); | 135 db_.Close(); |
96 return false; | 136 return false; |
97 } | 137 } |
98 | 138 |
99 if (!transaction.Commit()) { | 139 if (!transaction.Commit()) { |
100 db_.Close(); | 140 db_.Close(); |
101 return false; | 141 return false; |
102 } | 142 } |
| 143 |
| 144 public_suffix_domain_matching_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 145 switches::kEnablePasswordAutofillPublicSuffixDomainMatching); |
| 146 |
103 return true; | 147 return true; |
104 } | 148 } |
105 | 149 |
106 bool LoginDatabase::MigrateOldVersionsAsNeeded() { | 150 bool LoginDatabase::MigrateOldVersionsAsNeeded() { |
107 switch (meta_table_.GetVersionNumber()) { | 151 switch (meta_table_.GetVersionNumber()) { |
108 case 1: | 152 case 1: |
109 if (!db_.Execute("ALTER TABLE logins " | 153 if (!db_.Execute("ALTER TABLE logins " |
110 "ADD COLUMN password_type INTEGER") || | 154 "ADD COLUMN password_type INTEGER") || |
111 !db_.Execute("ALTER TABLE logins " | 155 !db_.Execute("ALTER TABLE logins " |
112 "ADD COLUMN possible_usernames BLOB")) { | 156 "ADD COLUMN possible_usernames BLOB")) { |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); | 394 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); |
351 form->other_possible_usernames = DeserializeVector(pickle); | 395 form->other_possible_usernames = DeserializeVector(pickle); |
352 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); | 396 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); |
353 return true; | 397 return true; |
354 } | 398 } |
355 | 399 |
356 bool LoginDatabase::GetLogins(const PasswordForm& form, | 400 bool LoginDatabase::GetLogins(const PasswordForm& form, |
357 std::vector<PasswordForm*>* forms) const { | 401 std::vector<PasswordForm*>* forms) const { |
358 DCHECK(forms); | 402 DCHECK(forms); |
359 // You *must* change LoginTableColumns if this query changes. | 403 // You *must* change LoginTableColumns if this query changes. |
360 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 404 const std::string sql_query = "SELECT origin_url, action_url, " |
361 "SELECT origin_url, action_url, " | |
362 "username_element, username_value, " | 405 "username_element, username_value, " |
363 "password_element, password_value, submit_element, " | 406 "password_element, password_value, submit_element, " |
364 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " | 407 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " |
365 "scheme, password_type, possible_usernames, times_used " | 408 "scheme, password_type, possible_usernames, times_used " |
366 "FROM logins WHERE signon_realm == ? ")); | 409 "FROM logins WHERE signon_realm == ? "; |
367 s.BindString(0, form.signon_realm); | 410 sql::Statement s; |
| 411 if (public_suffix_domain_matching_) { |
| 412 // We are extending the original SQL query with one that includes more |
| 413 // possible matches based on public suffix domain matching. Using a regexp |
| 414 // here is just an optimization to not have to parse all the stored entries |
| 415 // in the |logins| table. The result (scheme, domain and port) is verified |
| 416 // further down using GURL. See the functions SchemeMatches, |
| 417 // RegistryControlledDomainMatches and PortMatches. |
| 418 const std::string extended_sql_query = |
| 419 sql_query + "OR signon_realm REGEXP ? "; |
| 420 // TODO(nyquist) Re-enable usage of GetCachedStatement when |
| 421 // http://crbug.com/248608 is fixed. |
| 422 s.Assign(db_.GetUniqueStatement(extended_sql_query.c_str())); |
| 423 const GURL signon_realm(form.signon_realm); |
| 424 std::string domain = GetRegistryControlledDomain(signon_realm); |
| 425 // We need to escape . in the domain. Since the domain has already been |
| 426 // sanitized using GURL, we do not need to escape any other characters. |
| 427 ReplaceChars(domain, ".", "\\.", &domain); |
| 428 std::string scheme = signon_realm.scheme(); |
| 429 // We need to escape . in the scheme. Since the scheme has already been |
| 430 // sanitized using GURL, we do not need to escape any other characters. |
| 431 // The scheme soap.beep is an example with '.'. |
| 432 ReplaceChars(scheme, ".", "\\.", &scheme); |
| 433 const std::string port = signon_realm.port(); |
| 434 // For a signon realm such as http://foo.bar/, this regexp will match |
| 435 // domains on the form http://foo.bar/, http://www.foo.bar/, |
| 436 // http://www.mobile.foo.bar/. It will not match http://notfoo.bar/. |
| 437 // The scheme and port has to be the same as the observed form. |
| 438 std::string regexp = "^(" + scheme + ":\\/\\/)([\\w-]+\\.)*" + |
| 439 domain + "(:" + port + ")?\\/$"; |
| 440 s.BindString(0, form.signon_realm); |
| 441 s.BindString(1, regexp); |
| 442 } else { |
| 443 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); |
| 444 s.BindString(0, form.signon_realm); |
| 445 } |
368 | 446 |
369 while (s.Step()) { | 447 while (s.Step()) { |
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); | 448 scoped_ptr<PasswordForm> new_form(new PasswordForm()); |
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) | 449 if (!InitPasswordFormFromStatement(new_form.get(), s)) |
372 return false; | 450 return false; |
| 451 if (public_suffix_domain_matching_) { |
| 452 if (!SchemeMatches(new_form, form) || |
| 453 !RegistryControlledDomainMatches(new_form, form) || |
| 454 !PortMatches(new_form, form)) { |
| 455 // The database returned results that should not match. Skipping result. |
| 456 continue; |
| 457 } |
| 458 if (form.signon_realm != new_form->signon_realm) { |
| 459 // This is not a perfect match, so we need to create a new valid result. |
| 460 // We do this by copying over origin, signon realm and action from the |
| 461 // observed form and setting the original signon realm to what we found |
| 462 // in the database. We use the fact that |original_signon_realm| is |
| 463 // non-empty to communicate that this match was found using public |
| 464 // suffix matching. |
| 465 new_form->original_signon_realm = new_form->signon_realm; |
| 466 new_form->origin = form.origin; |
| 467 new_form->signon_realm = form.signon_realm; |
| 468 new_form->action = form.action; |
| 469 } |
| 470 } |
373 forms->push_back(new_form.release()); | 471 forms->push_back(new_form.release()); |
374 } | 472 } |
375 return s.Succeeded(); | 473 return s.Succeeded(); |
376 } | 474 } |
377 | 475 |
378 bool LoginDatabase::GetLoginsCreatedBetween( | 476 bool LoginDatabase::GetLoginsCreatedBetween( |
379 const base::Time begin, | 477 const base::Time begin, |
380 const base::Time end, | 478 const base::Time end, |
381 std::vector<content::PasswordForm*>* forms) const { | 479 std::vector<content::PasswordForm*>* forms) const { |
382 DCHECK(forms); | 480 DCHECK(forms); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { | 551 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { |
454 std::vector<string16> ret; | 552 std::vector<string16> ret; |
455 string16 str; | 553 string16 str; |
456 | 554 |
457 PickleIterator iterator(p); | 555 PickleIterator iterator(p); |
458 while (iterator.ReadString16(&str)) { | 556 while (iterator.ReadString16(&str)) { |
459 ret.push_back(str); | 557 ret.push_back(str); |
460 } | 558 } |
461 return ret; | 559 return ret; |
462 } | 560 } |
OLD | NEW |