fix: reject MAC address octets with a sign or whitespace (#424)

MacAddress.validate_mac_address parsed each octet with int(part, 16),
which also accepts a leading +/- sign and surrounding whitespace. As a
result strings like -a:-b:-c:-d:-e:-f, +a:+b:+c:+d:+e:+f and
" a: b: c: d: e: f" were accepted, with the sign/whitespace variants
silently normalized into a valid-looking MAC the caller never supplied.

Require both characters of each octet to be hex digits before the int()
conversion so malformed octets raise the mac_address_format error like
other invalid input.
This commit is contained in:
dualfroz committed 2026-09-06 00:23:06 +01:00
1 parent dfa8240f1f
commit 996db5006c
3 files changed
+14 -1

No files matched your search

+5
View File
@@ -56,6 +56,11 @@ class Network(BaseModel):
('00.00.5e.00.53.01.', None, False), # Extra separator at the end
('00:00:5e:00:53:', None, False), # Incomplete MAC address
(float(12345678910111213), None, False),
# int(..., 16) accepts a sign or whitespace; a MAC octet must not.
('-a:-b:-c:-d:-e:-f', None, False),
('+a:+b:+c:+d:+e:+f', None, False),
(' a: b: c: d: e: f', None, False),
('-aaa.-bbb.-ccc', None, False),
],
)
def test_format_for_mac_address(mac_address: Any, result: str, valid: bool):