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>

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.

1 participant