Repository navigation
Conversation
…ry is None LocalServer.__init__ already computed search_path from the caller-supplied env mapping, but resolved binary=None against os.environ['GRAPHICLE_BIN'] and the host PATH first. That raised FileNotFoundError when graphicle only existed on env['PATH'], preferred a host binary over env['PATH']/env['GRAPHICLE_BIN'], and leaked host GRAPHICLE_BIN into an isolated env. Compute search_path and env_bin from env when env is provided (falling back to os.environ only when env is None) before resolving binary=None, and add unit tests in sdk/python/tests/test_server.py. Signed-off-by: sama Pyb <peiyibopybsama@gmail.com>
VikramLex
left a comment
There was a problem hiding this comment.
Thanks for this, and for the tests: run against main's server.py, five of the six new tests fail, so they pin the bug well (all 15 SDK tests pass at this head on Python 3.10 and 3.13).
One change before we merge. With this version, a caller who passes an env without PATH (for example env={"HOME": tmp} to keep the server's state separate) and has graphicle on the host PATH, or in the host's GRAPHICLE_BIN, now gets FileNotFoundError: the lookup falls back to os.defpath (/bin:/usr/bin, or .;C:\bin on Windows, which also means the current folder). On main that call worked, because the binary was resolved to an absolute path from the host environment and the child never needed PATH.
Could the lookup use env only when it actually says where the binary is, and keep the host lookup otherwise? Something like:
from_env = env is not None and ("GRAPHICLE_BIN" in env or "PATH" in env)
search_path = env.get("PATH", "") if from_env else None
env_bin = env.get("GRAPHICLE_BIN") if from_env else os.environ.get("GRAPHICLE_BIN")
resolved = binary or env_bin or shutil.which("graphicle", path=search_path)Your six tests should still hold with that (the isolated case passes PATH, so it stays isolated). Please add one test for env={"HOME": ...} with graphicle only on the host PATH, and, if you like, make the FileNotFoundError message say "include GRAPHICLE_BIN or PATH in env" when env was given.
…and GRAPHICLE_BIN Only restrict binary resolution to caller-supplied env when env actually specifies GRAPHICLE_BIN or PATH (matched case-insensitively on Windows). Callers passing unrelated variables such as HOME retain host PATH / GRAPHICLE_BIN lookup, explicit binary= retains highest priority, and explicitly setting PATH or GRAPHICLE_BIN in env stays isolated without falling back to os.defpath. Include env hint in FileNotFoundError when env is supplied, and add regression tests covering HOME-only env, host GRAPHICLE_BIN fallback, explicit binary precedence, empty GRAPHICLE_BIN isolation, and platform case sensitivity. Signed-off-by: sama Pyb <peiyibopybsama@gmail.com>
|
Thanks for the review @VikramLex — updated in
Ready for another look. |
Summary
LocalServer.__init__already computedsearch_path = env.get("PATH", os.defpath) if env is not None else Nonefor resolving the executable, but whenbinarywasNone, it ranos.environ.get("GRAPHICLE_BIN") or shutil.which("graphicle")against the host process environment first. As a result:LocalServer(None, data_dir, env={"PATH": custom_bin})raisedFileNotFoundErrorwhengraphiclewas only incustom_bin, or selected a hostgraphicleinstead of the one incustom_bin.LocalServer(None, data_dir, env={"GRAPHICLE_BIN": custom_exe})ignoredenv["GRAPHICLE_BIN"].envleaked the host process'sGRAPHICLE_BIN.This change computes
search_pathandenv_binfromenvwhenenv is not None(falling back toos.environonly whenenv is None), and resolvesshutil.which("graphicle", path=search_path).Verification
python3 -B -m unittest discover -s tests -vinsdk/python: 15/15 OK (including 6 new tests insdk/python/tests/test_server.pycovering customenv["PATH"], customenv["GRAPHICLE_BIN"], emptyGRAPHICLE_BINfallback, isolatedenvnon-leakage, andenv=Nonedefault behavior).python3 -B -m py_compile graphicle_sdk/server.py tests/test_server.pyandgit diff --check: clean.