Skip to content

Commit 40d28c7

Browse files
committed
Restrict Discover and JCB cards to their issuer ranges
1 parent 22786cc commit 40d28c7

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎src/validators/card.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def jcb(value: str, /):
168168
(Literal[True]): If `value` is a valid JCB card number.
169169
(ValidationError): If `value` is an invalid JCB card number.
170170
"""
171-
pattern = re.compile(r"^35")
171+
pattern = re.compile(r"^35(?:2[89]|[3-8][0-9])")
172172
return card_number(value) and len(value) == 16 and pattern.match(value)
173173

174174

@@ -190,7 +190,9 @@ def discover(value: str, /):
190190
(Literal[True]): If `value` is a valid Discover card number.
191191
(ValidationError): If `value` is an invalid Discover card number.
192192
"""
193-
pattern = re.compile(r"^(60|64|65)")
193+
pattern = re.compile(
194+
r"^(6011|64[4-9]|65|622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[01][0-9]|92[0-5]))"
195+
)
194196
return card_number(value) and len(value) == 16 and pattern.match(value)
195197

196198

‎tests/test_card.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,68 @@ def test_returns_true_on_valid_mir(value: str):
217217
def test_returns_failed_on_valid_mir(value: str):
218218
"""Test returns failed on invalid Mir card (other payment systems)."""
219219
assert isinstance(mir(value), ValidationError)
220+
221+
222+
@pytest.mark.parametrize(
223+
"value, expected",
224+
[
225+
("6010000000000005", False),
226+
("6011000000000004", True),
227+
("6012000000000003", False),
228+
("6020000000000003", False),
229+
("6040000000000009", False),
230+
("6400000000000003", False),
231+
("6430000000000007", False),
232+
("6440000000000005", True),
233+
("6490000000000004", True),
234+
("6500000000000002", True),
235+
("6600000000000001", False),
236+
("6221250000000001", False),
237+
("6221260000000000", True),
238+
("6229250000000003", True),
239+
("6229260000000002", False),
240+
],
241+
)
242+
def test_discover_issuer_boundaries(value: str, expected: bool):
243+
"""Check Discover IIN boundaries using synthetic, Luhn-valid numbers."""
244+
assert card_number(value) is True
245+
result = discover(value)
246+
if expected:
247+
assert result is True
248+
else:
249+
assert isinstance(result, ValidationError)
250+
251+
252+
@pytest.mark.parametrize(
253+
"value, expected",
254+
[
255+
("3500000000000009", False),
256+
("3527000000000008", False),
257+
("3528000000000007", True),
258+
("3530000000000003", True),
259+
("3579000000000005", True),
260+
("3580000000000002", True),
261+
("3589000000000003", True),
262+
("3590000000000000", False),
263+
("3599000000000001", False),
264+
],
265+
)
266+
def test_jcb_issuer_boundaries(value: str, expected: bool):
267+
"""Check the inclusive 3528-3589 JCB range with valid checksums."""
268+
assert card_number(value) is True
269+
result = jcb(value)
270+
if expected:
271+
assert result is True
272+
else:
273+
assert isinstance(result, ValidationError)
274+
275+
276+
@pytest.mark.parametrize("value", ["6011000000000000", "6221260000000001"])
277+
def test_discover_rejects_invalid_checksums_in_valid_ranges(value: str):
278+
"""Keep checksum validation for accepted Discover prefixes."""
279+
assert isinstance(discover(value), ValidationError)
280+
281+
282+
def test_jcb_rejects_invalid_checksum_in_valid_range():
283+
"""Keep checksum validation for accepted JCB prefixes."""
284+
assert isinstance(jcb("3528000000000000"), ValidationError)

0 commit comments

Comments
 (0)