validation.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2013 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. 4 5#include "components/autofill/core/browser/validation.h" 6 7#include "base/strings/string_number_conversions.h" 8#include "base/strings/string_piece.h" 9#include "base/strings/string_util.h" 10#include "base/strings/utf_string_conversions.h" 11#include "base/time/time.h" 12#include "components/autofill/core/browser/autofill_regexes.h" 13#include "components/autofill/core/browser/credit_card.h" 14#include "components/autofill/core/browser/state_names.h" 15 16using base::StringPiece16; 17 18namespace { 19 20// The separator characters for SSNs. 21const base::char16 kSSNSeparators[] = {' ', '-', 0}; 22 23} // namespace 24 25namespace autofill { 26 27bool IsValidCreditCardExpirationDate(const base::string16& year, 28 const base::string16& month, 29 const base::Time& now) { 30 base::string16 year_cleaned, month_cleaned; 31 base::TrimWhitespace(year, base::TRIM_ALL, &year_cleaned); 32 base::TrimWhitespace(month, base::TRIM_ALL, &month_cleaned); 33 if (year_cleaned.length() != 4) 34 return false; 35 36 int cc_year; 37 if (!base::StringToInt(year_cleaned, &cc_year)) 38 return false; 39 40 int cc_month; 41 if (!base::StringToInt(month_cleaned, &cc_month)) 42 return false; 43 44 return IsValidCreditCardExpirationDate(cc_year, cc_month, now); 45} 46 47bool IsValidCreditCardExpirationDate(int year, 48 int month, 49 const base::Time& now) { 50 base::Time::Exploded now_exploded; 51 now.LocalExplode(&now_exploded); 52 53 if (year < now_exploded.year) 54 return false; 55 56 if (year == now_exploded.year && month < now_exploded.month) 57 return false; 58 59 return true; 60} 61 62bool IsValidCreditCardNumber(const base::string16& text) { 63 base::string16 number = CreditCard::StripSeparators(text); 64 65 // Credit card numbers are at most 19 digits in length [1]. 12 digits seems to 66 // be a fairly safe lower-bound [2]. Specific card issuers have more rigidly 67 // defined sizes. 68 // [1] http://www.merriampark.com/anatomycc.htm 69 // [2] http://en.wikipedia.org/wiki/Bank_card_number 70 const std::string type = CreditCard::GetCreditCardType(text); 71 if (type == kAmericanExpressCard && number.size() != 15) 72 return false; 73 if (type == kDinersCard && number.size() != 14) 74 return false; 75 if (type == kDiscoverCard && number.size() != 16) 76 return false; 77 if (type == kJCBCard && number.size() != 16) 78 return false; 79 if (type == kMasterCard && number.size() != 16) 80 return false; 81 if (type == kUnionPay && (number.size() < 16 || number.size() > 19)) 82 return false; 83 if (type == kVisaCard && number.size() != 13 && number.size() != 16) 84 return false; 85 if (type == kGenericCard && (number.size() < 12 || number.size() > 19)) 86 return false; 87 88 // Unlike all the other supported types, UnionPay cards lack Luhn checksum 89 // validation. 90 if (type == kUnionPay) 91 return true; 92 93 // Use the Luhn formula [3] to validate the number. 94 // [3] http://en.wikipedia.org/wiki/Luhn_algorithm 95 int sum = 0; 96 bool odd = false; 97 for (base::string16::reverse_iterator iter = number.rbegin(); 98 iter != number.rend(); 99 ++iter) { 100 if (!IsAsciiDigit(*iter)) 101 return false; 102 103 int digit = *iter - '0'; 104 if (odd) { 105 digit *= 2; 106 sum += digit / 10 + digit % 10; 107 } else { 108 sum += digit; 109 } 110 odd = !odd; 111 } 112 113 return (sum % 10) == 0; 114} 115 116bool IsValidCreditCardSecurityCode(const base::string16& text) { 117 if (text.size() < 3U || text.size() > 4U) 118 return false; 119 120 for (base::string16::const_iterator iter = text.begin(); 121 iter != text.end(); 122 ++iter) { 123 if (!IsAsciiDigit(*iter)) 124 return false; 125 } 126 return true; 127} 128 129bool IsValidCreditCardSecurityCode(const base::string16& code, 130 const base::string16& number) { 131 std::string type = CreditCard::GetCreditCardType(number); 132 size_t required_length = 3; 133 if (type == kAmericanExpressCard) 134 required_length = 4; 135 136 return code.length() == required_length; 137} 138 139bool IsValidEmailAddress(const base::string16& text) { 140 // E-Mail pattern as defined by the WhatWG. (4.10.7.1.5 E-Mail state) 141 const base::string16 kEmailPattern = base::ASCIIToUTF16( 142 "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@" 143 "[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"); 144 return MatchesPattern(text, kEmailPattern); 145} 146 147bool IsValidState(const base::string16& text) { 148 return !state_names::GetAbbreviationForName(text).empty() || 149 !state_names::GetNameForAbbreviation(text).empty(); 150} 151 152bool IsValidZip(const base::string16& text) { 153 const base::string16 kZipPattern = base::ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); 154 return MatchesPattern(text, kZipPattern); 155} 156 157bool IsSSN(const base::string16& text) { 158 base::string16 number_string; 159 base::RemoveChars(text, kSSNSeparators, &number_string); 160 161 // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S = 162 // serial number). The validation we do here is simply checking if the area, 163 // group, and serial numbers are valid. 164 // 165 // Historically, the area number was assigned per state, with the group number 166 // ascending in an alternating even/odd sequence. With that scheme it was 167 // possible to check for validity by referencing a table that had the highest 168 // group number assigned for a given area number. (This was something that 169 // Chromium never did though, because the "high group" values were constantly 170 // changing.) 171 // 172 // However, starting on 25 June 2011 the SSA began issuing SSNs randomly from 173 // all areas and groups. Group numbers and serial numbers of zero remain 174 // invalid, and areas 000, 666, and 900-999 remain invalid. 175 // 176 // References for current practices: 177 // http://www.socialsecurity.gov/employer/randomization.html 178 // http://www.socialsecurity.gov/employer/randomizationfaqs.html 179 // 180 // References for historic practices: 181 // http://www.socialsecurity.gov/history/ssn/geocard.html 182 // http://www.socialsecurity.gov/employer/stateweb.htm 183 // http://www.socialsecurity.gov/employer/ssnvhighgroup.htm 184 185 if (number_string.length() != 9 || !IsStringASCII(number_string)) 186 return false; 187 188 int area; 189 if (!base::StringToInt(StringPiece16(number_string.begin(), 190 number_string.begin() + 3), 191 &area)) { 192 return false; 193 } 194 if (area < 1 || 195 area == 666 || 196 area >= 900) { 197 return false; 198 } 199 200 int group; 201 if (!base::StringToInt(StringPiece16(number_string.begin() + 3, 202 number_string.begin() + 5), 203 &group) 204 || group == 0) { 205 return false; 206 } 207 208 int serial; 209 if (!base::StringToInt(StringPiece16(number_string.begin() + 5, 210 number_string.begin() + 9), 211 &serial) 212 || serial == 0) { 213 return false; 214 } 215 216 return true; 217} 218 219} // namespace autofill 220