From c5be70e8d86d816714c12cb241a51e94e6c4e97a Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 12 Nov 2026 10:54:37 +0530 Subject: [PATCH] Reject non-ASCII digits in checksum modules and ie.pps int() accepts digits from other scripts (e.g. ARABIC-INDIC DIGIT ONE), so the damm, verhoeff and iso7064 mod_11_2, mod_11_10 and mod_97_10 modules treated such values as valid digits. This also affected the lei and iso11649 modules which build on mod_97_10. The ie.pps format check used \d which has the same problem. Reject non-ASCII input with InvalidFormat and use an explicit ASCII character class in ie.pps. Co-Authored-By: Claude Sonnet 5.5 --- stdnum/damm.py | 2 ++ stdnum/ie/pps.py | 2 +- stdnum/iso7064/mod_11_10.py | 2 ++ stdnum/iso7064/mod_11_2.py | 2 ++ stdnum/iso7064/mod_97_10.py | 2 ++ stdnum/verhoeff.py | 2 ++ tests/test_damm.doctest | 11 +++++++++++ tests/test_ie_pps.doctest | 11 +++++++++++ tests/test_iso11649.doctest | 11 +++++++++++ tests/test_iso7064.doctest | 23 +++++++++++++++++++++++ tests/test_lei.doctest | 11 +++++++++++ tests/test_verhoeff.doctest | 11 +++++++++++ 12 files changed, 89 insertions(+), 1 deletion(-) diff --git a/stdnum/damm.py b/stdnum/damm.py index 2151b099..e1a3ad53 100644 --- a/stdnum/damm.py +++ b/stdnum/damm.py @@ -89,6 +89,8 @@ def validate(number: str, table: DammTable | None = None) -> str: """Check if the number provided passes the Damm algorithm.""" if not bool(number): raise InvalidFormat() + if not str(number).isascii(): + raise InvalidFormat() try: valid = checksum(number, table=table) == 0 except Exception: # noqa: B902 diff --git a/stdnum/ie/pps.py b/stdnum/ie/pps.py index 7ad007ab..8886bb71 100644 --- a/stdnum/ie/pps.py +++ b/stdnum/ie/pps.py @@ -58,7 +58,7 @@ from stdnum.util import clean -pps_re = re.compile(r'^\d{7}[A-W][ABHWTX]?$') +pps_re = re.compile(r'^[0-9]{7}[A-W][ABHWTX]?$') """Regular expression used to check syntax of PPS numbers.""" diff --git a/stdnum/iso7064/mod_11_10.py b/stdnum/iso7064/mod_11_10.py index 3d074692..bf0e459d 100644 --- a/stdnum/iso7064/mod_11_10.py +++ b/stdnum/iso7064/mod_11_10.py @@ -54,6 +54,8 @@ def calc_check_digit(number: str) -> str: def validate(number: str) -> str: """Check whether the check digit is valid.""" + if not str(number).isascii(): + raise InvalidFormat() try: valid = checksum(number) == 1 except Exception: # noqa: B902 diff --git a/stdnum/iso7064/mod_11_2.py b/stdnum/iso7064/mod_11_2.py index 095b6a37..0c9f3751 100644 --- a/stdnum/iso7064/mod_11_2.py +++ b/stdnum/iso7064/mod_11_2.py @@ -57,6 +57,8 @@ def calc_check_digit(number: str) -> str: def validate(number: str) -> str: """Check whether the check digit is valid.""" + if not str(number).isascii(): + raise InvalidFormat() try: valid = checksum(number) == 1 except Exception: # noqa: B902 diff --git a/stdnum/iso7064/mod_97_10.py b/stdnum/iso7064/mod_97_10.py index 9df5bd3a..7769a7d1 100644 --- a/stdnum/iso7064/mod_97_10.py +++ b/stdnum/iso7064/mod_97_10.py @@ -56,6 +56,8 @@ def calc_check_digits(number: str) -> str: def validate(number: str) -> str: """Check whether the check digit is valid.""" + if not str(number).isascii(): + raise InvalidFormat() try: valid = checksum(number) == 1 except Exception: # noqa: B902 diff --git a/stdnum/verhoeff.py b/stdnum/verhoeff.py index ee1c3458..5dcbdbec 100644 --- a/stdnum/verhoeff.py +++ b/stdnum/verhoeff.py @@ -87,6 +87,8 @@ def validate(number: str) -> str: """Check if the number provided passes the Verhoeff checksum.""" if not bool(number): raise InvalidFormat() + if not str(number).isascii(): + raise InvalidFormat() try: valid = checksum(number) == 0 except Exception: # noqa: B902 diff --git a/tests/test_damm.doctest b/tests/test_damm.doctest index 10633683..c7b496f3 100644 --- a/tests/test_damm.doctest +++ b/tests/test_damm.doctest @@ -89,3 +89,14 @@ http://www.md-software.de/math/DAMM_Quasigruppen.txt 9 >>> damm.checksum('8169', table=table) 0 + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> damm.validate('\u0665\u0667\u0662\u0664') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> damm.is_valid('\u0665\u0667\u0662\u0664') +False diff --git a/tests/test_ie_pps.doctest b/tests/test_ie_pps.doctest index e8f8680c..142cc7bb 100644 --- a/tests/test_ie_pps.doctest +++ b/tests/test_ie_pps.doctest @@ -55,3 +55,14 @@ The validate() function should check with old format if last letter is W or T. '1234567TW' >>> pps.validate('1234567TT') '1234567TT' + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> pps.validate('\u0661\u0662\u0663\u0664\u0665\u0666\u0667WH') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> pps.is_valid('\u0661\u0662\u0663\u0664\u0665\u0666\u0667WH') +False diff --git a/tests/test_iso11649.doctest b/tests/test_iso11649.doctest index 73442625..d806567f 100644 --- a/tests/test_iso11649.doctest +++ b/tests/test_iso11649.doctest @@ -135,3 +135,14 @@ They should all be valid iso11649 structured creditor reference numbers ... ''' >>> [x for x in numbers.splitlines() if x and not stdnum.iso11649.is_valid(x)] [] + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> stdnum.iso11649.validate('RF\u0661\u0668 \u0665\u0663\u0669\u0660 \u0660\u0667\u0665\u0664 \u0667\u0660\u0663\u0664') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> stdnum.iso11649.is_valid('RF\u0661\u0668 \u0665\u0663\u0669\u0660 \u0660\u0667\u0665\u0664 \u0667\u0660\u0663\u0664') +False diff --git a/tests/test_iso7064.doctest b/tests/test_iso7064.doctest index ddb5444d..8e2659b7 100644 --- a/tests/test_iso7064.doctest +++ b/tests/test_iso7064.doctest @@ -77,3 +77,26 @@ range of 02 to 98 as is used in IBAN. '97' >>> mod_97_10.calc_check_digits('5335') '98' + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> mod_11_10.validate('\u0667\u0669\u0664\u0666\u0662\u0663') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> mod_11_10.is_valid('\u0667\u0669\u0664\u0666\u0662\u0663') +False +>>> mod_11_2.validate('\u0660\u0667\u0669\u0664\u0660') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> mod_11_2.is_valid('\u0660\u0667\u0669\u0664\u0660') +False +>>> mod_97_10.validate('\u0660\u0668\u0666\u0668\u0666\u0660\u0660\u0661\u0662\u0665\u0666\u0665\u0661\u0665\u0660\u0660\u0661\u0661\u0662\u0661\u0667\u0665\u0661') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> mod_97_10.is_valid('\u0660\u0668\u0666\u0668\u0666\u0660\u0660\u0661\u0662\u0665\u0666\u0665\u0661\u0665\u0660\u0660\u0661\u0661\u0662\u0661\u0667\u0665\u0661') +False diff --git a/tests/test_lei.doctest b/tests/test_lei.doctest index bc2d5a5b..3462c024 100644 --- a/tests/test_lei.doctest +++ b/tests/test_lei.doctest @@ -130,3 +130,14 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not lei.is_valid(x)] [] + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> lei.validate('\u0662\u0661\u0663\u0668\u0660\u0660KUD\u0668LAJWSQ\u0669D\u0661\u0665') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> lei.is_valid('\u0662\u0661\u0663\u0668\u0660\u0660KUD\u0668LAJWSQ\u0669D\u0661\u0665') +False diff --git a/tests/test_verhoeff.doctest b/tests/test_verhoeff.doctest index f23eb3dc..d8677813 100644 --- a/tests/test_verhoeff.doctest +++ b/tests/test_verhoeff.doctest @@ -83,3 +83,14 @@ False '1' >>> verhoeff.is_valid('123451') True + + +Non-ASCII digits (here ARABIC-INDIC DIGITs) are not valid and should be +rejected instead of being treated as the digits they look like. + +>>> verhoeff.validate('\u0661\u0664\u0662\u0668\u0665\u0667\u0660') +Traceback (most recent call last): + ... +InvalidFormat: ... +>>> verhoeff.is_valid('\u0661\u0664\u0662\u0668\u0665\u0667\u0660') +False