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
.envout of the repository directory on real hosts. Put it at/etc/myapp/envor beside the binary, owned by the service account, mode0600. - Never
COPYit into an image. If a build needs a secret, useRUN --mount=type=secret,id=npmrcin BuildKit — the secret mounts for that one step and never lands in a layer. - For dev shells,
direnvloads.envrcper-directory so secrets aren't exported into every shell you ever open. - For systemd services, skip
EnvironmentFile=entirely and useLoadCredential=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 insystemctl showoutput. - If you want the file encrypted at rest,
dotenvxwraps it — but the better move is not having the file: pass secrets by file-descriptor or mounted credential, and reserve.envfor 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?
Keep reading
Related articles from the SudoSecurity blog.
RSA Is Simple. Deploying RSA Correctly Almost Never Is.
The capstone to our RSA series. The math checks out. Shipping it as written gets you breached. This is the nineteen-year-old attack that proves it, and what belongs in production instead.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
Implementing RSA, AES-GCM, and a TLS 1.3 Handshake from Scratch in Python
What is Encryption? The Math That Keeps Your Data Private
Encryption transforms readable data into unreadable ciphertext that only someone with the right key can reverse. It's what makes HTTPS, messaging apps, banking, and password storage work. Here's how it actually functions.