Skip to content

Autonomous research system for measurable, computer-executable research.

Python

fix(codex_sdk): keep provider credentials out of Codex run artifacts (#203)

Glen-SP5b50658b219d
diff --git a/praxist/plugins/agent_runtimes/codex_sdk/adapter.py b/praxist/plugins/agent_runtimes/codex_sdk/adapter.py
index 286197f..3a499d7 100644
--- a/praxist/plugins/agent_runtimes/codex_sdk/adapter.py
+++ b/praxist/plugins/agent_runtimes/codex_sdk/adapter.py
@@ -68,7 +68,6 @@ _SUBSCRIPTION_RUNTIME_OVERRIDES = (
"features.plugin_sharing=false",
"features.plugins=false",
"features.remote_plugin=false",
- "features.shell_snapshot=false",
"features.skill_mcp_dependency_install=false",
"features.workspace_dependencies=false",
"mcp_servers={}",
@@ -79,6 +78,22 @@ _SUBSCRIPTION_RUNTIME_OVERRIDES = (
"skills.include_instructions=false",
)
+# Flags that must always be applied regardless of subscription state. Keep
+# minimal and safety-focused here so they cannot be opted out by subscription
+# specific overrides.
+_ALWAYS_ON_SAFETY_OVERRIDES = ("features.shell_snapshot=false",)
+
+# Handed to Codex in place of the provider key whenever a relay fronts the
+# upstream. The relay holds the real credential, and `requires_openai_auth=false`
+# means Codex never validates this value.
+#
+# This is load-bearing rather than defensive: Codex captures its own environment
+# into `shell_snapshots/*.sh`, and `features.shell_snapshot=false` above is
+# accepted but not honoured by openai-codex 0.147.0, so the capture cannot be
+# switched off from here. Keeping the secret out of the child environment is the
+# part we control.
+_RELAY_CHILD_KEY_PLACEHOLDER = "praxist-relay-local-no-upstream-credential"
+
_SAFE_PROCESS_ENV_KEYS = frozenset(
{
"ALL_PROXY",
@@ -450,7 +465,7 @@ class CodexSdkRuntime:
relay: RelayHandle | None = None
staged_chatgpt_home: StagedChatgptHome | None = None
provider_id = "openai"
- config_overrides: tuple[str, ...] = ()
+ config_overrides: tuple[str, ...] = _ALWAYS_ON_SAFETY_OVERRIDES
subscription = _uses_chatgpt_subscription(request, context.env)
client: Any | None = None
try:
@@ -490,6 +505,7 @@ class CodexSdkRuntime:
log_dir = state_dir / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
config_overrides = (
+ *_ALWAYS_ON_SAFETY_OVERRIDES,
f"sqlite_home={json.dumps(str(sqlite_home))}",
f"log_dir={json.dumps(str(log_dir))}",
"cli_auth_credentials_store="
@@ -511,6 +527,12 @@ class CodexSdkRuntime:
upstream_extra_params=relay_extra_params,
drop_upstream_params=relay_drop_params,
)
+ # The relay owns the real credential for the upstream hop, so
+ # the child does not need it. Codex authenticates against the
+ # relay, which does not check this value, and an environment
+ # capture then has no secret to serialise.
+ client_env[key_var] = _RELAY_CHILD_KEY_PLACEHOLDER
+ client_env["OPENAI_API_KEY"] = _RELAY_CHILD_KEY_PLACEHOLDER
provider_id = "praxist_relay"
config_overrides += (
'model_provider="praxist_relay"',
@@ -820,6 +842,7 @@ def available_chatgpt_models() -> tuple[str, ...]:
"cli_auth_credentials_store=" + json.dumps(staged.credential_store),
f"sqlite_home={json.dumps(state_dir)}",
f"log_dir={json.dumps(state_dir)}",
+ *_ALWAYS_ON_SAFETY_OVERRIDES,
*_SUBSCRIPTION_RUNTIME_OVERRIDES,
),
env=_client_process_env("openai", os.environ, staged.path, subscription=True),
diff --git a/tests/unit/test_codex_sdk_adapter.py b/tests/unit/test_codex_sdk_adapter.py
index eaa31c6..2c64f28 100644
--- a/tests/unit/test_codex_sdk_adapter.py
+++ b/tests/unit/test_codex_sdk_adapter.py
@@ -1141,15 +1141,58 @@ class RuntimeExecutionTest(unittest.IsolatedAsyncioTestCase):
config = harness.configs[0].kwargs
overrides = config["config_overrides"]
self.assertIn('model_provider="praxist_relay"', overrides)
+ self.assertIn("features.shell_snapshot=false", overrides)
self.assertTrue(any(relay.base_url in value for value in overrides))
- self.assertEqual(config["env"]["DEEPSEEK_API_KEY"], "deepseek-test-key")
- self.assertEqual(config["env"]["OPENAI_API_KEY"], "deepseek-test-key")
+ # The relay holds the real credential (asserted above); the child process
+ # gets a placeholder so an environment capture has nothing to leak.
+ self.assertEqual(
+ config["env"]["DEEPSEEK_API_KEY"], "praxist-relay-local-no-upstream-credential"
+ )
+ self.assertEqual(
+ config["env"]["OPENAI_API_KEY"], "praxist-relay-local-no-upstream-credential"
+ )
self.assertTrue(str(config["env"]["CODEX_HOME"]).endswith("/home"))
self.assertEqual(harness.clients[0].thread_calls[0]["model"], "deepseek-v4-pro")
self.assertTrue(result.events[-1].payload["relay_used"])
self.assertEqual(harness.clients[0].close_calls, 1)
self.assertEqual(relay.close_calls, 1)
+ async def test_relay_keeps_provider_key_out_of_child_environment(self) -> None:
+ """No relay-path child env value may contain the provider credential.
+
+ Codex serialises its own environment into ``shell_snapshots/*.sh`` and
+ ``features.shell_snapshot=false`` is not honoured by openai-codex 0.147.0,
+ so the credential must not be in that environment in the first place. The
+ relay still receives it for the upstream hop.
+ """
+
+ secret = "sk-relay-child-env-secret-value"
+ runtime = CodexSdkRuntime()
+ harness = _SdkHarness()
+ relay = _Relay()
+ with (
+ tempfile.TemporaryDirectory() as tmp,
+ patch.object(adapter, "_load_sdk", side_effect=lambda: harness.sdk()),
+ patch.object(adapter, "start_relay", return_value=relay) as start,
+ ):
+ result = await runtime.execute(
+ _request(tmp, provider_ref="model_provider:deepseek_alias"),
+ AgentRuntimeExecutionContext(env={"DEEPSEEK_API_KEY": secret}),
+ )
+ await runtime.aclose()
+
+ self.assertTrue(result.success, result.error)
+ # The relay is the only component that needs the real credential.
+ self.assertEqual(start.call_args.kwargs["api_key"], secret)
+
+ child_env = harness.configs[0].kwargs["env"]
+ leaked = sorted(name for name, value in child_env.items() if secret in str(value))
+ self.assertEqual(leaked, [], f"provider key reached the child env via {leaked}")
+ # Config overrides are rendered into the Codex command line, so they must
+ # not carry it either.
+ overrides = harness.configs[0].kwargs["config_overrides"]
+ self.assertFalse([value for value in overrides if secret in str(value)])
+
async def test_openrouter_relay_uses_stable_non_secret_run_session_id(self) -> None:
runtime = CodexSdkRuntime()
harness = _SdkHarness()