Skip to content

vatin: reject a duplicated country code prefix (#420)#500

Open
CedricConday wants to merge 1 commit into
arthurdejong:masterfrom
CedricConday:fix/420-vatin-duplicate-country-code
Open

vatin: reject a duplicated country code prefix (#420)#500
CedricConday wants to merge 1 commit into
arthurdejong:masterfrom
CedricConday:fix/420-vatin-duplicate-country-code

Conversation

@CedricConday

Copy link
Copy Markdown

Closes #420

vatin.is_valid() accepts a VAT number with a doubled country code, while stdnum.eu.vat correctly rejects it:

>>> stdnum.vatin.is_valid('BE 0308.357.159')
True
>>> stdnum.vatin.is_valid('BE BE 0308.357.159')
True      # should be False
>>> stdnum.eu.vat.is_valid('BE BE 0308.357.159')
False

Cause

vatin.validate() stripped the leading country code itself (module.validate(number[2:])) and then handed the remainder to the country module, which strips its own optional country-code prefix again. For BE BE 0308.357.159 both strips fired, leaving the valid national number 0308357159. Countries whose module doesn't strip a prefix (e.g. BR) weren't affected, which is why it reproduced for BE/NL but not MX.

Fix

Validate the full number with the country module (it strips its own prefix exactly once), mirroring stdnum.eu.vat. Only fall back to stripping the country code for modules that don't recognise it — and guard that fallback so a doubled prefix (even written BE BE … with whitespace) is not stripped a second time.

Tests

Added doctests in tests/test_vatin.doctest for the doubled-prefix case (with and without whitespace). They fail on master and pass with the fix. The full test suite stays green (405 passed, 9 network-skipped), including the existing multi-country vatin doctests (FR/DE/BR/EL→GR/CHE/XI/EU).

vatin.validate() stripped the leading country code itself and then passed
the remainder to the country module, which strips its own optional country
code prefix again. For a doubled prefix such as 'BE BE 0308.357.159' both
strips fired, leaving a valid national number, so the VATIN validated even
though stdnum.eu.vat correctly rejects it.

Validate the full number with the country module (which strips its own
prefix once), mirroring stdnum.eu.vat, and only fall back to stripping the
country code for modules that do not recognise it - guarding that fallback
so a doubled prefix is not stripped a second time.

Closes arthurdejong#420.
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.

Wrong VATIN validation on duplicate country code

1 participant