blob: bea7e2acb257da1ce9da815f9d8bf66716d73e1c [file] [log] [blame]
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <utility>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/password_manager/password_manager_test_base.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/login/login_handler.h"
#include "chrome/browser/ui/login/login_handler_test_utils.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/content/browser/content_password_manager_driver.h"
#include "components/password_manager/content/browser/content_password_manager_driver_factory.h"
#include "components/password_manager/core/browser/http_auth_manager.h"
#include "components/password_manager/core/browser/http_auth_observer.h"
#include "components/password_manager/core/browser/password_form_manager.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "components/password_manager/core/common/password_manager_features.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "net/base/filename_util.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/platform/web_input_event.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/geometry/point.h"
using base::ASCIIToUTF16;
using base::Feature;
using testing::_;
using testing::ElementsAre;
namespace password_manager {
namespace {
class PasswordManagerBrowserTest : public PasswordManagerBrowserTestBase {
public:
PasswordManagerBrowserTest() {
// Turn off waiting for server predictions before filing. It makes filling
// behaviour more deterministic. Filling with server predictions is tested
// in PasswordFormManager unit tests.
password_manager::PasswordFormManager::
set_wait_for_server_predictions_for_filling(false);
}
~PasswordManagerBrowserTest() override = default;
};
class MockHttpAuthObserver : public password_manager::HttpAuthObserver {
public:
MOCK_METHOD2(OnAutofillDataAvailable,
void(const base::string16&, const base::string16&));
MOCK_METHOD0(OnLoginModelDestroying, void());
};
GURL GetFileURL(const char* filename) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath path;
base::PathService::Get(chrome::DIR_TEST_DATA, &path);
path = path.AppendASCII("password").AppendASCII(filename);
CHECK(base::PathExists(path));
return net::FilePathToFileURL(path);
}
// Handles |request| to "/basic_auth". If "Authorization" header is present,
// responds with a non-empty HTTP 200 page (regardless of its value). Otherwise
// serves a Basic Auth challenge.
std::unique_ptr<net::test_server::HttpResponse> HandleTestAuthRequest(
const net::test_server::HttpRequest& request) {
if (!base::StartsWith(request.relative_url, "/basic_auth",
base::CompareCase::SENSITIVE))
return nullptr;
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
if (base::Contains(request.headers, "Authorization")) {
http_response->set_code(net::HTTP_OK);
http_response->set_content("Success!");
} else {
http_response->set_code(net::HTTP_UNAUTHORIZED);
std::string realm = base::EndsWith(request.relative_url, "/empty_realm",
base::CompareCase::SENSITIVE)
? "\"\""
: "\"test realm\"";
http_response->AddCustomHeader("WWW-Authenticate", "Basic realm=" + realm);
}
return http_response;
}
class ObservingAutofillClient
: public autofill::TestAutofillClient,
public content::WebContentsUserData<ObservingAutofillClient> {
public:
~ObservingAutofillClient() override {}
// Wait until the autofill popup is shown.
void WaitForAutofillPopup() {
base::RunLoop run_loop;
run_loop_ = &run_loop;
run_loop.Run();
DCHECK(!run_loop_);
}
bool popup_shown() const { return popup_shown_; }
void ShowAutofillPopup(
const gfx::RectF& element_bounds,
base::i18n::TextDirection text_direction,
const std::vector<autofill::Suggestion>& suggestions,
bool autoselect_first_suggestion,
autofill::PopupType popup_type,
base::WeakPtr<autofill::AutofillPopupDelegate> delegate) override {
if (run_loop_)
run_loop_->Quit();
run_loop_ = nullptr;
popup_shown_ = true;
}
private:
explicit ObservingAutofillClient(content::WebContents* web_contents)
: run_loop_(nullptr), popup_shown_(false) {}
friend class content::WebContentsUserData<ObservingAutofillClient>;
base::RunLoop* run_loop_;
bool popup_shown_;
WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(ObservingAutofillClient);
};
WEB_CONTENTS_USER_DATA_KEY_IMPL(ObservingAutofillClient)
void TestPromptNotShown(const char* failure_message,
content::WebContents* web_contents) {
SCOPED_TRACE(testing::Message(failure_message));
NavigationObserver observer(web_contents);
std::string fill_and_submit =
"document.getElementById('username_failed').value = 'temp';"
"document.getElementById('password_failed').value = 'random';"
"document.getElementById('failed_form').submit()";
ASSERT_TRUE(content::ExecuteScript(web_contents, fill_and_submit));
observer.Wait();
EXPECT_FALSE(BubbleObserver(web_contents).IsSavePromptShownAutomatically());
}
// Generate HTML for a simple password form with the specified action URL.
std::string GeneratePasswordFormForAction(const GURL& action_url) {
return "<form method='POST' action='" + action_url.spec() + "'"
" onsubmit='return true;' id='testform'>"
" <input type='password' id='password_field'>"
"</form>";
}
// Inject an about:blank frame with a password form that uses the specified
// action URL into |web_contents|.
void InjectBlankFrameWithPasswordForm(content::WebContents* web_contents,
const GURL& action_url) {
std::string form_html = GeneratePasswordFormForAction(action_url);
std::string inject_blank_frame_with_password_form =
"var frame = document.createElement('iframe');"
"frame.id = 'iframe';"
"document.body.appendChild(frame);"
"frame.contentDocument.body.innerHTML = \"" + form_html + "\"";
ASSERT_TRUE(content::ExecuteScript(web_contents,
inject_blank_frame_with_password_form));
}
// Fills in a fake password and submits the form in |frame|, waiting for the
// submit navigation to finish. |action_url| is the form action URL to wait
// for.
void SubmitInjectedPasswordForm(content::WebContents* web_contents,
content::RenderFrameHost* frame,
const GURL& action_url) {
std::string submit_form =
"document.getElementById('password_field').value = 'pa55w0rd';"
"document.getElementById('testform').submit();";
NavigationObserver observer(web_contents);
observer.SetPathToWaitFor(action_url.path());
ASSERT_TRUE(content::ExecuteScript(frame, submit_form));
observer.Wait();
}
// Actual tests ---------------------------------------------------------------
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForNormalSubmit) {
NavigateToFile("/password/password_form.html");
// Fill a form and submit through a <input type="submit"> button. Nothing
// special.
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
// Save the password and check the store.
BubbleObserver bubble_observer(WebContents());
EXPECT_TRUE(bubble_observer.IsSavePromptShownAutomatically());
bubble_observer.AcceptSavePrompt();
WaitForPasswordStore();
CheckThatCredentialsStored("temp", "random");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptIfFormReappeared) {
NavigateToFile("/password/failed.html");
TestPromptNotShown("normal form", WebContents());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptIfFormReappearedWithPartsHidden) {
NavigateToFile("/password/failed_partly_visible.html");
TestPromptNotShown("partly visible form", WebContents());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptIfFormReappearedInputOutsideFor) {
NavigateToFile("/password/failed_input_outside.html");
TestPromptNotShown("form with input outside", WebContents());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptAfterCredentialsAPIPasswordStore) {
NavigateToFile("/password/password_form.html");
// Simulate the Credential Manager API function store() is called and
// PasswordManager instance is notified about that.
ChromePasswordManagerClient::FromWebContents(WebContents())
->NotifyStorePasswordCalled();
// Fill a form and submit through a <input type="submit"> button. The
// renderer should not send "PasswordFormsParsed" messages after the page
// was loaded.
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForSubmitWithSameDocumentNavigation) {
NavigateToFile("/password/password_navigate_before_submit.html");
// Fill a form and submit through a <input type="submit"> button. Nothing
// special. The form does an in-page navigation before submitting.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
LoginSuccessWithUnrelatedForm) {
// Log in, see a form on the landing page. That form is not related to the
// login form (=has different input fields), so we should offer saving the
// password.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_unrelated').value = 'temp';"
"document.getElementById('password_unrelated').value = 'random';"
"document.getElementById('submit_unrelated').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, LoginFailed) {
// Log in, see a form on the landing page. That form is not related to the
// login form (=has a different action), so we should offer saving the
// password.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_failed').value = 'temp';"
"document.getElementById('password_failed').value = 'random';"
"document.getElementById('submit_failed').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, Redirects) {
NavigateToFile("/password/password_form.html");
// Fill a form and submit through a <input type="submit"> button. The form
// points to a redirection page.
NavigationObserver observer1(WebContents());
std::string fill_and_submit =
"document.getElementById('username_redirect').value = 'temp';"
"document.getElementById('password_redirect').value = 'random';"
"document.getElementById('submit_redirect').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer1.Wait();
BubbleObserver bubble_observer(WebContents());
EXPECT_TRUE(bubble_observer.IsSavePromptShownAutomatically());
// The redirection page now redirects via Javascript. We check that the
// bubble stays.
NavigationObserver observer2(WebContents());
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(), "window.location.href = 'done.html';"));
observer2.Wait();
EXPECT_TRUE(bubble_observer.IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForSubmitUsingJavaScript) {
NavigateToFile("/password/password_form.html");
// Fill a form and submit using <button> that calls submit() on the form.
// This should work regardless of the type of element, as long as submit() is
// called.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForDynamicForm) {
// Adding a PSL matching form is a workaround explained later.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
GURL psl_orogin = embedded_test_server()->GetURL("psl.example.com", "/");
signin_form.signon_realm = psl_orogin.spec();
signin_form.origin = psl_orogin;
signin_form.username_value = base::ASCIIToUTF16("unused_username");
signin_form.password_value = base::ASCIIToUTF16("unused_password");
password_store->AddLogin(signin_form);
// Show the dynamic form.
ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL(
"example.com", "/password/dynamic_password_form.html"));
ASSERT_TRUE(content::ExecuteScript(
WebContents(), "document.getElementById('create_form_button').click();"));
// Blink has a timer for 0.3 seconds before it updates the browser with the
// new dynamic form. We wait for the form being detected by observing the UI
// state. The state changes due to the matching credential saved above. Later
// the form submission is definitely noticed by the browser.
BubbleObserver(WebContents()).WaitForManagementState();
// Fill the dynamic password form and submit.
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"document.dynamic_form.username.value = 'tempro';"
"document.dynamic_form.password.value = 'random';"
"document.dynamic_form.submit()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(BubbleObserver(WebContents()).IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForNavigation) {
NavigateToFile("/password/password_form.html");
// Don't fill the password form, just navigate away. Shouldn't prompt.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(), "window.location.href = 'done.html';"));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForSubFrameNavigation) {
NavigateToFile("/password/multi_frames.html");
// If you are filling out a password form in one frame and a different frame
// navigates, this should not trigger the infobar.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
observer.SetPathToWaitFor("/password/done.html");
std::string fill =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';";
std::string navigate_frame =
"var second_iframe = document.getElementById('second_frame');"
"second_iframe.contentWindow.location.href = 'done.html';";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill));
ASSERT_TRUE(content::ExecuteScript(WebContents(), navigate_frame));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForSameFormWithDifferentAction) {
// Log in, see a form on the landing page. That form is related to the login
// form (has a different action but has same input fields), so we should not
// offer saving the password.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
auto prompt_observer = std::make_unique<BubbleObserver>(WebContents());
std::string fill_and_submit =
"document.getElementById('username_different_action').value = 'temp';"
"document.getElementById('password_different_action').value = 'random';"
"document.getElementById('submit_different_action').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForActionMutation) {
NavigateToFile("/password/password_form_action_mutation.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
BubbleObserver prompt_observer(WebContents());
std::string fill_and_submit =
"document.getElementById('username_action_mutation').value = 'temp';"
"document.getElementById('password_action_mutation').value = 'random';"
"document.getElementById('submit_action_mutation').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"XHR_FINISHED\"")
break;
}
EXPECT_FALSE(prompt_observer.IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForFormWithEnteredUsername) {
// Log in, see a form on the landing page. That form is not related to the
// login form but has the same username as was entered previously, so we
// should not offer saving the password.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
auto prompt_observer = std::make_unique<BubbleObserver>(WebContents());
std::string fill_and_submit =
"document.getElementById('username_contains_username').value = 'temp';"
"document.getElementById('password_contains_username').value = 'random';"
"document.getElementById('submit_contains_username').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForDifferentFormWithEmptyAction) {
// Log in, see a form on the landing page. That form is not related to the
// signin form. The signin and the form on the landing page have empty
// actions, so we should offer saving the password.
NavigateToFile("/password/navigate_to_same_url_empty_actions.html");
NavigationObserver observer(WebContents());
auto prompt_observer = std::make_unique<BubbleObserver>(WebContents());
std::string fill_and_submit =
"document.getElementById('username').value = 'temp';"
"document.getElementById('password').value = 'random';"
"document.getElementById('submit-button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptAfterSubmitWithSubFrameNavigation) {
NavigateToFile("/password/multi_frames.html");
// Make sure that we prompt to save password even if a sub-frame navigation
// happens first.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
observer.SetPathToWaitFor("/password/done.html");
std::string navigate_frame =
"var second_iframe = document.getElementById('second_frame');"
"second_iframe.contentWindow.location.href = 'other.html';";
std::string fill_and_submit =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';"
"frame_doc.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), navigate_frame));
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptAfterSubmitFromSubFrameToMainFrame) {
NavigateToFile("/password/password_form_in_crosssite_iframe.html");
// Create an iframe and navigate cross-site.
NavigationObserver iframe_observer(WebContents());
iframe_observer.SetPathToWaitFor("/password/password_form_with_hash.html");
GURL iframe_url =
embedded_test_server()->GetURL("/password/password_form_with_hash.html");
std::string create_iframe =
base::StringPrintf("create_iframe('%s');", iframe_url.spec().c_str());
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer.Wait();
// Make sure that we prompt to save the password even if the sub-frame submits
// the form to the main frame.
NavigationObserver observer(WebContents());
std::string fill_and_submit =
"var first_frame = document.getElementById('iframe');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'Admin';"
"frame_doc.getElementById('password_field').value = '12345';"
"frame_doc.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
BubbleObserver prompt_observer(WebContents());
EXPECT_TRUE(prompt_observer.IsSavePromptShownAutomatically());
prompt_observer.AcceptSavePrompt();
WaitForPasswordStore();
// The typed credentials are saved despite the password is hashed before
// submission.
CheckThatCredentialsStored("Admin", "12345");
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForFailedLoginFromMainFrameWithMultiFramesSameDocument) {
NavigateToFile("/password/multi_frames.html");
// Make sure that we don't prompt to save the password for a failed login
// from the main frame with multiple frames in the same page.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_failed').value = 'temp';"
"document.getElementById('password_failed').value = 'random';"
"document.getElementById('submit_failed').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForFailedLoginFromSubFrameWithMultiFramesSameDocument) {
NavigateToFile("/password/multi_frames.html");
// Make sure that we don't prompt to save the password for a failed login
// from a sub-frame with multiple frames in the same page.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_failed').value = 'temp';"
"frame_doc.getElementById('password_failed').value = 'random';"
"frame_doc.getElementById('submit_failed').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.SetPathToWaitFor("/password/failed.html");
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForXHRSubmit) {
NavigateToFile("/password/password_xhr_submit.html");
// Verify that we show the save password prompt if a form returns false
// in its onsubmit handler but instead logs in/navigates via XHR.
// Note that calling 'submit()' on a form with javascript doesn't call
// the onsubmit handler, so we click the submit button instead.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForXHRSubmitWithoutNavigation) {
NavigateToFile("/password/password_xhr_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if XHR without navigation occurs and the form has been filled
// out we try and save the password. Note that in general the submission
// doesn't need to be via form.submit(), but for testing purposes it's
// necessary since we otherwise ignore changes made to the value of these
// fields by script.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"XHR_FINISHED\"")
break;
}
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForXHRSubmitWithoutNavigation_SignupForm) {
NavigateToFile("/password/password_xhr_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if XHR without navigation occurs and the form has been filled
// out we try and save the password. Note that in general the submission
// doesn't need to be via form.submit(), but for testing purposes it's
// necessary since we otherwise ignore changes made to the value of these
// fields by script.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('signup_username_field').value = 'temp';"
"document.getElementById('signup_password_field').value = 'random';"
"document.getElementById('confirmation_password_field').value = 'random';"
"document.getElementById('signup_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"XHR_FINISHED\"")
break;
}
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForXHRSubmitWithoutNavigationWithUnfilledForm) {
NavigateToFile("/password/password_xhr_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if XHR without navigation occurs and the form has NOT been
// filled out we don't prompt.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"XHR_FINISHED\"")
break;
}
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForXHRSubmitWithoutNavigationWithUnfilledForm_SignupForm) {
NavigateToFile("/password/password_xhr_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if XHR without navigation occurs and the form has NOT been
// filled out we don't prompt.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('signup_username_field').value = 'temp';"
"document.getElementById('signup_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"XHR_FINISHED\"")
break;
}
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForFetchSubmit) {
NavigateToFile("/password/password_fetch_submit.html");
// Verify that we show the save password prompt if a form returns false
// in its onsubmit handler but instead logs in/navigates via Fetch.
// Note that calling 'submit()' on a form with javascript doesn't call
// the onsubmit handler, so we click the submit button instead.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Flaky on chromeos: http://crbug.com/870372
#if defined(OS_CHROMEOS)
#define MAYBE_PromptForFetchSubmitWithoutNavigation \
DISABLED_PromptForFetchSubmitWithoutNavigation
#else
#define MAYBE_PromptForFetchSubmitWithoutNavigation \
PromptForFetchSubmitWithoutNavigation
#endif
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
MAYBE_PromptForFetchSubmitWithoutNavigation) {
NavigateToFile("/password/password_fetch_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if XHR without navigation occurs and the form has been filled
// out we try and save the password. Note that in general the submission
// doesn't need to be via form.submit(), but for testing purposes it's
// necessary since we otherwise ignore changes made to the value of these
// fields by script.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"FETCH_FINISHED\"")
break;
}
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Flaky on chromeos: http://crbug.com/870372
#if defined(OS_CHROMEOS)
#define MAYBE_PromptForFetchSubmitWithoutNavigation_SignupForm \
DISABLED_PromptForFetchSubmitWithoutNavigation_SignupForm
#else
#define MAYBE_PromptForFetchSubmitWithoutNavigation_SignupForm \
PromptForFetchSubmitWithoutNavigation_SignupForm
#endif
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
MAYBE_PromptForFetchSubmitWithoutNavigation_SignupForm) {
NavigateToFile("/password/password_fetch_submit.html");
// Need to pay attention for a message that Fetch has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if Fetch without navigation occurs and the form has been filled
// out we try and save the password. Note that in general the submission
// doesn't need to be via form.submit(), but for testing purposes it's
// necessary since we otherwise ignore changes made to the value of these
// fields by script.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('signup_username_field').value = 'temp';"
"document.getElementById('signup_password_field').value = 'random';"
"document.getElementById('confirmation_password_field').value = 'random';"
"document.getElementById('signup_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"FETCH_FINISHED\"")
break;
}
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForFetchSubmitWithoutNavigationWithUnfilledForm) {
NavigateToFile("/password/password_fetch_submit.html");
// Need to pay attention for a message that Fetch has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if Fetch without navigation occurs and the form has NOT been
// filled out we don't prompt.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"FETCH_FINISHED\"")
break;
}
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForFetchSubmitWithoutNavigationWithUnfilledForm_SignupForm) {
NavigateToFile("/password/password_fetch_submit.html");
// Need to pay attention for a message that Fetch has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
// Verify that if Fetch without navigation occurs and the form has NOT been
// filled out we don't prompt.
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"navigate = false;"
"document.getElementById('signup_username_field').value = 'temp';"
"document.getElementById('signup_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"FETCH_FINISHED\"")
break;
}
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptIfLinkClicked) {
NavigateToFile("/password/password_form.html");
// Verify that if the user takes a direct action to leave the page, we don't
// prompt to save the password even if the form is already filled out.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_click_link =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('link').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_click_link));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// Disabled due to flakiness on windows.
#if defined(OS_WIN)
#define MAYBE_VerifyPasswordGenerationUpload \
DISABLED_VerifyPasswordGenerationUpload
#else
#define MAYBE_VerifyPasswordGenerationUpload VerifyPasswordGenerationUpload
#endif
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
MAYBE_VerifyPasswordGenerationUpload) {
// Prevent Autofill requests from actually going over the wire.
net::TestURLFetcherFactory factory;
// Disable Autofill requesting access to AddressBook data. This causes
// the test to hang on Mac.
autofill::test::DisableSystemServices(browser()->profile()->GetPrefs());
// Visit a signup form.
NavigateToFile("/password/signup_form.html");
// Enter a password and save it.
NavigationObserver first_observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('other_info').value = 'stuff';"
"document.getElementById('username_field').value = 'my_username';"
"document.getElementById('password_field').value = 'password';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
first_observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
prompt_observer->AcceptSavePrompt();
// Now navigate to a login form that has similar HTML markup.
NavigateToFile("/password/password_form.html");
// Simulate a user click to force an autofill of the form's DOM value, not
// just the suggested value.
content::SimulateMouseClick(WebContents(), 0,
blink::WebMouseEvent::Button::kLeft);
WaitForElementValue("username_field", "my_username");
WaitForElementValue("password_field", "password");
// Submit the form and verify that there is no infobar (as the password
// has already been saved).
NavigationObserver second_observer(WebContents());
std::unique_ptr<BubbleObserver> second_prompt_observer(
new BubbleObserver(WebContents()));
std::string submit_form =
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), submit_form));
second_observer.Wait();
EXPECT_FALSE(second_prompt_observer->IsSavePromptShownAutomatically());
// Verify that we sent two pings to Autofill. One vote for of PASSWORD for
// the current form, and one vote for ACCOUNT_CREATION_PASSWORD on the
// original form since it has more than 2 text input fields and was used for
// the first time on a different form.
base::HistogramBase* upload_histogram =
base::StatisticsRecorder::FindHistogram(
"PasswordGeneration.UploadStarted");
ASSERT_TRUE(upload_histogram);
std::unique_ptr<base::HistogramSamples> snapshot =
upload_histogram->SnapshotSamples();
EXPECT_EQ(0, snapshot->GetCount(0 /* failure */));
EXPECT_EQ(2, snapshot->GetCount(1 /* success */));
autofill::test::ReenableSystemServices();
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, PromptForSubmitFromIframe) {
NavigateToFile("/password/password_submit_from_iframe.html");
// Submit a form in an iframe, then cause the whole page to navigate without a
// user gesture. We expect the save password prompt to be shown here, because
// some pages use such iframes for login forms.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"var iframe = document.getElementById('test_iframe');"
"var iframe_doc = iframe.contentDocument;"
"iframe_doc.getElementById('username_field').value = 'temp';"
"iframe_doc.getElementById('password_field').value = 'random';"
"iframe_doc.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForInputElementWithoutName) {
// Check that the prompt is shown for forms where input elements lack the
// "name" attribute but the "id" is present.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field_no_name').value = 'temp';"
"document.getElementById('password_field_no_name').value = 'random';"
"document.getElementById('input_submit_button_no_name').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForInputElementWithoutId) {
// Check that the prompt is shown for forms where input elements lack the
// "id" attribute but the "name" attribute is present.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementsByName('username_field_no_id')[0].value = 'temp';"
"document.getElementsByName('password_field_no_id')[0].value = 'random';"
"document.getElementsByName('input_submit_button_no_id')[0].click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForInputElementWithoutIdAndName) {
// Check that prompt is shown for forms where the input fields lack both
// the "id" and the "name" attributes.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"var form = document.getElementById('testform_elements_no_id_no_name');"
"var username = form.children[0];"
"username.value = 'temp';"
"var password = form.children[1];"
"password.value = 'random';"
"form.children[2].click()"; // form.children[2] is the submit button.
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
prompt_observer->AcceptSavePrompt();
// Check that credentials are stored.
WaitForPasswordStore();
CheckThatCredentialsStored("temp", "random");
}
// Test for checking that no prompt is shown for URLs with file: scheme.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptForFileSchemeURLs) {
GURL url = GetFileURL("password_form.html");
ui_test_utils::NavigateToURL(browser(), url);
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForLandingPageWithHTTPErrorStatusCode) {
// Check that no prompt is shown for forms where the landing page has
// HTTP status 404.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field_http_error').value = 'temp';"
"document.getElementById('password_field_http_error').value = 'random';"
"document.getElementById('input_submit_button_http_error').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// TODO(crbug.com/949908) The test is flaky (crashing) on all platforms.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DISABLED_DeleteFrameBeforeSubmit) {
NavigateToFile("/password/multi_frames.html");
NavigationObserver observer(WebContents());
// Make sure we save some password info from an iframe and then destroy it.
std::string save_and_remove =
"var first_frame = document.getElementById('first_frame');"
"var frame_doc = first_frame.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';"
"frame_doc.getElementById('input_submit_button').click();"
"first_frame.parentNode.removeChild(first_frame);";
// Submit from the main frame, but without navigating through the onsubmit
// handler.
std::string navigate_frame =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click();"
"window.location.href = 'done.html';";
ASSERT_TRUE(content::ExecuteScript(WebContents(), save_and_remove));
ASSERT_TRUE(content::ExecuteScript(WebContents(), navigate_frame));
observer.Wait();
// The only thing we check here is that there is no use-after-free reported.
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
UsernameAndPasswordValueAccessible) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("admin");
signin_form.password_value = base::ASCIIToUTF16("12345");
password_store->AddLogin(signin_form);
// Steps from https://crbug.com/337429#c37.
// Navigate to the page, click a link that opens a second tab, reload the
// first tab and observe that the password is accessible.
NavigateToFile("/password/form_and_link.html");
// Click on a link to open a new tab, then switch back to the first one.
EXPECT_EQ(1, browser()->tab_strip_model()->count());
std::string click = "document.getElementById('testlink').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), click));
EXPECT_EQ(2, browser()->tab_strip_model()->count());
browser()->tab_strip_model()->ActivateTabAt(0);
// Reload the original page to have the saved credentials autofilled.
NavigationObserver reload_observer(WebContents());
NavigateToFile("/password/form_and_link.html");
reload_observer.Wait();
// Now check that the username and the password are not accessible yet.
CheckElementValue("username_field", "");
CheckElementValue("password_field", "");
// Let the user interact with the page.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
// Wait until that interaction causes the username and the password value to
// be revealed.
WaitForElementValue("username_field", "admin");
WaitForElementValue("password_field", "12345");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PasswordValueAccessibleOnSubmit) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("admin");
signin_form.password_value = base::ASCIIToUTF16("random_secret");
password_store->AddLogin(signin_form);
NavigateToFile("/password/form_and_link.html");
// Get the position of the 'submit' button.
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(),
"var submitRect = document.getElementById('input_submit_button')"
".getBoundingClientRect();"));
int top;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(), "window.domAutomationController.send(submitRect.top);",
&top));
int left;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send(submitRect.left);", &left));
NavigationObserver submit_observer(WebContents());
// Submit the form via a tap on the submit button.
content::SimulateTapDownAt(WebContents(), gfx::Point(left + 1, top + 1));
content::SimulateTapAt(WebContents(), gfx::Point(left + 1, top + 1));
submit_observer.Wait();
std::string query = WebContents()->GetURL().query();
EXPECT_THAT(query, testing::HasSubstr("random_secret"));
}
// Test fix for crbug.com/338650.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DontPromptForPasswordFormWithDefaultValue) {
NavigateToFile("/password/password_form_with_default_value.html");
// Don't prompt if we navigate away even if there is a password value since
// it's not coming from the user.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
NavigateToFile("/password/done.html");
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DontPromptForPasswordFormWithReadonlyPasswordField) {
NavigateToFile("/password/password_form_with_password_readonly.html");
// Fill a form and submit through a <input type="submit"> button. Nothing
// special.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptWhenEnableAutomaticPasswordSavingSwitchIsNotSet) {
NavigateToFile("/password/password_form.html");
// Fill a form and submit through a <input type="submit"> button.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Test fix for crbug.com/368690.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptWhenReloading) {
NavigateToFile("/password/password_form.html");
std::string fill =
"document.getElementById('username_redirect').value = 'temp';"
"document.getElementById('password_redirect').value = 'random';";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill));
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
GURL url = embedded_test_server()->GetURL("/password/password_form.html");
NavigateParams params(browser(), url, ::ui::PAGE_TRANSITION_RELOAD);
ui_test_utils::NavigateToURL(&params);
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// Test that if a form gets dynamically added between the form parsing and
// rendering, and while the main frame still loads, it still is registered, and
// thus saving passwords from it works.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
FormsAddedBetweenParsingAndRendering) {
NavigateToFile("/password/between_parsing_and_rendering.html");
NavigationObserver observer(WebContents());
std::string submit =
"document.getElementById('username').value = 'temp';"
"document.getElementById('password').value = 'random';"
"document.getElementById('submit-button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), submit));
observer.Wait();
EXPECT_TRUE(BubbleObserver(WebContents()).IsSavePromptShownAutomatically());
}
// Test that if a hidden form gets dynamically added between the form parsing
// and rendering, it still is registered, and autofilling works.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
HiddenFormAddedBetweenParsingAndRendering) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("admin");
signin_form.password_value = base::ASCIIToUTF16("12345");
password_store->AddLogin(signin_form);
NavigateToFile("/password/between_parsing_and_rendering.html?hidden");
std::string show_form =
"document.getElementsByTagName('form')[0].style.display = 'block'";
ASSERT_TRUE(content::ExecuteScript(WebContents(), show_form));
// Wait until the username is filled, to make sure autofill kicked in.
WaitForElementValue("username", "admin");
WaitForElementValue("password", "12345");
}
// https://crbug.com/713645
// Navigate to a page that can't load some of the subresources. Create a hidden
// form when the body is loaded. Make the form visible. Chrome should autofill
// the form.
// The fact that the form is hidden isn't super important but reproduces the
// actual bug.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, SlowPageFill) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("admin");
signin_form.password_value = base::ASCIIToUTF16("12345");
password_store->AddLogin(signin_form);
GURL url =
embedded_test_server()->GetURL("/password/infinite_password_form.html");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::CURRENT_TAB,
ui_test_utils::BROWSER_TEST_NONE);
// Wait for autofill.
BubbleObserver bubble_observer(WebContents());
bubble_observer.WaitForManagementState();
// Show the form and make sure that the password was autofilled.
std::string show_form =
"document.getElementsByTagName('form')[0].style.display = 'block'";
ASSERT_TRUE(content::ExecuteScript(WebContents(), show_form));
CheckElementValue("username", "admin");
CheckElementValue("password", "12345");
}
// Test that if there was no previous page load then the PasswordManagerDriver
// does not think that there were SSL errors on the current page. The test opens
// a new tab with a URL for which the embedded test server issues a basic auth
// challenge.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoLastLoadGoodLastLoad) {
// We must use a new test server here because embedded_test_server() is
// already started at this point and adding the request handler to it would
// not be thread safe.
net::EmbeddedTestServer http_test_server;
// Teach the embedded server to handle requests by issuing the basic auth
// challenge.
http_test_server.RegisterRequestHandler(base::Bind(&HandleTestAuthRequest));
ASSERT_TRUE(http_test_server.Start());
LoginPromptBrowserTestObserver login_observer;
// We need to register to all sources, because the navigation observer we are
// interested in is for a new tab to be opened, and thus does not exist yet.
login_observer.Register(content::NotificationService::AllSources());
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
ASSERT_TRUE(password_store->IsEmpty());
content::NavigationController* nav_controller =
&WebContents()->GetController();
WindowedAuthNeededObserver auth_needed_observer(nav_controller);
// Navigate to a page requiring HTTP auth.
ui_test_utils::NavigateToURL(browser(),
http_test_server.GetURL("/basic_auth"));
auth_needed_observer.Wait();
WindowedAuthSuppliedObserver auth_supplied_observer(nav_controller);
// Offer valid credentials on the auth challenge.
ASSERT_EQ(1u, login_observer.handlers().size());
LoginHandler* handler = *login_observer.handlers().begin();
ASSERT_TRUE(handler);
NavigationObserver nav_observer(WebContents());
// Any username/password will work.
handler->SetAuth(base::UTF8ToUTF16("user"), base::UTF8ToUTF16("pwd"));
auth_supplied_observer.Wait();
// The password manager should be working correctly.
nav_observer.Wait();
WaitForPasswordStore();
BubbleObserver bubble_observer(WebContents());
EXPECT_TRUE(bubble_observer.IsSavePromptShownAutomatically());
bubble_observer.AcceptSavePrompt();
// Spin the message loop to make sure the password store had a chance to save
// the password.
WaitForPasswordStore();
EXPECT_FALSE(password_store->IsEmpty());
}
// Fill out a form and click a button. The Javascript removes the form, creates
// a similar one with another action, fills it out and submits. Chrome can
// manage to detect the new one and create a complete matching
// PasswordFormManager. Otherwise, the all-but-action matching PFM should be
// used. Regardless of the internals the user sees the bubble in 100% cases.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PreferPasswordFormManagerWhichFinishedMatching) {
NavigateToFile("/password/create_form_copy_on_submit.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string submit =
"document.getElementById('username').value = 'overwrite_me';"
"document.getElementById('password').value = 'random';"
"document.getElementById('non-form-button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), submit));
observer.Wait();
WaitForPasswordStore();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Test that if login fails and content server pushes a different login form
// with action URL having different schemes. Heuristic shall be able
// identify such cases and *shall not* prompt to save incorrect password.
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForLoginFailedAndServerPushSeperateLoginForm_HttpToHttps) {
std::string path =
"/password/separate_login_form_with_onload_submit_script.html";
GURL http_url(embedded_test_server()->GetURL(path));
ASSERT_TRUE(http_url.SchemeIs(url::kHttpScheme));
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
ui_test_utils::NavigateToURL(browser(), http_url);
observer.SetPathToWaitFor("/password/done_and_separate_login_form.html");
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForLoginFailedAndServerPushSeperateLoginForm_HttpsToHttp) {
// This test case cannot inject the scripts via content::ExecuteScript() in
// files served through HTTPS. Therefore the scripts are made part of the HTML
// site and executed on load.
std::string path =
"/password/separate_login_form_with_onload_submit_script.html";
GURL https_url(https_test_server().GetURL(path));
ASSERT_TRUE(https_url.SchemeIs(url::kHttpsScheme));
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
ui_test_utils::NavigateToURL(browser(), https_url);
observer.SetPathToWaitFor("/password/done_and_separate_login_form.html");
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// Tests whether a attempted submission of a malicious credentials gets blocked.
// This simulates a case which is described in http://crbug.com/571580.
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
NoPromptForSeperateLoginFormWhenSwitchingFromHttpsToHttp) {
std::string path = "/password/password_form.html";
GURL https_url(https_test_server().GetURL(path));
ASSERT_TRUE(https_url.SchemeIs(url::kHttpsScheme));
NavigationObserver form_observer(WebContents());
ui_test_utils::NavigateToURL(browser(), https_url);
form_observer.Wait();
std::string fill_and_submit_redirect =
"document.getElementById('username_redirect').value = 'user';"
"document.getElementById('password_redirect').value = 'password';"
"document.getElementById('submit_redirect').click()";
ASSERT_TRUE(
content::ExecuteScript(WebContents(), fill_and_submit_redirect));
NavigationObserver redirect_observer(WebContents());
redirect_observer.SetPathToWaitFor("/password/redirect.html");
redirect_observer.Wait();
WaitForPasswordStore();
BubbleObserver prompt_observer(WebContents());
EXPECT_TRUE(prompt_observer.IsSavePromptShownAutomatically());
// Normally the redirect happens to done.html. Here an attack is simulated
// that hijacks the redirect to a attacker controlled page.
GURL http_url(
embedded_test_server()->GetURL("/password/simple_password.html"));
std::string attacker_redirect =
"window.location.href = '" + http_url.spec() + "';";
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
attacker_redirect));
NavigationObserver attacker_observer(WebContents());
attacker_observer.SetPathToWaitFor("/password/simple_password.html");
attacker_observer.Wait();
EXPECT_TRUE(prompt_observer.IsSavePromptShownAutomatically());
std::string fill_and_submit_attacker_form =
"document.getElementById('username_field').value = 'attacker_username';"
"document.getElementById('password_field').value = 'attacker_password';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(
content::ExecuteScript(WebContents(), fill_and_submit_attacker_form));
NavigationObserver done_observer(WebContents());
done_observer.SetPathToWaitFor("/password/done.html");
done_observer.Wait();
EXPECT_TRUE(prompt_observer.IsSavePromptShownAutomatically());
prompt_observer.AcceptSavePrompt();
// Wait for password store and check that credentials are stored.
WaitForPasswordStore();
CheckThatCredentialsStored("user", "password");
// Password store clearing is required because there are 2 test iterations.
// TODO(https://crbug.com/831123): Remove store clearing when the old parser
// is gone and there is only one iteration in this test.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
password_store->Clear();
}
// Tests that after HTTP -> HTTPS migration the credential is autofilled.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
HttpMigratedCredentialAutofilled) {
// Add an http credential to the password store.
GURL https_origin = https_test_server().base_url();
ASSERT_TRUE(https_origin.SchemeIs(url::kHttpsScheme));
GURL::Replacements rep;
rep.SetSchemeStr(url::kHttpScheme);
GURL http_origin = https_origin.ReplaceComponents(rep);
autofill::PasswordForm http_form;
http_form.signon_realm = http_origin.spec();
http_form.origin = http_origin;
// Assume that the previous action was already HTTPS one matching the current
// page.
http_form.action = https_origin;
http_form.username_value = base::ASCIIToUTF16("user");
http_form.password_value = base::ASCIIToUTF16("12345");
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
password_store->AddLogin(http_form);
NavigationObserver form_observer(WebContents());
ui_test_utils::NavigateToURL(
browser(), https_test_server().GetURL("/password/password_form.html"));
form_observer.Wait();
WaitForPasswordStore();
// Let the user interact with the page, so that DOM gets modification events,
// needed for autofilling fields.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
WaitForElementValue("username_field", "user");
WaitForElementValue("password_field", "12345");
}
// Tests that obsolete HTTP credentials are moved when a site migrated to HTTPS
// and has HSTS enabled.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
ObsoleteHttpCredentialMovedOnMigrationToHstsSite) {
// Add an http credential to the password store.
GURL https_origin = https_test_server().base_url();
ASSERT_TRUE(https_origin.SchemeIs(url::kHttpsScheme));
GURL::Replacements rep;
rep.SetSchemeStr(url::kHttpScheme);
GURL http_origin = https_origin.ReplaceComponents(rep);
autofill::PasswordForm http_form;
http_form.signon_realm = http_origin.spec();
http_form.origin = http_origin;
http_form.username_value = base::ASCIIToUTF16("user");
http_form.password_value = base::ASCIIToUTF16("12345");
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
password_store->AddLogin(http_form);
// Treat the host of the HTTPS test server as HSTS.
AddHSTSHost(https_test_server().host_port_pair().host());
// Navigate to HTTPS page and trigger the migration.
NavigationObserver form_observer(WebContents());
ui_test_utils::NavigateToURL(
browser(), https_test_server().GetURL("/password/password_form.html"));
form_observer.Wait();
// Issue the query for HTTPS credentials.
WaitForPasswordStore();
// Realize there are no HTTPS credentials and issue the query for HTTP
// credentials instead.
WaitForPasswordStore();
// Sync with IO thread before continuing. This is necessary, because the
// credential migration triggers a query for the HSTS state which gets
// executed on the IO thread. The actual task is empty, because only the reply
// is relevant. By the time the reply is executed it is guaranteed that the
// migration is completed.
base::RunLoop run_loop;
base::PostTaskAndReply(FROM_HERE, {content::BrowserThread::IO},
base::BindOnce([]() {}), run_loop.QuitClosure());
run_loop.Run();
// Migration updates should touch the password store.
WaitForPasswordStore();
// Only HTTPS passwords should be present.
EXPECT_TRUE(
password_store->stored_passwords().at(http_origin.spec()).empty());
EXPECT_FALSE(
password_store->stored_passwords().at(https_origin.spec()).empty());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptWhenPasswordFormWithoutUsernameFieldSubmitted) {
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
EXPECT_TRUE(password_store->IsEmpty());
NavigateToFile("/password/form_with_only_password_field.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string submit =
"document.getElementById('password').value = 'password';"
"document.getElementById('submit-button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
prompt_observer->AcceptSavePrompt();
WaitForPasswordStore();
EXPECT_FALSE(password_store->IsEmpty());
}
// Test that if a form gets autofilled, then it gets autofilled on re-creation
// as well.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, ReCreatedFormsGetFilled) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("random");
password_store->AddLogin(signin_form);
NavigateToFile("/password/dynamic_password_form.html");
const std::string create_form =
"document.getElementById('create_form_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), create_form));
// Wait until the username is filled, to make sure autofill kicked in.
WaitForElementValue("username_id", "temp");
// Now the form gets deleted and created again. It should get autofilled
// again.
const std::string delete_form =
"var form = document.getElementById('dynamic_form_id');"
"form.parentNode.removeChild(form);";
ASSERT_TRUE(content::ExecuteScript(WebContents(), delete_form));
ASSERT_TRUE(content::ExecuteScript(WebContents(), create_form));
WaitForElementValue("username_id", "temp");
}
// Test that if the same dynamic form is created multiple times then all of them
// are autofilled.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, DuplicateFormsGetFilled) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("random");
password_store->AddLogin(signin_form);
NavigateToFile("/password/recurring_dynamic_form.html");
ASSERT_TRUE(content::ExecuteScript(WebContents(), "addForm();"));
// Wait until the username is filled, to make sure autofill kicked in.
WaitForJsElementValue("document.body.children[0].children[0]", "temp");
WaitForJsElementValue("document.body.children[0].children[1]", "random");
// Add one more form.
ASSERT_TRUE(content::ExecuteScript(WebContents(), "addForm();"));
// Wait until the username is filled, to make sure autofill kicked in.
WaitForJsElementValue("document.body.children[1].children[0]", "temp");
WaitForJsElementValue("document.body.children[1].children[1]", "random");
}
// Test that an autofilled credential is deleted then the password manager
// doesn't try to resurrect it on navigation.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
DeletedPasswordIsNotRevived) {
// At first let us save a credential to the password store.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.origin = embedded_test_server()->base_url();
signin_form.action = embedded_test_server()->base_url();
signin_form.username_value = base::ASCIIToUTF16("admin");
signin_form.password_value = base::ASCIIToUTF16("1234");
password_store->AddLogin(signin_form);
NavigateToFile("/password/password_form.html");
// Let the user interact with the page.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
// Wait until that interaction causes the username and the password value to
// be revealed.
WaitForElementValue("username_field", "admin");
// Now the credential is removed via the settings or the bubble.
password_store->RemoveLogin(signin_form);
WaitForPasswordStore();
// Submit the form. It shouldn't revive the credential in the store.
NavigationObserver observer(WebContents());
ASSERT_TRUE(content::ExecuteScript(
WebContents(), "document.getElementById('input_submit_button').click()"));
observer.Wait();
WaitForPasswordStore();
EXPECT_TRUE(password_store->IsEmpty());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForPushStateWhenFormDisappears) {
NavigateToFile("/password/password_push_state.html");
// Verify that we show the save password prompt if 'history.pushState()'
// is called after form submission is suppressed by, for example, calling
// preventDefault() in a form's submit event handler.
// Note that calling 'submit()' on a form with javascript doesn't call
// the onsubmit handler, so we click the submit button instead.
// Also note that the prompt will only show up if the form disappers
// after submission
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Similar to the case above, but this time the form persists after
// 'history.pushState()'. And save password prompt should not show up
// in this case.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
NoPromptForPushStateWhenFormPersists) {
NavigateToFile("/password/password_push_state.html");
// Set |should_delete_testform| to false to keep submitted form visible after
// history.pushsTate();
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"should_delete_testform = false;"
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'random';"
"document.getElementById('submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// The password manager should distinguish forms with empty actions. After
// successful login, the login form disappears, but the another one shouldn't be
// recognized as the login form. The save prompt should appear.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForPushStateWhenFormWithEmptyActionDisappears) {
NavigateToFile("/password/password_push_state.html");
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('ea_username_field').value = 'temp';"
"document.getElementById('ea_password_field').value = 'random';"
"document.getElementById('ea_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Similar to the case above, but this time the form persists after
// 'history.pushState()'. The password manager should find the login form even
// if the action of the form is empty. Save password prompt should not show up.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForPushStateWhenFormWithEmptyActionPersists) {
NavigateToFile("/password/password_push_state.html");
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"should_delete_testform = false;"
"document.getElementById('ea_username_field').value = 'temp';"
"document.getElementById('ea_password_field').value = 'random';"
"document.getElementById('ea_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
// Current and target URLs contain different parameters and references. This
// test checks that parameters and references in origins are ignored for
// form origin comparison.
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
PromptForPushStateWhenFormDisappears_ParametersInOrigins) {
NavigateToFile("/password/password_push_state.html?login#r");
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"add_parameters_to_target_url = true;"
"document.getElementById('pa_username_field').value = 'temp';"
"document.getElementById('pa_password_field').value = 'random';"
"document.getElementById('pa_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Similar to the case above, but this time the form persists after
// 'history.pushState()'. The password manager should find the login form even
// if target and current URLs contain different parameters or references.
// Save password prompt should not show up.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PromptForPushStateWhenFormPersists_ParametersInOrigins) {
NavigateToFile("/password/password_push_state.html?login#r");
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"should_delete_testform = false;"
"add_parameters_to_target_url = true;"
"document.getElementById('pa_username_field').value = 'temp';"
"document.getElementById('pa_password_field').value = 'random';"
"document.getElementById('pa_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_FALSE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
InFrameNavigationDoesNotClearPopupState) {
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("random123");
password_store->AddLogin(signin_form);
NavigateToFile("/password/password_form.html");
// Mock out the AutofillClient so we know how long to wait. Unfortunately
// there isn't otherwise a good event to wait on to verify that the popup
// would have been shown.
password_manager::ContentPasswordManagerDriverFactory* driver_factory =
password_manager::ContentPasswordManagerDriverFactory::FromWebContents(
WebContents());
ObservingAutofillClient::CreateForWebContents(WebContents());
ObservingAutofillClient* observing_autofill_client =
ObservingAutofillClient::FromWebContents(WebContents());
password_manager::ContentPasswordManagerDriver* driver =
driver_factory->GetDriverForFrame(WebContents()->GetMainFrame());
DCHECK(driver);
driver->GetPasswordAutofillManager()->set_autofill_client(
observing_autofill_client);
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(),
"var usernameRect = document.getElementById('username_field')"
".getBoundingClientRect();"));
// Trigger in page navigation.
std::string in_page_navigate = "location.hash = '#blah';";
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
in_page_navigate));
// Click on the username field to display the popup.
int top;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send(usernameRect.top);", &top));
int left;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send(usernameRect.left);", &left));
content::SimulateMouseClickAt(WebContents(), 0,
blink::WebMouseEvent::Button::kLeft,
gfx::Point(left + 1, top + 1));
// Make sure the popup would be shown.
observing_autofill_client->WaitForAutofillPopup();
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, ChangePwdFormBubbleShown) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('chg_username_field').value = 'temp';"
"document.getElementById('chg_password_field').value = 'random';"
"document.getElementById('chg_new_password_1').value = 'random1';"
"document.getElementById('chg_new_password_2').value = 'random1';"
"document.getElementById('chg_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
ChangePwdFormPushStateBubbleShown) {
NavigateToFile("/password/password_push_state.html");
NavigationObserver observer(WebContents());
observer.set_quit_on_entry_committed(true);
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('chg_username_field').value = 'temp';"
"document.getElementById('chg_password_field').value = 'random';"
"document.getElementById('chg_new_password_1').value = 'random1';"
"document.getElementById('chg_new_password_2').value = 'random1';"
"document.getElementById('chg_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, NoPromptOnBack) {
// Go to a successful landing page through submitting first, so that it is
// reachable through going back, and the remembered page transition is form
// submit. There is no need to submit non-empty strings.
NavigateToFile("/password/password_form.html");
NavigationObserver dummy_submit_observer(WebContents());
std::string just_submit =
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), just_submit));
dummy_submit_observer.Wait();
// Now go to a page with a form again, fill the form, and go back instead of
// submitting it.
NavigateToFile("/password/dummy_submit.html");
NavigationObserver observer(WebContents());
BubbleObserver prompt_observer(WebContents());
// The (dummy) submit is necessary to provisionally save the typed password.
// A user typing in the password field would not need to submit to
// provisionally save it, but the script cannot trigger that just by
// assigning to the field's value.
std::string fill_and_back =
"document.getElementById('password_field').value = 'random';"
"document.getElementById('input_submit_button').click();"
"window.history.back();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_back));
observer.Wait();
EXPECT_FALSE(prompt_observer.IsSavePromptShownAutomatically());
}
// Regression test for http://crbug.com/452306
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
ChangingTextToPasswordFieldOnSignupForm) {
NavigateToFile("/password/signup_form.html");
// In this case, pretend that username_field is actually a password field
// that starts as a text field to simulate placeholder.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string change_and_submit =
"document.getElementById('other_info').value = 'username';"
"document.getElementById('username_field').type = 'password';"
"document.getElementById('username_field').value = 'mypass';"
"document.getElementById('password_field').value = 'mypass';"
"document.getElementById('testform').submit();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), change_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Regression test for http://crbug.com/451631
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
SavingOnManyPasswordFieldsTest) {
// Simulate Macy's registration page, which contains the normal 2 password
// fields for confirming the new password plus 2 more fields for security
// questions and credit card. Make sure that saving works correctly for such
// sites.
NavigateToFile("/password/many_password_signup_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'username';"
"document.getElementById('password_field').value = 'mypass';"
"document.getElementById('confirm_field').value = 'mypass';"
"document.getElementById('security_answer').value = 'hometown';"
"document.getElementById('SSN').value = '1234';"
"document.getElementById('testform').submit();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
SaveWhenIFrameDestroyedOnFormSubmit) {
NavigateToFile("/password/frame_detached_on_submit.html");
// Need to pay attention for a message that XHR has finished since there
// is no navigation to wait for.
content::DOMMessageQueue message_queue;
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"var iframe = document.getElementById('login_iframe');"
"var frame_doc = iframe.contentDocument;"
"frame_doc.getElementById('username_field').value = 'temp';"
"frame_doc.getElementById('password_field').value = 'random';"
"frame_doc.getElementById('submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
std::string message;
while (message_queue.WaitForMessage(&message)) {
if (message == "\"SUBMISSION_FINISHED\"")
break;
}
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
}
// Check that a username and password are filled into forms in iframes
// that don't share the security origin with the main frame, but have PSL
// matched origins.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PSLMatchedCrossSiteFillTest) {
GURL main_frame_url = embedded_test_server()->GetURL(
"www.foo.com", "/password/password_form_in_crosssite_iframe.html");
NavigationObserver observer(WebContents());
ui_test_utils::NavigateToURL(browser(), main_frame_url);
observer.Wait();
// Create an iframe and navigate cross-site.
NavigationObserver iframe_observer(WebContents());
iframe_observer.SetPathToWaitFor("/password/crossite_iframe_content.html");
GURL iframe_url = embedded_test_server()->GetURL(
"abc.foo.com", "/password/crossite_iframe_content.html");
std::string create_iframe =
base::StringPrintf("create_iframe('%s');", iframe_url.spec().c_str());
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer.Wait();
// Store a password for autofill later.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = iframe_url.GetOrigin().spec();
signin_form.origin = iframe_url;
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("pa55w0rd");
password_store->AddLogin(signin_form);
WaitForPasswordStore();
// Visit the form again.
NavigationObserver reload_observer(WebContents());
ui_test_utils::NavigateToURL(browser(), main_frame_url);
reload_observer.Wait();
NavigationObserver iframe_observer_2(WebContents());
iframe_observer_2.SetPathToWaitFor("/password/crossite_iframe_content.html");
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer_2.Wait();
// Simulate the user interaction in the iframe which should trigger autofill.
// Click in the middle of the frame to avoid the border.
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(),
"var iframeRect = document.getElementById("
"'iframe').getBoundingClientRect();"));
int y;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.top +"
"iframeRect.bottom) / 2);",
&y));
int x;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.left + iframeRect.right)"
"/ 2);",
&x));
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(x, y));
std::string username_field;
std::string password_field;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractString(
RenderFrameHost(), "sendMessage('get_username');", &username_field));
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractString(
RenderFrameHost(), "sendMessage('get_password');", &password_field));
// Verify username and password have only been autofilled if FOAS on HTTP is
// not active.
if (base::FeatureList::IsEnabled(
password_manager::features::kFillOnAccountSelectHttp)) {
EXPECT_TRUE(username_field.empty());
EXPECT_TRUE(password_field.empty());
} else {
EXPECT_EQ("temp", username_field);
EXPECT_EQ("pa55w0rd", password_field);
}
}
// Check that a username and password are not filled in forms in iframes
// that don't have PSL matched origins.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PSLUnMatchedCrossSiteFillTest) {
GURL main_frame_url = embedded_test_server()->GetURL(
"www.foo.com", "/password/password_form_in_crosssite_iframe.html");
NavigationObserver observer(WebContents());
ui_test_utils::NavigateToURL(browser(), main_frame_url);
observer.Wait();
// Create an iframe and navigate cross-site.
NavigationObserver iframe_observer(WebContents());
iframe_observer.SetPathToWaitFor("/password/crossite_iframe_content.html");
GURL iframe_url = embedded_test_server()->GetURL(
"www.bar.com", "/password/crossite_iframe_content.html");
std::string create_iframe =
base::StringPrintf("create_iframe('%s');", iframe_url.spec().c_str());
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer.Wait();
// Store a password for autofill later.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = iframe_url.GetOrigin().spec();
signin_form.origin = iframe_url;
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("pa55w0rd");
password_store->AddLogin(signin_form);
WaitForPasswordStore();
// Visit the form again.
NavigationObserver reload_observer(WebContents());
ui_test_utils::NavigateToURL(browser(), main_frame_url);
reload_observer.Wait();
NavigationObserver iframe_observer_2(WebContents());
iframe_observer_2.SetPathToWaitFor("/password/crossite_iframe_content.html");
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer_2.Wait();
// Simulate the user interaction in the iframe which should trigger autofill.
// Click in the middle of the frame to avoid the border.
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(),
"var iframeRect = document.getElementById("
"'iframe').getBoundingClientRect();"));
int y;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.top +"
"iframeRect.bottom) / 2);",
&y));
int x;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.left + iframeRect.right)"
"/ 2);",
&x));
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(x, y));
// Verify username is not autofilled
std::string empty_username;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractString(
RenderFrameHost(), "sendMessage('get_username');", &empty_username));
EXPECT_EQ("", empty_username);
// Verify password is not autofilled
std::string empty_password;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractString(
RenderFrameHost(), "sendMessage('get_password');", &empty_password));
EXPECT_EQ("", empty_password);
}
// Check that a password form in an iframe of same origin will not be
// filled in until user interact with the iframe.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
SameOriginIframeAutoFillTest) {
// Visit the sign-up form to store a password for autofill later
NavigateToFile("/password/password_form_in_same_origin_iframe.html");
NavigationObserver observer(WebContents());
observer.SetPathToWaitFor("/password/done.html");
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string submit =
"var ifrmDoc = document.getElementById('iframe').contentDocument;"
"ifrmDoc.getElementById('username_field').value = 'temp';"
"ifrmDoc.getElementById('password_field').value = 'pa55w0rd';"
"ifrmDoc.getElementById('input_submit_button').click();";
ASSERT_TRUE(content::ExecuteScript(WebContents(), submit));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
prompt_observer->AcceptSavePrompt();
// Visit the form again
NavigationObserver reload_observer(WebContents());
NavigateToFile("/password/password_form_in_same_origin_iframe.html");
reload_observer.Wait();
// Verify password and username are not accessible yet.
CheckElementValue("iframe", "username_field", "");
CheckElementValue("iframe", "password_field", "");
// Simulate the user interaction in the iframe which should trigger autofill.
// Click in the middle of the frame to avoid the border.
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(
RenderFrameHost(),
"var iframeRect = document.getElementById("
"'iframe').getBoundingClientRect();"));
int y;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.top +"
"iframeRect.bottom) / 2);",
&y));
int x;
ASSERT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractInt(
RenderFrameHost(),
"window.domAutomationController.send((iframeRect.left + iframeRect.right)"
"/ 2);",
&x));
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(x, y));
// Verify username and password have been autofilled
WaitForElementValue("iframe", "username_field", "temp");
WaitForElementValue("iframe", "password_field", "pa55w0rd");
}
// The password manager driver will kill processes when they try to access
// passwords of sites other than the site the process is dedicated to, under
// site isolation.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
CrossSitePasswordEnforcement) {
// The code under test is only active under site isolation.
if (!content::AreAllSitesIsolatedForTesting()) {
return;
}
// Navigate the main frame.
GURL main_frame_url = embedded_test_server()->GetURL(
"/password/password_form_in_crosssite_iframe.html");
NavigationObserver observer(WebContents());
ui_test_utils::NavigateToURL(browser(), main_frame_url);
observer.Wait();
// Create an iframe and navigate cross-site.
NavigationObserver iframe_observer(WebContents());
iframe_observer.SetPathToWaitFor("/password/crossite_iframe_content.html");
GURL iframe_url = embedded_test_server()->GetURL(
"foo.com", "/password/crossite_iframe_content.html");
std::string create_iframe =
base::StringPrintf("create_iframe('%s');", iframe_url.spec().c_str());
ASSERT_TRUE(content::ExecuteScriptWithoutUserGesture(RenderFrameHost(),
create_iframe));
iframe_observer.Wait();
// The iframe should get its own process.
content::RenderFrameHost* main_frame = WebContents()->GetMainFrame();
content::RenderFrameHost* iframe = iframe_observer.render_frame_host();
content::SiteInstance* main_site_instance = main_frame->GetSiteInstance();
content::SiteInstance* iframe_site_instance = iframe->GetSiteInstance();
EXPECT_NE(main_site_instance, iframe_site_instance);
EXPECT_NE(main_frame->GetProcess(), iframe->GetProcess());
content::RenderProcessHostWatcher iframe_killed(
iframe->GetProcess(),
content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
// Try to get cross-site passwords from the subframe's process and wait for it
// to be killed.
std::vector<autofill::PasswordForm> password_forms;
password_forms.push_back(autofill::PasswordForm());
password_forms.back().origin = main_frame_url;
ContentPasswordManagerDriverFactory* factory =
ContentPasswordManagerDriverFactory::FromWebContents(WebContents());
EXPECT_TRUE(factory);
autofill::mojom::PasswordManagerDriver* driver =
factory->GetDriverForFrame(iframe);
EXPECT_TRUE(driver);
driver->PasswordFormsParsed(password_forms);
iframe_killed.Wait();
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, ChangePwdNoAccountStored) {
NavigateToFile("/password/password_form.html");
// Fill a form and submit through a <input type="submit"> button.
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('chg_password_wo_username_field').value = "
"'old_pw';"
"document.getElementById('chg_new_password_wo_username_1').value = "
"'new_pw';"
"document.getElementById('chg_new_password_wo_username_2').value = "
"'new_pw';"
"document.getElementById('chg_submit_wo_username_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
// No credentials stored before, so save bubble is shown.
EXPECT_TRUE(prompt_observer->IsSavePromptShownAutomatically());
prompt_observer->AcceptSavePrompt();
// Check that credentials are stored.
WaitForPasswordStore();
CheckThatCredentialsStored("", "new_pw");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest, ChangePwd1AccountStored) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.password_value = base::ASCIIToUTF16("pw");
signin_form.username_value = base::ASCIIToUTF16("temp");
password_store->AddLogin(signin_form);
// Check that password update bubble is shown.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit_change_password =
"document.getElementById('chg_password_wo_username_field').value = "
"'random';"
"document.getElementById('chg_new_password_wo_username_1').value = "
"'new_pw';"
"document.getElementById('chg_new_password_wo_username_2').value = "
"'new_pw';"
"document.getElementById('chg_submit_wo_username_button').click()";
ASSERT_TRUE(
content::ExecuteScript(WebContents(), fill_and_submit_change_password));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsUpdatePromptShownAutomatically());
// We emulate that the user clicks "Update" button.
const autofill::PasswordForm& pending_credentials =
ManagePasswordsUIController::FromWebContents(WebContents())
->GetPendingPassword();
prompt_observer->AcceptUpdatePrompt(pending_credentials);
WaitForPasswordStore();
CheckThatCredentialsStored("temp", "new_pw");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PasswordOverridenUpdateBubbleShown) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("pw");
password_store->AddLogin(signin_form);
// Disable autofill. If a password is autofilled then all the Javacript
// changes are discarded. The test would not be able to feed the new password
// below.
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeature(features::kFillOnAccountSelect);
// Check that password update bubble is shown.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'new_pw';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
// The stored password "pw" was overriden with "new_pw", so update prompt is
// expected.
EXPECT_TRUE(prompt_observer->IsUpdatePromptShownAutomatically());
const autofill::PasswordForm stored_form =
password_store->stored_passwords().begin()->second[0];
prompt_observer->AcceptUpdatePrompt(stored_form);
WaitForPasswordStore();
CheckThatCredentialsStored("temp", "new_pw");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
PasswordNotOverridenUpdateBubbleNotShown) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.username_value = base::ASCIIToUTF16("temp");
signin_form.password_value = base::ASCIIToUTF16("pw");
password_store->AddLogin(signin_form);
// Check that password update bubble is shown.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit =
"document.getElementById('username_field').value = 'temp';"
"document.getElementById('password_field').value = 'pw';"
"document.getElementById('input_submit_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
// The stored password "pw" was not overriden, so update prompt is not
// expected.
EXPECT_FALSE(prompt_observer->IsUpdatePromptShownAutomatically());
CheckThatCredentialsStored("temp", "pw");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
MultiplePasswordsWithPasswordSelectionEnabled) {
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
// It is important that these 3 passwords are different. Because if two of
// them are the same, it is going to be treated as a password update and the
// dropdown will not be shown.
std::string fill_and_submit =
"document.getElementById('chg_password_wo_username_field').value = "
"'pass1';"
"document.getElementById('chg_new_password_wo_username_1').value = "
"'pass2';"
"document.getElementById('chg_new_password_wo_username_2').value = "
"'pass3';"
"document.getElementById('chg_submit_wo_username_button').click()";
ASSERT_TRUE(content::ExecuteScript(WebContents(), fill_and_submit));
observer.Wait();
// 3 possible passwords are going to be shown in a dropdown when the password
// selection feature is enabled. The first one will be selected as the main
// password by default. All three will be in the all_possible_passwords
// list. The save password prompt is expected.
BubbleObserver bubble_observer(WebContents());
EXPECT_TRUE(bubble_observer.IsSavePromptShownAutomatically());
EXPECT_EQ(base::ASCIIToUTF16("pass1"),
ManagePasswordsUIController::FromWebContents(WebContents())
->GetPendingPassword()
.password_value);
EXPECT_THAT(
ManagePasswordsUIController::FromWebContents(WebContents())
->GetPendingPassword()
.all_possible_passwords,
ElementsAre(autofill::ValueElementPair(
base::ASCIIToUTF16("pass1"),
base::ASCIIToUTF16("chg_password_wo_username_field")),
autofill::ValueElementPair(
base::ASCIIToUTF16("pass2"),
base::ASCIIToUTF16("chg_new_password_wo_username_1")),
autofill::ValueElementPair(
base::ASCIIToUTF16("pass3"),
base::ASCIIToUTF16("chg_new_password_wo_username_2"))));
bubble_observer.AcceptSavePrompt();
WaitForPasswordStore();
CheckThatCredentialsStored("", "pass1");
}
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
ChangePwdWhenTheFormContainNotUsernameTextfield) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::TestPasswordStore> password_store =
static_cast<password_manager::TestPasswordStore*>(
PasswordStoreFactory::GetForProfile(
browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS)
.get());
autofill::PasswordForm signin_form;
signin_form.signon_realm = embedded_test_server()->base_url().spec();
signin_form.password_value = base::ASCIIToUTF16("pw");
signin_form.username_value = base::ASCIIToUTF16("temp");
password_store->AddLogin(signin_form);
// Check that password update bubble is shown.
NavigateToFile("/password/password_form.html");
NavigationObserver observer(WebContents());
std::unique_ptr<BubbleObserver> prompt_observer(
new BubbleObserver(WebContents()));
std::string fill_and_submit_change_password =
"document.getElementById('chg_text_field').value = '3';"
"document.getElementById('chg_password_withtext_field').value"
" = 'random';"
"document.getElementById('chg_new_password_withtext_username_1').value"
" = 'new_pw';"
"document.getElementById('chg_new_password_withtext_username_2').value"
" = 'new_pw';"
"document.getElementById('chg_submit_withtext_button').click()";
ASSERT_TRUE(
content::ExecuteScript(WebContents(), fill_and_submit_change_password));
observer.Wait();
EXPECT_TRUE(prompt_observer->IsUpdatePromptShownAutomatically());
const autofill::PasswordForm stored_form =
password_store->stored_passwords().begin()->second[0];
prompt_observer->AcceptUpdatePrompt(stored_form);
WaitForPasswordStore();
CheckThatCredentialsStored("temp", "new_pw");
}
// Test whether the password form with the username and password fields having
// ambiguity in id attribute gets autofilled correctly.
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
AutofillSuggestionsForPasswordFormWithAmbiguousIdAttribute) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::PasswordStore> password_store =
PasswordStoreFactory::GetForProfile(browser()->profile(),
ServiceAccessType::IMPLICIT_ACCESS);
autofill::PasswordForm login_form;
login_form.signon_realm = embedded_test_server()->base_url().spec();
login_form.action = embedded_test_server()->GetURL("/password/done.html");
login_form.username_value = base::ASCIIToUTF16("myusername");
login_form.password_value = base::ASCIIToUTF16("mypassword");
password_store->AddLogin(login_form);
// Now, navigate to the password form having ambiguous Ids for username and
// password fields and verify whether username and password is autofilled.
NavigateToFile("/password/ambiguous_password_form.html");
// Let the user interact with the page, so that DOM gets modification events,
// needed for autofilling fields.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
WaitForElementValue("ambiguous_form", 0 /* elements_index */, "myusername");
WaitForElementValue("ambiguous_form", 1 /* elements_index */, "mypassword");
}
// Test whether the password form having username and password fields without
// name and id attribute gets autofilled correctly.
IN_PROC_BROWSER_TEST_F(
PasswordManagerBrowserTest,
AutofillSuggestionsForPasswordFormWithoutNameOrIdAttribute) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::PasswordStore> password_store =
PasswordStoreFactory::GetForProfile(browser()->profile(),
ServiceAccessType::IMPLICIT_ACCESS);
autofill::PasswordForm login_form;
login_form.signon_realm = embedded_test_server()->base_url().spec();
login_form.action = embedded_test_server()->GetURL("/password/done.html");
login_form.username_value = base::ASCIIToUTF16("myusername");
login_form.password_value = base::ASCIIToUTF16("mypassword");
password_store->AddLogin(login_form);
// Now, navigate to the password form having no Ids for username and password
// fields and verify whether username and password is autofilled.
NavigateToFile("/password/ambiguous_password_form.html");
// Let the user interact with the page, so that DOM gets modification events,
// needed for autofilling fields.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
WaitForElementValue("no_name_id_form", 0 /* elements_index */, "myusername");
WaitForElementValue("no_name_id_form", 1 /* elements_index */, "mypassword");
}
// Test whether the change password form having username and password fields
// without name and id attribute gets autofilled correctly.
IN_PROC_BROWSER_TEST_F(PasswordManagerBrowserTest,
AutofillSuggestionsForChangePwdWithEmptyNames) {
// At first let us save credentials to the PasswordManager.
scoped_refptr<password_manager::PasswordStore> password_store =
PasswordStoreFactory::GetForProfile(browser()->profile(),
ServiceAccessType::IMPLICIT_ACCESS);
autofill::PasswordForm login_form;
login_form.signon_realm = embedded_test_server()->base_url().spec();
login_form.action = embedded_test_server()->GetURL("/password/done.html");
login_form.username_value = base::ASCIIToUTF16("myusername");
login_form.password_value = base::ASCIIToUTF16("mypassword");
password_store->AddLogin(login_form);
// Now, navigate to the password form having no Ids for username and password
// fields and verify whether username and password is autofilled.
NavigateToFile("/password/ambiguous_password_form.html");
// Let the user interact with the page, so that DOM gets modification events,
// needed for autofilling fields.
content::SimulateMouseClickAt(
WebContents(), 0, blink::WebMouseEvent::Button::kLeft, gfx::Point(1, 1));
WaitForElementValue("change_pwd_but_no_autocomplete", 0 /* elements_index */,
"myusername");