Intermediate · Backend security · topic 39 of 54 · 25 min
Keeping secrets safe
Explanation
What counts as a secret
A secret is any value that lets someone act as your app. Examples: a payment gateway key, an SMS provider token, a database connection string with its password, the key used to sign JWTs, a cloud access key. Anyone who has the secret can do what your app can do.
Never in code, never in git
Git keeps every version of every file forever. If you commit a key and delete it in the next commit, it is still in the history, and anyone with the repository can find it. Bots scan public GitHub repositories for keys within minutes of a push.
# .gitignore
.env
.env.local
.env.*.local
*.pem
node_modules/An environment variable is a named value given to your program by the system that runs it, outside the code. Your code reads process.env.NAME. On your laptop the values come from a .env file that git ignores; on the server they come from the hosting platform's settings.
// .env (never committed): RAZORPAY_KEY_SECRET=...
const keySecret = process.env.RAZORPAY_KEY_SECRET;
if (!keySecret) {
throw new Error('RAZORPAY_KEY_SECRET is not set');
}
// Commit a .env.example with empty values so others know which names existSecret managers
- Platform settings: Vercel, Render, Railway and similar hosts have a page to set environment variables per project and environment.
- Cloud secret managers: AWS Secrets Manager and Google Cloud Secret Manager store secrets encrypted, control who can read them, and keep an access log.
- HashiCorp Vault (and its open-source fork OpenBao): a self-hosted tool that can also create short-lived database passwords on demand.
Rotation means replacing a secret with a new one on a schedule (for example every 90 days) or when a team member leaves. Good secret managers can rotate some secrets automatically. Design your app so a new value can be loaded without changing code.
When a key leaks
- Revoke or disable the leaked key at the provider first. This stops the damage right away.
- Create a new key and put it in your environment or secret manager.
- Deploy so the app uses the new key, and check it works.
- Check the provider's logs and bills for anything the attacker did with the old key.
- Remove the key from git history if needed, but remember: removing it is not enough, revoking is what protects you.
- Write down what happened and add a check so it does not happen again.
| Place | OK for secrets? |
|---|---|
| Source code or git | Never |
| Frontend code or mobile app | Never (anyone can read it) |
| Local .env file listed in .gitignore | Yes, for development |
| Hosting platform environment settings | Yes |
| Cloud secret manager or Vault | Yes, best for teams and production |
| Chat messages, email, screenshots | No |
Watch the lesson
Keeping secrets safe · 1/7
API keys, database passwords and JWT signing keys are secrets. Whoever has them can act as your app.
Prefer a video by a teacher? Find free videos on “how to manage secrets environment variables api keys beginners” on YouTube ↗
In real life: The weekend AWS bill
A student pushed a side project to a public GitHub repository. The config file held a cloud access key. Within an hour, bots found the key and started many expensive servers for crypto mining. By Monday the bill was very large. The right response: disable the key in the cloud console immediately, delete the servers, create a new key stored in an environment variable, add the config file to .gitignore, turn on push protection, and contact the cloud provider's support about the charges.
Practice (0/3 done)
Write redact(value) that returns a NEW copy of an object where every key whose lower-case name contains one of SECRET_WORDS has its value replaced by "[REDACTED]". Go into nested objects and arrays. Keep other values and the key order the same. Do not change the original.
Write findSecrets(text) that scans a config text line by line. A line like NAME=value (spaces around = allowed) is a likely secret when NAME, in upper case, contains KEY, SECRET, TOKEN or PASSWORD and the value is not empty. Return an array of { line, name } objects, where line starts at 1.
Your team just found the SMS provider token in a public GitHub commit from last week. Write the step-by-step response plan you would follow today.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.