Skip to content

Stop unsloth start codex and openclaw from inheriting provider API keys - #10316

Merged
danielhanchen merged 11 commits into
unslothai:mainfrom
NilayYadav:fix-start-provider-env-leak
Sep 8, 2026
Merged

danielhanchen merged 11 commits into
unslothai:mainfrom
NilayYadav:fix-start-provider-env-leak

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Only the claude command strips provider keys from the launched agent's environment. codex and openclaw inherit the user's OPENAI_API_KEY and Codex tokens from the shell. The codex constant for this already existed but was only used by the subagent bridge, never by the command itself.

For openclaw this matters more than it looks. OpenClaw turns memory search on by default and uses OpenAI for it, so a user with OPENAI_API_KEY in their shell gets their memory quietly sent to OpenAI from a command that is meant to keep everything local.

Both commands now drop these keys, and the printed no-launch recipe unsets them too. The codex subagent parent is left alone since that is the user's own cloud Codex and needs its keys.

Tests cover both commands and the recipe. They fail on main and pass here.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T02:55:31.183052Z 44a93d4 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

GitHub Actions A/B reproduction

Pinned head: 14d82b5ce19c891dd705ba4e2d90a7e720d69e56.

Two branches staged onto fresh upstream main (49dac87ab89a037a83e29776ae9c80296daf4314), running the same workflow, the same probe file and the same dependency set (click 8.2.1, typer 0.27.1, no torch, no secrets). Only the implementation under test differs. The probe sets OPENAI_API_KEY/CODEX_API_KEY/CODEX_ACCESS_TOKEN to a canary value, captures the env subprocess.run is actually called with, and separately reads back the --no-launch recipe.

Without the fix — failing run (main only):

probe watched result
codex launch env OPENAI_API_KEY, CODEX_API_KEY, CODEX_ACCESS_TOKEN leaked ["CODEX_ACCESS_TOKEN", "CODEX_API_KEY", "OPENAI_API_KEY"]
openclaw launch env OPENAI_API_KEY leaked ["OPENAI_API_KEY"]
codex --no-launch recipe same three unset_lines=[]
openclaw --no-launch recipe OPENAI_API_KEY unset_lines=[]
AssertionError: codex launch inherited ['CODEX_ACCESS_TOKEN', 'CODEX_API_KEY', 'OPENAI_API_KEY'] (each == 'sk-probe-canary-0123456789')
AssertionError: openclaw launch inherited ['OPENAI_API_KEY'] (each == 'sk-probe-canary-0123456789')
4 failed in 1.42s

The launched argv is unchanged in both cases (codex --oss --profile unsloth_api, openclaw tui --local), so the canary reaches a real agent invocation, not a short-circuited one.

With the fix — passing run (main + this head):

PROBE launch agent=codex    watched=['OPENAI_API_KEY','CODEX_API_KEY','CODEX_ACCESS_TOKEN'] leaked=[]
PROBE launch agent=openclaw watched=['OPENAI_API_KEY']                                      leaked=[]
PROBE recipe agent=codex    unset_lines=['unset OPENAI_API_KEY','unset CODEX_API_KEY','unset CODEX_ACCESS_TOKEN']
PROBE recipe agent=openclaw unset_lines=['unset OPENAI_API_KEY']
4 passed in 1.16s

The same probe also passed on the previous head b8c621e5a (run), so the follow-up commit is test-only.

Review

No blocking findings in the changed code. _CODEX_ENV_KEY is UNSLOTH_STUDIO_AUTH_TOKEN, so nothing that is cleared is re-set afterwards, and the codex profile pins base_url with requires_openai_auth = false, so no routing variable needs the same treatment.

One gap was worth closing: codex --as-subagent deliberately keeps the user's cloud credentials — it calls _run without unset_env — but nothing asserted that. 14d82b5ce adds that assertion to test_connect_codex_as_subagent_preserves_cloud_parent; wiring unset_env = _CODEX_ENV_UNSET into the subagent branch makes it fail with 'unset OPENAI_API_KEY' unexpectedly found.

Full unsloth_cli suite on this head: 1178 passed, 14 skipped.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 14d82b5ce1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed that in unsloth_cli/commands/start.py only the claude command passes unset_env, so codex and openclaw still inherit OPENAI_API_KEY and the Codex tokens from the shell, and the no-launch recipe never unsets them. Will get this reviewed.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a99bcd5d54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

command,
launch = launch,
install_hint = install_hint,
unset_env = _OPENCLAW_ENV_UNSET,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Mask the OpenAI key instead of removing it

