Skip to content

Commit cde9e36

Browse files
committed
gh-125651: Use regex to avoid any unallowed character to passed to int
1 parent 6c7a1c5 commit cde9e36

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

Lib/test/test_uuid.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,17 @@ def test_exceptions(self):
232232
# Badly formed hex strings.
233233
badvalue(lambda: self.uuid.UUID(''))
234234
badvalue(lambda: self.uuid.UUID('abc'))
235-
badvalue(lambda: self.uuid.UUID("123_4567812345678123456781234567"))
236-
badvalue(lambda: self.uuid.UUID("123_4567812345678123456781_23456"))
235+
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781234567'))
236+
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456'))
237+
badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456'))
237238
badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567'))
238239
badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789'))
239240
badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678'))
241+
badvalue(lambda: self.uuid.UUID('0x123456781234567812345678z23456'))
242+
badvalue(lambda: self.uuid.UUID('0X123456781234567812345678z23456'))
243+
badvalue(lambda: self.uuid.UUID('+123456781234567812345678z234567'))
244+
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z23456 '))
245+
badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z2345 '))
240246

241247
# Badly formed bytes.
242248
badvalue(lambda: self.uuid.UUID(bytes='abc'))

Lib/uuid.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"""
4646

4747
import os
48+
import re
4849
import sys
4950

5051
from enum import Enum, _simple_enum
@@ -176,8 +177,8 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
176177
'or int arguments must be given')
177178
if hex is not None:
178179
hex = hex.replace('urn:', '').replace('uuid:', '')
179-
hex = hex.strip("{}").replace("-", "").replace("_", "")
180-
if len(hex) != 32:
180+
hex = hex.strip('{}').replace('-', '')
181+
if not re.fullmatch(r'[0-9A-Fa-f]{32}', hex):
181182
raise ValueError('badly formed hexadecimal UUID string')
182183
int = int_(hex, 16)
183184
if bytes_le is not None:

0 commit comments

Comments
 (0)