From bcd41bd2e95b68370eff72d2116f96ab175c9fb9 Mon Sep 17 00:00:00 2001 From: Triskell Studio Date: Wed, 24 Jun 2026 14:42:54 +0200 Subject: [PATCH] fix(card): mastercard() no longer accepts non-Mastercard 2-series BINs The Mastercard 2-series range is 2221-2720, but the prefix pattern matched 22-27 (i.e. 2200-2799). Numbers in 2200-2220 and 2721-2799 were wrongly accepted as Mastercard, including Mir cards (2200-2204): the same number passed both mir() and mastercard(). Use the precise 2-series sub-ranges. The Mir test cards are now included in the mastercard negative fixtures, which they failed before this change. --- src/validators/card.py | 2 +- tests/test_card.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/validators/card.py b/src/validators/card.py index 94b6637a..484a9dc0 100644 --- a/src/validators/card.py +++ b/src/validators/card.py @@ -80,7 +80,7 @@ def mastercard(value: str, /): (Literal[True]): If `value` is a valid Mastercard card number. (ValidationError): If `value` is an invalid Mastercard card number. """ - pattern = re.compile(r"^(51|52|53|54|55|22|23|24|25|26|27)") + pattern = re.compile(r"^(5[1-5]|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)") return card_number(value) and len(value) == 16 and pattern.match(value) diff --git a/tests/test_card.py b/tests/test_card.py index d0043921..c17f5d95 100644 --- a/tests/test_card.py +++ b/tests/test_card.py @@ -80,7 +80,13 @@ def test_returns_true_on_valid_mastercard(value: str): @pytest.mark.parametrize( "value", - visa_cards + amex_cards + unionpay_cards + diners_cards + jcb_cards + discover_cards, + visa_cards + + amex_cards + + unionpay_cards + + diners_cards + + jcb_cards + + discover_cards + + mir_cards, ) def test_returns_failed_on_valid_mastercard(value: str): """Test returns failed on valid mastercard."""