Skip to content

Fix NG NIN rejecting valid numbers with a leading zero#2106

Open
jichaowang02-lang wants to merge 1 commit into
data-privacy-stack:mainfrom
jichaowang02-lang:fix/ng-nin-leading-zero
Open

Fix NG NIN rejecting valid numbers with a leading zero#2106
jichaowang02-lang wants to merge 1 commit into
data-privacy-stack:mainfrom
jichaowang02-lang:fix/ng-nin-leading-zero

Conversation

@jichaowang02-lang

@jichaowang02-lang jichaowang02-lang commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Change Description

NgNinRecognizer validated the Verhoeff checksum on int(value):

def __check_nin(self, value: str) -> bool:
    return (
        len(value) == 11
        and value.isnumeric()
        and self._is_verhoeff_number(int(value))   # int() drops a leading zero
    )

int("00000000005")5, and _is_verhoeff_number then runs str(...) on it, so the checksum is computed on a shortened digit string. An 11‑digit NIN that begins with 0 — e.g. 00000000005, a genuinely valid Verhoeff number — is checked on fewer than 11 digits and wrongly rejected (a false negative for the ~10% of the NIN space that starts with 0). The NIN spec places no restriction forbidding a leading zero.

Fix

Pass the digit string to the Verhoeff check instead of int(value), so all 11 digits are validated. Non‑leading‑zero NINs are unaffected.

Checklist

  • I have reviewed the contribution guidelines
  • I have added tests to cover my changes
  • All new and existing tests passed

Tests

$ pytest tests/test_ng_nin_recognizer.py -q
14 passed
  • New analyze-level case for a leading-zero NIN (VALID_NIN_LEADING_ZERO) — fails before the fix, passes after.
  • New direct Verhoeff regression asserting a leading-zero NIN is accepted.
  • Corrected the prior all-zeros unit test: it passed int(0) — a single "0", which is a valid Verhoeff number — rather than the 11-digit all-zero string (which is not valid), so it never exercised the all-zero NIN.

NgNinRecognizer validated the Verhoeff checksum on `int(value)`, which drops a
leading zero and shortens the digit string. An 11-digit NIN beginning with `0`
(e.g. `00000000005`, a genuinely valid Verhoeff number) was therefore checked
on only 10 digits and wrongly rejected — a false negative for ~10% of the NIN
space.

Pass the digit string straight to the Verhoeff check instead of `int(value)`,
so all 11 digits are validated. Non-leading-zero NINs are unaffected.

Adds an analyze-level case for a leading-zero NIN (fails before the fix) and a
direct Verhoeff regression; also corrects the prior all-zeros unit test, which
passed `int(0)` (a single "0", which is valid) rather than the 11-digit
all-zero string (which is not a valid Verhoeff number).
Copilot AI review requested due to automatic review settings June 27, 2026 02:11

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants