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
|
// Copyright 2016 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/core/common/signatures.h"
#include <string_view>
#include "base/containers/span.h"
#include "base/hash/sha1.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "url/gurl.h"
namespace autofill {
namespace {
// Returns a copy of |input| without >= 5 consecutive digits.
std::string StripDigitsIfRequired(std::string_view input) {
static constexpr auto IsDigit = base::IsAsciiDigit<char>;
std::string result;
result.reserve(input.size());
for (size_t i = 0; i < input.size();) {
// If `input[i]` is not a digit, append it to `result` and move to the next
// character.
if (!IsDigit(input[i])) {
result.push_back(input[i]);
++i;
continue;
}
// If `input[i]` is a digit, find the range of consecutive digits starting
// at `i`. If this range is shorter than 5 characters append it to `result`.
auto end_it = std::ranges::find_if_not(input.substr(i), IsDigit);
std::string_view digits = base::MakeStringPiece(input.begin() + i, end_it);
DCHECK(std::ranges::all_of(digits, IsDigit));
if (digits.size() < 5)
base::StrAppend(&result, {digits});
i += digits.size();
}
return result;
}
template <size_t N>
uint64_t PackBytes(base::span<const uint8_t, N> bytes) {
static_assert(N <= 8u,
"Error: Can't pack more than 8 bytes into a uint64_t.");
uint64_t result = 0;
for (auto byte : bytes)
result = (result << 8) | byte;
return result;
}
} // namespace
// If a form name was set by Chrome, we should ignore it when calculating
// the form signature.
std::string GetDOMFormName(const std::string& form_name) {
#if BUILDFLAG(IS_IOS)
// In case of an empty form name, the synthetic name is created. Ignore it.
return (StartsWith(form_name, "gChrome~form~", base::CompareCase::SENSITIVE)
? std::string()
: form_name);
#else
return form_name;
#endif
}
FormSignature CalculateFormSignature(const FormData& form_data) {
const GURL& target_url = form_data.action();
const GURL& source_url = form_data.url();
std::string_view scheme = target_url.scheme_piece();
std::string_view host = target_url.host_piece();
// If target host or scheme is empty, set scheme and host of source url.
// This is done to match the Toolbar's behavior.
if (scheme.empty() || host.empty()) {
scheme = source_url.scheme_piece();
host = source_url.host_piece();
}
std::string form_signature_field_names;
for (const FormFieldData& field : form_data.fields()) {
switch (field.form_control_type()) {
case mojom::FormControlType::kInputCheckbox:
case mojom::FormControlType::kInputDate:
case mojom::FormControlType::kInputRadio:
break;
case mojom::FormControlType::kContentEditable:
case mojom::FormControlType::kInputEmail:
case mojom::FormControlType::kInputMonth:
case mojom::FormControlType::kInputNumber:
case mojom::FormControlType::kInputPassword:
case mojom::FormControlType::kInputSearch:
case mojom::FormControlType::kInputTelephone:
case mojom::FormControlType::kInputText:
case mojom::FormControlType::kInputUrl:
case mojom::FormControlType::kSelectOne:
case mojom::FormControlType::kTextArea:
base::StrAppend(
&form_signature_field_names,
{"&", StripDigitsIfRequired(base::UTF16ToUTF8(field.name()))});
}
}
std::string form_name = StripDigitsIfRequired(
GetDOMFormName(base::UTF16ToUTF8(form_data.name())));
std::string form_string = base::StrCat(
{scheme, "://", host, "&", form_name, form_signature_field_names});
return FormSignature(StrToHash64Bit(form_string));
}
FormSignature CalculateAlternativeFormSignature(const FormData& form_data) {
std::string_view scheme = form_data.action().scheme_piece();
std::string_view host = form_data.action().host_piece();
// If target host or scheme is empty, set scheme and host of source url.
// This is done to match the Toolbar's behavior.
if (scheme.empty() || host.empty()) {
scheme = form_data.url().scheme_piece();
host = form_data.url().host_piece();
}
std::string form_signature_field_types;
for (const FormFieldData& field : form_data.fields()) {
switch (field.form_control_type()) {
case mojom::FormControlType::kInputCheckbox:
case mojom::FormControlType::kInputDate:
case mojom::FormControlType::kInputRadio:
break;
case mojom::FormControlType::kContentEditable:
case mojom::FormControlType::kInputEmail:
case mojom::FormControlType::kInputMonth:
case mojom::FormControlType::kInputNumber:
case mojom::FormControlType::kInputPassword:
case mojom::FormControlType::kInputSearch:
case mojom::FormControlType::kInputTelephone:
case mojom::FormControlType::kInputText:
case mojom::FormControlType::kInputUrl:
case mojom::FormControlType::kSelectOne:
case mojom::FormControlType::kTextArea:
// We use the string representation of the FormControlType because
// changing the signature algorithm is non-trivial. If and when the
// sectioning algorithm changes, we could use the raw FormControlType
// enum instead.
base::StrAppend(
&form_signature_field_types,
{"&", FormControlTypeToString(field.form_control_type())});
}
}
std::string form_string =
base::StrCat({scheme, "://", host, form_signature_field_types});
// Add more non-empty elements (one of path, reference, or query ordered by
// preference) for small forms with 1-2 fields in order to prevent signature
// collisions.
if (form_data.fields().size() <= 2) {
// Path piece includes the slash "/", so a non-empty path must have length
// longer than 1.
if (form_data.url().path_piece().length() > 1) {
base::StrAppend(&form_string, {form_data.url().path_piece()});
} else if (form_data.url().has_ref()) {
base::StrAppend(&form_string, {"#", form_data.url().ref_piece()});
} else if (form_data.url().has_query()) {
base::StrAppend(&form_string, {"?", form_data.url().query_piece()});
}
}
return FormSignature(StrToHash64Bit(form_string));
}
FieldSignature CalculateFieldSignatureByNameAndType(
std::u16string_view field_name,
FormControlType field_type) {
return FieldSignature(
StrToHash32Bit(base::StrCat({base::UTF16ToUTF8(field_name), "&",
FormControlTypeToString(field_type)})));
}
FieldSignature CalculateFieldSignatureForField(
const FormFieldData& field_data) {
return CalculateFieldSignatureByNameAndType(field_data.name(),
field_data.form_control_type());
}
uint64_t StrToHash64Bit(std::string_view str) {
auto bytes = base::as_byte_span(str);
const base::SHA1Digest digest = base::SHA1Hash(bytes);
return PackBytes(base::span(digest).first<8>());
}
uint32_t StrToHash32Bit(std::string_view str) {
auto bytes = base::as_byte_span(str);
const base::SHA1Digest digest = base::SHA1Hash(bytes);
return PackBytes(base::span(digest).first<4>());
}
int64_t HashFormSignature(FormSignature form_signature) {
return static_cast<uint64_t>(form_signature.value()) % 1021;
}
int64_t HashFieldSignature(FieldSignature field_signature) {
return static_cast<uint64_t>(field_signature.value()) % 1021;
}
} // namespace autofill
|