From 6c7a1c5fdb8b3c0aaa7ed755b3a6925651e6ce80 Mon Sep 17 00:00:00 2001 From: Davide Date: Thu, 17 Oct 2024 18:15:49 +0200 Subject: [PATCH 1/5] gh-125651: Fix UUID hex parsing with underscores --- Lib/test/test_uuid.py | 2 ++ Lib/uuid.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index e177464c00f7a6..b1450843f85ab0 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -232,6 +232,8 @@ def test_exceptions(self): # Badly formed hex strings. badvalue(lambda: self.uuid.UUID('')) badvalue(lambda: self.uuid.UUID('abc')) + badvalue(lambda: self.uuid.UUID("123_4567812345678123456781234567")) + badvalue(lambda: self.uuid.UUID("123_4567812345678123456781_23456")) badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567')) badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789')) badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678')) diff --git a/Lib/uuid.py b/Lib/uuid.py index 4d4f06cfc9ebbe..8db69383077b65 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -176,7 +176,7 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None, 'or int arguments must be given') if hex is not None: hex = hex.replace('urn:', '').replace('uuid:', '') - hex = hex.strip('{}').replace('-', '') + hex = hex.strip("{}").replace("-", "").replace("_", "") if len(hex) != 32: raise ValueError('badly formed hexadecimal UUID string') int = int_(hex, 16) From cde9e36a80f982fa4dbb3cfa97bf1acf97cdbd15 Mon Sep 17 00:00:00 2001 From: Davide Date: Fri, 18 Oct 2024 09:50:16 +0200 Subject: [PATCH 2/5] gh-125651: Use regex to avoid any unallowed character to passed to int --- Lib/test/test_uuid.py | 10 ++++++++-- Lib/uuid.py | 5 +++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index b1450843f85ab0..86e8d4ce555b28 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -232,11 +232,17 @@ def test_exceptions(self): # Badly formed hex strings. badvalue(lambda: self.uuid.UUID('')) badvalue(lambda: self.uuid.UUID('abc')) - badvalue(lambda: self.uuid.UUID("123_4567812345678123456781234567")) - badvalue(lambda: self.uuid.UUID("123_4567812345678123456781_23456")) + badvalue(lambda: self.uuid.UUID('123_4567812345678123456781234567')) + badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456')) + badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456')) badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567')) badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789')) badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678')) + badvalue(lambda: self.uuid.UUID('0x123456781234567812345678z23456')) + badvalue(lambda: self.uuid.UUID('0X123456781234567812345678z23456')) + badvalue(lambda: self.uuid.UUID('+123456781234567812345678z234567')) + badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z23456 ')) + badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z2345 ')) # Badly formed bytes. badvalue(lambda: self.uuid.UUID(bytes='abc')) diff --git a/Lib/uuid.py b/Lib/uuid.py index 8db69383077b65..b6f8f2b215c148 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -45,6 +45,7 @@ """ import os +import re import sys from enum import Enum, _simple_enum @@ -176,8 +177,8 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None, 'or int arguments must be given') if hex is not None: hex = hex.replace('urn:', '').replace('uuid:', '') - hex = hex.strip("{}").replace("-", "").replace("_", "") - if len(hex) != 32: + hex = hex.strip('{}').replace('-', '') + if not re.fullmatch(r'[0-9A-Fa-f]{32}', hex): raise ValueError('badly formed hexadecimal UUID string') int = int_(hex, 16) if bytes_le is not None: From 6719b851019138eb1130c1aee5aec1c4c53f1b16 Mon Sep 17 00:00:00 2001 From: Davide Date: Fri, 18 Oct 2024 11:03:46 +0200 Subject: [PATCH 3/5] gh-125651: Remove duplicated check in test --- Lib/test/test_uuid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 86e8d4ce555b28..07654dff84e7b4 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -234,7 +234,6 @@ def test_exceptions(self): badvalue(lambda: self.uuid.UUID('abc')) badvalue(lambda: self.uuid.UUID('123_4567812345678123456781234567')) badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456')) - badvalue(lambda: self.uuid.UUID('123_4567812345678123456781_23456')) badvalue(lambda: self.uuid.UUID('1234567812345678123456781234567')) badvalue(lambda: self.uuid.UUID('123456781234567812345678123456789')) badvalue(lambda: self.uuid.UUID('123456781234567812345678z2345678')) From 965db0141f1d097410051b2de6573cefec4a4088 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 18 Oct 2024 09:45:35 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst diff --git a/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst b/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst new file mode 100644 index 00000000000000..e5964c83fd8712 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst @@ -0,0 +1 @@ +Fix parsing of HEX encoded UUID string From 5961212eafdfdba1f70c03b35ce3f110ef647f29 Mon Sep 17 00:00:00 2001 From: Davide Date: Fri, 18 Oct 2024 12:29:06 +0200 Subject: [PATCH 5/5] gh-125651: Add test for unicode sequence and adjust news message --- Lib/test/test_uuid.py | 1 + .../next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 07654dff84e7b4..4717b3ff28add2 100755 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -242,6 +242,7 @@ def test_exceptions(self): badvalue(lambda: self.uuid.UUID('+123456781234567812345678z234567')) badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z23456 ')) badvalue(lambda: self.uuid.UUID(' 123456781234567812345678z2345 ')) + badvalue(lambda: self.uuid.UUID('\uff10123456781234567812345678z234567')) # Badly formed bytes. badvalue(lambda: self.uuid.UUID(bytes='abc')) diff --git a/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst b/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst index e5964c83fd8712..00c79d2dd87366 100644 --- a/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst +++ b/Misc/NEWS.d/next/Library/2024-10-18-09-45-34.gh-issue-125651.M0wSAS.rst @@ -1 +1 @@ -Fix parsing of HEX encoded UUID string +Fix HEX parsing of :class:`uuid.UUID`.