Skip to content
Web Design & Development

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.

MediaScaleUp Editorial Team 2 min read 13 views
Secrets and Environment Variables, Done Properly

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?

  1. Keep .env files out of version control
  2. Use different credentials for each environment
  3. Rotate secrets on a schedule and after staff changes
  4. Enable secret scanning on repositories
  5. Grant each key only the permissions it needs
What to do and what to avoid with secrets and environment variables, side by side
Good practice against the usual mistakes, from the sources listed below.

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

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.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development