Skip to content

Commit bcd41bd

Browse files
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.
1 parent 70de324 commit bcd41bd

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

src/validators/card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def mastercard(value: str, /):
8080
(Literal[True]): If `value` is a valid Mastercard card number.
8181
(ValidationError): If `value` is an invalid Mastercard card number.
8282
"""
83-
pattern = re.compile(r"^(51|52|53|54|55|22|23|24|25|26|27)")
83+
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)")
8484
return card_number(value) and len(value) == 16 and pattern.match(value)
8585

8686

tests/test_card.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ def test_returns_true_on_valid_mastercard(value: str):
8080

8181
@pytest.mark.parametrize(
8282
"value",
83-
visa_cards + amex_cards + unionpay_cards + diners_cards + jcb_cards + discover_cards,
83+
visa_cards
84+
+ amex_cards
85+
+ unionpay_cards
86+
+ diners_cards
87+
+ jcb_cards
88+
+ discover_cards
89+
+ mir_cards,
8490
)
8591
def test_returns_failed_on_valid_mastercard(value: str):
8692
"""Test returns failed on valid mastercard."""

0 commit comments

Comments
 (0)