Skip to content

fix(security): drop startup secrets from the environment before agents spawn shells - #134

Closed
BaaRai wants to merge 1 commit into
mainfrom
fix/shell-env-secrets
Closed

BaaRai wants to merge 1 commit into
mainfrom
fix/shell-env-secrets

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Problem

ARTEX_PG_DSN (it holds the Postgres password), ANTHROPIC_API_KEY and OPENAI_API_KEY stay in the server's process environment for the whole run. Every subprocess copies os.Environ() when it is spawned:

  • the agent Bash shell and background tasks: norma tool.withEnv (tool/task.go, in the BaaRai/Norma v0.4.4 fork)
  • MCP stdio servers: norma mcp/stdio.go
  • custom tools: server/customtool.go:400, c.Env = append(os.Environ(), sessionEnv...)

So an agent, or a prompt injection steering it, can read the database password or the LLM key with env.

Fix

These three variables are read only at startup:

  • ARTEX_PG_DSN: config.PostgresDSN via db.DSN, called only from server.NewManager (all other callers are tests)
  • the API keys: agent.FromEnv, called only from server.New

main now unsets them right after server.New returns (scrubSecretEnv). All the spawn paths above read the environment when the process starts, so this one change covers them. Nothing needs to change in the norma fork or in customtool.go.

Nothing in ARTEX relies on norma's llm.NewProvider falling back to ANTHROPIC_API_KEY / OPENAI_API_KEY later. Every profile path rejects an empty key or reuses the key held in memory (s.llmCfg.APIKey) before it builds a provider. A restart still works: start.sh relaunches artex with its own unchanged environment.

Limits (not fixed here)

  • /proc/<pid>/environ: os.Unsetenv changes the Go runtime's copy of the environment, not the block the kernel recorded at exec. A shell running as the same user can still read /proc/<artex pid>/environ. In Docker it can also read /proc/1/environ from start.sh, which is PID 1 and keeps the full environment. Closing that path means running agent shells as another user, or not passing the secrets through the environment (for example a mounted secrets file). Both are out of scope for this PR.
  • Startup goroutines: server.New starts some goroutines before it returns. The one that matters is discoverEmptyMCPsOnStartup, which on a first run can spawn an MCP server before the scrub. That discovery process is short-lived. Agent shells start only after an LLM round trip, so they are spawned after the scrub.
  • ARTEX_LLM_PROXY can carry proxy credentials and is not scrubbed. It is not in the scope of this change.

Verification

Run in WSL (kali), Go 1.26.7, against a throwaway postgres:16-alpine container on port 55432 (removed afterwards):

  • go build -buildvcs=false ./...: OK. -buildvcs=false only because the worktree's git dir is a Windows path.
  • go vet ./...: OK
  • gofmt: the changed files are clean
  • go test ./server ./agent ./cmd/artex ./config ./db -count=1: all pass except TestCoreTaskLifecyclePG in ./server. That test is known to fail in this environment for timing reasons, and it fails the same way on the PR2 branch. There is no ./tool package in this repo; the shell code lives in norma.
  • New test TestScrubSecretEnvHidesCredentialsFromSubprocesses (cmd/artex): sets the three variables, calls scrubSecretEnv, spawns sh -c env, and asserts that none of them reaches the child while a non-secret variable still does. Passes.

Not verified: a real docker compose build and run, and an end-to-end agent shell session. I did not touch the live stack.

🤖 Generated with Claude Code

…s spawn shells

ARTEX_PG_DSN, ANTHROPIC_API_KEY and OPENAI_API_KEY are read once at
startup (server.NewManager, agent.FromEnv in server.New) and never again.
They stayed in the process environment, and every subprocess copies
os.Environ() when it is spawned: the agent Bash shell and background
tasks (norma tool.withEnv), MCP stdio servers and custom tools
(server/customtool.go). An agent could print the database password or the
LLM key with `env`.

Unset them in main right after server.New returns. Adds a test that a
shell spawned afterwards no longer sees them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@BaaRai BaaRai closed this Nov 3, 2026
@BaaRai
BaaRai deleted the fix/shell-env-secrets branch November 3, 2026 20:31
@BaaRai BaaRai mentioned this pull request Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant