bpo-39991: Enhance uuid parser for MAC address (GH-19045)

Reject valid IPv6 addresses which doesn't contain "::" but have
a length of 17 characters.
This commit is contained in:
Victor Stinner 2020-03-17 18:36:44 +01:00 committed by GitHub
parent 5b1ef200d3
commit ebf6bb9f5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 31 deletions

View file

@ -679,6 +679,58 @@ class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
class BaseTestInternals:
_uuid = py_uuid
def check_parse_mac(self, aix):
if not aix:
patch = mock.patch.multiple(self.uuid,
_MAC_DELIM=b':',
_MAC_OMITS_LEADING_ZEROES=False)
else:
patch = mock.patch.multiple(self.uuid,
_MAC_DELIM=b'.',
_MAC_OMITS_LEADING_ZEROES=True)
with patch:
# Valid MAC addresses
if not aix:
tests = (
(b'52:54:00:9d:0e:67', 0x5254009d0e67),
(b'12:34:56:78:90:ab', 0x1234567890ab),
)
else:
# AIX format
tests = (
(b'fe.ad.c.1.23.4', 0xfead0c012304),
)
for mac, expected in tests:
self.assertEqual(self.uuid._parse_mac(mac), expected)
# Invalid MAC addresses
for mac in (
b'',
# IPv6 addresses with same length than valid MAC address
# (17 characters)
b'fe80::5054:ff:fe9',
b'123:2:3:4:5:6:7:8',
# empty 5rd field
b'52:54:00:9d::67',
# only 5 fields instead of 6
b'52:54:00:9d:0e'
# invalid character 'x'
b'52:54:00:9d:0e:6x'
# dash separator
b'52-54-00-9d-0e-67',
):
if aix:
mac = mac.replace(b':', b'.')
with self.subTest(mac=mac):
self.assertIsNone(self.uuid._parse_mac(mac))
def test_parse_mac(self):
self.check_parse_mac(False)
def test_parse_mac_aix(self):
self.check_parse_mac(True)
def test_find_under_heading(self):
data = '''\
Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll