1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/autofill/core/common/autofill_data_validation.h"
7 #include "components/autofill/core/common/form_data.h"
8 #include "components/autofill/core/common/form_field_data.h"
9 #include "components/autofill/core/common/password_form_fill_data.h"
14 const size_t kMaxDataLength
= 1024;
16 // Allow enough space for all countries (roughly 300 distinct values) and all
17 // timezones (roughly 400 distinct values), plus some extra wiggle room.
18 const size_t kMaxListSize
= 512;
20 bool IsValidString(const std::string
& str
) {
21 return str
.size() <= kMaxDataLength
;
24 bool IsValidString16(const base::string16
& str
) {
25 return str
.size() <= kMaxDataLength
;
28 bool IsValidGURL(const GURL
& url
) {
29 return url
.is_empty() || url
.is_valid();
32 bool IsValidFormFieldData(const FormFieldData
& field
) {
34 IsValidString16(field
.label
) &&
35 IsValidString16(field
.name
) &&
36 IsValidString16(field
.value
) &&
37 IsValidString(field
.form_control_type
) &&
38 IsValidString(field
.autocomplete_attribute
) &&
39 IsValidString16Vector(field
.option_values
) &&
40 IsValidString16Vector(field
.option_contents
);
43 bool IsValidFormData(const FormData
& form
) {
44 if (!IsValidString16(form
.name
) ||
45 !IsValidGURL(form
.origin
) ||
46 !IsValidGURL(form
.action
))
49 if (form
.fields
.size() > kMaxListSize
)
52 for (std::vector
<FormFieldData
>::const_iterator it
= form
.fields
.begin();
53 it
!= form
.fields
.end(); ++it
) {
54 if (!IsValidFormFieldData(*it
))
61 bool IsValidPasswordFormFillData(const PasswordFormFillData
& form
) {
62 if (!IsValidFormData(form
.basic_data
) ||
63 !IsValidString(form
.preferred_realm
))
66 for (PasswordFormFillData::LoginCollection::const_iterator it
=
67 form
.additional_logins
.begin();
68 it
!= form
.additional_logins
.end(); ++it
) {
69 if (!IsValidString16(it
->first
) ||
70 !IsValidString16(it
->second
.password
) ||
71 !IsValidString(it
->second
.realm
))
75 for (PasswordFormFillData::UsernamesCollection::const_iterator it
=
76 form
.other_possible_usernames
.begin();
77 it
!= form
.other_possible_usernames
.end(); ++it
) {
78 if (!IsValidString16(it
->first
.username
) ||
79 !IsValidString16(it
->first
.password
) ||
80 !IsValidString(it
->first
.realm
) ||
81 !IsValidString16Vector(it
->second
))
88 bool IsValidString16Vector(const std::vector
<base::string16
>& v
) {
89 if (v
.size() > kMaxListSize
)
92 for (std::vector
<base::string16
>::const_iterator it
= v
.begin();
93 it
!= v
.end(); ++it
) {
94 if (!IsValidString16(*it
))
101 bool IsValidFormDataVector(const std::vector
<FormData
>& v
) {
102 if (v
.size() > kMaxListSize
)
105 for (std::vector
<FormData
>::const_iterator it
= v
.begin(); it
!= v
.end();
107 if (!IsValidFormData(*it
))
114 } // namespace autofill