summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/autofill_profile_validation_util.cc
blob: 14f084dc98184e0b99809bd9886dfc87cb269bf6 (plain)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2017 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 "components/autofill/core/browser/autofill_profile_validation_util.h"

#include <string>
#include <utility>

#include "base/check.h"
#include "base/i18n/case_conversion.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "components/autofill/core/browser/geo/country_data.h"
#include "components/autofill/core/browser/validation.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
#include "third_party/libphonenumber/dist/cpp/src/phonenumbers/phonenumberutil.h"

namespace autofill {

namespace {

using ::i18n::addressinput::COUNTRY;
using ::i18n::addressinput::ADMIN_AREA;
using ::i18n::addressinput::LOCALITY;
using ::i18n::addressinput::DEPENDENT_LOCALITY;
using ::i18n::addressinput::SORTING_CODE;
using ::i18n::addressinput::POSTAL_CODE;
using ::i18n::addressinput::STREET_ADDRESS;
using ::i18n::addressinput::RECIPIENT;

using i18nAddressData = ::i18n::addressinput::AddressData;
using i18nAddressField = ::i18n::addressinput::AddressField;
using i18nAddressProblem = ::i18n::addressinput::AddressProblem;
using i18nFieldProblemMap = ::i18n::addressinput::FieldProblemMap;

using ::i18n::addressinput::INVALID_FORMAT;
using ::i18n::addressinput::MISMATCHING_VALUE;
using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
using ::i18n::addressinput::UNEXPECTED_FIELD;
using ::i18n::addressinput::UNKNOWN_VALUE;
using ::i18n::addressinput::UNSUPPORTED_FIELD;

using ::i18n::phonenumbers::PhoneNumberUtil;

const i18nAddressField kFields[] = {COUNTRY, ADMIN_AREA, LOCALITY,
                                    DEPENDENT_LOCALITY, POSTAL_CODE};
const i18nAddressProblem kProblems[] = {
    UNEXPECTED_FIELD, MISSING_REQUIRED_FIELD, UNKNOWN_VALUE,
    INVALID_FORMAT,   MISMATCHING_VALUE,      UNSUPPORTED_FIELD};

// If the |address_field| is valid, set the validity state of the
// |address_field| in the |profile| to the |state| and return true.
// Otherwise, return false.
bool SetValidityStateForAddressField(const AutofillProfile* profile,
                                     i18nAddressField address_field,
                                     AutofillDataModel::ValidityState state) {
  ServerFieldType server_field = i18n::TypeForField(address_field,
                                                    /*billing=*/false);
  if (server_field == UNKNOWN_TYPE)
    return false;
  DCHECK(profile);
  profile->SetValidityState(server_field, state, AutofillDataModel::CLIENT);
  return true;
}

// Set the validity state of all address fields in the |profile| to |state|.
void SetAllAddressValidityStates(const AutofillProfile* profile,
                                 AutofillDataModel::ValidityState state) {
  DCHECK(profile);
  for (auto field : kFields)
    SetValidityStateForAddressField(profile, field, state);
}

// Returns all relevant pairs of (field, problem), where field is in
// |kFields|, and problem is in |kProblems|.
i18nFieldProblemMap* CreateFieldProblemMap() {
  i18nFieldProblemMap* filter = new i18nFieldProblemMap();
  for (auto field : kFields) {
    for (auto problem : kProblems) {
      filter->insert(std::make_pair(field, problem));
    }
  }
  return filter;
}

// GetFilter() will make sure that the validation only returns problems that
// are relevant.
const i18nFieldProblemMap* GetFilter() {
  static const i18nFieldProblemMap* const filter = CreateFieldProblemMap();
  return filter;
}

// Initializes |address| data from the address info in the |profile|.
void InitializeAddressFromProfile(const AutofillProfile& profile,
                                  i18nAddressData* address) {
  address->region_code =
      base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_COUNTRY));
  address->administrative_area =
      base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_STATE));
  address->locality = base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_CITY));
  address->dependent_locality =
      base::UTF16ToUTF8(profile.GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY));
  // The validation is case insensitive, and the postal codes are always upper
  // case.
  address->postal_code = base::UTF16ToUTF8(
      base::i18n::ToUpper(profile.GetRawInfo(ADDRESS_HOME_ZIP)));
}

