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