Skip to content

Commit 1aab13d

Browse files
committed
Reject trailing newlines in MAC addresses and regional identifiers
1 parent 7bffa5e commit 1aab13d

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎src/validators/i18n/fi.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def fi_business_id(value: str, /):
5454
"""
5555
if not value:
5656
return False
57-
if not re.match(_business_id_pattern(), value):
57+
if not re.fullmatch(_business_id_pattern(), value):
5858
return False
5959
factors = [7, 9, 10, 5, 8, 4, 2]
6060
numbers = map(int, value[:7])
@@ -92,7 +92,7 @@ def fi_ssn(value: str, /, *, allow_temporal_ssn: bool = True):
9292
if not value:
9393
return False
9494
ssn_check_marks = "0123456789ABCDEFHJKLMNPRSTUVWXY"
95-
if not (result := re.match(_ssn_pattern(ssn_check_marks), value)):
95+
if not (result := re.fullmatch(_ssn_pattern(ssn_check_marks), value)):
9696
return False
9797
gd = result.groupdict()
9898
checksum = int(gd["date"] + gd["serial"])

‎src/validators/i18n/fr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def fr_ssn(value: str):
9393
"""
9494
if not value:
9595
return False
96-
matched = re.match(_ssn_pattern(), value)
96+
matched = re.fullmatch(_ssn_pattern(), value)
9797
if not matched:
9898
return False
9999
groups = list(matched.groups())

‎src/validators/i18n/ind.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def ind_aadhar(value: str):
2424
(Literal[True]): If `value` is a valid aadhar card number.
2525
(ValidationError): If `value` is an invalid aadhar card number.
2626
"""
27-
return re.match(r"^[2-9]{1}\d{3}\s\d{4}\s\d{4}$", value)
27+
return re.fullmatch(r"^[2-9]{1}\d{3}\s\d{4}\s\d{4}$", value)
2828

2929

3030
@validator

‎src/validators/mac_address.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ def mac_address(value: str, /):
3333
if ":" in value and "-" in value:
3434
return False
3535

36-
return re.match(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value) if value else False
36+
return re.fullmatch(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value) if value else False

‎tests/test_complete_values.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Validate complete MAC addresses and regional identifiers."""
2+
3+
from typing import Callable
4+
5+
import pytest
6+
7+
from validators import ValidationError, mac_address
8+
from validators.i18n.fi import fi_business_id, fi_ssn
9+
from validators.i18n.fr import fr_ssn
10+
from validators.i18n.ind import ind_aadhar
11+
12+
13+
@pytest.mark.parametrize(
14+
"validate,value",
15+
[
16+
(mac_address, "01:23:45:67:ab:CD"),
17+
(mac_address, "01-23-45-67-ab-CD"),
18+
(fi_business_id, "0112038-9"),
19+
(fi_ssn, "010101-0101"),
20+
(fr_ssn, "1 84 12 76 451 089 46"),
21+
(fr_ssn, "1 84 12 76 451 089"),
22+
(ind_aadhar, "3675 9834 6015"),
23+
],
24+
)
25+
@pytest.mark.parametrize("suffix", ["", "\n", "\r\n", " ", "\nextra"])
26+
def test_complete_identifier(validate: Callable[[str], object], value: str, suffix: str):
27+
"""Accept the identifier alone and reject any trailing characters."""
28+
if suffix:
29+
assert isinstance(validate(value + suffix), ValidationError)
30+
else:
31+
assert validate(value) is True

0 commit comments

Comments
 (0)