void SetEmptyValidityIfEmpty(const AutofillProfile* profile) {
  if (profile->GetRawInfo(ADDRESS_HOME_COUNTRY).empty())
    profile->SetValidityState(ADDRESS_HOME_COUNTRY, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
  if (profile->GetRawInfo(ADDRESS_HOME_STATE).empty())
    profile->SetValidityState(ADDRESS_HOME_STATE, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
  if (profile->GetRawInfo(ADDRESS_HOME_CITY).empty())
    profile->SetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
  if (profile->GetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY).empty())
    profile->SetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                              AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
  if (profile->GetRawInfo(ADDRESS_HOME_ZIP).empty())
    profile->SetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
}

void SetInvalidIfUnvalidated(const AutofillProfile* profile) {
  if (profile->GetValidityState(ADDRESS_HOME_COUNTRY,
                                AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_COUNTRY, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_STATE,
                                AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_STATE, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                                AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                              AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }
}

void MaybeApplyValidToFields(const AutofillProfile* profile) {
  // The metadata works from top to bottom. Therefore, a so far UNVALIDATED
  // subregion can only be validated if its super-region is VALID. In  this
  // case, it's VALID if it has not been marked as INVALID or EMPTY.

  if (profile->GetValidityState(ADDRESS_HOME_STATE,
                                AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_STATE, AutofillDataModel::VALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::CLIENT) ==
          AutofillDataModel::UNVALIDATED &&
      profile->GetValidityState(ADDRESS_HOME_STATE,
                                AutofillDataModel::CLIENT) ==
          AutofillDataModel::VALID) {
    profile->SetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::VALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                                AutofillDataModel::CLIENT) ==
          AutofillDataModel::UNVALIDATED &&
      profile->GetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::CLIENT) ==
          AutofillDataModel::VALID) {
    profile->SetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                              AutofillDataModel::VALID,
                              AutofillDataModel::CLIENT);
  }

  // ZIP only depends on COUNTRY. If it's not so far marked as INVALID or EMPTY,
  // then it's VALID.
  if (profile->GetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::CLIENT) ==
      AutofillDataModel::UNVALIDATED) {
    profile->SetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::VALID,
                              AutofillDataModel::CLIENT);
  }
}

void ApplyValidOnlyIfAllChildrenNotInvalid(const AutofillProfile* profile) {
  if (profile->GetValidityState(ADDRESS_HOME_STATE,
                                AutofillDataModel::CLIENT) ==
          AutofillDataModel::INVALID &&
      profile->GetValidityState(ADDRESS_HOME_ZIP, AutofillDataModel::CLIENT) ==
          AutofillDataModel::INVALID) {
    profile->SetValidityState(ADDRESS_HOME_COUNTRY, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::CLIENT) ==
      AutofillDataModel::INVALID) {
    profile->SetValidityState(ADDRESS_HOME_STATE, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }

  if (profile->GetValidityState(ADDRESS_HOME_DEPENDENT_LOCALITY,
                                AutofillDataModel::CLIENT) ==
      AutofillDataModel::INVALID) {
    profile->SetValidityState(ADDRESS_HOME_CITY, AutofillDataModel::INVALID,
                              AutofillDataModel::CLIENT);
  }
}

}  // namespace

