Skip to content

Lesson 1 of 12 · 14m

The .env File Trap

A .env file is a plaintext file your whole process can read, and everything downstream of that process inherits what it loaded.

Boot a Node app that uses dotenv and watch what actually happens. require('dotenv').config() opens .env in the working directory, splits each line on =, and assigns the results to process.env. That is the entire mechanism. No encryption, no scoping, no audit trail. From that point on, DB_PASSWORD is a plain string sitting in the environment block of the process — which every dependency, every spawned child, and every crash reporter can read.

What the file is and isn't

The mental model most people carry: the .env file is "the secrets layer." It's gitignored, so it's safe. The ignore line only stops git add from picking it up. It does nothing about every other place the file lands: the Docker image layer produced by COPY . ., the tarball your deploy script ships, the editor's .env.swp, the backup job that tars /srv/app nightly, and the CI workspace that gets archived as a build artifact. docker history on an image built with a careless COPY shows the layer; dive lets you pull the file back out of it. A layer is forever — pushing a "fixed" image doesn't rewrite the one already in the registry.

The second half of the wrong model: secrets in env vars are "in memory, so safer than files." The environment is not private memory — /proc/<pid>/environ, ps eww, docker inspect, and kubectl describe all print it, and the next lesson dissects exactly who can read it. Here the point is simpler: the file itself is the leak surface, because files get copied, archived, and snapshotted far more often than anyone audits.

The file also trains a bad habit at the repo level. .env.example with real-looking placeholder values teaches contributors to fill it in and accidentally commit the filled copy — the ignore rule covers .env, not .env.local or .env.production unless you write them all. Every .env* variant needs its own ignore line.

Real-world evidence

The Codecov bash uploader compromise in early 2021 is the clean example. Attackers modified the uploader script so it ran env and POSTed the output to an external IP. Every CI that piped that script to bash shipped its entire environment — tokens, deploy keys, cloud credentials — for roughly two months before anyone noticed. There was no fancy exploitation: the environment is just a convenient bag that already contains every secret the process holds.

Closer to home: any unhandled exception in a framework with a debug error page, a printenv in a debug endpoint, a core dump left behind by a crashing service (/proc/sys/kernel/core_pattern), or a systemd unit that logs Environment= into the journal — each one is a plaintext broadcast of the file's contents.

What to actually do

  • Keep .env out of the repository directory on real hosts. Put it at /etc/myapp/env or beside the binary, owned by the service account, mode 0600.
  • Never COPY it into an image. If a build needs a secret, use RUN --mount=type=secret,id=npmrc in BuildKit — the secret mounts for that one step and never lands in a layer.
  • For dev shells, direnv loads .envrc per-directory so secrets aren't exported into every shell you ever open.
  • For systemd services, skip EnvironmentFile= entirely and use LoadCredential=db_pass:/run/cred/db_pass — systemd places the value on a tmpfs under $CREDENTIALS_DIRECTORY, readable only by the service's user, never in the unit file, never in systemctl show output.
  • If you want the file encrypted at rest, dotenvx wraps it — but the better move is not having the file: pass secrets by file-descriptor or mounted credential, and reserve .env for non-secret config.

The test for whether a secret belongs in .env: would you be comfortable printing the file on a sticker on the laptop? The file is that exposed. Treat it accordingly.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.