Surfacing daemon and generated registrations run python -m with the project directory on sys.path
Description
The surfacing daemon is started as python -m memtomem_stm daemon run --detached with the hook process's working directory inherited. Under -m, Python puts the working directory at sys.path[0], ahead of the standard library and site-packages, so a module in the project directory is imported instead of the installed one. Opening an untrusted checkout in a host that has the mms hook PostToolUse hook installed can therefore run code from that checkout as the user.
The 0.7.3 fix for GHSA-hqh4-75hx-xq46 hardened executable lookup (PATH); it does not change sys.path.
Details
src/memtomem_stm/daemon/spawn.py:33builds[sys.executable, "-m", "memtomem_stm", "daemon", "run", "--detached"]and callssubprocess.Popenwith nocwd=, no-P/-I, and noPYTHONSAFEPATH.- It is reached from
src/memtomem_stm/cli/hook_cmd.py:686-690when no warm daemon answers.hook.use_daemonandhook.auto_spawnboth default toTrue. - The console-script entry points (
mms,memtomem-stm,memtomem-stm-proxy) are not affected: theirsys.path[0]is the script directory.
Same launch shape, confirmed by reading the source but not run end to end:
src/memtomem_stm/cli/proxy.py:978(_registration_command) registers<python> -m memtomem_stmwith MCP hosts, which usually start servers in the project directory.src/memtomem_stm/cli/proxy.py:6549configures the bundled demo upstream as<python> -m memtomem_stm.demo_server.
Reproduction (0.7.7 source at 38a605af, macOS, Python 3.13.2)
- In an empty directory create
memtomem_stm/__init__.py(empty) andmemtomem_stm/__main__.pythat writes a marker file. - With a fresh
HOME(so no daemon is running) and default configuration, run from that directory:mms hook --host claudewith aPostToolUsepayload forReadon stdin. - The marker file appears; the shadow
__main__received['daemon', 'run', '--detached'].
A directory holding only click.py is enough as well: python -m memtomem_stm --help from there imports it. So is a types.py, in an environment that has not already loaded types at startup (a plain python -m venv).
Controls, all negative: the same command from an empty directory; the mms console script from the shadow directories; python -P -m memtomem_stm from the shadow directories.
Impact
Arbitrary code execution as the user, in a detached process, on the first eligible tool call (Read, Grep, Glob, Bash by default) made in an untrusted project directory while no daemon for the current configuration is alive. A project that merely has a top-level module whose name collides with something the daemon imports also breaks daemon startup there.
Fix
Every interpreter STM starts or registers is launched with -P (safe_module_argv() in src/memtomem_stm/utils/exec_env.py), so Python no longer puts the working directory first on its import path. A PYTHONPATH entry that names the working directory still applies; that is the user's own environment.
An in-process guard (removing the working directory from sys.path at the top of the entry modules) was tried and dropped: Python imports from the working directory before any STM code runs (runpy needs types, among others), so in a plain venv a project types.py still ran.
Remediation after upgrading
Existing registrations are kept on upgrade and still start without -P. STM cannot protect such a launch from inside, so each one has to be refreshed, passing the same --config <path> if the registration uses a non-default config:
- project
.mcp.json: from that project,mms register --client json --replace-registration(mms registerprints this command when it keeps such an entry); - Claude Code user scope:
mms register --client claude --replace-registration; - Codex:
mms register --client codex --replace-registration; - a host config edited or copied by hand: insert
"-P"before"-m"in the entry'sargs; - the demo upstream: set
upstream_servers.demo.argsinstm_proxy.jsonto["-P", "-m", "memtomem_stm.demo_server"].
Verification status
Verified locally on macOS (Python 3.12, 3.13, 3.14). Windows is unverified until the post-merge CI matrix runs; -P is an interpreter flag with no platform-specific behaviour.