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