:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(hogli): don't execvp wrap when embedded (#63113)
Raúl Negrón-Otero3509ea4c026a
diff --git a/tools/hogli/src/hogli/__main__.py b/tools/hogli/src/hogli/__main__.pyindex 9493e7f54ef6..804f930f3fab 100644--- a/tools/hogli/src/hogli/__main__.py+++ b/tools/hogli/src/hogli/__main__.py@@ -2,14 +2,9 @@from __future__ import annotations-from .cli import cli---def main() -> None:- """Execute the Click application."""-- cli()-+# Route through cli.main (not cli directly) so `python -m hogli` marks itself+# as the process entrypoint, matching the `hogli` console script.+from .cli import mainif __name__ == "__main__": # pragma: no cover - module entry pointmain()diff --git a/tools/hogli/src/hogli/cli.py b/tools/hogli/src/hogli/cli.pyindex 0fe1c7aa74b9..003478da5485 100644--- a/tools/hogli/src/hogli/cli.py+++ b/tools/hogli/src/hogli/cli.py@@ -349,6 +349,11 @@ _WRAP_FILE_PLACEHOLDER = "{file}"# `hogli run`). Manifest commands opt in via `needs_secrets: true`._BUILTIN_COMMANDS_NEEDING_SECRETS = frozenset({"run"})+# False until `main()` flips it (the only path the `hogli` script and+# `python -m hogli` both take). The secret-wrap re-exec is gated on it; see+# `_maybe_reexec_via_wrap` for why embedded callers must not execvp.+_is_process_entrypoint = False+def _load_env_file(path: os.PathLike[str],@@ -446,7 +451,14 @@ def _maybe_reexec_via_wrap(secrets: dict[str, Any], env_files: list[Path]) -> NoReturns normally if no re-exec is needed (caller falls through to directfile loading). Otherwise replaces the current process via ``os.execvp``.++ Only ever re-execs when hogli owns the process (``_is_process_entrypoint``).+ Embedded in-process callers (CliRunner tests, library use) fall through to+ direct file loading so execvp can't replace and kill the host process."""+ if not _is_process_entrypoint:+ return+secrets_file: Path = secrets["file"]marker: str = secrets["marker"]wrap: list[str] | None = secrets["wrap"]@@ -691,7 +703,9 @@ def _fire_post_command_hooks(command: str | None, exit_code: int) -> None:def main() -> None:- """Main entry point."""+ """Main entry point — the only path allowed to re-exec via the secret wrap."""+ global _is_process_entrypoint+ _is_process_entrypoint = Truecli()diff --git a/tools/hogli/tests/test_cli.py b/tools/hogli/tests/test_cli.pyindex a75a12b8a4d3..1a3acf822364 100644--- a/tools/hogli/tests/test_cli.py+++ b/tools/hogli/tests/test_cli.py@@ -494,6 +494,8 @@ class TestApplyEnvConfig:)for k in ("PRELOADED", "HOGLI_SECRETS_WRAPPED"):monkeypatch.delenv(k, raising=False)+ # Re-exec only fires when hogli owns the process (real CLI entrypoint).+ monkeypatch.setattr("hogli.cli._is_process_entrypoint", True)with (patch("shutil.which", return_value="/usr/local/bin/op"),@@ -515,6 +517,46 @@ class TestApplyEnvConfig:# sentinel set to prevent infinite re-exec loopassert os.environ["HOGLI_SECRETS_WRAPPED"] == "1"+ def test_embedded_invocation_never_reexecs(self, tmp_path, monkeypatch: pytest.MonkeyPatch) -> None:+ """When hogli is invoked in-process (not the process entrypoint), the+ wrap must never os.execvp — even with the marker present and the wrap+ binary installed. execvp would replace and silently kill the host+ process (e.g. a click CliRunner test runner). Instead it falls through+ to direct file loading, skipping unresolved refs.++ This is the regression guard for the silent-death bug: any pytest run+ that drives the CLI through CliRunner on a machine with the wrap binary+ installed and marker refs in the secrets file would otherwise exec away+ the test process mid-run.+ """+ from hogli.cli import _apply_env_config++ secrets_file = tmp_path / ".env.local"+ secrets_file.write_text("API_KEY=op://vault/item/credential\nLITERAL=keep_me\n")+ secrets = {+ "file": secrets_file,+ "marker": "op://",+ "wrap": ["op", "run", "--env-file", "{file}", "--"],+ }+ monkeypatch.setattr(+ "hogli.cli.get_manifest",+ lambda: self._make_manifest(secrets_config=secrets, needs_secrets_commands={"start"}),+ )+ # Embedded, not a real entrypoint: this is what CliRunner / library use sees.+ monkeypatch.setattr("hogli.cli._is_process_entrypoint", False)+ for k in ("API_KEY", "LITERAL", "HOGLI_SECRETS_WRAPPED"):+ monkeypatch.delenv(k, raising=False)++ with (+ patch("shutil.which", return_value="/usr/local/bin/op"),+ patch("os.execvp") as mock_exec,+ ):+ _apply_env_config("start")++ mock_exec.assert_not_called()+ assert "API_KEY" not in os.environ # op:// ref skipped, not leaked as a literal+ assert os.environ["LITERAL"] == "keep_me"+def test_marker_hit_but_wrap_binary_missing_falls_back_with_warning(self, tmp_path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:@@ -543,6 +585,9 @@ class TestApplyEnvConfig:)for k in ("OPENAI_API_KEY", "LITERAL", "HOGLI_SECRETS_WRAPPED"):monkeypatch.delenv(k, raising=False)+ # The missing-binary warning is only meaningful for a real entrypoint+ # that would otherwise re-exec; simulate one.+ monkeypatch.setattr("hogli.cli._is_process_entrypoint", True)with (patch("shutil.which", return_value=None),@@ -631,6 +676,9 @@ class TestApplyEnvConfig:lambda: self._make_manifest(secrets_config=secrets, needs_secrets_commands=opted_in),)monkeypatch.delenv("HOGLI_SECRETS_WRAPPED", raising=False)+ # This test exercises the needs_secrets gate, not the entrypoint gate —+ # run as a real entrypoint so the wrap can fire when the gate opens.+ monkeypatch.setattr("hogli.cli._is_process_entrypoint", True)with (patch("shutil.which", return_value="/usr/local/bin/op"),