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/time.h" | 18 #include "base/time.h" |
17 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.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 GetPSLDomain(std::string signon_realm_str) { | |
Ilya Sherman
2013/06/06 09:25:35
nit: Pass by const reference.
Ilya Sherman
2013/06/06 09:25:35
Please spell out acronyms like PSL; or better yet,
palmer
2013/06/06 21:16:42
|GetPublicDomainSuffix|? |GetPublicSuffix|?
nyquist
2013/06/07 22:51:10
Is |GetRegistryControlledDomain| OK? Since we tech
| |
53 GURL signon_realm(signon_realm_str); | |
palmer
2013/06/06 21:16:42
Declare this const if the callee takes a const &.
nyquist
2013/06/07 22:51:10
Done.
| |
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 psl_domain_matching_ = CommandLine::ForCurrentProcess()->HasSwitch( | |
116 switches::kEnablePasswordAutofillPSLDomainMatching); | |
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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 form->origin = GURL(tmp); | 342 form->origin = GURL(tmp); |
328 tmp = s.ColumnString(COLUMN_ACTION_URL); | 343 tmp = s.ColumnString(COLUMN_ACTION_URL); |
329 form->action = GURL(tmp); | 344 form->action = GURL(tmp); |
330 form->username_element = s.ColumnString16(COLUMN_USERNAME_ELEMENT); | 345 form->username_element = s.ColumnString16(COLUMN_USERNAME_ELEMENT); |
331 form->username_value = s.ColumnString16(COLUMN_USERNAME_VALUE); | 346 form->username_value = s.ColumnString16(COLUMN_USERNAME_VALUE); |
332 form->password_element = s.ColumnString16(COLUMN_PASSWORD_ELEMENT); | 347 form->password_element = s.ColumnString16(COLUMN_PASSWORD_ELEMENT); |
333 form->password_value = decrypted_password; | 348 form->password_value = decrypted_password; |
334 form->submit_element = s.ColumnString16(COLUMN_SUBMIT_ELEMENT); | 349 form->submit_element = s.ColumnString16(COLUMN_SUBMIT_ELEMENT); |
335 tmp = s.ColumnString(COLUMN_SIGNON_REALM); | 350 tmp = s.ColumnString(COLUMN_SIGNON_REALM); |
336 form->signon_realm = tmp; | 351 form->signon_realm = tmp; |
352 form->original_signon_realm = ""; | |
353 form->is_psl_origin_match = false; | |
337 form->ssl_valid = (s.ColumnInt(COLUMN_SSL_VALID) > 0); | 354 form->ssl_valid = (s.ColumnInt(COLUMN_SSL_VALID) > 0); |
338 form->preferred = (s.ColumnInt(COLUMN_PREFERRED) > 0); | 355 form->preferred = (s.ColumnInt(COLUMN_PREFERRED) > 0); |
339 form->date_created = base::Time::FromTimeT( | 356 form->date_created = base::Time::FromTimeT( |
340 s.ColumnInt64(COLUMN_DATE_CREATED)); | 357 s.ColumnInt64(COLUMN_DATE_CREATED)); |
341 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0); | 358 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0); |
342 int scheme_int = s.ColumnInt(COLUMN_SCHEME); | 359 int scheme_int = s.ColumnInt(COLUMN_SCHEME); |
343 DCHECK((scheme_int >= 0) && (scheme_int <= PasswordForm::SCHEME_OTHER)); | 360 DCHECK((scheme_int >= 0) && (scheme_int <= PasswordForm::SCHEME_OTHER)); |
344 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int); | 361 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int); |
345 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE); | 362 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE); |
346 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_GENERATED); | 363 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_GENERATED); |
347 form->type = static_cast<PasswordForm::Type>(type_int); | 364 form->type = static_cast<PasswordForm::Type>(type_int); |
348 Pickle pickle( | 365 Pickle pickle( |
349 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAMES)), | 366 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAMES)), |
350 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); | 367 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); |
351 form->other_possible_usernames = DeserializeVector(pickle); | 368 form->other_possible_usernames = DeserializeVector(pickle); |
352 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); | 369 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); |
353 return true; | 370 return true; |
354 } | 371 } |
355 | 372 |
356 bool LoginDatabase::GetLogins(const PasswordForm& form, | 373 bool LoginDatabase::GetLogins(const PasswordForm& form, |
357 std::vector<PasswordForm*>* forms) const { | 374 std::vector<PasswordForm*>* forms) const { |
358 DCHECK(forms); | 375 DCHECK(forms); |
359 // You *must* change LoginTableColumns if this query changes. | 376 // You *must* change LoginTableColumns if this query changes. |
360 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 377 std::string signon_realm_operator = psl_domain_matching_ ? "regexp" : "=="; |
361 "SELECT origin_url, action_url, " | 378 std::string sql_query = "SELECT origin_url, action_url, " |
palmer
2013/06/06 21:16:42
I think you can declare these strings const? I'm b
nyquist
2013/06/07 22:51:10
Done.
| |
362 "username_element, username_value, " | 379 "username_element, username_value, " |
363 "password_element, password_value, submit_element, " | 380 "password_element, password_value, submit_element, " |
364 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " | 381 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " |
365 "scheme, password_type, possible_usernames, times_used " | 382 "scheme, password_type, possible_usernames, times_used " |
366 "FROM logins WHERE signon_realm == ? ")); | 383 "FROM logins WHERE signon_realm " + signon_realm_operator + " ? "; |
367 s.BindString(0, form.signon_realm); | 384 sql::Statement s; |
385 if (psl_domain_matching_) { | |
386 // TODO(nyquist) Re-enable usage of cached statements if possible. | |
387 // s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); | |
palmer
2013/06/06 21:16:42
Perhaps you still can? Have you tried it? It shoul
nyquist
2013/06/07 22:51:10
Will try again before I submit this. I agree that
| |
388 s.Assign(db_.GetUniqueStatement(sql_query.c_str())); | |
389 std::string domain = GetPSLDomain(form.signon_realm); | |
390 // We need to escape and . in the domain. Since the domain has already been | |
391 // sanitized using GURL, we do not need to escape any other characters. | |
392 ReplaceChars(domain, ".", "\\.", &domain); | |
393 // For a domain such as foo.bar, this regexp will match domains for any | |
394 // scheme 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 std::string regexp = "(\\w+?:\\/\\/)([\\w\\-_]+\\.)*" + domain + "\\/$"; | |
palmer
2013/06/06 21:16:42
Can we instead use an explicit list of schemata, i
nyquist
2013/06/07 22:51:10
Done. Also added ^ at start of query. Still keepin
| |
397 s.BindString(0, regexp); | |
398 } else { | |
399 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); | |
400 s.BindString(0, form.signon_realm); | |
401 } | |
368 | 402 |
369 while (s.Step()) { | 403 while (s.Step()) { |
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); | 404 scoped_ptr<PasswordForm> new_form(new PasswordForm()); |
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) | 405 if (!InitPasswordFormFromStatement(new_form.get(), s)) |
372 return false; | 406 return false; |
407 if (psl_domain_matching_) { | |
408 std::string found_psl_domain = GetPSLDomain(new_form->signon_realm); | |
palmer
2013/06/06 21:16:42
More constiness, if possible
nyquist
2013/06/07 22:51:10
Done.
| |
409 std::string form_psl_domain = GetPSLDomain(form.signon_realm); | |
410 if (found_psl_domain != form_psl_domain) { | |
411 // The database returned results that should not match. Skipping result. | |
412 continue; | |
413 } | |
414 if (form.signon_realm != new_form->signon_realm) { | |
415 // This is not a perfect match, so we need to create a new valid result. | |
416 // We do this by copying over origin, signon realm and action from the | |
417 // observed form and setting the original signon realm to what we found | |
418 // in the database. We also set a flag to notify the caller that this | |
419 // is not a perfect match. | |
420 new_form->is_psl_origin_match = true; | |
421 new_form->original_signon_realm = new_form->signon_realm; | |
422 new_form->origin = form.origin; | |
423 new_form->signon_realm = form.signon_realm; | |
424 new_form->action = form.action; | |
425 } | |
426 } | |
373 forms->push_back(new_form.release()); | 427 forms->push_back(new_form.release()); |
374 } | 428 } |
375 return s.Succeeded(); | 429 return s.Succeeded(); |
376 } | 430 } |
377 | 431 |
378 bool LoginDatabase::GetLoginsCreatedBetween( | 432 bool LoginDatabase::GetLoginsCreatedBetween( |
379 const base::Time begin, | 433 const base::Time begin, |
380 const base::Time end, | 434 const base::Time end, |
381 std::vector<content::PasswordForm*>* forms) const { | 435 std::vector<content::PasswordForm*>* forms) const { |
382 DCHECK(forms); | 436 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 { | 507 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { |
454 std::vector<string16> ret; | 508 std::vector<string16> ret; |
455 string16 str; | 509 string16 str; |
456 | 510 |
457 PickleIterator iterator(p); | 511 PickleIterator iterator(p); |
458 while (iterator.ReadString16(&str)) { | 512 while (iterator.ReadString16(&str)) { |
459 ret.push_back(str); | 513 ret.push_back(str); |
460 } | 514 } |
461 return ret; | 515 return ret; |
462 } | 516 } |
OLD | NEW |