Skip to content

Commit

Permalink
For mozilla-mobile#11360: Fix crash when saving credit cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Dec 3, 2021
1 parent 131dd4b commit 6dfb70e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.components.support.utils

import androidx.annotation.DrawableRes
import mozilla.components.support.utils.ext.toCreditCardNumber
import kotlin.math.floor
import kotlin.math.log10

Expand Down Expand Up @@ -253,22 +254,23 @@ internal object CreditCardUtils {
*/
@Suppress("ComplexMethod")
fun getCreditCardIIN(cardNumber: String): CreditCardIIN? {
val safeCardNumber = cardNumber.toCreditCardNumber()
for (issuer in creditCardIINs) {
if (issuer.cardNumberMaxLength.size == 1 &&
issuer.cardNumberMaxLength[0] != cardNumber.length
issuer.cardNumberMaxLength[0] != safeCardNumber.length
) {
continue
} else if (issuer.cardNumberMaxLength.size > 1 &&
(
cardNumber.length < issuer.cardNumberMaxLength[0] ||
cardNumber.length > issuer.cardNumberMaxLength[1]
safeCardNumber.length < issuer.cardNumberMaxLength[0] ||
safeCardNumber.length > issuer.cardNumberMaxLength[1]
)
) {
continue
}

val prefixLength = floor(log10(issuer.startRange.toDouble())) + 1
val prefix = cardNumber.substring(0, prefixLength.toInt()).toInt()
val prefix = safeCardNumber.substring(0, prefixLength.toInt()).toInt()

if (prefix >= issuer.startRange && prefix <= issuer.endRange) {
return issuer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.support.utils.ext

/**
* Strips characters other than digits from a string.
* Used to strip a credit card number user input of spaces and separators.
*/
fun String.toCreditCardNumber(): String {
return this.filter { it.isDigit() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class CreditCardUtilsTest {
Pair("6278592974938779", "unionpay"),
Pair("8171999927660000", "unionpay"),
Pair("30569309025904", "diners"),
Pair("38520000023237", "diners")
Pair("38520000023237", "diners"),
Pair("3 8 5 2 0 0 0 0 0 2 3 2 3 7", "diners")
)

for ((cardNumber, cardType) in recognizedCards) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mozilla.components.support.utils.ext

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class StringTest {

@Test
fun `GIVEN a string credit card number WHEN calling toCreditCardNumber THEN any character that is not a digit will removed`() {
val number = "385 - 2 0 0 - 0 0 0 2 3 2 3 7"
val validResult = "38520000023237"

assertEquals(validResult, number.toCreditCardNumber())
}
}
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ permalink: /changelog/
* Removes deprecated `GeckoLoginDelegateWrapper`. Please use `GeckoAutocompleteStorageDelegate`. [#11311](https://github.com/mozilla-mobile/android-components/issues/11311)
* Added setting for HTTPS-Only mode [#5935](https://github.com/mozilla-mobile/focus-android/issues/5935)

* **support-utils**
* 🌟️ Add `String.toCreditCardNumber` for removing characters other than digits from a credit card string number.
* 🚒 Bug fixed [issue #11360](https://github.com/mozilla-mobile/android-components/issues/11360) - Crash when saving credit cards

# 95.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v94.0.0...v95.0.0)
* [Milestone](https://github.com/mozilla-mobile/android-components/milestone/142?closed=1)
Expand Down

0 comments on commit 6dfb70e

Please sign in to comment.