Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions hermes_cli/codex_runtime_plugin_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from pathlib import Path
from typing import Any, Optional

from hermes_constants import get_hermes_home

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -445,9 +447,16 @@ def _build_hermes_tools_mcp_entry() -> dict:
env: dict[str, str] = {}
# HERMES_HOME passes through if set so the MCP subprocess sees the
# same config / auth / sessions DB as the parent CLI.
hermes_home = os.environ.get("HERMES_HOME")
# Derive HERMES_HOME canonically from get_hermes_home() rather than
# reading os.environ at migrate-time. This prevents pytest tempdir
# leakage into user's real ~/.codex/config.toml (Bug #26250-C).
# Sanity check: the path must exist and not be a pytest tempdir.
hermes_home = str(get_hermes_home())
if hermes_home:
env["HERMES_HOME"] = hermes_home
# Sanity checks: path must exist and not be a pytest tempdir
hermes_path = Path(hermes_home)
if hermes_path.exists() and "pytest-of-" not in hermes_home:
env["HERMES_HOME"] = hermes_home
# PYTHONPATH passes through so a worktree-launched hermes finds the
# branch's modules instead of the installed package.
pythonpath = os.environ.get("PYTHONPATH")
Expand Down
75 changes: 75 additions & 0 deletions tests/hermes_cli/test_codex_runtime_plugin_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,78 @@ def test_summary_reports_migration_count(self, tmp_path):
assert "Migrated 2 MCP server(s)" in summary
assert "- a" in summary
assert "- b" in summary

class TestHERMESHomeSanity:
"""Bug #26250-C: HERMES_HOME should not leak pytest tempdir into config.toml."""

def test_pytest_tempdir_blocked(self, tmp_path, monkeypatch):
"""When HERMES_HOME points at a pytest tempdir, it should NOT be written."""
import os

# Simulate pytest monkeypatch setting HERMES_HOME to a tempdir
fake_tempdir = tmp_path / "pytest-of-test123"
fake_tempdir.mkdir(exist_ok=True)

# Save original HERMES_HOME
original_hermes_home = os.environ.get("HERMES_HOME")
output_content = None
try:
monkeypatch.setenv("HERMES_HOME", str(fake_tempdir))
report = migrate(
{"mcp_servers": {"test-server": {"command": "echo"}}},
codex_home=tmp_path,
expose_hermes_tools=False,
)
# Cache output before pytest cleans up tmp_path
output_content = (tmp_path / "config.toml").read_text()
finally:
# Restore original HERMES_HOME
if original_hermes_home is None:
os.environ.pop("HERMES_HOME", None)
else:
os.environ["HERMES_HOME"] = original_hermes_home

# Verify migration succeeded
assert report.written
# BUT HERMES_HOME from tempdir should NOT be in the output
assert output_content is not None
assert "HERMES_HOME" not in output_content, (
"HERMES_HOME from pytest tempdir leaked into config.toml"
)
# The test-server entry should still be there
assert "[mcp_servers.test-server]" in output_content

def test_nonexistent_hermes_home_blocked(self, tmp_path, monkeypatch):
"""When HERMES_HOME points at a nonexistent path, it should NOT be written."""
import os

fake_path = tmp_path / "does-not-exist" / ".hermes"

# Save original HERMES_HOME
original_hermes_home = os.environ.get("HERMES_HOME")
output_content = None
try:
monkeypatch.setenv("HERMES_HOME", str(fake_path))
report = migrate(
{"mcp_servers": {"test-server": {"command": "echo"}}},
codex_home=tmp_path,
expose_hermes_tools=False,
)
# Cache output before pytest cleans up tmp_path
output_content = (tmp_path / "config.toml").read_text()
finally:
# Restore original HERMES_HOME
if original_hermes_home is None:
os.environ.pop("HERMES_HOME", None)
else:
os.environ["HERMES_HOME"] = original_hermes_home

# Verify migration succeeded
assert report.written
# BUT HERMES_HOME for a nonexistent path should NOT be in the output
assert output_content is not None
assert "HERMES_HOME" not in output_content, (
"HERMES_HOME for nonexistent path leaked into config.toml"
)
# The test-server entry should still be there
assert "[mcp_servers.test-server]" in output_content
Loading