From 1b6e72fb2b329309ca7242ee86a08eb100729083 Mon Sep 17 00:00:00 2001 From: itzzdev09 Date: Tue, 17 Nov 2026 13:00:40 +0530 Subject: [PATCH] Require ASCII digits in French department numbers fr_department() converted string input with int(), which also accepts surrounding whitespace, a sign, underscores and non-ASCII decimal digits, so values such as " 75", "+75", "7_5" or fullwidth "75" validated. Match the string against [0-9]+ before converting it. --- src/validators/i18n/fr.py | 6 +++--- tests/i18n/test_fr.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/validators/i18n/fr.py b/src/validators/i18n/fr.py index 5552ef61..0d905dd4 100644 --- a/src/validators/i18n/fr.py +++ b/src/validators/i18n/fr.py @@ -57,10 +57,10 @@ def fr_department(value: typing.Union[str, int]): if isinstance(value, str): if value in ("2A", "2B"): # Corsica return True - try: - value = int(value) - except ValueError: + # int() also takes signs, underscores, surrounding whitespace and non-ASCII digits + if not re.fullmatch(r"[0-9]+", value): return False + value = int(value) return 1 <= value <= 19 or 21 <= value <= 95 or 971 <= value <= 976 # Overseas departments diff --git a/tests/i18n/test_fr.py b/tests/i18n/test_fr.py index f25c87c5..9a65895a 100644 --- a/tests/i18n/test_fr.py +++ b/tests/i18n/test_fr.py @@ -80,6 +80,12 @@ def test_returns_true_on_valid_department(value: Union[str, int]): ("96",), ("20",), (20,), + (" 75",), + ("75\n",), + ("+75",), + ("7_5",), + ("75",), # fullwidth digits + ("٧٥",), # Arabic-Indic digits ], ) def test_returns_failed_validation_on_invalid_department(value: Union[str, int]):