Secrets and Environment Variables, Done Properly
Keeping API keys, database passwords and other credentials out of source code, using environment variables or a secrets manager instead.
On this page
Why does it matter?
Secrets committed to a repository are easy to leak and hard to fully remove, and leaked keys are often exploited within hours.
What are the numbers?
- Common practice load secrets from environment variables or a secrets manager
- Laravel uses a .env file that should not be committed
- Leak response rotate the secret; deleting it from the repository is not enough
- Scanning repository hosts and tools can scan commits for secrets
What should I do?
- Keep .env files out of version control
- Use different credentials for each environment
- Rotate secrets on a schedule and after staff changes
- Enable secret scanning on repositories
- Grant each key only the permissions it needs
What should I avoid?
Avoid:
- Hard-coded keys in source code
- Sharing secrets over chat or email
- One admin key used everywhere
- Exposing server secrets to front-end bundles
When should I get help?
Short answer Bring in help when a secret has been leaked, or when setting up deployment for several environments.
Where this comes from
- OWASP — Secrets Management Cheat Sheet
- Laravel Documentation — Configuration
The figures and practices above come from the sources listed.
Working on something like this?
We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.
Where to go next
Spotted something wrong? Report an error on this page. We correct on the page and say what changed.