Skip to content

fix(security): remove startup secrets from the environment before child processes spawn - #136

Merged
BaaRai merged 2 commits into
mainfrom
fix/shell-env-secrets
Nov 3, 2026
Merged

BaaRai merged 2 commits into
mainfrom
fix/shell-env-secrets

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Problem

ARTEX_PG_DSN (it contains the Postgres password), ANTHROPIC_API_KEY and OPENAI_API_KEY stay in the server's process environment for the whole run. Child processes copy os.Environ() when they are spawned:

  • agent Bash shells and background tasks: norma tool/bash.go, tool/task.go, tool/shell_session.go (BaaRai/Norma v0.4.4, the replace target in go.mod)
  • MCP stdio servers: norma mcp/stdio.go
  • custom tools: server/customtool.go:400

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

Fix

The three variables are read only at startup:

  • ARTEX_PG_DSN: config.PostgresDSN through db.DSN, called only from server.NewManager (server/manager.go:379); the other callers are tests
  • the API keys: agent.FromEnv (agent/provider.go:207), called only from server.New (server/server.go:247)

main now calls scrubSecretEnv() right after server.New returns, which unsets the three variables. All the spawn paths above read the environment when the child starts, so this one change in cmd/artex covers them; nothing changes in norma or in customtool.go.

Later provider builds do not depend on these variables: the profile paths reject an empty key (loadProfileConfig) or reuse the key already in memory (s.llmCfg.APIKey) before calling NewProvider, so norma's llm.NewProvider env fallback is never reached with an empty key.

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 process running as the same user can still read /proc/<artex pid>/environ (and, in Docker, the environment of PID 1). So this does not fully protect the secrets. Closing that path needs agent shells running as another user, or secrets passed some other way than the environment (for example a mounted file).
  • Startup goroutines: server.New starts a few goroutines before it returns. On a first run, discoverEmptyMCPsOnStartup can spawn an MCP stdio server for tool discovery before the scrub runs. That process is short-lived.
  • Other variables that may carry credentials (for example ARTEX_LLM_PROXY) are not touched.

Verification

WSL (kali), Go 1.26.7, throwaway postgres:16-alpine 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 -buildvcs=false ./...: OK
  • gofmt -l on the changed files: clean
  • go test ./server ./agent ./cmd/artex ./config ./db -count=1: agent, cmd/artex, config, db pass. server fails only on TestCoreTaskLifecyclePG, which is known to fail in this environment on timing and is unrelated to this change.
  • New test TestScrubSecretEnvHidesSecretsFromChildren (cmd/artex): sets the three variables plus a normal one, calls scrubSecretEnv, runs sh -c env, and checks that none of the three reaches the child while the normal variable does. It fails when scrubSecretEnv is turned into a no-op and passes with it.

Not verified: a Docker build and run, and an end-to-end agent shell session.

BaaRai and others added 2 commits November 3, 2026 21:46
…ld processes spawn

ARTEX_PG_DSN, ANTHROPIC_API_KEY and OPENAI_API_KEY are read only during
startup (server.NewManager and agent.FromEnv in server.New), but stayed in
the process environment. Agent shells, custom tools and MCP stdio servers
copy os.Environ() when they start, so they could read the database password
and the LLM keys. main now unsets the three variables right after server.New
returns.
The scrub ran in main after server.New returned, but New resumes every
non-terminal task before returning, and those loops start workers that run
shells. Move the scrub into New, right after both environment readers, and
start MCP discovery after it as well: both paths spawn child processes.

New takes an option rather than scrubbing unconditionally. The test suite
builds several servers in one process and still needs ARTEX_PG_DSN on later
calls, so only the binary opts in.
@BaaRai
BaaRai force-pushed the fix/shell-env-secrets branch from 428a1b2 to e6f4de5 Compare November 3, 2026 21:14
@BaaRai
BaaRai merged commit d45f0c7 into main Nov 3, 2026
2 checks passed
neartana pushed a commit to neartana/ARTEX that referenced this pull request Nov 3, 2026
New accepted opts ...Option but never called them, so scrubSecrets stayed
false in the binary and ScrubSecretEnv was unreachable: the protection merged
in BaaRai#136 never engaged. The unit test passed because it called the scrub
function directly instead of going through New.

Apply the options in the constructor and add a test that builds a Server
through New with WithSecretScrub and asserts the secrets are gone. Verified to
fail without the loop.
@BaaRai BaaRai mentioned this pull request Nov 4, 2026
BaaRai added a commit that referenced this pull request Nov 4, 2026
Move the Unreleased entries into a 0.5.0 section and add the changes merged since v0.4.0 (#128, #136-#147), with known issues.
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