---
name: env-config-audit
description: Finds hardcoded secrets and config in code, moves them to environment variables, maintains a documented .env.example, ensures .env is git-ignored and checks git history for leaks. Use when the user asks to audit secrets, clean up config or before publishing a repo.
---

# Env and Config Audit

Find secrets and environment-specific values hardcoded in a repository, move them to environment variables, and make the setup documented and safe. This is about secrets handling, so be strict about not exposing values.

## Hard rules

- Never print, log, quote or commit a secret value. When reporting, show the file, line, and secret type only, with the value masked (`sk-****`).
- Never put real values into `.env.example`, tests, docs or commit messages.
- If a secret is found in git history or was ever public, tell the user to rotate it. Removing it from the code does not make it safe.
- Do not rewrite git history (filter-repo, force push) without the user's explicit confirmation; explain the consequences first.

## Step 1: Protect before you change

1. Check `.gitignore`. It must ignore `.env`, `.env.*` (except `.env.example`), key files (`*.pem`, `*.key`), and local credential files.
2. If a file with secrets is tracked, stop and tell the user: `git ls-files | grep -E "\.env|\.pem|\.key|credentials"`. Untracking uses `git rm --cached <file>`, which is safe for the file on disk, but still confirm first.
3. Add ignore rules first, then create any new secret-holding file.

## Step 2: Scan the working tree

Use a dedicated scanner if one is installed (`gitleaks`, `trufflehog`, `detect-secrets`). Otherwise grep:

```bash
git grep -nIE "(api[_-]?key|secret|token|passwd|password|private[_-]?key)\s*[:=]\s*['\"][^'\"]{8,}"
git grep -nIE "AKIA[0-9A-Z]{16}|ghp_[A-Za-z0-9]{36}|sk-[A-Za-z0-9]{20,}|-----BEGIN [A-Z ]*PRIVATE KEY-----"
git grep -nIE "(postgres|mysql|mongodb|redis|amqp)://[^ '\"]*:[^ '\"]*@"
```

Also look for hardcoded, environment-specific config: hostnames, ports, URLs of internal services, bucket names, account ids, feature toggles, IP addresses, and email addresses of real people. Ignore test fixtures that are clearly fake, but say you did.

## Step 3: Classify each finding

- Secret (credential, token, key, connection string with password): must move to env and be rotated if it was committed.
- Config that varies per environment (URLs, ports, hostnames): move to env with a default for local development.
- Constant that is the same everywhere: leave in code.

## Step 4: Move to environment variables

1. Read the value through the project's existing config mechanism (a settings module, `process.env`, `os.environ`). Do not introduce a new config library without asking.
2. Fail fast on missing required secrets with a clear error naming the variable, not the value. Give optional values a safe default.
3. Keep names consistent and prefixed by service where the project does (`PAYMENTS_API_KEY`).
4. Do not read env vars scattered throughout the code; centralize reads in one config module if the project already does so.

```python
API_KEY = os.environ["PAYMENTS_API_KEY"]            # required, raises KeyError if missing
TIMEOUT = float(os.environ.get("PAYMENTS_TIMEOUT", "10"))
```

## Step 5: Maintain .env.example

- One line per variable, grouped, with a comment on purpose, format and whether it is required.
- Placeholders only: `PAYMENTS_API_KEY=` or `PAYMENTS_API_KEY=changeme`.
- Every variable the code reads must be listed; every listed variable must be used. Verify by grepping env reads against the file.
- Mention in the README how to copy it: `cp .env.example .env`.

## Step 6: Check git history

```bash
git log --all --oneline -S"<non-secret marker, e.g. variable name>" -- .
git log --all --diff-filter=A --name-only --format= | grep -E "\.env$|\.pem$|id_rsa|credentials"
gitleaks git --redact --log-opts="--all" .     # if installed (older versions: gitleaks detect)
```

Report whether secrets exist in past commits, in which files and commits (hashes only, values masked). For each one say: rotate now, and optionally clean history later with the user's approval. Remind the user that forks, clones and CI logs may still hold copies.

## Output format

1. Findings table: `file:line | type | action taken | needs rotation (yes/no)`; values always masked.
2. List of new or renamed environment variables, and whether `.env.example` and docs were updated.
3. `.gitignore` status.
4. History scan result and the explicit rotation list for the user.
5. What you could not check (private branches, CI settings, other repos).

## Pitfalls

- Base64 or "obfuscated" secrets are still secrets.
- Frontend bundles expose every env var baked in at build time; public keys only.
- Do not commit `.env` "just for the team"; use a secret manager or a shared vault.
- Docker `ARG`/`ENV` and CI config files also leak secrets; check them too.
- Rotating a key without updating the deployment breaks production; tell the user the order of operations.

Source: [AnyModel skills catalog](https://anymodel.org/en/skills) · License: [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
