Direct answer
The sandbox limits which resources commands can affect, while approval policy determines when to ask the user. Combine these distinct controls according to least privilege.
What this guide helps you solve
Secure configuration of Codex CLI sandbox, network and command approval
Distinguish between sandbox and approval
The sandbox defines where commands can read, write and use the network; the approval policy determines whether actions require human confirmation. Setting approval to never does not expand the sandbox, and danger-full-access does not mean every action receives review.
prompt and AGENTS.md can only affect model intent and cannot replace runtime security controls. When dealing with untrusted repositories, file, network, and approvals should be tightened at the same time.
Select sandbox mode by task
read-only is suitable for investigation, code explanation and solution review; workspace-write is suitable for implementation in most repositories; danger-full-access allows wider system access and is only suitable for environments that have external isolation provided by containers or virtual machines.
Don't just open up the entire system just because a cache directory is needed for one build. Prioritize adding the minimum necessary directory through --add-dir, or adjusting the build cache location.
codex -s read-only "分析失败原因,不要修改文件。"
codex -s workspace-write "实现已确认的修复并运行测试。"
# 仅在外部隔离环境中评估是否需要:
codex -s danger-full-accessSelect approval strategy
untrusted runs only trusted read-only commands directly and requests confirmation for others. on-request lets the model request elevated access when necessary. never initiates no approval flow and returns a restricted-action failure directly to the model.
On-request is usually used for local interactive development, and untrusted can be used to review unfamiliar repositories. never is more suitable for tasks whose boundaries are already fixed by the CI sandbox and cannot wait for human input.
codex -s read-only -a untrusted
codex -s workspace-write -a on-request
codex exec -s workspace-write -a never "运行检查并输出结果;不要修改源文件。"Only open necessary directories and networks
When tasks need to access adjacent workspaces, use --add-dir to explicitly add them instead of setting the working root directory to a common parent directory. Networking needs should also differentiate between dependent downloads, API calls, and web searches.
Turning on --search provides native web search tools to the model; it is not a universal switch for all network requests. When working with private code, you still need to follow your organization's data egress policy.
codex -C /work/app --add-dir /work/shared -s workspace-write
codex -C /work/app -s read-only --search "只查阅官方来源,核对这个公开 API 的当前参数。"Identify high-risk options for bypassing controls
Options that bypass approvals and sandboxing allow model-generated commands to run directly on the host. Use them only in automation where containers, virtual machines or disposable runners already provide reliable isolation.
Do not use on personal workstations, production servers, hosts containing cloud credentials, or untrusted repositories. Tutorials shouldn't package it as a "confirmation-free trick" either.
Establish a least privilege baseline for your team
Team baselines should be defined by work scenarios rather than personal preferences. Interactive development, read-only review, CI fixes, and production diagnostics require different combinations and are constrained by project configuration and runtime environment.
Regularly review approved commands, additional directories and network scopes. Broad access granted for temporary troubleshooting should not remain in shared configuration.
- Unfamiliar repositories are read-only first.
- Daily implementation prioritizes workspace-write + on-request.
- CI must have external isolation when using the non-interactive policy.
- Secrets do not go into ordinary files the model can read.
- Destructive commands are always controlled by humans or specialized processes.
Official sources and verification scope
This guide is based on public official documentation. Commands and configuration may change between client versions, so verify the linked sources before use.
Read the verification methodology