When OpenClaw is launched natively from a project containing OPENAI_API_KEY in .env, this does not prevent the credential leak: _launch implements unset_env by deleting the variable from child_env, after which OpenClaw's documented environment loading reads the current directory's .env without overriding variables that are already defined. The key is therefore restored and can still select remote OpenAI embeddings for memory search. Pass an explicit empty OPENAI_API_KEY for the OpenClaw process (as the generated one-line recipe already does), or explicitly disable/configure remote memory search.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenClaw does not read OPENAI_API_KEY from a project .env, so there is nothing to mask here. loadDotEnv() loads <cwd>/.env through loadWorkspaceDotEnvFile, which drops every entry rejected by shouldBlockWorkspaceDotEnvKey, and OPENAI_API_KEY is in BLOCKED_PROVIDER_AUTH_WORKSPACE_DOTENV_KEYS (openclaw/openclaw src/infra/dotenv.ts, in place since #54631 and #57767). The global runtime dotenv cannot restore it either: the command sets OPENCLAW_STATE_DIR to the session dir, so loadGlobalRuntimeDotEnvFiles reads <session>/.env and skips ~/.config/openclaw/gateway.env.

The one path that does refill a deleted key is the login-shell import, and it only runs when the user sets OPENCLAW_LOAD_SHELL_ENV=1 or enables env.shellEnv in their own config, which the session config here replaces. Asking OpenClaw to import your login shell is an explicit opt-in, so it stays the user's call.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: a99bcd5d54

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Pushed one follow-up to the branch, 648422a.

The openclaw list was missing CODEX_API_KEY. OpenClaw resolves its openai provider from CORE_PROVIDER_AUTH_ENV_VAR_CANDIDATES.openai = ["CODEX_API_KEY", "OPENAI_API_KEY"], so CODEX_API_KEY is tried first and dropping only OPENAI_API_KEY left the leak open for anyone who has it set.

Verified against the real client (openclaw 2026.9.2) driven by the child environment this command builds, using the session config write_openclaw_config actually writes. With CODEX_API_KEY=sk-fake-... in the parent shell and the old one-entry list, openclaw memory status --deep reached api.openai.com and came back with a 401 echoing that key. With both entries dropped it reports No API key found for provider "openai" and makes no call. Memory search is on by default in that config (Provider: openai, text-embedding-3-small, sources memory, sessions), which is the path your description is about.

Also checked the boundary so the list stays minimal: CODEX_ACCESS_TOKEN and ANTHROPIC_API_KEY do not resolve for openai embeddings, so neither belongs here. And with no key at all, openclaw memory search still returns keyword hits and exits 0, so this degrades rather than breaking.

unsloth_cli/tests/test_start.py and test_codex_subagent_mcp.py pass, 616 total. The openclaw case of test_launch_drops_provider_credentials fails on the one-entry list and passes now.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 648422aba7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

oobabooga pushed a commit to oobabooga/unsloth-test that referenced this pull request Sep 7, 2026
…hell

OPENCLAW_LOAD_SHELL_ENV makes OpenClaw run a login shell and import any provider
key it cannot see. The keys dropped for the launch are exactly the ones it looks
for, so on a machine with that switch set the session got them straight back out
of the user's profile and the strip did nothing.

Verified against openclaw 2026.9.2: with the switch on, loadShellEnvFallback
applied CODEX_API_KEY and OPENAI_API_KEY to the child environment this command
builds. Setting the switch to 0 in the child reports skippedReason disabled and
applies neither.

OPENCLAW_STATE_DIR stays a real path on purpose, since OpenClaw falls back to
the user's home when it is blank.
@danielhanchen

Copy link
Copy Markdown
Member

Merged latest main into the branch and pushed one follow-up, f7d608f9f.

Merge conflict

One conflict, in unsloth_cli/tests/test_start.py, and it was purely additive: this branch appended test_launch_drops_provider_credentials at the end of the file while main had gained the five test_openclaw_memory_search_* tests from #10320. Kept both, nothing dropped. unsloth_cli/commands/start.py merged clean.

The strip was being undone on the openclaw side

OPENCLAW_LOAD_SHELL_ENV makes OpenClaw run a login shell and import any provider key it cannot currently see. The keys it looks for are resolved from listKnownProviderAuthEnvVarNames, which for openai is exactly CODEX_API_KEY then OPENAI_API_KEY (src/secrets/provider-env-vars.ts:22-29), the two this branch drops. hasExplicitEnvBinding is Object.hasOwn (src/infra/shell-env.ts:263-265), so a deleted name reads as missing and is refilled, while a name that is present but blank counts as an intentional override and is left alone.

So on a machine with that switch set in the shell, the session got both keys straight back out of the user's profile, and the launch strip did nothing. The WSL path was already immune, since the bridge blanks rather than deletes.

Driven against the real client (openclaw 2026.9.2, its own loadShellEnvFallback from the published dist/), fed the exact child environment this command builds, with a scratch HOME whose profile exports a canary:

tree child had the key directly fallback verdict key present after
main CODEX_API_KEY, OPENAI_API_KEY already-has-keys both
this branch at 648422a none applied CODEX_API_KEY, OPENAI_API_KEY both
with f7d608f9f none disabled none

f7d608f9f sets OPENCLAW_LOAD_SHELL_ENV=0 in the child env beside the two scoping vars. "0" is in OpenClaw's falsy set (src/utils/boolean.ts:19-20), so shouldEnableShellEnvFallback is false and the login shell is never probed. OPENCLAW_STATE_DIR deliberately stays a real path, since OpenClaw falls back to the user's real home when it is blank.

Four new tests cover it, parametrized over 1/true/yes/on, plus the --no-launch recipe. They fail on 648422a and pass here.

Rest of the review

No other blocking findings.

  • codex --as-subagent returns before the modified _run, so the cloud parent keeps its credentials. Confirmed by launching a real recording executable through that path.
  • Nothing self-inflicted: _CODEX_ENV_KEY is UNSLOTH_STUDIO_AUTH_TOKEN, which is in neither list, and _launch does child_env.update(env) after the removals. Worth stating because codex raises EnvVar when a provider's env_key is unset or empty, and requires_openai_auth = false does not exempt it (bearer_auth_for_provider runs before that short-circuit). If env_key ever moved onto a strip list, every session would fail on its first request.
  • The generated provider writes no env_http_headers, so OPENAI_ORGANIZATION and OPENAI_PROJECT cannot ride along to the local server. OPENAI_BASE_URL is not read for routing on codex 0.115.0 and later, and only ever fed the built-in openai provider rather than a config-pinned custom one, so neither needs adding here.
  • Cross-platform: the recipe already renders per platform from unset_env, so codex and openclaw pick up Remove-Item Env:NAME on native Windows and export NAME= plus the WSLENV bridge under WSL for free. Checked Linux and macOS with a real exec, Windows at the _launch boundary, and the WSL bridge with the shim detector engaged. Also checked a grandchild process, so an MCP server started by the agent is clean too.
  • Backwards compatible: no config format changes, an older config.toml keeps its own [model_providers.*], an older openclaw.json keeps its unrelated keys, and the codex_subagent_mcp bridge still shares the same _CODEX_ENV_UNSET tuple.

One thing this cannot reach, for a separate issue

OpenClaw imports Codex CLI credentials from ~/.codex/auth.json (and the macOS Keychain item Codex Auth) through external-cli-sync, for provider openai. resolveCodexCliHomePath reads the real OS home unless CODEX_HOME is set, and that is deliberate upstream, so no amount of env stripping closes it. Scoping CODEX_HOME for the openclaw session would, but it would also make codex look logged out if run from inside that session, so that trade-off seemed worth raising rather than deciding here.

Full unsloth_cli suite on this head: 1232 passed, 9 skipped. The 44 failures in test_inference_chat.py, test_studio_secure_flag.py and test_studio_cloudflare_flag.py reproduce identically on unmodified main.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added 2 commits September 7, 2026 12:26
OpenClaw ORs env.shellEnv.enabled with OPENCLAW_LOAD_SHELL_ENV, so a persisted
true wins over the environment guard and the login shell import runs anyway.
write_openclaw_config preserves the top-level env section, so a --persist or
reused config carried that opt-in into the session and handed the provider keys
back after the launch dropped them.

Verified against openclaw 2026.9.2 with a config holding env.shellEnv.enabled
true: the fallback applied CODEX_API_KEY and OPENAI_API_KEY before this, and
reports skippedReason disabled after.
@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 36efe065d5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

oobabooga pushed a commit to oobabooga/unsloth-test that referenced this pull request Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 0ca9c98c99

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

oobabooga added a commit to oobabooga/unsloth-test that referenced this pull request Sep 8, 2026
oobabooga added a commit to oobabooga/unsloth-test that referenced this pull request Sep 8, 2026
@danielhanchen
danielhanchen merged commit 05d98ea into unslothai:main Sep 8, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants