fix(discovery): store env discovery cache per-user, not in world-writable tmp#1011
Open
OfficialAbhinavSingh wants to merge 1 commit into
Open
Conversation
The discovery cache lived at a fixed, world-writable path in the shared temp directory (tempfile.gettempdir()/openenv_discovery_cache.json). On a multi-user host another local user could pre-create that file with attacker-chosen client_module_path/client_class_name; the victim's discover(use_cache=True) would load it and later importlib.import_module() the attacker-named module (CWE-377 insecure temp file + CWE-427 uncontrolled load path). - Move the cache to a per-user directory ($XDG_CACHE_HOME or ~/.cache/openenv). - Ignore a cache file not owned by the current user or writable by group/others (POSIX) before loading it. - Write the cache with 0600 permissions. Adds regression tests covering the per-user location, the ownership/permission guard, and that a world-writable planted cache is ignored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EnvironmentDiscoverystored its cache at a fixed, world-writable path in the shared temp directory:tempfile.gettempdir() / "openenv_discovery_cache.json". On a multi-user host, another local user can pre-create that file with attacker-chosenclient_module_path/client_class_name; the victim'sdiscover(use_cache=True)(the default) then loads it and rebuildsEnvironmentInfo, and a laterget_client_class()/get_action_class()/get_observation_class()callsimportlib.import_module(client_module_path)+getattr(...)on the attacker-supplied names. This moves the cache to a per-user location and refuses to trust a foreign/world-writable cache file.Type of Change
Alignment Checklist
.claude/docs/PRINCIPLES.mdand this PR aligns with our principles.claude/docs/INVARIANTS.mdand no invariants are violatedruff format --check,ruff check,usort check) and the discovery/auto-env tests — all passRFC Status
Details
src/openenv/auto/_discovery.py:_load_cache()json.loads that file and rebuildsEnvironmentInfo(**env_data)with no ownership or integrity check;_save_cache()writes it with default permissions. The trust issue is CWE-377 (insecure temp file) escalating to CWE-427 (uncontrolled load path) viaimport_module.Fix (three small, layered changes):
_default_cache_file()returns$XDG_CACHE_HOME/openenv/…or~/.cache/openenv/discovery_cache.json, removing the shared-writable premise entirely._is_trusted_cache_file()(POSIX) requires the file be owned by the current user and not group/other-writable; otherwise_load_cache()logs and returnsNone(falls back to live discovery). No-op on non-POSIX._save_cache()creates the parent dir andchmod 0o600s the file.Behavior is unchanged for the normal single-user case;
clear_cache()and the cache round-trip work exactly as before.Test Plan
New
TestCacheSecurityclass intests/envs/test_discovery.py:test_cache_file_is_per_user_not_shared_tmp— path is not under the shared temp dir.test_world_writable_cache_is_not_trusted/test_owner_only_cache_is_trusted— the guard.test_load_cache_ignores_world_writable_file— a planted world-writable cache pointingclient_module_pathatos(→system) is ignored, not loaded.test_saved_cache_is_owner_only— saved file is0600and round-trips.Verified (synthetic): a world-writable planted cache →
_load_cache()returnsNone(logs "Ignoring discovery cache … not owned by the current user"); legit own-file round-trip loads and is0600.101 passed, 8 skippedacrosstest_discovery.py+test_auto_env.py;ruff format --check,ruff check,usort checkclean.Claude Code Review
Self-reviewed: layered fix (relocate + guard + perms), POSIX-gated so Windows behavior is unchanged, no public API change, no new dependency, docstrings in HF doc-builder format.
Note
Medium Risk
Security fix around dynamic imports from cached metadata; behavior is unchanged for normal single-user caches but multi-user hosts gain stricter trust rules on POSIX.
Overview
Fixes a local privilege / cache poisoning issue where
EnvironmentDiscoverywrote and read a fixed JSON cache under the shared temp directory, so another user could plantclient_module_path/ class names that later driveimport_module.The cache path moves to
$XDG_CACHE_HOME/openenv/discovery_cache.json(or~/.cache/openenv/…). On load,_is_trusted_cache_file(POSIX) requires owner-only, non–group/other-writable files; untrusted caches are ignored and discovery falls back to live package scan. On save, the parent dir is created and the file ischmod 0o600on POSIX.Adds
TestCacheSecuritycovering per-user path, trust checks, ignoring planted world-writable cache, and owner-only saved files.Reviewed by Cursor Bugbot for commit 81dfc72. Bugbot is set up for automated code reviews on this repo. Configure here.