Skip to content

Commit

Permalink
whitelist some Popen calls, fix #358
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Feb 25, 2022
1 parent 3439fc0 commit 946ac67
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
([#352](https://github.com/mitmproxy/pdoc/issues/352), [@denised](https://github.com/denised))
- Improve linking of classes that are re-exported in a common top-level namespace.
- Make it more clear that Markdown ist the default docformat. ([@Dliwk](https://github.com/Dliwk))
- Fix compatiblity with code using `ctypes.util.find_library`.
([#358](https://github.com/mitmproxy/pdoc/issues/358), [@bubalis](https://github.com/bubalis))

# 2022-02-14: pdoc 10.0.1

Expand Down
45 changes: 31 additions & 14 deletions pdoc/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pkgutil
import platform
import re
import shutil
import subprocess
import sys
import traceback
Expand Down Expand Up @@ -150,6 +151,35 @@ def parse_spec(spec: Path | str) -> str:
return spec


def _noop(*args, **kwargs):
pass


class PdocDefusedPopen(subprocess.Popen):
if platform.system() == "Windows": # pragma: no cover
_noop_exe = "echo.exe"
else: # pragma: no cover
_noop_exe = "echo"

def __init__(self, *args, **kwargs): # pragma: no cover
command_allowed = (
args
and args[0]
and args[0][0]
in (
# these invocations may all come from https://github.com/python/cpython/blob/main/Lib/ctypes/util.py,
# which we want to keep working.
"/sbin/ldconfig",
"ld",
shutil.which("gcc") or shutil.which("cc"),
shutil.which("objdump"),
)
)
if not command_allowed:
kwargs["executable"] = self._noop_exe
super().__init__(*args, **kwargs)


@contextmanager
def mock_some_common_side_effects():
"""
Expand All @@ -158,21 +188,8 @@ def mock_some_common_side_effects():
Note that this function must not be used for security purposes, it's easily bypassable.
"""
if platform.system() == "Windows": # pragma: no cover
noop_exe = "echo.exe"
else: # pragma: no cover
noop_exe = "echo"

def noop(*args, **kwargs):
pass

class PdocDefusedPopen(subprocess.Popen):
def __init__(self, *args, **kwargs): # pragma: no cover
kwargs["executable"] = noop_exe
super().__init__(*args, **kwargs)

with patch("subprocess.Popen", new=PdocDefusedPopen), patch(
"os.startfile", new=noop, create=True
"os.startfile", new=_noop, create=True
), patch("sys.stdout", new=io.StringIO()), patch(
"sys.stderr", new=io.StringIO()
), patch(
Expand Down

0 comments on commit 946ac67

Please sign in to comment.