From a6d587eb207f419d92c9e0fdc8a149697b3f1460 Mon Sep 17 00:00:00 2001 From: Stefan Hoelzl <1478183+stefanhoelzl@users.noreply.github.com> Date: Fri, 5 Apr 2024 21:29:48 +0200 Subject: [PATCH 1/5] Fix realpath ValueError on null byte Calling os.path.realpath with a path containg a null byte ("\x00") it raised an ValueError on posix systems. This fix will catch the ValueError similar to how other path operations are handling such an Error. e.g. os.path.exists, os.path.ismount, os.fspath, os.path.islink --- Lib/posixpath.py | 2 +- Lib/test/test_posixpath.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 0e8bb5ab10d916..5b1e3ab1fa626d 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -460,7 +460,7 @@ def realpath(filename, *, strict=False): if not stat.S_ISLNK(st.st_mode): path = newpath continue - except OSError: + except (OSError, ValueError): if strict: raise path = newpath diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 807f985f7f4df7..b840be60b96a62 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -646,6 +646,15 @@ def test_realpath_resolve_first(self): safe_rmdir(ABSTFN + "/k") safe_rmdir(ABSTFN) + @skip_if_ABSTFN_contains_backslash + def test_realpath_null_byte(self): + path_with_null_byte = ABSTFN + "/\x00" + try: + os.mkdir(ABSTFN) + self.assertEqual(realpath(path_with_null_byte), path_with_null_byte) + finally: + safe_rmdir(ABSTFN) + def test_relpath(self): (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") try: From 146970f73b05073623743ad49304fea448839385 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 7 Apr 2024 06:46:47 +0000 Subject: [PATCH 2/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-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst diff --git a/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst b/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst new file mode 100644 index 00000000000000..d63f9a06de0d47 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst @@ -0,0 +1 @@ +Fix :exc:`ValueError` when :func:`os.path.realpath` is called with a path containing a null byte. Patch by Stefan Hoelzl. From e2a4b6890f92e218968a165111d1b9974a91b8f5 Mon Sep 17 00:00:00 2001 From: Stefan Hoelzl <1478183+stefanhoelzl@users.noreply.github.com> Date: Sun, 7 Apr 2024 08:50:22 +0200 Subject: [PATCH 3/5] Added Stefan Hoelzl to Misc/ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index fe014a364dd82d..27d24fae34348d 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -773,6 +773,7 @@ Benjamin Hodgson Joerg-Cyril Hoehle Douwe Hoekstra Robert Hoelzl +Stefan Hoelzl Gregor Hoffleit Chris Hoffman Tim Hoffmann From 8da4ebb9966918c19438ab2a6728c4ee41a6d828 Mon Sep 17 00:00:00 2001 From: Stefan Hoelzl <1478183+stefanhoelzl@users.noreply.github.com> Date: Sun, 7 Apr 2024 17:03:32 +0200 Subject: [PATCH 4/5] added test for strict mode --- Lib/test/test_posixpath.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index b840be60b96a62..10658e4977ab91 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -655,6 +655,15 @@ def test_realpath_null_byte(self): finally: safe_rmdir(ABSTFN) + @skip_if_ABSTFN_contains_backslash + def test_realpath_raises_value_error_on_null_byte_in_strict_mode(self): + path_with_null_byte = ABSTFN + "/\x00" + try: + os.mkdir(ABSTFN) + self.assertRaises(ValueError, realpath, path_with_null_byte, strict=True) + finally: + safe_rmdir(ABSTFN) + def test_relpath(self): (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar") try: From e32467fa62dc1678e52128b30855c378616d33dc Mon Sep 17 00:00:00 2001 From: Stefan Hoelzl <1478183+stefanhoelzl@users.noreply.github.com> Date: Sun, 7 Apr 2024 17:06:45 +0200 Subject: [PATCH 5/5] updated NEWS with info that is only affects non-strict mode --- .../Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst b/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst index d63f9a06de0d47..3caa9dcaddc5d2 100644 --- a/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst +++ b/Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst @@ -1 +1,3 @@ -Fix :exc:`ValueError` when :func:`os.path.realpath` is called with a path containing a null byte. Patch by Stefan Hoelzl. +Fix :exc:`ValueError` when :func:`os.path.realpath` is called +with a path containing a null byte in non-strict mode. +Patch by Stefan Hoelzl.