# Secrets and API keys in OpenAI Codex CLI: keep credentials out of the agent's reach

Source: https://botpassword.com/agents/codex
Last checked: 2026-10-25

Codex CLI is OpenAI's terminal coding agent. It supports MCP servers over stdio and streamable HTTP, configured in `~/.codex/config.toml` (or `.codex/config.toml` for trusted projects) or with `codex mcp add`. It reads `AGENTS.md` for project guidance and controls which environment variables reach commands through `shell_environment_policy`.

## MCP support

Yes. Transports: stdio, http.

- user: `~/.codex/config.toml`
- project: `.codex/config.toml`

Add a server with `codex mcp add <server-name> --env VAR1=VALUE1 -- <stdio server-command>`.

## Environment variables and .env files

Each `[mcp_servers.<name>]` table can set `env` (values for the server), `env_vars` (names of local variables to forward), `bearer_token_env_var` and `env_http_headers` (header name to variable name) so that values stay out of the file. For commands Codex runs, `[shell_environment_policy]` has `inherit` (`none` or `core`), `set`, `ignore_default_excludes` and `filters` with `exclude` or `include` patterns. The advanced-config page states `ignore_default_excludes` defaults to `true`, so names containing `KEY`, `SECRET` or `TOKEN` are not removed automatically unless you set it to `false`. The documentation reviewed does not describe loading `.env` files.

## File access

Commands run inside a sandbox set by `sandbox_mode`. In `workspace-write`, the workspace is writable, `network_access` defaults to `false`, and some environments keep `.git/`, `.agents/`, `.codex/` and `.aws/` read-only. `danger-full-access` disables sandboxing. The pages reviewed do not state the default mode or a way to deny reads of specific files.

## Credential risks

- **CVE-2025-61260 (Codex CLI 0.23.0 and earlier).** Check Point reported that Codex automatically loaded a project-local `.env` and `.codex/config.toml` without confirmation. A committed `.env` could redirect `CODEX_HOME` to a project directory, and an MCP entry there could run arbitrary commands when the developer ran `codex`. OpenAI fixed it in 0.23.0.
- **Secret-looking variables are inherited by default.** With `ignore_default_excludes = true` (the documented default), names containing `KEY`, `SECRET` or `TOKEN` are not stripped from commands the agent runs, so an exported API key is visible to anything it executes unless you set `inherit = "none"` or add `exclude` filters.

## FAQ

### Where is the Codex CLI MCP configuration file?

`~/.codex/config.toml` for your user, shared by the CLI, the IDE extension and the desktop app. A trusted project can also use `.codex/config.toml`.

### How do I keep API keys out of Codex config.toml?

Use `env_vars = ["NAME"]` to forward a variable from your shell, `bearer_token_env_var` for an HTTP server, or `env_http_headers` to map a header to a variable name.

### How do I stop Codex passing secrets to commands it runs?

Set `[shell_environment_policy]` with `inherit = "none"` or `"core"`, or `ignore_default_excludes = false` plus `exclude` patterns such as `*KEY*`, `*SECRET*` and `*TOKEN*`.

### How do I give OpenAI Codex CLI an API key without it seeing the key?

Don't put the key in the prompt, in a file OpenAI Codex CLI can read, or in an environment variable it can print. Give it a tool that performs the authenticated step instead: a credential broker exposed over MCP or a CLI that injects the key into the command or HTTP request and returns only the result. BotPassword is that broker, through `bpw mcp` and the `bpw` CLI.

## Sources

- https://learn.chatgpt.com/docs/extend/mcp?surface=cli
- https://learn.chatgpt.com/docs/config-file/config-advanced
- https://research.checkpoint.com/2025/openai-codex-cli-command-injection-vulnerability/
- https://nvd.nist.gov/vuln/detail/cve-2025-61260
