| OLD | NEW | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |    1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #include "modules/payments/PaymentsValidators.h" |    5 #include "components/payments/payments_validators.h" | 
|    6  |    6  | 
|    7 #include "bindings/core/v8/ScriptRegexp.h" |    7 #include "third_party/re2/src/re2/re2.h" | 
|    8 #include "platform/weborigin/KURL.h" |    8 #include "url/gurl.h" | 
|    9 #include "wtf/text/StringImpl.h" |  | 
|   10  |    9  | 
|   11 namespace blink { |   10 namespace payments { | 
|   12  |   11  | 
|   13 // We limit the maximum length of string to 2048 bytes for security reasons. |   12 // We limit the maximum length of string to 2048 bytes for security reasons. | 
|   14 static const int maxiumStringLength = 2048; |   13 static const int maximumStringLength = 2048; | 
|   15  |   14  | 
|   16 bool PaymentsValidators::isValidCurrencyCodeFormat( |   15 bool PaymentsValidators::isValidCurrencyCodeFormat( | 
|   17     const String& code, |   16     const std::string& code, | 
|   18     const String& system, |   17     const std::string& system, | 
|   19     String* optionalErrorMessage) { |   18     std::string* optionalErrorMessage) { | 
|   20   if (system == "urn:iso:std:iso:4217") { |   19   if (system == "urn:iso:std:iso:4217") { | 
|   21     if (ScriptRegexp("^[A-Z]{3}$", TextCaseSensitive).match(code) == 0) |   20     if (RE2::FullMatch(code, "[A-Z]{3}")) | 
|   22       return true; |   21       return true; | 
|   23  |   22  | 
|   24     if (optionalErrorMessage) |   23     if (optionalErrorMessage) | 
|   25       *optionalErrorMessage = "'" + code + |   24       *optionalErrorMessage = "'" + code + | 
|   26                               "' is not a valid ISO 4217 currency code, should " |   25                               "' is not a valid ISO 4217 currency code, should " | 
|   27                               "be 3 upper case letters [A-Z]"; |   26                               "be 3 upper case letters [A-Z]"; | 
|   28  |   27  | 
|   29     return false; |   28     return false; | 
|   30   } |   29   } | 
|   31  |   30  | 
|   32   if (!KURL(KURL(), system).isValid()) { |   31   if (code.size() > maximumStringLength) { | 
|   33     if (optionalErrorMessage) |   32     if (optionalErrorMessage) | 
|   34       *optionalErrorMessage = "The currency system is not a valid URL"; |   33       *optionalErrorMessage = | 
|   35  |   34           "The currency code should be at most 2048 characters long"; | 
|   36     return false; |   35     return false; | 
|   37   } |   36   } | 
 |   37   if (!GURL(system).is_valid()) { | 
 |   38     if (optionalErrorMessage) | 
 |   39       *optionalErrorMessage = | 
 |   40           "The system should be a valid URL"; | 
 |   41     return false; | 
 |   42   } | 
 |   43   return true; | 
 |   44 } | 
|   38  |   45  | 
|   39   if (code.length() <= maxiumStringLength) |   46 bool PaymentsValidators::isValidAmountFormat( | 
 |   47     const std::string& amount, | 
 |   48     std::string* optionalErrorMessage) { | 
 |   49   if (RE2::FullMatch(amount, "-?[0-9]+(\\.[0-9]+)?")) | 
|   40     return true; |   50     return true; | 
|   41  |   51  | 
|   42   if (optionalErrorMessage) |   52   if (optionalErrorMessage) | 
|   43     *optionalErrorMessage = |  | 
|   44         "The currency code should be at most 2048 characters long"; |  | 
|   45  |  | 
|   46   return false; |  | 
|   47 } |  | 
|   48  |  | 
|   49 bool PaymentsValidators::isValidAmountFormat(const String& amount, |  | 
|   50                                              String* optionalErrorMessage) { |  | 
|   51   if (ScriptRegexp("^-?[0-9]+(\\.[0-9]+)?$", TextCaseSensitive).match(amount) == |  | 
|   52       0) |  | 
|   53     return true; |  | 
|   54  |  | 
|   55   if (optionalErrorMessage) |  | 
|   56     *optionalErrorMessage = "'" + amount + "' is not a valid amount format"; |   53     *optionalErrorMessage = "'" + amount + "' is not a valid amount format"; | 
|   57  |   54  | 
|   58   return false; |   55   return false; | 
|   59 } |   56 } | 
|   60  |   57  | 
|   61 bool PaymentsValidators::isValidCountryCodeFormat( |   58 bool PaymentsValidators::isValidCountryCodeFormat( | 
|   62     const String& code, |   59     const std::string& code, | 
|   63     String* optionalErrorMessage) { |   60     std::string* optionalErrorMessage) { | 
|   64   if (ScriptRegexp("^[A-Z]{2}$", TextCaseSensitive).match(code) == 0) |   61   if (RE2::FullMatch(code, "[A-Z]{2}")) | 
|   65     return true; |   62     return true; | 
|   66  |   63  | 
|   67   if (optionalErrorMessage) |   64   if (optionalErrorMessage) | 
|   68     *optionalErrorMessage = "'" + code + |   65     *optionalErrorMessage = "'" + code + | 
|   69                             "' is not a valid CLDR country code, should be 2 " |   66                             "' is not a valid CLDR country code, should be 2 " | 
|   70                             "upper case letters [A-Z]"; |   67                             "upper case letters [A-Z]"; | 
|   71  |   68  | 
|   72   return false; |   69   return false; | 
|   73 } |   70 } | 
|   74  |   71  | 
|   75 bool PaymentsValidators::isValidLanguageCodeFormat( |   72 bool PaymentsValidators::isValidLanguageCodeFormat( | 
|   76     const String& code, |   73     const std::string& code, | 
|   77     String* optionalErrorMessage) { |   74     std::string* optionalErrorMessage) { | 
|   78   if (ScriptRegexp("^([a-z]{2,3})?$", TextCaseSensitive).match(code) == 0) |   75   if (RE2::FullMatch(code, "([a-z]{2,3})?")) | 
|   79     return true; |   76     return true; | 
|   80  |   77  | 
|   81   if (optionalErrorMessage) |   78   if (optionalErrorMessage) | 
|   82     *optionalErrorMessage = "'" + code + |   79     *optionalErrorMessage = "'" + code + | 
|   83                             "' is not a valid BCP-47 language code, should be " |   80                             "' is not a valid BCP-47 language code, should be " | 
|   84                             "2-3 lower case letters [a-z]"; |   81                             "2-3 lower case letters [a-z]"; | 
|   85  |   82  | 
|   86   return false; |   83   return false; | 
|   87 } |   84 } | 
|   88  |   85  | 
|   89 bool PaymentsValidators::isValidScriptCodeFormat(const String& code, |   86 bool PaymentsValidators::isValidScriptCodeFormat( | 
|   90                                                  String* optionalErrorMessage) { |   87     const std::string& code, | 
|   91   if (ScriptRegexp("^([A-Z][a-z]{3})?$", TextCaseSensitive).match(code) == 0) |   88     std::string* optionalErrorMessage) { | 
 |   89   if (RE2::FullMatch(code, "([A-Z][a-z]{3})?")) | 
|   92     return true; |   90     return true; | 
|   93  |   91  | 
|   94   if (optionalErrorMessage) |   92   if (optionalErrorMessage) | 
|   95     *optionalErrorMessage = "'" + code + |   93     *optionalErrorMessage = "'" + code + | 
|   96                             "' is not a valid ISO 15924 script code, should be " |   94                             "' is not a valid ISO 15924 script code, should be " | 
|   97                             "an upper case letter [A-Z] followed by 3 lower " |   95                             "an upper case letter [A-Z] followed by 3 lower " | 
|   98                             "case letters [a-z]"; |   96                             "case letters [a-z]"; | 
|   99  |   97  | 
|  100   return false; |   98   return false; | 
|  101 } |   99 } | 
|  102  |  100  | 
|  103 bool PaymentsValidators::isValidShippingAddress( |  101 bool PaymentsValidators::isValidShippingAddress( | 
|  104     const mojom::blink::PaymentAddressPtr& address, |  102     const blink::mojom::PaymentAddressPtr& address, | 
|  105     String* optionalErrorMessage) { |  103     std::string* optionalErrorMessage) { | 
|  106   if (!isValidCountryCodeFormat(address->country, optionalErrorMessage)) |  104   if (!isValidCountryCodeFormat(address->country, optionalErrorMessage)) | 
|  107     return false; |  105     return false; | 
|  108  |  106  | 
|  109   if (!isValidLanguageCodeFormat(address->language_code, optionalErrorMessage)) |  107   if (!isValidLanguageCodeFormat(address->language_code, optionalErrorMessage)) | 
|  110     return false; |  108     return false; | 
|  111  |  109  | 
|  112   if (!isValidScriptCodeFormat(address->script_code, optionalErrorMessage)) |  110   if (!isValidScriptCodeFormat(address->script_code, optionalErrorMessage)) | 
|  113     return false; |  111     return false; | 
|  114  |  112  | 
|  115   if (address->language_code.isEmpty() && !address->script_code.isEmpty()) { |  113   if (address->language_code.empty() && !address->script_code.empty()) { | 
|  116     if (optionalErrorMessage) |  114     if (optionalErrorMessage) | 
|  117       *optionalErrorMessage = |  115       *optionalErrorMessage = | 
|  118           "If language code is empty, then script code should also be empty"; |  116           "If language code is empty, then script code should also be empty"; | 
|  119  |  117  | 
|  120     return false; |  118     return false; | 
|  121   } |  119   } | 
|  122  |  120  | 
|  123   return true; |  121   return true; | 
|  124 } |  122 } | 
|  125  |  123  | 
|  126 bool PaymentsValidators::isValidErrorMsgFormat(const String& error, |  124 bool PaymentsValidators::isValidErrorMsgFormat( | 
|  127                                                String* optionalErrorMessage) { |  125     const std::string& error, | 
|  128   if (error.length() <= maxiumStringLength) |  126     std::string* optionalErrorMessage) { | 
 |  127   if (error.length() <= maximumStringLength) | 
|  129     return true; |  128     return true; | 
|  130  |  129  | 
|  131   if (optionalErrorMessage) |  130   if (optionalErrorMessage) | 
|  132     *optionalErrorMessage = |  131     *optionalErrorMessage = | 
|  133         "Error message should be at most 2048 characters long"; |  132         "Error message should be at most 2048 characters long"; | 
|  134  |  133  | 
|  135   return false; |  134   return false; | 
|  136 } |  135 } | 
|  137  |  136  | 
|  138 }  // namespace blink |  137 }  // namespace payments | 
| OLD | NEW |