Skip to content

Fix card_number accepting non-ASCII decimal digits - #596

Closed
adityajai119 wants to merge 1 commit into
python-validators:masterfrom
adityajai119:fix/card-number-non-ascii-digits
Closed

adityajai119 wants to merge 1 commit into
python-validators:masterfrom
adityajai119:fix/card-number-non-ascii-digits

Conversation

@adityajai119

Copy link
Copy Markdown

Summary

card_number() converted each character with int(), which accepts Unicode decimal digits. A card number written with Arabic-Indic or fullwidth digits therefore passed the Luhn check and was reported as valid.

The fix adds an ASCII guard before character conversion.

Verification

  • Added two regression cases for Arabic-Indic and fullwidth digit forms of a known-valid card number.
  • Before fix: 2 failed, 128 deselected.
  • After fix: 130 passed in tests/test_card.py; ruff check clean.

@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 (cc49ecb): tests/test_card.py (130 tests) and the card.py doctests (9) both pass, and the behavior change matches the intent.

On master, int() accepts any Unicode decimal digit, so transliterations of Luhn-valid numbers pass: "٤٢٤٢٤٢٤٢٤٢٤٢٤٢٤٢" (Arabic-Indic), "424242424242" (fullwidth), and "४२४२४२४२४२४२४२" (Devanagari) all return True, including mixed-script values like "4242٤٢٤٢424242". On this branch all of them are rejected while plain ASCII "4242424242424242" still passes. Non-string input degrades the same way on both branches (the decorator wraps the exception into a ValidationError; only the reason string changes), and bytes input behaves identically to master.

One completeness note, not a request to expand this PR: the same list(map(int, value)) pattern exists in src/validators/i18n/ru.py (ru_inn), and it has the same hole on master - ru_inn("٧٧٠٧٠٨٣٨٩٣"), the Arabic-Indic transliteration of the valid INN 7707083893, returns True. The other i18n validators I checked are safe: fi_business_id gates on an ASCII-only [0-9] regex and the es_* validators already call .isascii() on the digit body. ru_inn may be worth the same one-line guard in a follow-up.

AI assistance covered the analysis and this review text.

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