Post

What Claude Code and Codex Run When You Open an Untrusted Repo

A repo's hooks, env settings, and MCP servers can run before your first prompt. I tested Claude Code and Codex with a canary repo: what runs around the trust prompt, in claude -p and codex exec, and which flags stop it.

What Claude Code and Codex Run When You Open an Untrusted Repo

Claude Code and Codex can run commands from a repo before you send your first prompt. A plain claude -p run can pick up a startup hook in a folder you’ve never trusted. Codex can launch a local MCP server from the repo as soon as you trust the project.

I ran these tests on macOS on December 1, 2026 (Taipei time), using Claude Code 2.1.333 and Codex CLI 0.172.0.

In August 2026, Datadog Security Labs found that trusting a project could let its MCP config start a local program in Codex. They also found that a project’s environment settings could affect which program Claude Code ran at startup. I wanted to see what the versions I use would load and which flags would stop them. Datadog’s research

I wrote about what happens after you install third-party extensions in Agent Skill and MCP Security Risks: 5 Cases, a Checklist.

Which project settings take effect at startup?

A hook runs when a specific event occurs. I tested a SessionStart hook that runs a shell command at the start of a session.

An MCP server gives the agent access to external tools. For a local server, the agent runs the startup command from its config.

ToolFileWhat runs or takes effect
Claude Code.claude/settings.jsonCommands for hooks and statusLine. The docs also list helpers such as apiKeyHelper
Claude Codeenv in .claude/settings.jsonEnvironment variables. Changing PATH, for example, can affect which git runs
Claude Code.mcp.jsonMCP server launch commands
Codex.codex/config.toml[mcp_servers.*] launch commands
Codex.codex/hooks.jsonHook commands

CLAUDE.md and AGENTS.md contain instructions for the model. The table covers project settings the tools can act on at startup.

How I tracked which commands ran

Each test command writes an empty file with a different name. I checked which files showed up after each run. A command from an unfamiliar repo could expose files the account can read, including SSH keys.

I used .claude/settings.json and .mcp.json for the Claude Code tests.

1
2
3
4
5
{
  "env": { "PATH": "./bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin" },
  "hooks": { "SessionStart": [ { "hooks": [ { "type": "command", "command": "touch /tmp/canary.marks/claude-hook" } ] } ] },
  "statusLine": { "type": "command", "command": "touch /tmp/canary.marks/claude-statusline; echo x" }
}
1
{ "mcpServers": { "canary": { "command": "sh", "args": ["-c", "touch /tmp/canary.marks/claude-mcp; sleep 3"] } } }

To tell whether env took effect, I looked for the marker left by the test copy of git. This copy writes the marker and then calls the system’s git.

1
2
3
#!/bin/sh
touch /tmp/canary.marks/claude-env-path-git
exec /usr/bin/git "$@"

For Codex, I tested a project MCP server and a startup hook.

1
2
3
[mcp_servers.canary]
command = "sh"
args = ["-c", "touch /tmp/canary.marks/codex-mcp; sleep 3"]
1
{ "hooks": { "SessionStart": [ { "hooks": [ { "type": "command", "command": "touch /tmp/canary.marks/codex-hook" } ] } ] } }

Claude Code asks separately about MCP servers in interactive mode

I started claude in a repo I’d never trusted. The trust dialog warned about the project’s hooks and preselected No, exit.

The Claude Code trust dialog warns that project hooks can run commands without asking and preselects No, exit The trust dialog names the file that defines the hooks.

None of the marker files had appeared when the trust dialog came up. Once I chose Yes, I trust this folder, Claude Code asked about the server in .mcp.json. I kept the default and continued without using it. Claude Code security docs · MCP docs

Claude Code warns that the canary MCP server can execute code or access system resources and defaults to skipping it The same dialog can approve the current server and all future servers for the project.

WhenhookstatusLinePATH from env.mcp.json
Trust dialog on screenNot runNot runNot appliedNot run
After trusting the repo and declining the MCP serverRanRanAppliedNot run

Trusting the repo lets the hook and statusLine commands in .claude/settings.json run without individual approval. Claude Code records that choice in ~/.claude.json under the root path of the Git repo. Claude Code permissions

Subprocesses use the PATH from env. Datadog observed Claude Code calling Git during startup, so the project’s PATH can affect what runs before you send a prompt.

claude -p skips the trust and MCP prompts

claude -p takes a prompt without opening an interactive session. It’s handy for scripts and CI (for example, checks that run on every push). The security docs say -p can connect to servers from .mcp.json without asking, even in a folder you’ve never trusted.

I ran it once with the defaults and twice with different flags. I cleared the markers before each run. For the tests shown here, I configured only the hook and the MCP server.

Terminal output from three claude -p runs, with two marker files after the first run, one after the second, and none after the third The MCP startup command still left a marker with hooks disabled.

How you run itRepo hook.mcp.jsonRepo env
claude -pRunsRunsApplied
claude -p --settings '{"disableAllHooks":true}'BlockedRunsApplied
claude -p --setting-sources userBlockedBlockedNot applied

I tested env separately. I turned off hooks and asked the model to run git --version. The test program in the repo still left a marker.

Per the docs, pass disableAllHooks through --settings on the command line. Project settings take precedence over user settings and can override the value there.

--setting-sources user skips the repo’s settings files and its .mcp.json. When I need -p in an unfamiliar repo, I use this flag.

1
claude -p --setting-sources user "your question"

