From d495438622e364f6f197a51422766dd4e5878d7e Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 17 Nov 2026 12:43:49 +0400 Subject: [PATCH] Use ASCII-only character checks in remaining format validation Regular expressions using \d and the str.isalpha() and str.isalnum() methods match characters outside of 0-9 and A-Z (e.g. ARABIC-INDIC DIGIT THREE or LATIN SMALL LETTER E WITH ACUTE), so numbers containing them were accepted as valid by br.cnpj, fi.hetu, is_.kennitala, ro.onrc, si.maticna, ad.nrt, es.cae and eu.banknote. Use explicit ASCII character classes and comparisons instead. Co-Authored-By: Claude Sonnet 5.5 --- stdnum/ad/nrt.py | 2 +- stdnum/br/cnpj.py | 2 +- stdnum/es/cae.py | 2 +- stdnum/eu/banknote.py | 7 ++++++- stdnum/fi/hetu.py | 4 ++-- stdnum/is_/kennitala.py | 4 ++-- stdnum/ro/onrc.py | 2 +- stdnum/si/maticna.py | 2 +- tests/test_ad_nrt.doctest | 6 ++++++ tests/test_br_cnpj.doctest | 6 ++++++ tests/test_es_cae.doctest | 6 ++++++ tests/test_eu_banknote.doctest | 6 ++++++ tests/test_fi_hetu.doctest | 6 ++++++ tests/test_is_kennitala.doctest | 6 ++++++ tests/test_ro_onrc.doctest | 6 ++++++ tests/test_si_maticna.doctest | 6 ++++++ 16 files changed, 63 insertions(+), 10 deletions(-) diff --git a/stdnum/ad/nrt.py b/stdnum/ad/nrt.py index 2d986796..d261f75d 100644 --- a/stdnum/ad/nrt.py +++ b/stdnum/ad/nrt.py @@ -66,7 +66,7 @@ def validate(number: str) -> str: number = compact(number) if len(number) != 8: raise InvalidLength() - if not number[0].isalpha() or not number[-1].isalpha(): + if not ('A' <= number[0] <= 'Z') or not ('A' <= number[-1] <= 'Z'): raise InvalidFormat() if not isdigits(number[1:-1]): raise InvalidFormat() diff --git a/stdnum/br/cnpj.py b/stdnum/br/cnpj.py index 27ed419e..b8bc15d2 100644 --- a/stdnum/br/cnpj.py +++ b/stdnum/br/cnpj.py @@ -47,7 +47,7 @@ # Minimal regex of valid characters -_cnpj_re = re.compile(r'^[\dA-Z]+$') +_cnpj_re = re.compile(r'^[0-9A-Z]+$') def compact(number: str) -> str: diff --git a/stdnum/es/cae.py b/stdnum/es/cae.py index 2cfb3407..8480e990 100644 --- a/stdnum/es/cae.py +++ b/stdnum/es/cae.py @@ -225,7 +225,7 @@ def validate(number: str) -> str: raise InvalidFormat() if not isdigits(number[9:12]): raise InvalidFormat() - if not number[12].isalpha(): + if not ('A' <= number[12] <= 'Z'): raise InvalidFormat() return number diff --git a/stdnum/eu/banknote.py b/stdnum/eu/banknote.py index 47f83ea6..63b3d6cf 100644 --- a/stdnum/eu/banknote.py +++ b/stdnum/eu/banknote.py @@ -34,10 +34,15 @@ from __future__ import annotations +import re + from stdnum.exceptions import * from stdnum.util import clean, isdigits +_prefix_re = re.compile(r'^[0-9A-Z]{2}$') + + def compact(number: str) -> str: """Convert the number to the minimal representation. This strips the number of any valid separators and removes surrounding whitespace.""" @@ -53,7 +58,7 @@ def checksum(number: str) -> int: def validate(number: str) -> str: """Check if the number is a valid banknote serial number.""" number = compact(number) - if not number[:2].isalnum() or not isdigits(number[2:]): + if not _prefix_re.match(number[:2]) or not isdigits(number[2:]): raise InvalidFormat() if len(number) != 12: raise InvalidLength() diff --git a/stdnum/fi/hetu.py b/stdnum/fi/hetu.py index d3772933..69e5673f 100644 --- a/stdnum/fi/hetu.py +++ b/stdnum/fi/hetu.py @@ -57,8 +57,8 @@ # Finnish personal identity codes are composed of date part, century # indicating sign, individual number and control character. # ddmmyyciiiC -_hetu_re = re.compile(r'^(?P[0123]\d)(?P[01]\d)(?P\d\d)' - r'(?P[-+ABCDEFYXWVU])(?P\d\d\d)' +_hetu_re = re.compile(r'^(?P[0123][0-9])(?P[01][0-9])(?P[0-9][0-9])' + r'(?P[-+ABCDEFYXWVU])(?P[0-9][0-9][0-9])' r'(?P[0-9ABCDEFHJKLMNPRSTUVWXY])$') diff --git a/stdnum/is_/kennitala.py b/stdnum/is_/kennitala.py index 31030123..538eb9cf 100644 --- a/stdnum/is_/kennitala.py +++ b/stdnum/is_/kennitala.py @@ -53,8 +53,8 @@ # organisations instead of birth date, the registration date is used, # and number 4 is added to the first digit. _kennitala_re = re.compile( - r'^(?P[01234567]\d)(?P[01]\d)(?P\d\d)' - r'(?P\d\d)(?P\d)' + r'^(?P[01234567][0-9])(?P[01][0-9])(?P[0-9][0-9])' + r'(?P[0-9][0-9])(?P[0-9])' r'(?P[09])$') diff --git a/stdnum/ro/onrc.py b/stdnum/ro/onrc.py index 5467b032..b0f7af70 100644 --- a/stdnum/ro/onrc.py +++ b/stdnum/ro/onrc.py @@ -58,7 +58,7 @@ # This pattern should match numbers that for some reason have a full date # as last field for the old format -_old_onrc_fulldate_re = re.compile(r'^([A-Z][0-9]+/[0-9]+/)\d{2}[.]\d{2}[.](\d{4})$') +_old_onrc_fulldate_re = re.compile(r'^([A-Z][0-9]+/[0-9]+/)[0-9]{2}[.][0-9]{2}[.]([0-9]{4})$') # This pattern should match all valid numbers in the old format _old_onrc_re = re.compile(r'^[A-Z][0-9]+/[0-9]+/[0-9]+$') diff --git a/stdnum/si/maticna.py b/stdnum/si/maticna.py index 28e472b3..37fac11c 100644 --- a/stdnum/si/maticna.py +++ b/stdnum/si/maticna.py @@ -76,7 +76,7 @@ def validate(number: str) -> str: raise InvalidLength() if not isdigits(number[:6]): raise InvalidFormat() - if not re.match(r'^([A-Za-z0-9]\d{2})?$', number[7:]): + if not re.match(r'^([A-Za-z0-9][0-9]{2})?$', number[7:]): raise InvalidFormat() if calc_check_digit(number) != number[6]: raise InvalidChecksum() diff --git a/tests/test_ad_nrt.doctest b/tests/test_ad_nrt.doctest index 254d1ef9..6846d295 100644 --- a/tests/test_ad_nrt.doctest +++ b/tests/test_ad_nrt.doctest @@ -136,3 +136,9 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not nrt.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> nrt.is_valid('U-132950-\u00e9') +False diff --git a/tests/test_br_cnpj.doctest b/tests/test_br_cnpj.doctest index bb9ca1f6..88c8a678 100644 --- a/tests/test_br_cnpj.doctest +++ b/tests/test_br_cnpj.doctest @@ -56,3 +56,9 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not cnpj.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> cnpj.is_valid('07.195.358/000\u0661-66') +False diff --git a/tests/test_es_cae.doctest b/tests/test_es_cae.doctest index ae3df35c..b5f96084 100644 --- a/tests/test_es_cae.doctest +++ b/tests/test_es_cae.doctest @@ -257,3 +257,9 @@ These should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not cae.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> cae.is_valid('ES00008V1488\u00e9') +False diff --git a/tests/test_eu_banknote.doctest b/tests/test_eu_banknote.doctest index 0e5585ac..e23454cd 100644 --- a/tests/test_eu_banknote.doctest +++ b/tests/test_eu_banknote.doctest @@ -69,3 +69,9 @@ These should all be valid serial numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not banknote.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> banknote.is_valid('P\u00e96007033744') +False diff --git a/tests/test_fi_hetu.doctest b/tests/test_fi_hetu.doctest index 88ecdd6a..49b9cdd4 100644 --- a/tests/test_fi_hetu.doctest +++ b/tests/test_fi_hetu.doctest @@ -108,3 +108,9 @@ compact() and format() don't do much special: '131052A308T' >>> hetu.format('131052a308t') '131052A308T' + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> hetu.is_valid('131052-30\u0668T') +False diff --git a/tests/test_is_kennitala.doctest b/tests/test_is_kennitala.doctest index 6e05b17b..d7a540fc 100644 --- a/tests/test_is_kennitala.doctest +++ b/tests/test_is_kennitala.doctest @@ -44,3 +44,9 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not kennitala.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> kennitala.is_valid('120174-33\u06699') +False diff --git a/tests/test_ro_onrc.doctest b/tests/test_ro_onrc.doctest index 93b7da7a..7e326eb0 100644 --- a/tests/test_ro_onrc.doctest +++ b/tests/test_ro_onrc.doctest @@ -244,3 +244,9 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not onrc.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> onrc.is_valid('J1/384/13.0\u0663.2015') +False diff --git a/tests/test_si_maticna.doctest b/tests/test_si_maticna.doctest index ccbf9e0d..bc35b5f2 100644 --- a/tests/test_si_maticna.doctest +++ b/tests/test_si_maticna.doctest @@ -119,3 +119,9 @@ These have been found online and should all be valid numbers. ... ''' >>> [x for x in numbers.splitlines() if x and not maticna.is_valid(x)] [] + + +Non-ASCII digits and letters are not valid and should be rejected. + +>>> maticna.is_valid('187603101\u0663') +False