Skip to content

Reject non-ASCII Unicode digits in numeric barcodes - #275

Open
amalsaidov wants to merge 1 commit into
WhyNotHugo:mainfrom
amalsaidov:fix-non-ascii-digit-validation
Open

amalsaidov wants to merge 1 commit into
WhyNotHugo:mainfrom
amalsaidov:fix-non-ascii-digit-validation

Conversation

@amalsaidov

Copy link
Copy Markdown

Problem

str.isdigit() returns True for many non-ASCII characters, e.g. superscript two (², U+00B2) and the Arabic-Indic digits (٠–٩, U+0660–U+0669). The numeric barcode classes validate their input with a bare .isdigit() check, so these characters pass validation and then:

  • crash with an undocumented ValueError when int() later rejects the character (superscripts/circled digits), instead of the documented IllegalCharacterError; or
  • are silently encoded into a corrupt barcode whose str() form contains non-ASCII characters (Arabic-Indic digits, which int() happens to accept).
>>> from barcode import EAN13
>>> str(EAN13("٣" * 12))      # Arabic-Indic three
'٣٣٣٣٣٣٣٣٣٣٣٣8'               # silently accepted, corrupt
>>> EAN13("12345678901²")     # superscript two
ValueError: invalid literal for int() with base 10: '²'

The same pattern affects EAN-13/8/14, UPC-A, ITF and PZN.

Fix

Require the input to be ASCII in addition to numeric in each validator, so non-ASCII digits are rejected consistently with the documented IllegalCharacterError.

Tests

Added a parametrized regression test (test_non_ascii_digits_rejected) covering EAN-13/8/14, UPC-A, ITF and PZN with both a superscript digit and an Arabic-Indic digit. The test fails on main (raw ValueError or no error) and passes with the fix. Full suite green, ruff clean.

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.
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.

1 participant