---
name: managing-secrets
description: Keep secrets out of Git, logs, and browser bundles - ignore rules, .env.example, startup checks, public prefixes, short-lived credentials - and respond when a secret leaks. Use when adding an environment variable or credential, reviewing .env files, or when a secret may have been committed.
license: MIT
compatibility: Optional - gitleaks, to scan for secrets.
metadata:
author: famiglio
category: security
---
# Managing Secrets
A secret in Git, in a log, or in the browser is public. Keep secrets in the environment, or encrypted with dotenvx (see the `encrypting-env-files` skill).
## Procedure
1. **Ignore the secret files** in `.gitignore`:
```text
.env
.env.*
!.env.example
.env.keys
*.pem
*.key
```
With dotenvx, encrypted `.env` files can be committed. `.env.keys` never.
2. **Keep `.env.example` up to date,** with every variable and no real value:
```text
DATABASE_URL=postgresql://user:password@localhost:5432/app
API_KEY=
```
3. **Check the variables at startup,** in one module (see the `configuring-twelve-factor-apps` skill). The error names the key, never the value.
4. **Keep secrets out of the browser.** A variable named `NEXT_PUBLIC_*`, `VITE_*`, `PUBLIC_*`, or `REACT_APP_*` ends up in the bundle.
<!-- ‹guest · 2026-12-05› -->
5. **Keep secrets out of logs.** Redact headers, cookies, and passwords in the logger (see the `writing-structured-logs` skill).
6. **In CI, prefer OIDC** to stored keys, for clouds and for npm (see the `publishing-to-npm` skill).
7. **If a secret leaked,** stop and tell the user: they must revoke it and create a new one. Rewriting the history does not help once it was pushed. Never run commands that rewrite a shared history without approval.
## Verification
```bash
git ls-files | grep -E '(^|/)\.env\.keys$|\.(pem|key|p12)$' && echo "Private key files are tracked." || echo "No key files."
git ls-files | grep -E '(^|/)\.env($|\.)' | grep -vE '\.env\.(example|sample|template)$' || echo "No .env files tracked."
```
Check that every tracked `.env` file holds only encrypted values. If gitleaks is installed, also run `gitleaks git --redact`.
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.