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" |
16 #include "base/string_util.h" | |
15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.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 std::string& signon_realm_str) { | |
53 const GURL signon_realm(signon_realm_str); | |
54 return net::registry_controlled_domains::GetDomainAndRegistry( | |
55 signon_realm, | |
56 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
57 } | |
58 | |
48 } // namespace | 59 } // namespace |
49 | 60 |
50 LoginDatabase::LoginDatabase() { | 61 LoginDatabase::LoginDatabase() { |
51 } | 62 } |
52 | 63 |
53 LoginDatabase::~LoginDatabase() { | 64 LoginDatabase::~LoginDatabase() { |
54 } | 65 } |
55 | 66 |
56 bool LoginDatabase::Init(const base::FilePath& db_path) { | 67 bool LoginDatabase::Init(const base::FilePath& db_path) { |
57 // Set pragmas for a small, private database (based on WebDatabase). | 68 // Set pragmas for a small, private database (based on WebDatabase). |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 if (!MigrateOldVersionsAsNeeded()) { | 104 if (!MigrateOldVersionsAsNeeded()) { |
94 LOG(WARNING) << "Unable to migrate database"; | 105 LOG(WARNING) << "Unable to migrate database"; |
95 db_.Close(); | 106 db_.Close(); |
96 return false; | 107 return false; |
97 } | 108 } |
98 | 109 |
99 if (!transaction.Commit()) { | 110 if (!transaction.Commit()) { |
100 db_.Close(); | 111 db_.Close(); |
101 return false; | 112 return false; |
102 } | 113 } |
114 | |
115 public_suffix_domain_matching_ = CommandLine::ForCurrentProcess()->HasSwitch( | |
116 switches::kEnablePasswordAutofillPublicSuffixDomainMatching); | |
117 | |
103 return true; | 118 return true; |
104 } | 119 } |
105 | 120 |
106 bool LoginDatabase::MigrateOldVersionsAsNeeded() { | 121 bool LoginDatabase::MigrateOldVersionsAsNeeded() { |
107 switch (meta_table_.GetVersionNumber()) { | 122 switch (meta_table_.GetVersionNumber()) { |
108 case 1: | 123 case 1: |
109 if (!db_.Execute("ALTER TABLE logins " | 124 if (!db_.Execute("ALTER TABLE logins " |
110 "ADD COLUMN password_type INTEGER") || | 125 "ADD COLUMN password_type INTEGER") || |
111 !db_.Execute("ALTER TABLE logins " | 126 !db_.Execute("ALTER TABLE logins " |
112 "ADD COLUMN possible_usernames BLOB")) { | 127 "ADD COLUMN possible_usernames BLOB")) { |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); | 365 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); |
351 form->other_possible_usernames = DeserializeVector(pickle); | 366 form->other_possible_usernames = DeserializeVector(pickle); |
352 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); | 367 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); |
353 return true; | 368 return true; |
354 } | 369 } |
355 | 370 |
356 bool LoginDatabase::GetLogins(const PasswordForm& form, | 371 bool LoginDatabase::GetLogins(const PasswordForm& form, |
357 std::vector<PasswordForm*>* forms) const { | 372 std::vector<PasswordForm*>* forms) const { |
358 DCHECK(forms); | 373 DCHECK(forms); |
359 // You *must* change LoginTableColumns if this query changes. | 374 // You *must* change LoginTableColumns if this query changes. |
360 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 375 const std::string sql_query = "SELECT origin_url, action_url, " |
361 "SELECT origin_url, action_url, " | |
362 "username_element, username_value, " | 376 "username_element, username_value, " |
363 "password_element, password_value, submit_element, " | 377 "password_element, password_value, submit_element, " |
364 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " | 378 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " |
365 "scheme, password_type, possible_usernames, times_used " | 379 "scheme, password_type, possible_usernames, times_used " |
366 "FROM logins WHERE signon_realm == ? ")); | 380 "FROM logins WHERE signon_realm == ? "; |
367 s.BindString(0, form.signon_realm); | 381 sql::Statement s; |
382 if (public_suffix_domain_matching_) { | |
383 const std::string extended_sql_query = | |
384 sql_query + "OR signon_realm REGEXP ? "; | |
385 // TODO(nyquist) Re-enable usage of cached statements if possible. | |
386 // s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); | |
387 s.Assign(db_.GetUniqueStatement(extended_sql_query.c_str())); | |
388 std::string domain = GetRegistryControlledDomain(form.signon_realm); | |
389 // We need to escape ., - and _ in the domain. Since the domain has already | |
palmer
2013/06/11 18:55:45
The comment doesn't match the code --- you don't e
nyquist
2013/06/11 23:54:43
I don't think _ has to be escaped in regexp. Updat
| |
390 // been sanitized using GURL, we do not need to escape any other characters. | |
391 ReplaceChars(domain, ".", "\\.", &domain); | |
392 ReplaceChars(domain, "-", "\\-", &domain); | |
393 // For a domain such as foo.bar, this regexp will match domains for http | |
394 // and https and on the form: http://foo.bar/, http://www.foo.bar/, | |
395 // http://www.mobile.foo.bar/. It will not match http://notfoo.bar/. | |
396 // It also matches any port, such as http://foo.bar:8080/. | |
397 std::string regexp = | |
398 "^((http|https):\\/\\/)([\\w\\-_]+\\.)*" + domain + "(:\\d+)?\\/$"; | |
palmer
2013/06/11 18:55:45
It doesn't feel right to match loosely on the port
nyquist
2013/06/11 23:54:43
Made this more strict. Now both scheme and port ha
| |
399 s.BindString(0, form.signon_realm); | |
400 s.BindString(1, regexp); | |
401 } else { | |
402 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); | |
403 s.BindString(0, form.signon_realm); | |
404 } | |
368 | 405 |
369 while (s.Step()) { | 406 while (s.Step()) { |
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); | 407 scoped_ptr<PasswordForm> new_form(new PasswordForm()); |
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) | 408 if (!InitPasswordFormFromStatement(new_form.get(), s)) |
372 return false; | 409 return false; |
410 if (public_suffix_domain_matching_) { | |
411 const std::string found_registry_controlled_domain = | |
412 GetRegistryControlledDomain(new_form->signon_realm); | |
413 const std::string form_registry_controlled_domain = | |
414 GetRegistryControlledDomain(form.signon_realm); | |
415 if (found_registry_controlled_domain != form_registry_controlled_domain) { | |
416 // The database returned results that should not match. Skipping result. | |
417 continue; | |
418 } | |
419 if (form.signon_realm != new_form->signon_realm) { | |
420 // This is not a perfect match, so we need to create a new valid result. | |
421 // We do this by copying over origin, signon realm and action from the | |
422 // observed form and setting the original signon realm to what we found | |
423 // in the database. We use the fact that |original_signon_realm| is | |
424 // non-empty to communicate that this match was found using public | |
425 // suffix matching. | |
426 new_form->original_signon_realm = new_form->signon_realm; | |
427 new_form->origin = form.origin; | |
428 new_form->signon_realm = form.signon_realm; | |
429 new_form->action = form.action; | |
430 } | |
431 } | |
373 forms->push_back(new_form.release()); | 432 forms->push_back(new_form.release()); |
374 } | 433 } |
375 return s.Succeeded(); | 434 return s.Succeeded(); |
376 } | 435 } |
377 | 436 |
378 bool LoginDatabase::GetLoginsCreatedBetween( | 437 bool LoginDatabase::GetLoginsCreatedBetween( |
379 const base::Time begin, | 438 const base::Time begin, |
380 const base::Time end, | 439 const base::Time end, |
381 std::vector<content::PasswordForm*>* forms) const { | 440 std::vector<content::PasswordForm*>* forms) const { |
382 DCHECK(forms); | 441 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 { | 512 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { |
454 std::vector<string16> ret; | 513 std::vector<string16> ret; |
455 string16 str; | 514 string16 str; |
456 | 515 |
457 PickleIterator iterator(p); | 516 PickleIterator iterator(p); |
458 while (iterator.ReadString16(&str)) { | 517 while (iterator.ReadString16(&str)) { |
459 ret.push_back(str); | 518 ret.push_back(str); |
460 } | 519 } |
461 return ret; | 520 return ret; |
462 } | 521 } |
OLD | NEW |