From b9967f84e0445ba3381aa939040e097c4549b9b8 Mon Sep 17 00:00:00 2001 From: Diogo Zani Date: Sat, 28 Nov 2026 12:51:11 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20rejeita=20d=C3=ADgitos=20n=C3=A3o=20ASCI?= =?UTF-8?q?I=20na=20valida=C3=A7=C3=A3o=20de=20CPF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + README.md | 2 +- README_EN.md | 2 +- brutils/cpf.py | 17 ++++++++++++++--- tests/test_cpf.py | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3f..84e8debd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- `is_valid_cpf` aceitava dígitos de outros alfabetos, como `٩`, e levantava `ValueError` com algarismos sobrescritos, como `²` [#809](https://github.com/brazilian-utils/python/issues/809) - Cálculo do dígito verificador em `is_valid_legal_process` e `generate_legal_process`, que rejeitava números de processo reais e gerava números inválidos [#785](https://github.com/brazilian-utils/python/pull/785) ## [2.5.0] - 2026-06-30 diff --git a/README.md b/README.md index 5ba23fd5..f2b74588 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ ela apenas valida o formato da string. Argumentos: -- cpf (str): O CPF a ser validado, uma string de 11 dígitos +- cpf (str): O CPF a ser validado, uma string de 11 dígitos de 0 a 9 Retorna: diff --git a/README_EN.md b/README_EN.md index 0b9167a8..e5934a83 100644 --- a/README_EN.md +++ b/README_EN.md @@ -130,7 +130,7 @@ validates the format of the string. Args: -- cpf (str): The CPF to be validated, a 11-digit string +- cpf (str): The CPF to be validated, an 11-digit string of ASCII digits (0-9) Returns: diff --git a/brutils/cpf.py b/brutils/cpf.py index c2e418ae..2385b2e7 100644 --- a/brutils/cpf.py +++ b/brutils/cpf.py @@ -71,7 +71,12 @@ def display(cpf: str) -> str: backward compatibility. """ - if not cpf.isdigit() or len(cpf) != 11 or len(set(cpf)) == 1: + if ( + not cpf.isascii() + or not cpf.isdigit() + or len(cpf) != 11 + or len(set(cpf)) == 1 + ): return None return "{}.{}.{}-{}".format(cpf[:3], cpf[3:6], cpf[6:9], cpf[9:]) @@ -133,7 +138,12 @@ def validate(cpf: str) -> bool: backward compatibility. """ - if not cpf.isdigit() or len(cpf) != 11 or len(set(cpf)) == 1: + if ( + not cpf.isascii() + or not cpf.isdigit() + or len(cpf) != 11 + or len(set(cpf)) == 1 + ): return False return all(_hashdigit(cpf, i + 10) == int(v) for i, v in enumerate(cpf[9:])) @@ -148,7 +158,8 @@ def is_valid(cpf: str) -> bool: validates the format of the string. Args: - cpf (str): The CPF to be validated, a 11-digit string + cpf (str): The CPF to be validated, an 11-digit string of + ASCII digits (0-9) Returns: bool: True if the checksum digits match the base number, diff --git a/tests/test_cpf.py b/tests/test_cpf.py index 84f49e70..ef24d759 100644 --- a/tests/test_cpf.py +++ b/tests/test_cpf.py @@ -31,11 +31,19 @@ def test_display(self): self.assertIsNone(display("0000000000a")) self.assertIsNone(display("000000000000")) + # When CPF contains non-ASCII digits, returns None + self.assertIsNone(display("52٩٩8224725")) + self.assertIsNone(display("0000000001¹")) + def test_validate(self): self.assertIs(validate("52513127765"), True) self.assertIs(validate("52599927765"), True) self.assertIs(validate("00000000000"), False) + # When CPF contains non-ASCII digits, returns False + self.assertIs(validate("525٩٩٩27765"), False) + self.assertIs(validate("5251312776" + "⁵"), False) + def test_is_valid(self): # When cpf is not string, returns False self.assertIs(is_valid(1), False) @@ -63,6 +71,15 @@ def test_is_valid(self): # False self.assertIs(is_valid("11144477732"), False) + # When CPF contains digits from other alphabets, returns False + self.assertIs(is_valid("52٩٩8224725"), False) + self.assertIs(is_valid("11144477735"), False) + + # When CPF contains superscript digits, returns False instead of + # raising ValueError + self.assertIs(is_valid("1114447773" + "⁵"), False) + self.assertIs(is_valid("²" * 11), False) + # When cpf is valid self.assertIs(is_valid("11144477735"), True) self.assertIs(is_valid("11111111200"), True)