1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/content/renderer/password_form_conversion_utils.h"
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/strings/stringprintf.h"
#include "content/public/test/render_view_test.h"
#include "google_apis/gaia/gaia_urls.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_form_element.h"
#include "third_party/blink/public/web/web_local_frame.h"
using blink::WebFormElement;
using blink::WebLocalFrame;
namespace autofill {
namespace {
// A builder to produce HTML code for a password form composed of the desired
// number and kinds of username and password fields.
class PasswordFormBuilder {
public:
// Creates a builder to start composing a new form. The form will have the
// specified |action| URL.
explicit PasswordFormBuilder(const char* action) {
base::StringAppendF(
&html_, "<FORM name=\"Test\" action=\"%s\" method=\"post\">", action);
}
PasswordFormBuilder(const PasswordFormBuilder&) = delete;
PasswordFormBuilder& operator=(const PasswordFormBuilder&) = delete;
// Appends a new text-type field at the end of the form, having the specified
// |name_and_id|, |value|, and |autocomplete| attributes. The |autocomplete|
// argument can take two special values, namely:
// 1.) nullptr, causing no autocomplete attribute to be added,
// 2.) "", causing an empty attribute (i.e. autocomplete="") to be added.
void AddTextField(const char* name_and_id,
const char* value,
const char* autocomplete) {
std::string autocomplete_attribute(
autocomplete ? base::StringPrintf("autocomplete=\"%s\"", autocomplete)
: "");
base::StringAppendF(
&html_, "<INPUT type=\"text\" name=\"%s\" id=\"%s\" value=\"%s\" %s/>",
name_and_id, name_and_id, value, autocomplete_attribute.c_str());
}
// Appends a new password-type field at the end of the form, having the
// specified |name_and_id|, |value|, and |autocomplete| attributes. Special
// values for |autocomplete| are the same as in AddTextField.
void AddPasswordField(const char* name_and_id,
const char* value,
const char* autocomplete) {
std::string autocomplete_attribute(
autocomplete ? base::StringPrintf("autocomplete=\"%s\"", autocomplete)
: "");
base::StringAppendF(
&html_,
"<INPUT type=\"password\" name=\"%s\" id=\"%s\" value=\"%s\" %s/>",
name_and_id, name_and_id, value, autocomplete_attribute.c_str());
}
// Appends a new hidden-type field at the end of the form, having the
// specified |name_and_id| and |value| attributes.
void AddHiddenField(const char* name_and_id, const char* value) {
base::StringAppendF(
&html_, "<INPUT type=\"hidden\" name=\"%s\" id=\"%s\" value=\"%s\" />",
name_and_id, name_and_id, value);
}
// Returns the HTML code for the form containing the fields that have been
// added so far.
std::string ProduceHTML() const { return html_ + "</FORM>"; }
private:
std::string html_;
};
class PasswordFormConversionUtilsTest : public content::RenderViewTest {
public:
PasswordFormConversionUtilsTest() = default;
PasswordFormConversionUtilsTest(const PasswordFormConversionUtilsTest&) =
delete;
PasswordFormConversionUtilsTest& operator=(
const PasswordFormConversionUtilsTest&) = delete;
~PasswordFormConversionUtilsTest() override = default;
protected:
// Loads the given |html| and retrieves the sole WebFormElement from it.
void LoadWebFormFromHTML(const std::string& html,
WebFormElement* form,
const char* origin) {
if (origin)
LoadHTMLWithUrlOverride(html.c_str(), origin);
else
LoadHTML(html.c_str());
GetFirstForm(form);
}
void TearDown() override { content::RenderViewTest::TearDown(); }
private:
void GetFirstForm(WebFormElement* form) {
WebLocalFrame* frame = GetMainFrame();
ASSERT_TRUE(frame);
std::vector<WebFormElement> forms = frame->GetDocument().GetTopLevelForms();
ASSERT_LE(1U, forms.size());
*form = forms[0];
}
};
TEST_F(PasswordFormConversionUtilsTest, IsGaiaReauthFormIgnored) {
struct TestCase {
const char* origin;
struct KeyValue {
constexpr KeyValue() : name(nullptr), value(nullptr) {}
constexpr KeyValue(const char* new_name, const char* new_value)
: name(new_name), value(new_value) {}
const char* name;
const char* value;
};
std::array<KeyValue, 2> hidden_fields;
bool expected_form_is_reauth;
};
constexpr auto cases = std::to_array<TestCase>({
// A common password form is parsed successfully.
TestCase{"https://example.com",
{TestCase::KeyValue(), TestCase::KeyValue()},
false},
// A common password form, even if it appears on a GAIA reauth url,
// is parsed successfully.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue(), TestCase::KeyValue()},
false},
// Not a transactional reauth.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue",
"https://passwords.google.com/settings"),
TestCase::KeyValue()},
false},
// A reauth form that is not for a password site is parsed successfully.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue", "https://mail.google.com"),
TestCase::KeyValue("rart", "")},
false},
// A reauth form for a password site is recognised as such.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue", "https://passwords.google.com"),
TestCase::KeyValue("rart", "")},
true},
// Path, params or fragment in "continue" should not have influence.
TestCase{
"https://accounts.google.com",
{TestCase::KeyValue(
"continue", "https://passwords.google.com/path?param=val#frag"),
TestCase::KeyValue("rart", "")},
true},
// Password site is inaccessible via HTTP, but because of HSTS the
// following link should still continue to https://passwords.google.com.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue", "http://passwords.google.com"),
TestCase::KeyValue("rart", "")},
true},
// Make sure testing sites are disabled as well.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue(
"continue",
"https://passwords-ac-testing.corp.google.com/settings"),
TestCase::KeyValue("rart", "")},
true},
// Specifying default port doesn't change anything.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue", "passwords.google.com:443"),
TestCase::KeyValue("rart", "")},
true},
// Fully qualified domain should work as well.
TestCase{"https://accounts.google.com",
{TestCase::KeyValue("continue",
"https://passwords.google.com./settings"),
TestCase::KeyValue("rart", "")},
true},
// A correctly looking form, but on a different page.
TestCase{"https://google.com",
{TestCase::KeyValue("continue", "https://passwords.google.com"),
TestCase::KeyValue("rart", "")},
false},
});
for (const TestCase& test_case : cases) {
SCOPED_TRACE(testing::Message("origin=")
<< test_case.origin
<< ", hidden_fields[0]=" << test_case.hidden_fields[0].name
<< "/" << test_case.hidden_fields[0].value
<< ", hidden_fields[1]=" << test_case.hidden_fields[1].name
<< "/" << test_case.hidden_fields[1].value
<< ", expected_form_is_reauth="
<< test_case.expected_form_is_reauth);
std::unique_ptr<PasswordFormBuilder> builder(new PasswordFormBuilder(""));
builder->AddTextField("username", "", nullptr);
builder->AddPasswordField("password", "", nullptr);
for (const TestCase::KeyValue& hidden_field : test_case.hidden_fields) {
if (hidden_field.name)
builder->AddHiddenField(hidden_field.name, hidden_field.value);
}
std::string html = builder->ProduceHTML();
WebFormElement form;
LoadWebFormFromHTML(html, &form, test_case.origin);
EXPECT_EQ(test_case.expected_form_is_reauth,
IsGaiaReauthenticationForm(form));
}
}
TEST_F(PasswordFormConversionUtilsTest, IsGaiaWithSkipSavePasswordForm) {
struct TestCase {
const char* origin;
bool expected_form_has_skip_save_password;
};
const auto cases = std::to_array<TestCase>({
// A common password form is parsed successfully.
TestCase{"https://example.com", false},
// A common GAIA sign-in page, with no skip save password argument.
TestCase{"https://accounts.google.com", false},
// A common GAIA sign-in page, with "0" skip save password argument.
TestCase{"https://accounts.google.com/?ssp=0", false},
// A common GAIA sign-in page, with skip save password argument.
TestCase{"https://accounts.google.com/?ssp=1", true},
// The Gaia page that is used to start a Chrome sign-in flow when Desktop
// Identity Consistency is enable.
TestCase{
GaiaUrls::GetInstance()->signin_chrome_sync_dice().spec().c_str(),
true},
});
for (const TestCase& test_case : cases) {
SCOPED_TRACE(testing::Message("origin=")
<< test_case.origin
<< ", expected_form_has_skip_save_password="
<< test_case.expected_form_has_skip_save_password);
std::unique_ptr<PasswordFormBuilder> builder(new PasswordFormBuilder(""));
builder->AddTextField("username", "", nullptr);
builder->AddPasswordField("password", "", nullptr);
std::string html = builder->ProduceHTML();
WebFormElement form;
LoadWebFormFromHTML(html, &form, test_case.origin);
EXPECT_EQ(test_case.expected_form_has_skip_save_password,
IsGaiaWithSkipSavePasswordForm(form));
}
}
} // namespace
} // namespace autofill
|