namespace profile_validation_util {

void ValidateProfile(const AutofillProfile* profile,
                     AddressValidator* address_validator) {
  DCHECK(address_validator);
  DCHECK(profile);
  ValidateAddressStrictly(profile, address_validator);
  ValidatePhoneNumber(profile);
  ValidateEmailAddress(profile);
}

AddressValidator::Status ValidateAddress(const AutofillProfile* profile,
                                         AddressValidator* address_validator) {
  DCHECK(address_validator);
  DCHECK(profile);

  SetAllAddressValidityStates(profile, AutofillDataModel::UNVALIDATED);

  if (!base::Contains(
          CountryDataMap::GetInstance()->country_codes(),
          base::UTF16ToUTF8(profile->GetRawInfo(ADDRESS_HOME_COUNTRY)))) {
    // If the country code is not in the database, the country code and the
    // profile are invalid, and other fields cannot be validated, because it is
    // unclear which, if any, rule should apply.
    SetValidityStateForAddressField(profile, COUNTRY,
                                    AutofillDataModel::INVALID);
    SetEmptyValidityIfEmpty(profile);
    return AddressValidator::SUCCESS;
  }

  // The COUNTRY was already listed in the CountryDataMap, therefore it's valid.
  SetValidityStateForAddressField(profile, COUNTRY, AutofillDataModel::VALID);

  i18nAddressData address;
  InitializeAddressFromProfile(*profile, &address);
  i18nFieldProblemMap problems;
  // status denotes if the rule was successfully loaded before validation.
  AddressValidator::Status status =
      address_validator->ValidateAddress(address, GetFilter(), &problems);

  // The address fields for which validation is not supported by the metadata
  // will be marked as UNSUPPORTED_FIELDs. These fields should be treated like
  // VALID fields to stay consistent. INVALID_FORMATs, MISMATCHING_VALUEs or
  // UNKNOWN_VALUEs are INVALID. MISSING_REQUIRED_FIELD would be marked as EMPTY
  // along other empty fields. UNEXPECTED_FIELD would mean that there is also no
  // metadata for validation, therefore, they are also UNSUPPORTED_FIELDs, and
  // thus they would be treated as VALID fields.
  for (auto problem : problems) {
    if (problem.second == UNSUPPORTED_FIELD) {
      SetValidityStateForAddressField(profile, problem.first,
                                      AutofillDataModel::VALID);

    } else if (problem.second == INVALID_FORMAT ||
               problem.second == MISMATCHING_VALUE ||
               problem.second == UNKNOWN_VALUE) {
      SetValidityStateForAddressField(profile, problem.first,
                                      AutofillDataModel::INVALID);
    }
  }

  SetEmptyValidityIfEmpty(profile);

  // Fields (except COUNTRY) could be VALID, only if the rules were available.
  if (status == AddressValidator::SUCCESS)
    MaybeApplyValidToFields(profile);

  return status;
}

void ValidateAddressStrictly(const AutofillProfile* profile,
                             AddressValidator* address_validator) {
  DCHECK(address_validator);
  DCHECK(profile);

  // If the rules were loaded successfully, add a second layer of validation:
  // 1. For a field to stay valid after the first run, all the fields that
  // depend on that field for validation need to not be invalid on the first
  // run, otherwise there is a chance that the data on that field was also
  // invalid (incorrect.)
  // Example: 1225 Notre-Dame Ouest, Montreal, Quebec, H3C 2A3, United States.
  // A human validator can see that the country is most probably the invalid
  // field. The first step helps us validate the rules interdependently.
  // 2. All the address fields that could not be validated (UNVALIDATED),
  // should be considered as invalid.

  if (ValidateAddress(profile, address_validator) ==
      AddressValidator::SUCCESS) {
    ApplyValidOnlyIfAllChildrenNotInvalid(profile);
    SetInvalidIfUnvalidated(profile);
  }
}

void ValidateEmailAddress(const AutofillProfile* profile) {
  const base::string16& email = profile->GetRawInfo(EMAIL_ADDRESS);
  if (email.empty()) {
    profile->SetValidityState(EMAIL_ADDRESS, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
    return;
  }

  profile->SetValidityState(EMAIL_ADDRESS,
                            autofill::IsValidEmailAddress(email)
                                ? AutofillDataModel::VALID
                                : AutofillDataModel::INVALID,
                            AutofillDataModel::CLIENT);
}

void ValidatePhoneNumber(const AutofillProfile* profile) {
  const std::string& phone_number =
      base::UTF16ToUTF8(profile->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
  if (phone_number.empty()) {
    profile->SetValidityState(PHONE_HOME_WHOLE_NUMBER, AutofillDataModel::EMPTY,
                              AutofillDataModel::CLIENT);
    return;
  }

  const std::string& country_code =
      base::UTF16ToUTF8(profile->GetRawInfo(ADDRESS_HOME_COUNTRY));
  if (!base::Contains(CountryDataMap::GetInstance()->country_codes(),
                      country_code)) {
    // If the country code is not in the database, the phone number cannot be
    // validated.
    profile->SetValidityState(PHONE_HOME_WHOLE_NUMBER,
                              AutofillDataModel::UNVALIDATED,
                              AutofillDataModel::CLIENT);
    return;
  }

  PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
  profile->SetValidityState(
      PHONE_HOME_WHOLE_NUMBER,
      phone_util->IsPossibleNumberForString(phone_number, country_code)
          ? AutofillDataModel::VALID
          : AutofillDataModel::INVALID,
      AutofillDataModel::CLIENT);
}

}  // namespace profile_validation_util
}  // namespace autofill