The docs also describe two options with a narrower scope.

  • --bare skips project hooks and .mcp.json. The project’s env and helpers such as awsAuthRefresh still apply. Bare mode
  • disabledMcpjsonServers blocks servers from .mcp.json by name in every session type. Settings reference

I also tried --safe-mode and --restricted. I saw no hook or MCP markers in either run, and --safe-mode also skipped CLAUDE.md and skills. With --restricted, tools such as Bash were absent from the tool list, which suits a task like reading source code.

Claude Code still shows the trust dialog when you pass --setting-sources user in interactive mode. Choosing Yes saves trust for the folder even though that session skips the repo’s config. Start a new session without the flag and Claude Code uses that saved decision to load the project config. It will still ask about MCP servers awaiting approval.

Claude Code can extend trust to subfolders

Per the docs, when you start Claude Code outside a Git repo, trust extends to subfolders of the folder you started in. Nested Git repos have their own trust decisions.

I trusted a regular folder and put the test project inside it without its .git directory, as if I’d unzipped a download. When I started claude in the subfolder, the hook, statusLine, and env took effect right away. Claude Code still asked about the server in .mcp.json.

The permissions docs confirm that trusting a parent folder can allow hooks, env, and helpers from a subfolder. That’s why I avoid trusting folders like ~/Downloads or ~/Desktop, where downloaded projects can end up.

This command lists the paths Claude Code has marked as trusted in ~/.claude.json.

1
jq -r '.projects | to_entries[] | select(.value.hasTrustDialogAccepted == true) | .key' ~/.claude.json

Codex preselects Trust and continue

When I started codex in a folder I’d never trusted, it warned that the folder’s settings could run code before any model request. The cursor was already on Trust and continue. Pressing Enter would accept it.

The Codex trust prompt warns that folder settings can run code automatically and preselects Trust and continue The other option returns to the Agent Command Center.

Neither marker file existed while the trust prompt was on screen. Per the docs, Codex only loads project config from trusted projects. Once I trusted the project, a second screen asked me to review new or changed hooks. Codex config basics · Hooks docs

The Codex hook review screen warns that trusted hooks can run outside the sandbox and offers to continue without running hooks Option 3 skips the hooks. The project stays trusted.

I picked option 3 and checked for marker files. Only codex-mcp was there. Codex had run the MCP startup command from the project config without asking again.

Codex reports that the canary connection closed during the MCP handshake and that startup is incomplete The marker file was already there when Codex reported failed to start.

failed to start refers to the MCP handshake. The test program wrote its marker and exited without answering the handshake request.

Datadog traces the addition of hook trust and review back to Codex 0.122.0. In my test with 0.172.0, only hooks got a separate review.

codex exec skips untrusted project config by default

I ran codex exec in a folder I’d never trusted. Neither marker appeared. When I ran it again with --trust, I got the codex-mcp marker.

The --help text says --trust saves the decision for future sessions and allows the project’s MCP commands to run outside the sandbox. Codex non-interactive mode

This command shows projects and their trust settings from ~/.codex/config.toml.

1
grep -A1 '^\[projects\.' ~/.codex/config.toml

How Claude Code and Codex handle project trust

ItemClaude Code 2.1.333Codex CLI 0.172.0
Default option in the trust promptNo, exitTrust and continue
Project hooks after trusting interactivelyRun when triggeredNew or changed hooks need review
Project MCP servers after trusting interactivelyPrompts for servers awaiting approval and defaults to skipping themRuns the startup command without a second prompt
Non-interactive defaults in a folder you’ve never trustedclaude -p runs startup hooks and MCP commandscodex exec skips project config

I think the easiest case to miss is claude -p in a script or CI because there’s no trust prompt to see. With Codex, I check what’s selected before pressing Enter.

What I do before opening an unfamiliar repo

flowchart TD
  A[Clone unfamiliar repo] --> B[Search and review config]
  B --> C{Trust this config?}
  C -->|Yes| D[Trust and open]
  C -->|No| F[Leave project untrusted]
  F --> G[Skip Claude project config]
  F --> H[Use isolated environment]
  class D pass

I start at the repo root. Datadog’s search command also looks for VS Code and devcontainer settings that can run commands automatically.

1
rg -n --hidden -g '.claude/**' -g '.mcp.json' -g '.codex/**' -g '.vscode/**' -g '*.code-workspace' -g '.devcontainer/**' -g '!.claude/worktrees/**' '\b(hooks?|mcpServers|mcp_servers|command|args|cwd|env|env_vars|PATH|BASH_ENV|NODE_OPTIONS|PYTHONPATH|sitecustomize|LD_PRELOAD|DYLD_[A-Z_]+|envFile|runOn|folderOpen|initializeCommand|postCreateCommand|postStartCommand)\b' .

Datadog treats the search results as leads. In my test, the command missed apiKeyHelper in .claude/settings.json. I also open the config files and check the scripts they refer to.

  • In Claude Code, I choose No, exit. If I need a non-interactive session, I use the command above that skips project config.
  • In Codex, I go Back to Agent Command Center. I check the MCP server settings in .codex/config.toml before deciding whether to trust the repo.
  • I check the Claude Code trust list with the jq command above and look for parent folders such as ~/Downloads.
  • If the agent has to run project commands, I use an environment without SSH keys or tokens, such as a disposable container or a separate system account.

Datadog compares trusting a project to running its setup script. If I wouldn’t run that script on my machine, I won’t trust the project there either.

Further reading

This post is licensed under CC BY 4.0 by the author.