Skip to content

Reject card numbers of impossible length in card_number() - #594

Open
yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:card-number-length
Open

yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:card-number-length

Conversation

@yuchengshen931122-creator

Copy link
Copy Markdown

card_number() only computes the Luhn checksum, so any string of digits with a matching check digit is a valid card number, whatever its length: card_number("0"), card_number("18") and the 20-digit card_number("62123456789012345676") are all True.

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.

About the bounds: 19 is the maximum that ISO/IEC 7812-1 is commonly cited for, and it agrees with the 19-digit Visa numbers of #556. For the minimum we could not read the standard itself; 8 is the loosest reading of the public summaries (the 2017 edition is said to require 10, and the large networks issue 12 or more). We chose the loosest value so that nothing plausible is rejected. If you prefer 10 or 12, it is one number in the code and one test value.

This is separate from #567 / #568, which are about non-ASCII digits in the same function; the branch merges cleanly with #568, #558 and #569.

The added tests (9 cases, each value Luhn-valid so that only the length decides) include 7 that fail on master. The full suite including doctests passes on Python 3.13 (1,583 passed); Ruff lint and format, Pyright and Bandit report nothing. Other Python versions are left to CI.

Found while writing specification-based tests for a university software-testing course project (NYCU). Prepared with AI assistance; reproduced and tested locally.

🤖 Generated with Claude Code

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>

@mjanthony819 mjanthony819 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally against this branch (b0d62fd): the full card suite (tests/test_card.py, 137 tests) and the card.py doctests both pass, and the boundary behavior matches the diff's intent.

  • Luhn-valid 8-digit and 19-digit values still pass ("40128886", "6212345678901234569").
  • The previously-True cases from the issue ("18", "0", and the 20-digit "62123456789012345676") are now rejected; on master all three return True.
  • The only inputs that change result are all-digit strings outside 8-19, which is the point of the change. Values with separators or other non-digit characters still fail in the existing parse path, same as before, so the character counting in len() introduces no gap.
  • Non-string input degrades the same way on both branches: the decorator wraps the TypeError into a ValidationError (only the reason string differs).
  • visa/mastercard/unionpay (16 digits) and amex (15) call card_number() first, so they inherit the gate; their own stricter length checks run afterwards, so no brand validator is affected.

One observation, not a request: the floor of 8 matches the pre-2017 ISO/IEC 7812 layout (6-digit IIN plus account digit plus check digit). The 2017 revision's 8-digit IIN would imply a floor of 10, but 8 is the defensible minimum for cards in circulation and fits the issue's description.

AI assistance covered the analysis and this review text.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants