Skip to content

Commit b0d62fd

Browse files
Reject card numbers of impossible length in card_number()
card_number() only computed the Luhn checksum, so any string of digits with a matching check digit was a valid card number, whatever its length: card_number("0") # True card_number("18") # True card_number("62123456789012345676") # True (20 digits) A card number has at most 19 digits and, made of an issuer identification number, an account number and a check digit, at least 8. Numbers outside that range are now rejected before the checksum. The brand validators already check their own length and are not affected. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 22786cc commit b0d62fd

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎src/validators/card.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
def card_number(value: str, /):
1212
"""Return whether or not given value is a valid generic card number.
1313
14-
This validator is based on [Luhn's algorithm][1].
14+
This validator is based on [Luhn's algorithm][1]. Numbers of fewer than 8
15+
or more than 19 digits are rejected, since no card number has such a length.
1516
1617
[1]: https://github.com/mmcloughlin/luhn
1718
@@ -20,6 +21,8 @@ def card_number(value: str, /):
2021
True
2122
>>> card_number('4242424242424241')
2223
ValidationError(func=card_number, args={'value': '4242424242424241'})
24+
>>> card_number('18')
25+
ValidationError(func=card_number, args={'value': '18'})
2326
2427
Args:
2528
value:
@@ -31,6 +34,10 @@ def card_number(value: str, /):
3134
"""
3235
if not value:
3336
return False
37+
if not 8 <= len(value) <= 19:
38+
# an issuer identification number, an account number and a check digit
39+
# take at least 8 digits; the longest card number has 19
40+
return False
3441
try:
3542
digits = list(map(int, value))
3643
odd_sum = sum(digits[-1::-2])

‎tests/test_card.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ def test_returns_true_on_valid_card_number(value: str):
4343
assert card_number(value)
4444

4545

46+
# every value below passes the Luhn check, so only its length decides
47+
@pytest.mark.parametrize("value", ["40128886", "6212345678901234569"])
48+
def test_returns_true_on_card_number_of_shortest_and_longest_length(value: str):
49+
"""Test returns true on card numbers of 8 and of 19 digits."""
50+
assert card_number(value)
51+
52+
53+
@pytest.mark.parametrize(
54+
"value",
55+
[
56+
"0",
57+
"00",
58+
"18",
59+
"0000000",
60+
"4012886",
61+
"62123456789012345676",
62+
"00000000000000000000000000000000",
63+
],
64+
)
65+
def test_returns_failed_validation_on_card_number_of_impossible_length(value: str):
66+
"""Test returns failed validation on card numbers of fewer than 8 or more than 19 digits."""
67+
assert isinstance(card_number(value), ValidationError)
68+
69+
4670
@pytest.mark.parametrize(
4771
"value",
4872
[

0 commit comments

Comments
 (0)