From 30bdb6a70f838ea32cb554cfc62bfcdb4cd57300 Mon Sep 17 00:00:00 2001 From: amalsaidov <58995855+amalsaidov@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:14:20 +0700 Subject: [PATCH] Reject non-ASCII Unicode digits in numeric barcodes str.isdigit() returns True for non-ASCII characters such as superscript two (U+00B2) or the Arabic-Indic digits (U+0660..U+0669), so the EAN, UPC, ITF and PZN input validation accepted them. These then either crashed with a raw ValueError when int() rejected the character, or were silently encoded into a corrupt barcode whose string form contained non-ASCII characters. Require the input to be ASCII as well as numeric so these are rejected consistently with the documented IllegalCharacterError. --- .coverage | Bin 0 -> 53248 bytes barcode/codex.py | 2 +- barcode/ean.py | 6 ++++-- barcode/itf.py | 2 +- barcode/upc.py | 2 +- docs/changelog.rst | 3 +++ tests/test_init.py | 18 ++++++++++++++++++ 7 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 .coverage diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..b7f8b2fbf248b88f850c6676eb11d2ceb3c285b3 GIT binary patch literal 53248 zcmeI4Z)_Y#6~JeA?~nI(_vUQJy`~|}BA~X3eOD)e{4r45iJ@((gwiwu0ln;<_v}r5 zyUy-jY*SgurIAvVFKs16kP1WssR*G80Tp~eYDGas;X_j)LWN4>R!WEvAt}ODiy_CG z{d0Ft;?%h3b|9yt5t7XfsXs)`EWC+KRAyp-Wi11e6 z9d1d;#KIj&xt9ELS&`(v^?QZxC351+gg&I)M*HP)`7P;bd5idac2C z)9RFNt7T(SR}FK_C~2-?j;rp39+s{;`WRcY;25l0tYFTQXSsqg22!p*4l1T>qoUcf z>cjeMpI0EL&DCdI{{Se_jd7EePo-!yZS@sySzG!P^X+n4URISm%k_C3oxPEJGtCDvubSNbzU>}1PZUC>fHK1!BYtTMlGN`MCPE+_Al0or6xD3XbR*1DJ$f^cm z&t|h)DFX8S)yfp>9FXuUcCFm^ps^pgZ?4go&Gs(o&Ja24QD3TR8V9O+qx1XJ zrev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RvROTuc-<9eKT8f3seHSr&M* z=?^h->hyrXp8t2yql7L(?Ei0ilzx+5fO`SY(C6r@^bCELzCusHJ%N+-Df(h-?U+Xb zNB{{S0VIF~kN^@u0!RP}AOR$R1fmgOfj<}P@RE4Dm$YZRgi)V&0Z{x1dsp{Kmter2_OL^ zfCP{L5 None: pzn = pzn[: self.digits] - if not pzn.isdigit(): + if not (pzn.isascii() and pzn.isdigit()): raise IllegalCharacterError("PZN can only contain numbers.") if len(pzn) != self.digits: raise NumberOfDigitsError( diff --git a/barcode/ean.py b/barcode/ean.py index 85a4e4a..713dea5 100644 --- a/barcode/ean.py +++ b/barcode/ean.py @@ -54,7 +54,8 @@ def __init__( no_checksum: bool = False, guardbar: bool = False, ) -> None: - if not ean[: self.digits].isdigit(): + base_candidate = ean[: self.digits] + if not (base_candidate.isascii() and base_candidate.isdigit()): raise IllegalCharacterError(f"EAN code can only contain numbers {ean}.") if len(ean) < self.digits: @@ -65,7 +66,8 @@ def __init__( base = ean[: self.digits] if no_checksum: # Use the thirteenth digit if given in parameter, otherwise pad with zero - if len(ean) > self.digits and ean[self.digits].isdigit(): + check_digit = ean[self.digits : self.digits + 1] + if check_digit.isascii() and check_digit.isdigit(): last = int(ean[self.digits]) else: last = 0 diff --git a/barcode/itf.py b/barcode/itf.py index 0bb0a0a..a1b6db2 100644 --- a/barcode/itf.py +++ b/barcode/itf.py @@ -41,7 +41,7 @@ def __init__( narrow: int = 2, wide: int = 5, ) -> None: - if not code.isdigit(): + if not (code.isascii() and code.isdigit()): raise IllegalCharacterError("ITF code can only contain numbers.") # Length must be even, prepend 0 if necessary if len(code) % 2 != 0: diff --git a/barcode/upc.py b/barcode/upc.py index 42938d5..38a0140 100644 --- a/barcode/upc.py +++ b/barcode/upc.py @@ -48,7 +48,7 @@ def __init__( """ self.ean = make_ean upc = upc[: self.digits] - if not upc.isdigit(): + if not (upc.isascii() and upc.isdigit()): raise IllegalCharacterError("UPC code can only contain numbers.") if len(upc) != self.digits: raise NumberOfDigitsError( diff --git a/docs/changelog.rst b/docs/changelog.rst index 9a15b52..1142b11 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -9,6 +9,9 @@ v0.17.0 barcodes. (#251) * Fix ISBN-10 and ISSN barcodes being truncated to 10 and 8 digits. * Fix Code128 emitting different (but valid) output depending on invocation. +* Reject non-ASCII Unicode digits (such as ``²`` or Arabic-Indic digits) in + numeric barcodes with ``IllegalCharacterError`` instead of crashing with a + ``ValueError`` or silently producing a corrupt barcode. v0.16.1 ~~~~~~~ diff --git a/tests/test_init.py b/tests/test_init.py index b78703a..47943a4 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -42,3 +42,21 @@ def test_generate_with_bytesio() -> None: barcode.generate("ean13", "123455559121112", output=bio) # XXX: File is not 100% deterministic; needs to be addressed at some point. # assert len(bio.getvalue()) == 6127 # noqa: ERA001 + + +# Non-ASCII Unicode characters for which ``str.isdigit()`` is True but which are +# not ASCII 0-9: superscript two and the Arabic-Indic digit three. +NON_ASCII_DIGITS = ["\N{SUPERSCRIPT TWO}", "\N{ARABIC-INDIC DIGIT THREE}"] + + +@pytest.mark.parametrize( + ("code_type", "length"), + [("ean13", 12), ("ean8", 7), ("ean14", 13), ("upca", 11), ("itf", 6), ("pzn", 6)], +) +@pytest.mark.parametrize("char", NON_ASCII_DIGITS) +def test_non_ascii_digits_rejected(code_type: str, length: int, char: str) -> None: + # ``str.isdigit()`` accepts non-ASCII Unicode digits, so these used to either + # crash with a raw ValueError or silently produce a corrupt barcode instead + # of raising the documented IllegalCharacterError. + with pytest.raises(barcode.errors.IllegalCharacterError): + barcode.get_barcode(code_type, char * length)