Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Codex CLI Sandboxing and Security

Codex CLI Sandboxing and Security

Codex CLI has built-in sandboxing mechanisms and security policies to protect your system and data while helping you program. This section details these security mechanisms and how to configure them.


Sandbox mechanism

Codex executes commands and manipulates files in an isolated sandbox environment. This design ensures:

The sandbox is the first line of defense in Codex's security strategy, ensuring the AI's actions do not exceed your expected scope.


execution policy

Codex uses execution policies (Execution Policy) to control the behavior of different types of operations:

Strategy type

Strategy Behavior Applicable scenarios
ask Ask for confirmation before each execution Daily development (default)
approve Automatically approve execution Fully trusted environment
deny Reject all operations that may have side effects Read-only mode

Operations requiring confirmation

The following types of operations trigger confirmation requests:

Configure execution policy

Set the execution policy in the configuration

[exec]

default_policy = "ask"

For security reasons, it is not recommended to set the default policy to approve, unless you fully understand the potential risks.


Confirmation dialog

When Codex needs to perform a sensitive operation, it displays a confirmation dialog:



══════════════════════════════════════════════════════

  确认执行

══════════════════════════════════════════════════════



  Codex plans to perform the following operations:



  命令: rm -rf node_modules/

  目录: /path/to/project



  这将永久删除目录及其所有内容



  [Y] 确认执行  [N] 取消  [A] 始终允许此类操作

══════════════════════════════════════════════════════

Option descriptions

Option Description
Y Execute this once
N Cancel the operation
A Add a rule to auto-approve in the future

Be careful when choosing "Always allow", ensuring the operation is truly safe.


File access control

Protected directories

Certain system directories are protected by default, and Codex will ask for extra confirmation when accessing them:

directory Description
~/.ssh/ SSH keys and configuration
~/.aws/ AWS credentials
/etc/ System configuration
~/.git-credentials Git credential storage

Custom protection rules

You can add custom protection rules in the configuration:

Configure file access rules

[sandbox]

Allowed directories

allowed_paths = [

    "~/projects/*",

    "/workspace/*"

]

Protected directories (require extra confirmation)

protected_paths = [

    "~/.ssh/*",

    "~/secrets/*"

]


Network access control

Codex can restrict the access scope of network requests:

Configure network access

[network]

Allowed domains

allowed_domains = [

    "github.com",

    "api.openai.com"

]

Whether to allow local network access

allow_localhost = false

Whether to allow private networks

allow_private_network = false

Restricting network access prevents Codex from accidentally connecting to untrusted services.


Working directory restrictions

You can restrict Codex to operate only in specific directories:

Restrict working directories

[sandbox]

Codex can only operate in these directories

working_directories = [

    "~/projects/*",

    "/workspace/*"

]

Behavior when outside directories: warn or deny

out_of_bounds = "deny"


Command whitelist/blacklist

Whitelist mode (recommended)

Only allow explicitly listed commands to execute:

Command whitelist

[commands]

Whitelist mode

whitelist_enabled = true

allowed_commands = [

    "git",

    "npm",

    "node",

    "python",

    "cargo"

]

Blacklist mode

Prohibit specific commands from executing:

Command blacklist

[commands]

Blacklist mode

blacklist_enabled = true

blocked_commands = [

    "rm -rf /",

    "dd",

    "mkfs"

]

Whitelist mode is more secure and is recommended for production environments or when handling sensitive projects.


Sensitive data handling

Automatic masking

Codex automatically detects and masks sensitive information:

Environment variable protection

Certain environment variables are automatically hidden:



AWS_ACCESS_KEY_ID

AWS_SECRET_ACCESS_KEY

OPENAI_API_KEY

DATABASE_URL

Manually specify sensitive data

You can manually mark content to be protected in the configuration:

Custom sensitive data patterns

[sensitive]

Regex patterns to be masked

patterns = [

    "password\s*=\s*[^\s]+",

    "api_key\s*=\s*[^\s]+",

    "secret\s*=\s*[^\s]+"

]


Audit logs

Codex can record audit logs of all operations:

Enable audit logging

[audit]

enabled = true

log_file = "~/.codex/log/audit.log"

Log content

Audit logs record the following information:

View audit logs

View recent audit logs

tail

-f

~

/

.codex

/

log

/

audit.log

Audit logs are important for security auditing and troubleshooting, especially in team environments.


Security best practices

Development environment

Production environment

Sensitive projects


Common questions

Q: Where is the execution policy configured?

The execution policy is mainly set through ~/.codex/config.toml the configuration file. Some settings can also be configured via environment variables.

Q: What if I accidentally clicked "Always allow"?

Delete the relevant rule in the configuration file, or delete the entire configuration file and restart Codex, and the rules will be reset.

Q: Can Codex access my GitHub token?

Codex requests the necessary GitHub permissions to perform operations. You can revoke these permissions at any time in GitHub settings.

Q: How to completely disable file modifications?

Set the execution policy to deny, which will prohibit Codex from performing any operation that may modify the file system.