Skip to content

Commit 8eb3521

Browse files
committed
vatin: reject a duplicated country code prefix (#420)
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 #420.
1 parent 006192e commit 8eb3521

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

‎stdnum/vatin.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,29 @@ def validate(number: str) -> str:
8787
This performs the country-specific check for the number.
8888
"""
8989
number = clean(number, '').strip()
90-
module = _get_cc_module(number[:2])
90+
cc = number[:2].upper()
91+
module = _get_cc_module(cc)
9192
try:
92-
return number[:2].upper() + module.validate(number[2:])
93+
# Most country modules accept and strip the optional country code
94+
# prefix themselves, so the number can be validated as it is.
95+
result = module.validate(number)
96+
if not result.upper().startswith(cc):
97+
result = cc + result
9398
except ValidationError:
94-
return module.validate(number)
99+
# Other country modules only accept the national number, so retry
100+
# with the country code prefix removed.
101+
national = number[2:]
102+
result = module.validate(national)
103+
# If the country module stripped a second country code off the
104+
# national number then the prefix was duplicated and the number is
105+
# not valid (see #420). A national number that legitimately starts
106+
# with the country code, such as the Mexican RFC MXDE111111GR2,
107+
# keeps its prefix when compacted and is accepted.
108+
if (re.sub(r'[^0-9A-Za-z]', '', national)[:2].upper() == cc and
109+
not module.compact(national).upper().startswith(cc)):
110+
raise InvalidFormat()
111+
result = cc + result
112+
return result
95113

96114

97115
def is_valid(number: str) -> bool:

‎tests/test_vatin.doctest‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,26 @@ True
104104
'EU191849184'
105105

106106

107-
Check seemingly double country codes are handled correctly:
107+
A duplicated country code prefix should not be accepted (#420). This used to
108+
pass because the country code was stripped twice, once here and once by the
109+
country module:
110+
111+
>>> vatin.is_valid('BE 0308.357.159')
112+
True
113+
>>> vatin.is_valid('BE BE 0308.357.159')
114+
False
115+
>>> vatin.is_valid('BEBE0308357159')
116+
False
117+
>>> vatin.validate('BE BE 0308.357.159')
118+
Traceback (most recent call last):
119+
...
120+
InvalidFormat: ...
121+
122+
123+
Check seemingly double country codes are handled correctly. A national number
124+
may legitimately start with the country code, in which case it is kept:
108125

109126
>>> vatin.validate('MX MXDE 111111 GR2') # the second "MX" is part of the number
110127
'MXMXDE111111GR2'
128+
>>> vatin.validate('MXDE 111111 GR2') # here "MX" is only the country code
129+
'MXDE111111GR2'

0 commit comments

Comments
 (0)