Skip to content

Fix EsNieRecognizer.validate_result raising instead of returning False#2146

Merged
SharonHart merged 1 commit into
data-privacy-stack:mainfrom
MohamedAklamaash:fix/es-nie-validate-result-isdigit
Jul 7, 2026
Merged

Fix EsNieRecognizer.validate_result raising instead of returning False#2146
SharonHart merged 1 commit into
data-privacy-stack:mainfrom
MohamedAklamaash:fix/es-nie-validate-result-isdigit

Conversation

@MohamedAklamaash

@MohamedAklamaash MohamedAklamaash commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #2145.

isdigit was referenced without being called, so the digit guard in validate_result was always truthy and never actually checked anything. The recognizer's default pattern already limits the middle characters to digits, so this stayed hidden in normal use, but the constructor explicitly accepts a custom patterns argument, and a looser pattern lets non-digit text reach the checksum line further down and raise a ValueError instead of returning False.

The fix is one character, calling isdigit() instead of referencing it.

Added a regression test that builds the recognizer with a custom pattern loose enough to trigger the old crash, and asserts analyze() now returns an empty list instead of raising.

isdigit was referenced without being called, so the digit guard was
always truthy and never actually checked anything. Under the
recognizer's default pattern this stays hidden since the middle
characters are already constrained to digits, but a custom patterns
argument (which the constructor explicitly supports) can let non-digit
text reach the checksum line further down and raise a ValueError
instead of returning False.

Fixes data-privacy-stack#2145

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a correctness bug in the Spain NIE predefined recognizer where validate_result() could raise ValueError (instead of returning False) when the recognizer is instantiated with a looser custom regex pattern.

Changes:

  • Corrected the digit-guard in EsNieRecognizer.validate_result by calling isdigit() instead of referencing the method.
  • Added a regression test which configures EsNieRecognizer with a custom loose pattern and asserts analyze() returns no results (and does not crash) on non-digit middle segments.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
presidio-analyzer/presidio_analyzer/predefined_recognizers/country_specific/spain/es_nie_recognizer.py Fixes validate_result() digit validation so invalid matches are rejected without exceptions.
presidio-analyzer/tests/test_es_nie_recognizer.py Adds regression coverage for the custom-pattern crash scenario described in #2145.

@SharonHart SharonHart merged commit 16ab0fd into data-privacy-stack:main Jul 7, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EsNieRecognizer.validate_result can raise ValueError instead of returning False

3 participants