OWASP Secrets Management
Applications carry secrets: database passwords, API keys, signing keys, and access tokens. A leaked secret hands attackers a ready-made key to your systems. Secrets management covers how teams create, store, deliver, rotate, and retire these values safely.
The Spare Key Comparison
Nobody tapes a house key to the front door. People keep keys in a safe place, hand out copies carefully, and change the lock when a key goes missing. Secrets deserve the same care.
Where Secrets Leak
| Leak Point | Typical Cause |
|---|---|
| Source code repository | A developer commits a key to a public or shared repository |
| Configuration files | Plain text files travel with the application package |
| Log files | Debug output prints connection strings |
| Chat and tickets | Teammates paste keys to save time |
| Container images | Build steps bake secrets into image layers |
| Client-side code | Keys sit inside web pages or mobile apps |
Secret Life Cycle
Create → Store → Deliver → Use → Rotate → Revoke
| | | | | |
Strong Vault Short- Least Scheduled Immediate
random with lived privilege change shutdown
value access access after after
control use leak
Better Storage Options
Dedicated Secrets Managers
Tools such as HashiCorp Vault and the managers offered by major cloud providers store secrets encrypted, check who asks, record every access, and support automatic rotation.
Environment Variables
Environment variables keep secrets out of source code. They suit small projects, yet they can leak through crash dumps and process listings. Prefer a secrets manager for production.
Code Pattern
import os
db_password = os.environ["DB_PASSWORD"] # loaded at runtime
# Avoid: db_password = "Summer2026!" # hard-coded secret
Rotation Matters
Every secret should have an owner and an expiry plan. Short-lived credentials reduce the value of a theft, because the stolen key stops working soon. Dynamic secrets take the idea further: the manager creates a fresh database login for each application instance and deletes it after use.
Catching Leaks Early
- Add a secret scanner to developer machines and to the build pipeline.
- Block commits that contain key patterns.
- Scan the full history of old repositories, because deleted files remain in version history.
- Monitor public code hosting sites for your organization's key formats.
When a Secret Leaks
- Revoke or rotate the exposed secret at once.
- Review access logs to learn whether anyone used it.
- Remove the secret from code, history, and images.
- Find the cause and fix the process that allowed the leak.
Deleting a file from a repository does not fix the problem. Treat every exposed secret as stolen.
Best Practices
- Grant each application its own credentials with minimal permissions.
- Separate secrets for development, testing, and production.
- Encrypt secrets at rest and in transit.
- Audit who reads each secret.
- Keep secrets out of client-side code, since users can inspect everything the browser downloads.
A Small Team Starting Plan
Small teams do not need a complex platform on day one. Move every secret out of the code into environment variables, add a secret scanner to the repository, give each service its own key, and write down who owns each key. Upgrade to a full secrets manager as the number of services grows. Progress in small steps beats waiting for a perfect system.
Key Takeaways
- Secrets open doors, so protect them like keys.
- Code, logs, chat, and images leak secrets most often.
- Secrets managers add storage, access control, and rotation.
- Fast revocation limits damage after a leak.
