Skip to content

fix(hooks): keep the opened project off sys.path in session hooks - #1769

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:fix/hooks-python-safe-path
Oct 23, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
potiuk:fix/hooks-python-safe-path

Conversation

@potiuk

@potiuk potiuk commented Oct 23, 2026

Copy link
Copy Markdown
Member

Two session hooks run python3 -m <package> with the project being opened as their working directory:

  • plugins/magpie-setup/hooks/check-upgrade.sh (setup_preflight.isolated session-start)
  • tools/agent-isolation/container-gateway-hook.sh (container_gateway serve / stop)

python3 -m puts the working directory on sys.path ahead of PYTHONPATH. A setup_preflight/ or container_gateway/ package committed to the opened repository therefore ran instead of Magpie's own copy. It ran outside the sandbox, as soon as the session started. The container-gateway hook's header already says it "executes only code from locations the operator installed or pinned, never from the repository being opened". Until now, that promise didn't hold for the working directory.

The fix runs both hooks with python3 -P. That keeps the working directory off sys.path and keeps PYTHONPATH. -I would also have dropped PYTHONPATH, which both hooks rely on. Both packages already declare requires-python = ">=3.11", and 3.11 is the version that added -P.

Tests:

  • New tools/setup-preflight/tests/test_session_hook.py runs check-upgrade.sh from a project that carries a planted setup_preflight package. The test fails on main and passes with this change.
  • The container-gateway hook tests now assert -P in the dry-run command for both start and stop.

The isolated-setup doctor probe (probe-3-container-gateway.sh) also uses python3 -m from the project directory. It is out of scope here: it runs inside the agent's sandboxed session against in-repo sources by design, not as a hook outside the sandbox.


Generative AI disclosure: this change was prepared with Claude Code (Opus 5.5) and reviewed by me before submission.

🤖 Generated with Claude Code

The magpie-setup session-start hook and the container-gateway hook run
`python3 -m <package>` with the project being opened as their working
directory. `-m` puts that directory on sys.path ahead of PYTHONPATH, so a
`setup_preflight/` or `container_gateway/` package committed to the
project ran in place of Magpie's own, outside the sandbox, as soon as a
session started. The container-gateway hook's trust model already says it
never runs code from the repository being opened; this makes that hold.

Run both with `python3 -P`, which leaves the working directory off
sys.path and keeps PYTHONPATH. Both packages already require Python 3.11,
where `-P` was added.

Add a test that runs check-upgrade.sh from a project carrying a planted
`setup_preflight` package (it fails without the fix), and assert `-P` in
the container-gateway hook's dry-run command.

Generated-by: Claude Code (Opus 5.5)
@github-actions github-actions Bot added family:setup setup-* skills family:tools tools/* capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) substrate:sandbox Tool substrate: agent isolation, egress control, settings audit substrate:setup Tool substrate: adopter-side setup state resolved at runtime (lock parsing, floors, fingerprints) labels Oct 23, 2026
@potiuk
potiuk enabled auto-merge (squash) October 23, 2026 08:57
@potiuk
potiuk merged commit 759f83b into apache:main Oct 23, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:setup setup-* skills family:tools tools/* substrate:sandbox Tool substrate: agent isolation, egress control, settings audit substrate:setup Tool substrate: adopter-side setup state resolved at runtime (lock parsing, floors, fingerprints)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant