Skip to content

Commit dccae70

Browse files
johnslavikjaraco
authored andcommitted
pythongh-142315: Don't pass the "real path" of Pdb script target to system functions (pythonGH-142371)
* Pick target depending on preconditions * Clarify the news fragment * Add test capturing missed expectation. * Add more idiomatic safe realpath helper * Restore logic where existance and directoriness are checked on realpath. * Link GH issue to test. * Extract a function to check the target. Remove the _safe_realpath, now no longer needed. * Extract method for replacing sys_path, and isolate realpath usage there. * Revert "Extract method for replacing sys_path, and isolate realpath usage there." This reverts commit 855aac3. * Restore _safe_realpath. --------- (cherry picked from commit d716e3b) Co-authored-by: Bartosz Sławecki <[email protected]> Co-authored-by: Jason R. Coombs <[email protected]>
1 parent e96367d commit dccae70

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

Lib/pdb.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,37 @@ class _ExecutableTarget:
172172

173173
class _ScriptTarget(_ExecutableTarget):
174174
def __init__(self, target):
175-
self._target = os.path.realpath(target)
175+
self._check(target)
176+
self._target = self._safe_realpath(target)
176177

177-
if not os.path.exists(self._target):
178+
# If PYTHONSAFEPATH (-P) is not set, sys.path[0] is the directory
179+
# of pdb, and we should replace it with the directory of the script
180+
if not sys.flags.safe_path:
181+
sys.path[0] = os.path.dirname(self._target)
182+
183+
@staticmethod
184+
def _check(target):
185+
"""
186+
Check that target is plausibly a script.
187+
"""
188+
if not os.path.exists(target):
178189
print(f'Error: {target} does not exist')
179190
sys.exit(1)
180-
if os.path.isdir(self._target):
191+
if os.path.isdir(target):
181192
print(f'Error: {target} is a directory')
182193
sys.exit(1)
183194

184-
# If safe_path(-P) is not set, sys.path[0] is the directory
185-
# of pdb, and we should replace it with the directory of the script
186-
if not sys.flags.safe_path:
187-
sys.path[0] = os.path.dirname(self._target)
195+
@staticmethod
196+
def _safe_realpath(path):
197+
"""
198+
Return the canonical path (realpath) if it is accessible from the userspace.
199+
Otherwise (for example, if the path is a symlink to an anonymous pipe),
200+
return the original path.
201+
202+
See GH-142315.
203+
"""
204+
realpath = os.path.realpath(path)
205+
return realpath if os.path.exists(realpath) else path
188206

189207
def __repr__(self):
190208
return self._target

Lib/test/test_pdb.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,6 +3070,24 @@ def _assert_find_function(self, file_content, func_name, expected):
30703070
self.assertEqual(
30713071
expected, pdb.find_function(func_name, os_helper.TESTFN))
30723072

3073+
def _fd_dir_for_pipe_targets(self):
3074+
"""Return a directory exposing live file descriptors, if any."""
3075+
proc_fd = "/proc/self/fd"
3076+
if os.path.isdir(proc_fd) and os.path.exists(os.path.join(proc_fd, '0')):
3077+
return proc_fd
3078+
3079+
dev_fd = "/dev/fd"
3080+
if os.path.isdir(dev_fd) and os.path.exists(os.path.join(dev_fd, '0')):
3081+
if sys.platform.startswith("freebsd"):
3082+
try:
3083+
if os.stat("/dev").st_dev == os.stat(dev_fd).st_dev:
3084+
return None
3085+
except FileNotFoundError:
3086+
return None
3087+
return dev_fd
3088+
3089+
return None
3090+
30733091
def test_find_function_empty_file(self):
30743092
self._assert_find_function(b'', 'foo', None)
30753093

@@ -3128,6 +3146,47 @@ def test_spec(self):
31283146
stdout, _ = self.run_pdb_script(script, commands)
31293147
self.assertIn('None', stdout)
31303148

3149+
def test_script_target_anonymous_pipe(self):
3150+
"""
3151+
_ScriptTarget doesn't fail on an anonymous pipe.
3152+
3153+
GH-142315
3154+
"""
3155+
fd_dir = self._fd_dir_for_pipe_targets()
3156+
if fd_dir is None:
3157+
self.skipTest('anonymous pipe targets require /proc/self/fd or /dev/fd')
3158+
3159+
read_fd, write_fd = os.pipe()
3160+
3161+
def safe_close(fd):
3162+
try:
3163+
os.close(fd)
3164+
except OSError:
3165+
pass
3166+
3167+
self.addCleanup(safe_close, read_fd)
3168+
self.addCleanup(safe_close, write_fd)
3169+
3170+
pipe_path = os.path.join(fd_dir, str(read_fd))
3171+
if not os.path.exists(pipe_path):
3172+
self.skipTest('fd directory does not expose anonymous pipes')
3173+
3174+
script_source = 'marker = "via_pipe"\n'
3175+
os.write(write_fd, script_source.encode('utf-8'))
3176+
os.close(write_fd)
3177+
3178+
original_path0 = sys.path[0]
3179+
self.addCleanup(sys.path.__setitem__, 0, original_path0)
3180+
3181+
target = pdb._ScriptTarget(pipe_path)
3182+
code_text = target.code
3183+
namespace = target.namespace
3184+
exec(code_text, namespace)
3185+
3186+
self.assertEqual(namespace['marker'], 'via_pipe')
3187+
self.assertEqual(namespace['__file__'], target.filename)
3188+
self.assertIsNone(namespace['__spec__'])
3189+
31313190
def test_find_function_first_executable_line(self):
31323191
code = textwrap.dedent("""\
31333192
def foo(): pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Pdb can now run scripts from anonymous pipes used in process substitution.
2+
Patch by Bartosz Sławecki.

0 commit comments

Comments
 (0)