diff --git a/.coverage b/.coverage new file mode 100644 index 0000000..b7f8b2f Binary files /dev/null and b/.coverage differ diff --git a/barcode/codex.py b/barcode/codex.py index ea271f7..5ed6fb1 100644 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -113,7 +113,7 @@ class PZN7(Code39): def __init__(self, pzn: str, writer: BaseWriter | None = None) -> 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)