# Secrets in environment variables: who can actually read them

> Environment variables are the standard way to hand a program a secret, and the standard way it leaks. Every path that can read an env var on Linux, and what that means for code an AI agent runs.

Environment variables are the standard way to hand a program a secret, and the standard way it leaks. Every path that can read an env var on Linux, and what that means for code an AI agent runs.

November 23, 2026 · 5 min read · The Sandscale team

“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](https://12factor.net/config) 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](https://man7.org/linux/man-pages/man5/proc_pid_environ.5.html) 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`, `printenv` and `set` in 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 inspect` shows a container’s configured environment, as described in the [Docker CLI reference](https://docs.docker.com/reference/cli/docker/inspect/), 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.

1. **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.
2. **Make them short-lived.** A credential that expires when the run ends is worth little to anyone who copies it.
3. **Scope them narrowly.** A token that can read one repository is better than one that can push to all of them.
4. **Keep them out of files and images.** Do not write them to `.env` files in a directory that gets snapshotted or uploaded, and never bake them into images.
5. **Keep the network tight.** A secret that cannot be sent anywhere is much less dangerous. Pair secrets with an [egress allowlist](https://sandscale.dev/blog/egress-allowlist-design-for-agent-sandboxes).
6. **Watch the output.** Do not feed raw `env` output 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](https://sandscale.dev/docs/guides/vault-secrets) 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](https://sandscale.dev/security/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](https://sandscale.dev/blog/secrets-out-of-agent-prompts-vault-dlp) covers which paths Vault and DLP close and which ones stay open.

**On this page**

1. Path 1: the process itself
2. Path 2: every child process
3. Path 3: /proc on Linux
4. Path 4: tooling that prints the environment
5. Path 5: the agent’s own context
6. What to do instead, or as well
7. How it works on Sandscale

**Run your agents in a box of their own**

Each sandbox is a Firecracker microVM. Free includes 100 awake vCPU-hours a month.

[Start free](https://cloud.sandscale.dev/auth/signup)

## Keep reading

- [November 23, 2026 · **A checklist for reviewing a pull request an AI agent wrote** · Agent-written PRs fail in different places than human ones. A practical review checklist: what to read first, the lines that deserve the most suspicion, and what to run before you approve.](https://sandscale.dev/blog/reviewing-an-agent-diff-checklist)
- [November 23, 2026 · **A threat model for AI coding agents: what can go wrong, and where to put the controls** · A practical threat model for coding agents: the assets at risk, the ways untrusted text gets in, the attacker goals, and which control belongs at which layer.](https://sandscale.dev/blog/threat-model-for-ai-coding-agents)

---
Source: https://sandscale.dev/blog/secrets-in-environment-variables-risks
