Repository navigation
fix(plugin): run the CLI on a safe sys.path and write temp files without following symlinks - #261
Conversation
…out following symlinks The background te ran as python -m with the repository as cwd, so a json.py, runpy.py, sitecustomize.py or tokeneconomizer/ in the checkout, or an inherited PYTHONPATH with an empty, . or relative element, ran with the plugin's rights outside the agent's sandbox. It now starts as python -E -s -X utf8 -c with a bootstrap that puts the plugin's own directory first and drops the empty entry; the aider probe runs in an empty temp dir. write_json and _write_text create their temp file with O_CREAT|O_EXCL|O_NOFOLLOW after removing any stale entry, and settings files are read with O_NOFOLLOW, so a symlink planted at the temp or settings name no longer redirects the write or the read. Regression tests in tests/test_live_safety.py fail without the fix. Fixes microsoft#260 🎯 Fix: 90 % 💥 Break: 5 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 86 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Kevin Veen-Birkenbach (@kevinveenbirkenbach) please sign the CLA. |
|
@microsoft-github-policy-service agree [company="Kevin Veen-Birkenbach"] |
|
@microsoft-github-policy-service agree |
|
Kevin Veen-Birkenbach (@kevinveenbirkenbach) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
|
@microsoft-github-policy-service agree |
Summary
Plugin hooks run outside the coding agent's sandbox, while the agent, or a cloned
repository, may write the repository itself. Three paths let content placed in the
repository act with the plugin's rights. This change closes them.
Fixes #260
Issue 1: the background
teimports modules from the repositoryLiveContext.spawnstartedpython -m tokeneconomizer.plugin.cliwithcwd=self.repo.root. With-m, Python puts the working directory first onsys.path, so atokeneconomizer/package or a module such asjson.pyin therepository root was imported in place of the plugin's own code. Inherited
PYTHONPATHelements that are empty,.or relative do the same, and they alsolet a repository
sitecustomize.pyrun at interpreter startup. Session folds, theloop-guard judge, pending applies and the repository map all take this path.
Fix: the child now starts as
python -E -s -X utf8 -c <bootstrap> <core> ....-Eignores everyPYTHON*variable, so an inheritedPYTHONPATHno longermatters. UTF-8 mode, which came from
PYTHONUTF8, now comes from-X utf8.-sskips the user site.-cadds. It then puts the plugin's owndirectory, passed as the first argument, in front of
sys.path, before anythingelse is imported, and starts the CLI through
runpy.run_module.The working directory stays the repository root, so the CLI behaves as before.
PYTHONPATHandPYTHONUTF8stay in the environment for the processestestarts, as before.
-PorPYTHONSAFEPATHalone would cover only Python 3.11 andlater, and neither ignores
PYTHONPATH.Issue 2: the aider probe imports
aiderfrom the repositoryrepomap._has_aiderranpython -c "import aider.repomap"with nocwd. Thebackground
te repomap refreshruns in the repository, so anaider/packagethere was imported and run. The probe now runs in a fresh empty temporary
directory.
-Iwas not used because it would also hide an aider installed in theuser site.
Issue 3: settings writes and reads follow a symlink
write_jsonand_write_textindenylist.pyopened<path>.te.tmpwithopen(tmp, "w")._guard_targetchecks only the final path, so a symlink placedat
.claude/settings.local.json.te.tmpsent the write to its target, for examplethe user-wide
~/.claude/settings.json. Withtoolprune=autothis happenswithout any user action.
Fix:
_open_tmpremoves any entry at the temporary name, then createsthe file with
O_CREAT | O_EXCL | O_NOFOLLOW(O_BINARYwhere defined, sonewline handling matches
open(..., "w"))._read_textreads the settings file withO_NOFOLLOW. A file thatis swapped for a link after
_guard_targetchecked it is refused instead ofread and written back.
_read_settingsand both Codex config reads use it.Tests
The new tests are in
tests/test_live_safety.py:test_a_symlink_at_the_temporary_name_is_not_written_through[write_json|_write_text]:the symlink's target stays unchanged, the settings file is written, and no
temporary entry is left behind.
test_a_settings_file_swapped_for_a_symlink_after_the_guard_is_not_read.test_the_background_te_imports_nothing_the_repository_carries, run with noPYTHONPATH, with aPYTHONPATHthat has an empty element, and withPYTHONPATH=.. It placesjson.py,runpy.py,sitecustomize.pyand atokeneconomizer/package in the repository, runs the exact command linespawnbuilds, from the repository, and checks that none of them ran.test_the_aider_probe_imports_no_aider_the_repository_carries.Results on Python 3.14 (
python -I -m pytest):tests/test_live_safety.pytests/test_live_hooks.pytests/test_live_repomap.pytests/test_microskills_forget.pyNegative controls:
main, every new test fails.sys.pathentry,the
PYTHONPATHcases, the aider probe and the swapped-file read fail.I could not run a Python 3.10 interpreter locally.
-E,-s,-X utf8and thebootstrap all exist in 3.10, and the 3.10 jobs of the CI matrix are the check
there.
Not addressed
_guard_targetresolves the settings directory(
.claude,.codex,.github) before the write. A concurrent swap of thatdirectory for a symlink between the check and
os.replacewould still carrythe write elsewhere. Closing it needs directory-relative opens and renames
(
dir_fd), which Windows lacks, so it is left for a separate change..git/info/exclude:_exclude_from_gitchecks for symlinks and thenappends, which leaves the same kind of window.
🤖 Generated with Claude Code