Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Security

Secrets Management Best Practices

Opens with credits

A practical guide to secrets management for developers who build and deploy apps. It covers where secrets should live, keeping them out of git and catching leaks, least privilege, rotation, CI/CD with OIDC instead of long-lived keys, what the browser can and cannot see, log redaction, and what to do in the first hour after a leak.

Written by Tauseef Fayyaz7 chapters, 28 practicesAbout 19 min25 quiz questionsReviewed 12 October 2026

What you will learn

7 chapters · about 19 min

  1. 1Where secrets should liveThe right home for a secret depends on the size of your team and setup, but there are a few places a secret should never be.5 practices · 4 min
  2. 2Catching leaks before and after they happenTools that stop secrets reaching a repository, and the order of steps when one gets through.4 practices · 3 min
  3. Least privilege and short-lived credentialsLimit what each secret can do and how long it works, so a leak causes as little damage as possible.4 practices · 3 minOpens with credits
  4. RotationReplacing secrets on a schedule and on demand, without downtime.3 practices · 2 minOpens with credits
  5. CI/CD secrets and OIDCPipelines hold the keys to production, so treat them like production: short-lived credentials, narrow trust and reviewed changes.5 practices · 3 minOpens with credits
  6. Frontend versus backend secretsAnything shipped to the browser or a mobile app is public. This chapter is about drawing that line clearly.4 practices · 2 minOpens with credits
  7. Logging, redaction and incident responseKeep secrets out of the logs you collect, and know exactly what to do in the first hour after a leak.3 practices · 2 minOpens with credits

About this guide

Secrets management is how you store, share, use and replace the credentials your software runs on: database passwords, API keys, signing keys and cloud tokens. Most real leaks are not clever attacks. They are an .env file committed to a public repo, a key printed in a CI log, or a "public" environment variable that was never meant to reach the browser. Good secrets management best practices make those mistakes hard to make and cheap to recover from.

This guide starts with the two things that prevent most incidents: putting secrets in the right place, and keeping them out of git with scanning and push protection. It then covers least privilege, rotation, CI/CD pipelines that use OpenID Connect (OIDC) instead of stored cloud keys, the line between frontend and backend secrets, log redaction, and a step-by-step response for when a secret leaks anyway.

The advice follows the OWASP Secrets Management Cheat Sheet, GitHub's documentation on secret scanning, push protection and OIDC, and cloud vendor guidance such as AWS Secrets Manager. Each practice tells you what to do and how to check you have done it.

Common questions

Is it safe to store secrets in environment variables?

Environment variables are an acceptable way to pass secrets to a server process, as long as the values come from a secret store at deploy or start time and are never committed or baked into builds. For containers, files mounted from a secret store are safer, since they do not appear in process listings or docker inspect output.

What should I do if I accidentally pushed an API key to GitHub?

Revoke or rotate the key with the provider immediately, then deploy the replacement. Removing it from git history is secondary, because clones and scanners may already have it. Check the provider's logs for any use of the old key.

Are NEXT_PUBLIC_ environment variables secret?

No. Next.js inlines NEXT_PUBLIC_ variables into the JavaScript sent to the browser at build time, so anyone can read them. Keep secrets in variables without the prefix and use them only in server code.

What is OIDC in GitHub Actions?

OpenID Connect lets a GitHub Actions job get a short-lived token from your cloud provider instead of using stored access keys. The cloud provider checks claims such as the repository and environment before issuing credentials that last only for that job.

How often should I rotate secrets?

Rotate immediately when a secret may be exposed or someone with access leaves. For routine rotation, automate it where your provider supports it; every 30 to 90 days is a common schedule for keys and database passwords. Short-lived credentials remove the need for most manual rotation.

What is the best way to store secrets for a small project?

Use your hosting platform's encrypted environment variables (Vercel, Netlify, Render and similar) for deployed values, a git-ignored .env for local development, and an .env.example in the repo. Move to a dedicated secrets manager when several services or people need the same secrets.

Sources (8)

Checked against these, last on 12 October 2026.

  1. Secrets Management Cheat Sheet, OWASPcheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
  2. About secret scanning, GitHub Docsdocs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning
  3. About push protection, GitHub Docsdocs.github.com/en/code-security/secret-scanning/introduction/about-push-protection
  4. OpenID Connect, GitHub Docsdocs.github.com/en/actions/concepts/security/openid-connect
  5. Using secrets in GitHub Actions, GitHub Docsdocs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets
  6. Deployments and environments, GitHub Docsdocs.github.com/en/actions/reference/workflows-and-actions/deployments-and-environments
  7. Rotate AWS Secrets Manager secrets, AWSdocs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets.html
  8. How to use environment variables in Next.js, Next.js docsnextjs.org/docs/app/guides/environment-variables

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com