Track 09 · Lesson 2 of 9Shipping safely
Secrets in git and Docker
Lesson 2 of 9· 8 min
Deleting a secret in a new commit doesn’t remove it. Every old commit, tag, clone and fork still has it, and so does every image built while it was there. The fix is to make the leaked value worthless.
Clean up a committed key
Try the steps in any order
- Working key
The latest code
the file on main
- Working key
Every older commit
git log -p finds it
- Working key
Tag v1.0
points at the old commit
- Working key
Teammates’ clones
until each one re-clones
- Working key
A fork on GitHub
you can’t rewrite someone else’s repo
- Working key
Image build-41 in the registry
docker history shows it
Rotate, revoke, then tidy#
- Rotate: create a new key and deploy with it.
- Revoke: kill the old one at the provider, so it stops working everywhere.
- Review the provider’s access logs for the time it was exposed.
- Purge, optionally:
git filter-reporewrites history, at the cost of every commit hash changing and every clone needing replacing. Once the key is dead, this is tidying.
Images remember too#
A Docker image is a stack of layers plus metadata. ENV and build ARG values are recorded where anyone who pulls the image can read them with docker history, and COPY . . happily copies a .env file into a layer.
How the key gets into the image
Pull it as a stranger and look
Dockerfile
FROM node:24-alpine
WORKDIR /app
COPY . .
ENV PAYMENTS_KEY=sk_live_51HxQ9
RUN npm ci && npm run build
CMD ["npm", "start"]$ docker history --no-trunc image
ENV PAYMENTS_KEY=sk_live_51HxQ9
COPY . . # buildkit
WORKDIR /app$ docker run --rm image cat /app/.env
cat: can't open '/app/.env': No such file or directoryUse build secret mounts (RUN --mount=type=secret) for keys the build needs, pass runtime secrets on the server, and keep .env in .dockerignore.
A leaked key’s whole life
Then the fix
Leaked secret lifecycle
Developer
Git history
Image registry
Attacker
Cloud provider
Step 1 of 7 · Developer → Git history
Commit key in Dockerfile
- Secrets and their blast radius:
- Any value that grants access: API keys, signing keys, SSH keys, tokens. Blast radius is how much damage one leaked secret allows. An admin API key for Cloudinary means read, overwrite or delete every customer upload.
- Rotation vs revocation vs purge:
- Rotation: issue a new secret and switch the app to it. Revocation: kill the old one so it stops working. Purge: delete the old value from history. Rotation plus revocation is the real fix; once the old value is dead, a purge is optional tidying.
- Git history and filter-repo:
- Deleting a secret in a new commit does not remove it: every old commit, tag and branch still contains it, and every clone has a copy. git filter-repo rewrites history to strip it, but that changes every commit hash and forces everyone to re-clone.
- Docker image layers, build args and ENV:
- A Docker image is a stack of layers plus metadata. Values passed as ARG or ENV during the build are recorded in the image config and visible to anyone with the image via docker history or docker inspect, even if never used by the app.
In a code review
CriticalDevOpsSecret committed to git or baked into an image
Fix: Rotate and revoke first, review provider access logs for the exposure window, delete old images and stale CI secrets. Purging history is optional once the key is dead.
0 of 3 checked
Check yourself