You've just realised the notes-api repo (where its code lives on GitHub) was public for a whole day, with the .env file holding JWT_SECRET committed (saved in the git history) inside it. JWT_SECRET is the key the server uses to sign login tokens. First reflex: delete the file from git, push, done. Sadly, it isn't that simple.
A secret is a value that grants access: a database password, JWT_SECRET, an API key. On a server, managing secrets is like keeping a spare house key safe, and the secret is the key. You don't tape it to the front door (git), and you don't print it in a brochure that many people get (a frontend bundle or a Docker image, both covered below). You keep it in a locked box (a 600 file). If that key ever went missing, you change the lock (rotation: making a new secret) instead of only looking for the key. How to detect a leak and handle it as an incident is in the Environment Variables & Secrets lesson of the Security track. This is the operations side, on web-1.
Config, secret and public
Every value that differs between environments (the places the app runs, such as your laptop or web-1) is read from an environment variable, but each kind is handled differently:
| Variable | Kind | Why |
|---|---|---|
PORT, NODE_ENV, LOG_LEVEL |
config | differs per server, but not secret |
DATABASE_URL, JWT_SECRET |
secret | grants access; DATABASE_URL holds a password |
VITE_API_URL (the notes-web frontend) |
public | the VITE_ prefix sends its value to the browser |
- The prefixes
VITE_,NEXT_PUBLIC_andPUBLIC_mean the value is written into the frontend's JavaScript bundle (the JavaScript file the browser downloads), so anyone can read it. A variable with a public prefix is always public, whatever its name, and must never hold a secret. - Of the rest, a name containing
SECRET,PASSWORD,TOKEN,KEYorCREDENTIALis a secret. The others are config.
On the server: one 600 file, read by systemd
All of notes-api's secrets live in one file, 600 and owned by root (the File System & Permissions lesson), read by systemd through EnvironmentFile= (the Systemd & Service Management lesson):
deploy@web-1:~$ ls -l /etc/notes-api.env
-rw------- 1 root root 152 Oct 8 17:20 /etc/notes-api.env
deploy@web-1:~$ sudo cat /etc/notes-api.env
DATABASE_URL=postgres://notes:83hQLRdx8M1klbAQvNcZPwBn@localhost:5432/notes
JWT_SECRET=0b08ce8c4914826707aa575513330eaf0e3783035d7eab9656197a23af0f6382
- Change it with
sudoedit /etc/notes-api.env(it opens a root-owned file in a text editor, through sudo), thensudo systemctl restart notes-api. systemd reads this file once, when it starts the service. The app's running process never learns that the file changed. - Plain values may be written straight into the unit file (
Environment=NODE_ENV=production), but not secrets: unit files are usually644, and any user may runsystemctl show notes-api -p Environment, which prints whatEnvironment=holds.
Not in git, not in the image, not in the logs
- Git:
.envgoes into.gitignore(the list of files that are not committed). What gets committed is.env.example: the variable names only, with no secret values, so teammates know what to fill in. Note:.gitignoredoes not apply to a file that is already tracked (followed by git). Take the file out of git first withgit rm --cached .env(the file on your disk stays). - A Docker image (a package holding the app and everything it needs, covered from the What Is Docker lesson on): no
ENV JWT_SECRET=...orCOPY .envin a Dockerfile (the image's recipe). Those two lines store the secret in a layer (a part) of the image forever, and the secret travels wherever the image is sent. Give the secret when the container (a running image) starts, for exampledocker run --env-file /etc/notes-api.env. - Logs: no
console.log(process.env)(it prints every environment variable), and no printing the whole config at start. Logs are read by far more people than a600file.
Secrets in CI/CD
The pipeline (the automatic path from a push to a deploy) that deploys notes-api needs secrets too, for example an SSH key to log in to web-1. In GitHub Actions (GitHub's automation feature), secrets are stored encrypted in the repo settings and read with ${{ secrets.DEPLOY_SSH_KEY }}. For a cloud provider such as AWS, OIDC is better still:
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: aws-actions/configure-aws-credentials@v6
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
aws-region: ap-southeast-1
With OIDC, GitHub proves the identity of the workflow (an automated list of steps) to AWS. Then AWS gives temporary credentials (access keys) that expire on their own after a short time. No permanent AWS_ACCESS_KEY_ID is stored, so there is nothing to leak. GitHub Actions is covered in full in the CI/CD lessons later.
Rotation: change the lock
git rm --cached .env only changes the next commit. The old commits on GitHub still hold JWT_SECRET, and more importantly, the server still uses it. So anyone who copied JWT_SECRET can still make a forged admin token, and the server accepts it. The only thing that stops it is rotation: change the secret, so the leaked value becomes useless.
deploy@web-1:~$ openssl rand -hex 32
4681783844dbb75403ab029609e5318d0bbcd6ed92fd6aa73605922c6414b322
deploy@web-1:~$ sudoedit /etc/notes-api.env
deploy@web-1:~$ sudo systemctl restart notes-api
Then from outside, try two tokens: a forged one, and Budi's old one (a real token made before the rotation). The first request tries to delete note 1 and prints only its status code. The second asks for the list of notes with Budi's token:
$ curl -s -o /dev/null -w "%{http_code}\n" -X DELETE https://notes.example.com/notes/1 -H "Authorization: Bearer $FORGED_TOKEN"
401
$ curl -s https://notes.example.com/notes -H "Authorization: Bearer $BUDI_TOKEN"
{"error":"Invalid token"}
openssl rand -hex 32makes 32 random bytes, written as 64 hex characters (the digits 0 to 9 and the letters a to f).- The steps are always three: a new secret, write it into the file, restart. Without the restart, the app's old process still uses the old secret.
- The forged token is refused with
401(not proven to be logged in). The side effect: every old token is refused too, Budi's included, so everyone has to log in again. That is a fair price for a leaked secret. - Rotate too when a team member leaves, and on a schedule (for example every 90 days). For a database password, change it in PostgreSQL and in the file, then restart right away, so the app fails to connect to the database for only a few seconds.
- After rotating, clean up the git history if needed (with
git filter-repo, for example). Rotation always comes first.
Public, secret or config?
In the exercise, classifyEnvVar(name, value) returns { name, category, shouldGitignore }. Check in this order:
- A name starting with
PUBLIC_,NEXT_PUBLIC_orVITE_(startsWith) is"public". - Otherwise, a name containing
SECRET,PASSWORD,TOKEN,KEYorCREDENTIAL(includes) is"secret". - Anything else is
"config".
shouldGitignore is true only for "secret".
⚠️ Mistakes that leave a secret wide open
- Thinking removal from git is enough. A secret that was ever pushed must be treated as leaked. Rotate first, clean the history later.
- Changing the file without a restart. The app's running process uses the old value until it is restarted.
- A secret in a unit file's
Environment=. Anyone on the server can read it throughsystemctl show. UseEnvironmentFile=with a600file. - A secret in a variable with a public prefix.
VITE_STRIPE_SECRET_KEYstill goes to the browser. Secrets belong on the backend (the code that runs on the server) only. - A fallback value in code.
process.env.JWT_SECRET || "secret"(whenJWT_SECRETis empty, use"secret") keeps the app running on a secret written in git. It is better for the app to fail at start when the secret is empty.
TL;DR: Keep three kinds of environment variables apart: config (not secret), secrets (grant access: names with SECRET, PASSWORD, TOKEN, KEY, CREDENTIAL) and public values (prefixed VITE_, NEXT_PUBLIC_, PUBLIC_, they always reach the browser). On the server web-1, every secret lives in /etc/notes-api.env (600, owned by root), read by systemd through EnvironmentFile=. Secrets are never in a unit file's Environment=, in git (.env in .gitignore, .env.example committed), in a Docker image or in the logs. In CI (the server that runs the deploy automatically), use the repo's secrets (stored encrypted in the repo settings), and OIDC for the cloud so there is no permanent credential (access key). A leaked secret is handled by rotation (making a new secret): openssl rand -hex 32 for a new secret, sudoedit to write it into the file, systemctl restart, then everyone logs in again.
⚡ Config, secret or public
Write:
PORT, NODE_ENV, LOG_LEVEL config
DATABASE_URL, JWT_SECRET secret
VITE_API_URL public
Guess first: a variable prefixed VITE_ is sent to the browser. Is VITE_STRIPE_SECRET_KEY secret or public?
Public. The VITE_ prefix writes its value into the JavaScript bundle the browser downloads, whatever its name. Secrets belong on the backend (the code on the server) only.
Meaning: Environment variables (settings the app receives when it starts) come in three kinds. A variable prefixed VITE_, NEXT_PUBLIC_ or PUBLIC_ is always public. Of the rest, a name containing SECRET, PASSWORD, TOKEN, KEY or CREDENTIAL is a secret (a value that grants access), anything else is config (not secret).
⚡ .env stays out of git
Write:
git rm --cached .env
Meaning: .env (the file holding secrets) goes into .gitignore, the list of files that are not committed. What gets committed is .env.example, with the variable names only. .gitignore does not apply to a file that is already tracked (followed by git), so take it out of git first with git rm --cached.
⚡ On the server: one 600 file
Write:
deploy@web-1:~$ ls -l /etc/notes-api.env
-rw------- 1 root root 152 Oct 8 17:20 /etc/notes-api.env
Meaning: Every secret lives in one 600 file owned by root, so only root may read it. systemd (the program that starts the app) reads it through the EnvironmentFile= line of the unit file (the app's settings file for systemd). Secrets are never in the unit file's Environment= line (every user can read it), in a Docker image (a package holding the app) or in the logs.
⚡ Leaked? Change the lock
Write:
deploy@web-1:~$ openssl rand -hex 32
deploy@web-1:~$ sudoedit /etc/notes-api.env
deploy@web-1:~$ sudo systemctl restart notes-api
Guess first: .env, holding JWT_SECRET (the key for signing login tokens), was deleted from git and pushed. Is it safe now?
Not yet. The old commits on GitHub still hold JWT_SECRET, and the server still uses it. So anyone who copied it can make a forged admin token, and the server still accepts it.
Meaning: Only rotation, changing the secret, stops forged tokens: make a new secret (openssl rand -hex 32), write it into the file (sudoedit opens a root-owned file), then restart. The side effect: every old token is refused and everyone logs in again.