Skip to content
Pwnversity
DSO-SECRETSintermediate · 6 min · upd. 2026-10

Secrets management

CWE-798

Leaked secrets (API keys, passwords, tokens, certificates) are one of the most common breach causes. Secrets management aims for them to never be in the code or the repo, to be stored encrypted, rotated, and audited. It’s not a solved problem: a secret shows up in a commit, a log, a container image’s environment variable, or a config file almost daily.

A secret is any data whose compromise grants access to something. The risk depends on where it ends up and how long it lives:

  • In the repo: a secret in Git stays in the history forever, even if the next commit removes it. A leaked public repo is an immediate disaster; a private one, a bomb waiting for an access leak.
  • In logs and error messages: tokens printed in a trace end up in the SIEM, in support, on screens.
  • In container images: an ENV API_KEY=... or a .env file copied into the image stays in a recoverable layer (see containers).
  • In environment variables: visible to any process in the container and often in crash dumps.
  • Long-lived secret: a key that never expires and never rotates is a secret that, once leaked, stays useful for years.

From worst to best, the path a mature team walks:

  1. Hardcoded in the code — never. It stays in the history.
  2. .env file outside the repo — better than nothing, but flat and unrotated.
  3. Orchestrator environment variables — separates secret from code, but visible in the process.
  4. Secrets manager (Vault, cloud KMS/Secrets Manager) — encrypted at rest, access control, auditing, rotation.
  5. Dynamic secrets — the manager generates ephemeral credentials on demand (e.g. a DB user/password pair valid for one hour). A secret that doesn’t exist until needed can’t be leaked ahead of time.
flowchart LR
    APP["Application"] -->|authenticate identity| VAULT["Secrets manager"]
    VAULT -->|validate| IDP["Identity (OIDC / IAM role)"]
    VAULT -->|short-TTL ephemeral credential| APP
    VAULT -->|rotation / revocation| DB[(Backend: DB, cloud, PKI)]
    APP -. uses .-> DB
    AUDIT["Audit log"] -.-> VAULT

The key to the pattern: the app doesn’t store the secret; it authenticates with its identity (an IAM role, a pipeline OIDC token) and the manager hands it a short-lived credential.

Because secrets leak anyway, you have to detect them. Scanning looks for patterns (known key formats) and high entropy (random strings):

Ventana de terminal
# gitleaks over the full history
gitleaks detect --source . --report-format sarif --report-path leaks.sarif
# trufflehog, also verifying whether the secret is still live
trufflehog git file://. --only-verified

In CI, as a pre-merge gate and as a local hook:

name: secret-scan
on: [pull_request]
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 } # full history
- uses: gitleaks/gitleaks-action@v2

A pre-commit hook running gitleaks locally stops the secret from even leaving the machine.

Secrets are one of the most profitable targets in any authorized assessment:

  • Git history mining: git log -p, gitleaks, and trufflehog over a repo’s full history (or forks and gists) surface keys the team thinks were deleted.
  • Images and artifacts: inspecting an image’s layers (docker history, extracting layers) reveals .env, SSH keys, and tokens copied into the build.
  • Cloud and CI metadata: from an SSRF or a compromised pipeline, steal the associated role’s token (see cloud metadata and CI/CD).
  • Reuse: a leaked long-lived secret gets tried against every service in the org; lack of rotation amplifies the impact.
  • Prevent at the source: pre-commit hooks + CI gate with scanning. Keeping the secret out is cheaper than rotating it.
  • Secrets manager with granular access control, auditing, and, where possible, dynamic secrets.
  • Automatic rotation and short TTLs: shrink the window a leak is useful.
  • Identity instead of secrets: pipeline OIDC into the cloud (no static keys), IAM roles for services.
  • Leak response: if a secret leaks, revoke and rotate it immediately; deleting it from Git history (BFG, filter-repo) is not enough — assume it’s compromised.
  • Don’t put it in environment variables if you can mount it as an ephemeral file or fetch it at runtime.
  • Zero secrets in code or versioned configuration.
  • Secret scanning in pre-commit and CI, over the full history.
  • Secrets manager (Vault / KMS / Secrets Manager) as the single source.
  • Dynamic or short-lived secrets where the backend allows.
  • Identity-based auth (OIDC/IAM), not static keys, in pipelines.
  • Automatic rotation and defined TTLs.
  • Revoke+rotate procedure for a leak.
  • Secrets kept out of logs, error messages, and container images.