Repository navigation
Reject card numbers of impossible length in card_number() - #594
Open
yuchengshen931122-creator wants to merge 1 commit into
Open
yuchengshen931122-creator wants to merge 1 commit into
yuchengshen931122-creator wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-digitcard_number("62123456789012345676")are allTrue.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