From 588d83df8ab987a031f35c1139f8753f6d2cb1b0 Mon Sep 17 00:00:00 2001 From: "gcasto@chromium.org" Date: Sat, 8 Jun 2013 05:03:12 +0000 Subject: [PATCH] [PasswordAutofill] Only autofill passwords into fields BUG=247513 Review URL: https://chromiumcodereview.appspot.com/15848020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@205022 0039d316-1c4b-4281-b951-d872f2087c98 --- .../password_autofill_agent_browsertest.cc | 61 ++++++++++++++++++++++ .../autofill/common/password_form_fill_data.cc | 1 + .../content/renderer/password_autofill_agent.cc | 11 +++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc index 6292c52acced..1c8ddbaf9492 100644 --- a/chrome/renderer/autofill/password_autofill_agent_browsertest.cc +++ b/chrome/renderer/autofill/password_autofill_agent_browsertest.cc @@ -89,6 +89,7 @@ class PasswordAutofillAgentTest : public ChromeRenderViewTest { FormFieldData password_field; password_field.name = ASCIIToUTF16(kPasswordName); password_field.value = password1_; + password_field.form_control_type = "password"; fill_data_.basic_data.fields.push_back(password_field); fill_data_.additional_logins[username2_] = password2_; @@ -280,6 +281,66 @@ TEST_F(PasswordAutofillAgentTest, NoInitialAutocompleteForFilledField) { CheckTextFieldsState("bogus", false, std::string(), false); } +TEST_F(PasswordAutofillAgentTest, NoAutocompleteForTextFieldPasswords) { + const char kTextFieldPasswordFormHTML[] = + "
" + " " + " " + " " + "
"; + LoadHTML(kTextFieldPasswordFormHTML); + + // Retrieve the input elements so the test can access them. + WebDocument document = GetMainFrame()->document(); + WebElement element = + document.getElementById(WebString::fromUTF8(kUsernameName)); + ASSERT_FALSE(element.isNull()); + username_element_ = element.to(); + element = document.getElementById(WebString::fromUTF8(kPasswordName)); + ASSERT_FALSE(element.isNull()); + password_element_ = element.to(); + + // Set the expected form origin URL. + std::string origin("data:text/html;charset=utf-8,"); + origin += kTextFieldPasswordFormHTML; + fill_data_.basic_data.origin = GURL(origin); + + SimulateOnFillPasswordForm(fill_data_); + + // Fields should still be empty. + CheckTextFieldsState(std::string(), false, std::string(), false); +} + +TEST_F(PasswordAutofillAgentTest, NoAutocompleteForPasswordFieldUsernames) { + const char kPasswordFieldUsernameFormHTML[] = + "
" + " " + " " + " " + "
"; + LoadHTML(kPasswordFieldUsernameFormHTML); + + // Retrieve the input elements so the test can access them. + WebDocument document = GetMainFrame()->document(); + WebElement element = + document.getElementById(WebString::fromUTF8(kUsernameName)); + ASSERT_FALSE(element.isNull()); + username_element_ = element.to(); + element = document.getElementById(WebString::fromUTF8(kPasswordName)); + ASSERT_FALSE(element.isNull()); + password_element_ = element.to(); + + // Set the expected form origin URL. + std::string origin("data:text/html;charset=utf-8,"); + origin += kPasswordFieldUsernameFormHTML; + fill_data_.basic_data.origin = GURL(origin); + + SimulateOnFillPasswordForm(fill_data_); + + // Fields should still be empty. + CheckTextFieldsState(std::string(), false, std::string(), false); +} + // Tests that having a matching username does not preclude the autocomplete. TEST_F(PasswordAutofillAgentTest, InitialAutocompleteForMatchingFilledField) { username_element_.setValue(WebString::fromUTF8(kAliceUsername)); diff --git a/components/autofill/common/password_form_fill_data.cc b/components/autofill/common/password_form_fill_data.cc index e31823df50d8..99e089e57089 100644 --- a/components/autofill/common/password_form_fill_data.cc +++ b/components/autofill/common/password_form_fill_data.cc @@ -42,6 +42,7 @@ void InitPasswordFormFillData( FormFieldData password_field; password_field.name = form_on_page.password_element; password_field.value = preferred_match->password_value; + password_field.form_control_type = "password"; // Fill basic form data. result->basic_data.origin = form_on_page.origin; diff --git a/components/autofill/content/renderer/password_autofill_agent.cc b/components/autofill/content/renderer/password_autofill_agent.cc index 551afd9726e4..567647219483 100644 --- a/components/autofill/content/renderer/password_autofill_agent.cc +++ b/components/autofill/content/renderer/password_autofill_agent.cc @@ -74,13 +74,20 @@ static bool FindFormInputElements(WebKit::WebFormElement* fe, break; } + // Only fill saved passwords into password fields and usernames into + // text fields. + WebKit::WebInputElement input_element = + temp_elements[i].to(); + if (input_element.isPasswordField() != + (data.fields[j].form_control_type == "password")) + continue; + // This element matched, add it to our temporary result. It's possible // there are multiple matches, but for purposes of identifying the form // one suffices and if some function needs to deal with multiple // matching elements it can get at them through the FormElement*. // Note: This assignment adds a reference to the InputElement. - result->input_elements[data.fields[j].name] = - temp_elements[i].to(); + result->input_elements[data.fields[j].name] = input_element; found_input = true; } } -- 2.11.4.GIT