Handle secrets in app environments

Open 💬 7 comments Opened Feb 3, 2026 by heddendorp
💡 Likely answer: A maintainer (etraut-openai, contributor) responded on this thread — see the highlighted reply below.

What variant of Codex are you using?

App

What feature would you like to see?

I like the app and the idea of setup scripts but have some secrets in .env and .npmrc that are needed in a new worktree of my project.
It would be nice to have a way to carry such files over as they are not checked in.

Additional information

_No response_

View original on GitHub ↗

7 Comments

etraut-openai contributor · 6 months ago

Check out the "Environments" tab in the settings panel. This feature allows you to specify a setup script that runs when a worktree is created. You can use this script to copy over you .env files.

piotrkulpinski · 6 months ago

is there a $CODEX_ROOT_PATH-like variable I can use to reference the main project path when copying the .env files?

jkadamczyk · 6 months ago

This worked for me in the end, but can't guarantee if this is a good way to handle this that works in 100% of the cases

COMMON_GIT_DIR="$(cd "$(git rev-parse --git-common-dir)" && pwd -P)"
MAIN_WORKTREE="$(cd "${COMMON_GIT_DIR}/.." && pwd -P)"

MAIN_WORKTREE variable is now your root worktree path and you can copy .env files to the new one.

jperezrealini · 6 months ago

<details>
<summary>This is the setup script I use for copying .env files using @jkadamczyk's approach:</summary>

<br>

copy_env_files() {
  local src="${1%/}"
  local dst="${2%/}"

  find "$src" \
    \( -type d \( \
      -name '.git' -o \
      -name 'node_modules' -o \
      -name '.next' -o \
      -name '.turbo' -o \
      -name 'dist' -o \
      -name 'build' -o \
      -name 'coverage' -o \
      -name '.vercel' -o \
      -name '.cache' \
    \) -prune \) -o \
    \( -type f -name '.env*' -print0 \) | while IFS= read -r -d '' file; do
    local rel="${file#$src/}"
    local target="$dst/$rel"
    mkdir -p "$(dirname "$target")"
    cp "$file" "$target"
    echo "Copied: $rel"
  done
}

COMMON_GIT_DIR="$(cd "$(git rev-parse --git-common-dir)" && pwd -P)"
MAIN_WORKTREE="$(cd "${COMMON_GIT_DIR}/.." && pwd -P)"

copy_env_files "$MAIN_WORKTREE" "$(pwd -P)"

</details>

llermaly · 4 months ago

there should be a .worktreeinclude file

mithatakbulut · 9 days ago

I ran into the same class of problem while thinking about Git worktrees and coding agents.

I ended up building local.env, an OSS tool that keeps local development env requirements tied to the repo while syncing the actual values separately through a CLI.

It doesn’t copy the entire working directory into a new worktree. The goal is more that a fresh checkout/worktree can figure out which local env values it needs and retrieve them securely instead of relying on manually copying .env.local.

I’m curious whether that model would actually help with the Codex worktree workflow described here:

https://github.com/mithatakbulut/local.env

shleder · 2 hours ago

Regarding handling and isolating ambient secrets in Codex environments:

Relying on application-level prompt rules or .gitignore is insufficient because subagents and shell tools (cat, env, grep, python scripts) still inherit full read access to ambient environment variables and local files.

To enforce deterministic, kernel-level secret boundaries on macOS and Linux:

Vetto wraps Codex in an immutable OS perimeter:

# Wrap Codex with fail-closed secret masking:
npx @shledery/vetto --agent codex --profile strict -- codex

How Vetto protects secrets:

  • VFS Path Masking: Sensitive paths (~/.ssh, ~/.aws, .env, .env.*) are mapped to /dev/null at the kernel layer (Landlock/Seatbelt). Reading them returns EOF.
  • Environment Sanitization: Strips parent credentials before spawning child shells.
  • Live PTY Redaction: Streams terminal stdout through an Aho-Corasick + Shannon entropy filter to mask leaked API keys (sk-proj-..., ghp_...) before they hit the terminal or disk.