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 const std::string extended_sql_query = | |
413 sql_query + "OR signon_realm REGEXP ? "; | |
Mike Mammarella
2013/06/20 20:20:23
I might include a comment here that notes that the
nyquist
2013/06/21 21:01:27
Done.
| |
414 // TODO(nyquist) Re-enable usage of GetCachedStatement when | |
415 // http://crbug.com/248608 is fixed. | |
416 s.Assign(db_.GetUniqueStatement(extended_sql_query.c_str())); | |
417 const GURL signon_realm(form.signon_realm); | |
418 std::string domain = GetRegistryControlledDomain(signon_realm); | |
419 // We need to escape . and - in the domain. Since the domain has already | |
Mike Mammarella
2013/06/20 20:20:23
Why do you need to escape - ? Does it have some sp
nyquist
2013/06/21 21:01:27
The - is used when using things like A-Z. However,
Ilya Sherman
2013/06/21 21:16:24
Hmm, this "presumably" worries me. Isn't the URL
| |
420 // been sanitized using GURL, we do not need to escape any other characters. | |
421 ReplaceChars(domain, ".", "\\.", &domain); | |
422 ReplaceChars(domain, "-", "\\-", &domain); | |
423 std::string scheme = signon_realm.scheme(); | |
424 // We need to escape . and - in the scheme. Since the scheme has already | |
425 // been sanitized using GURL, we do not need to escape any other characters. | |
426 // view-source is an example with '-' and soap.beep is an example with '.'. | |
427 ReplaceChars(scheme, ".", "\\.", &scheme); | |
428 ReplaceChars(scheme, "-", "\\-", &scheme); | |
429 const std::string port = signon_realm.port(); | |
430 // For a signon realm such as http://foo.bar/, this regexp will match | |
431 // domains on the form http://foo.bar/, http://www.foo.bar/, | |
432 // http://www.mobile.foo.bar/. It will not match http://notfoo.bar/. | |
433 // The scheme and port has to be the same as the observed form. | |
434 std::string regexp = "^(" + scheme + ":\\/\\/)([\\w\\-_]+\\.)*" + | |
Mike Mammarella
2013/06/20 20:20:23
_ is not actually a valid character for DNS names,
nyquist
2013/06/21 21:01:27
Removed _. Since - is now the last character in th
| |
435 domain + "(:" + port + ")?\\/$"; | |
436 s.BindString(0, form.signon_realm); | |
437 s.BindString(1, regexp); | |
438 } else { | |
439 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); | |
440 s.BindString(0, form.signon_realm); | |
441 } | |
368 | 442 |
369 while (s.Step()) { | 443 while (s.Step()) { |
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); | 444 scoped_ptr<PasswordForm> new_form(new PasswordForm()); |
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) | 445 if (!InitPasswordFormFromStatement(new_form.get(), s)) |
372 return false; | 446 return false; |
447 if (public_suffix_domain_matching_) { | |
448 if (!SchemeMatches(new_form, form) || | |
449 !RegistryControlledDomainMatches(new_form, form) || | |
450 !PortMatches(new_form, form)) { | |
451 // The database returned results that should not match. Skipping result. | |
452 continue; | |
453 } | |
454 if (form.signon_realm != new_form->signon_realm) { | |
455 // This is not a perfect match, so we need to create a new valid result. | |
456 // We do this by copying over origin, signon realm and action from the | |
457 // observed form and setting the original signon realm to what we found | |
458 // in the database. We use the fact that |original_signon_realm| is | |
459 // non-empty to communicate that this match was found using public | |
460 // suffix matching. | |
461 new_form->original_signon_realm = new_form->signon_realm; | |
462 new_form->origin = form.origin; | |
463 new_form->signon_realm = form.signon_realm; | |
464 new_form->action = form.action; | |
465 } | |
466 } | |
373 forms->push_back(new_form.release()); | 467 forms->push_back(new_form.release()); |
374 } | 468 } |
375 return s.Succeeded(); | 469 return s.Succeeded(); |
376 } | 470 } |
377 | 471 |
378 bool LoginDatabase::GetLoginsCreatedBetween( | 472 bool LoginDatabase::GetLoginsCreatedBetween( |
379 const base::Time begin, | 473 const base::Time begin, |
380 const base::Time end, | 474 const base::Time end, |
381 std::vector<content::PasswordForm*>* forms) const { | 475 std::vector<content::PasswordForm*>* forms) const { |
382 DCHECK(forms); | 476 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 { | 547 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { |
454 std::vector<string16> ret; | 548 std::vector<string16> ret; |
455 string16 str; | 549 string16 str; |
456 | 550 |
457 PickleIterator iterator(p); | 551 PickleIterator iterator(p); |
458 while (iterator.ReadString16(&str)) { | 552 while (iterator.ReadString16(&str)) { |
459 ret.push_back(str); | 553 ret.push_back(str); |
460 } | 554 } |
461 return ret; | 555 return ret; |
462 } | 556 } |
OLD | NEW |