"Put it in an environment variable" is the default advice for secrets, and it is good advice compared with the alternatives. The Twelve-Factor App made it popular for solid reasons: environment variables keep secrets out of the repository and are easy to change per deployment.
But an environment variable is not a vault. It is a string sitting in a process's memory, copied to every child process, visible through the filesystem to anyone with enough access, and easy to print by accident. For ordinary services that trade-off is usually fine. For code an AI agent wrote, which you have to assume might be hostile, it is worth knowing exactly where the value can go.
Path 1: the process itself
Any code running in the process can read its own environment. In Node it is process.env, in Python os.environ, in Go os.Getenv. There is no permission check, because there is nothing to check: the environment belongs to the process.
This is the one people forget when they think about agent code. If you start a sandbox with DATABASE_URL in its environment so the tests can run, then the code the agent wrote, the test files, every dependency and every install script in that sandbox can read DATABASE_URL. Isolation keeps the value away from other runs. It does nothing to keep it away from this one.
Path 2: every child process
By default, a child process inherits its parent's environment. Your agent runs npm test; npm runs a shell; the shell runs Node; Node runs the test framework; the test framework spawns workers. Every one of them gets the full environment unless someone deliberately cleared it.
That includes tools you did not think of as part of the run: a Git credential helper, a linter that phones home for updates, a build tool's telemetry. Each is code with your secret in its environment.
What helps: start subprocesses with an explicit, minimal environment when you control the call. Many process APIs accept an env argument; pass only what the child needs instead of inheriting everything.
Path 3: /proc on Linux
On Linux, /proc/<pid>/environ contains the initial environment a program was started with. The man page notes that access is governed by a ptrace access mode check, which in practice means other processes running as the same user can usually read it. Two details from the same page are worth knowing: the file shows the environment as it was at execve, so removing a variable later in the program does not remove it from /proc, and the entries are separated by null bytes.
The practical consequence: on a shared machine, any process running as your user can read the secrets in the environment of your other processes. On a developer laptop, that is every program you run. In a single-purpose sandbox, it is only the code in that sandbox, which is the right boundary to aim for.
Path 4: tooling that prints the environment
A surprising amount of tooling will print environment variables if asked, or if something goes wrong:
-
env,printenvandsetin a shell, which an agent may run while debugging. - Debug pages that dump the environment, such as PHP's
phpinfo(). - CI logs from a step that echoed a variable, or ran with shell tracing (
set -x) on. - Container inspection:
docker inspectshows a container's configured environment, as described in the Docker CLI reference, to anyone who can talk to the Docker daemon. - Crash and error reporters that attach process context to a report.
For agents, the first item matters most. A model debugging a failing connection will often run env to see what is set. If the output goes back into the model's context, your secret is now in a prompt, possibly in a log of prompts, and possibly sent to a model provider.
Path 5: the agent's own context
That last point deserves its own heading. Anything the agent reads can end up in its context window: command output, file contents, error messages. A secret that appears in any of those is one tool call away from being included in the next prompt. From there it can be echoed in a commit message, a PR description or a chat reply.
What helps: scan what goes into prompts and tool calls for credential patterns before it leaves, and treat command output that contains a secret as an incident, not just noise.
What to do instead, or as well
You will keep using environment variables. The goal is to shrink what each one is worth and how far it travels.
- Give each run only the secrets its task needs. A test run that does not talk to the database should not have the database password.
- Make them short-lived. A credential that expires when the run ends is worth little to anyone who copies it.
- Scope them narrowly. A token that can read one repository is better than one that can push to all of them.
- Keep them out of files and images. Do not write them to
.envfiles in a directory that gets snapshotted or uploaded, and never bake them into images. - Keep the network tight. A secret that cannot be sent anywhere is much less dangerous. Pair secrets with an egress allowlist.
- Watch the output. Do not feed raw
envoutput back to the model; filter or mask known secret names first.
How it works on Sandscale
On Sandscale, Vault hands a sandbox its secrets as environment variables when it starts. You reference a secret by path when you create the sandbox, and per the vault guide the value goes to the sandbox only: not to your code, the API's answer, a log, the run's record, the audit log or a model, and the audit entry names the variables, not the values. Separately, on every plan, DLP scans prompts and MCP tool calls for credentials before they go anywhere.
That keeps secrets out of your code, prompts and logs. It does not hide them from the code inside the sandbox: everything in this post about paths 1 to 4 still applies within that run. Our post on keeping secrets out of agent prompts covers which paths Vault and DLP close and which ones stay open.
Originally published on the Sandscale blog.