Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/password_manager/login_database.cc

Issue 15660018: [autofill] Add support for PSL domain matching for password autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments from palmer Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 const std::string sql_query = "SELECT origin_url, action_url, "
361 "SELECT origin_url, action_url, "
362 "username_element, username_value, " 378 "username_element, username_value, "
363 "password_element, password_value, submit_element, " 379 "password_element, password_value, submit_element, "
364 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " 380 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, "
365 "scheme, password_type, possible_usernames, times_used " 381 "scheme, password_type, possible_usernames, times_used "
366 "FROM logins WHERE signon_realm == ? ")); 382 "FROM logins WHERE signon_realm == ? ";
367 s.BindString(0, form.signon_realm); 383 sql::Statement s;
384 if (psl_domain_matching_) {
385 const std::string extended_sql_query =
386 sql_query + "OR signon_realm REGEXP ? ";
387 // TODO(nyquist) Re-enable usage of cached statements if possible.
388 // s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str()));
389 s.Assign(db_.GetUniqueStatement(extended_sql_query.c_str()));
390 std::string domain = GetRegistryControlledDomain(form.signon_realm);
391 // We need to escape ., - and _ in the domain. Since the domain has already
392 // been sanitized using GURL, we do not need to escape any other characters.
393 ReplaceChars(domain, ".", "\\.", &domain);
394 ReplaceChars(domain, "-", "\\-", &domain);
395 // For a domain such as foo.bar, this regexp will match domains for http
396 // and https and on the form: http://foo.bar/, http://www.foo.bar/,
397 // http://www.mobile.foo.bar/. It will not match http://notfoo.bar/.
398 // It also matches any port, such as http://foo.bar:8080/.
399 std::string regexp =
400 "^((http|https):\\/\\/)([\\w\\-_]+\\.)*" + domain + "(:\\d+)?\\/$";
401 s.BindString(0, form.signon_realm);
402 s.BindString(1, regexp);
403 } else {
404 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str()));
405 s.BindString(0, form.signon_realm);
406 }
368 407
369 while (s.Step()) { 408 while (s.Step()) {
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); 409 scoped_ptr<PasswordForm> new_form(new PasswordForm());
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) 410 if (!InitPasswordFormFromStatement(new_form.get(), s))
372 return false; 411 return false;
412 if (psl_domain_matching_) {
413 const std::string found_registry_controlled_domain =
414 GetRegistryControlledDomain(new_form->signon_realm);
415 const std::string form_registry_controlled_domain =
416 GetRegistryControlledDomain(form.signon_realm);
417 if (found_registry_controlled_domain != form_registry_controlled_domain) {
418 // The database returned results that should not match. Skipping result.
419 continue;
420 }
421 if (form.signon_realm != new_form->signon_realm) {
422 // This is not a perfect match, so we need to create a new valid result.
423 // We do this by copying over origin, signon realm and action from the
424 // observed form and setting the original signon realm to what we found
425 // in the database. We also set a flag to notify the caller that this
426 // is not a perfect match.
427 new_form->is_psl_origin_match = true;
428 new_form->original_signon_realm = new_form->signon_realm;
429 new_form->origin = form.origin;
430 new_form->signon_realm = form.signon_realm;
431 new_form->action = form.action;
432 }
433 }
373 forms->push_back(new_form.release()); 434 forms->push_back(new_form.release());
374 } 435 }
375 return s.Succeeded(); 436 return s.Succeeded();
376 } 437 }
377 438
378 bool LoginDatabase::GetLoginsCreatedBetween( 439 bool LoginDatabase::GetLoginsCreatedBetween(
379 const base::Time begin, 440 const base::Time begin,
380 const base::Time end, 441 const base::Time end,
381 std::vector<content::PasswordForm*>* forms) const { 442 std::vector<content::PasswordForm*>* forms) const {
382 DCHECK(forms); 443 DCHECK(forms);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { 514 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const {
454 std::vector<string16> ret; 515 std::vector<string16> ret;
455 string16 str; 516 string16 str;
456 517
457 PickleIterator iterator(p); 518 PickleIterator iterator(p);
458 while (iterator.ReadString16(&str)) { 519 while (iterator.ReadString16(&str)) {
459 ret.push_back(str); 520 ret.push_back(str);
460 } 521 }
461 return ret; 522 return ret;
462 } 523 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698