Secrets and Sensitive Data Handling

Intermediate · 10 min

The day-to-day discipline of keeping secrets out of version control and out of logs, as an app developer rather than an infrastructure team.

Before thisThe Twelve-Factor App and Environment Config

IBM Technology · 10 min

Secrets and Sensitive Data Handling

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by IBM TechnologyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A secret — an API key, a database password, a signing key, a third-party token — that ends up in a git repository is compromised the moment it's pushed, even if the commit is later reverted, because git history keeps every prior commit and a public repo may already have been cloned or scraped.
  • Secrets belong in environment variables or a secret manager, read at runtime, never hardcoded into source files and never committed as literal values, even in a `.env` file that accidentally loses its `.gitignore` entry.
  • If a secret is ever committed, the only correct response is to treat it as burned and rotate it immediately — removing it from git history does not undo the exposure, because you can't prove no one already has a copy.
  • Logging a secret, a token, or PII in plaintext turns your log aggregator into a second, usually far less access-controlled, copy of your most sensitive data — log pipelines are built for searchability and retention, not for the access controls a credential store has.
  • Treat log statements as a review surface, the same way you'd review a diff for a SQL query: before logging a request, a payload, or an error object, ask what fields are actually in it, not just what you expect to be in it.

Why secrets are a developer discipline, not just an ops concern

Every real application needs things it can’t hardcode: a database password, an API key for a payment processor, a signing key for issued tokens, a third-party service credential. These are collectively “secrets,” and the question of where they physically live in production — a vault cluster, a cloud secret manager, an encrypted config store — is genuinely an infrastructure problem, covered at that level in Secrets and Config Management. This lesson is about something narrower and more immediate: the habits an individual developer needs, every single day, writing ordinary application code, to avoid being the reason a secret leaks in the first place. Infrastructure can build the most sophisticated vault in the world; it does nothing if a developer pastes an API key directly into a source file and commits it.

The video above (IBM Technology) frames secrets management from roughly this angle — the problem secret sprawl and plaintext storage create, and what centralizing secret handling actually buys you. It’s a good overview of the “why this is a real problem” case; the version-control and logging specifics below are where the day-to-day discipline actually lives.

Never commit a secret — and know why “just revert it” doesn’t work

The single most common way secrets leak isn’t a sophisticated attack — it’s a developer typing a real API key directly into a config file, a test script, or a source file “just to get it working,” then running git add . and committing it along with everything else. It happens constantly because, in the moment, it’s the fastest way to test something, and the commit looks completely unremarkable in a diff.

The reason this is worse than it sounds: git history is permanent by default. If you commit a secret and then, in a later commit, remove it or replace it with a placeholder, the original value is still sitting in the repository’s history, reachable by anyone who can check out that earlier commit or run git log -p. If the repository is public — or becomes public later, or is cloned by a teammate before the fix lands — the secret is out. There are automated bots that continuously scan public GitHub for patterns that look like AWS keys, Stripe keys, and other common credential formats, and they’re fast; a key can be harvested within minutes of a push, long before a human notices the mistake.

This leads to the one rule that matters more than any tooling: if a secret is ever committed, treat it as compromised and rotate it — revoke the old value and issue a new one — immediately. Rewriting git history (with something like git filter-repo) to scrub the secret out is good practice for hygiene, but it is not the fix, because you cannot prove that no one already has a copy from before the rewrite. Rotation is the only step that actually closes the exposure.

The practical defenses, layered:

  • .gitignore your local secret files (.env, credentials.json, and similar) from day one of a project — before the first secret ever gets added, not after.
  • Commit a .env.example with the same variable names and placeholder or dummy values, so teammates know what’s needed without the real values ever existing in a file git tracks.
  • Use a pre-commit secret scanner (tools like gitleaks or git-secrets are common, low-friction choices) that checks staged changes for patterns that look like keys or tokens before the commit completes, catching the mistake before it ever reaches a remote.

Where secrets should actually live

The alternative to hardcoding is reading secrets from the environment at runtime — the same environment-variable mechanism covered in The Twelve-Factor App and Environment Config, applied specifically to sensitive values rather than ordinary config. Locally, that usually means a .env file loaded by a library like dotenv, kept out of version control. In production, it usually means the values are injected by the deployment platform itself (a container orchestrator’s secret store, a cloud provider’s parameter store, or a dedicated secret manager) rather than living in a checked-in config file at all.

The distinction that matters day to day isn’t “environment variable versus secret manager” as a binary choice — plenty of small services run fine on environment variables alone. It’s this: wherever the secret lives, your application code should ask for it by name at startup and never, under any circumstance, have its actual value typed into a source file, a config file tracked by git, or a comment “for reference.” If you ever find yourself writing const apiKey = "sk_live_..." instead of const apiKey = process.env.STRIPE_API_KEY, stop — that’s the exact moment this entire discipline exists to prevent.

Logging hygiene: the leak nobody notices

Version control gets most of the attention, but logs are an equally common, quieter leak path, precisely because logging feels harmless. A line like:

logger.info(`Login attempt: ${JSON.stringify(req.body)}`)

looks like reasonable debugging instrumentation. The problem is that req.body on a login route usually is the username and password, and JSON.stringify-ing the whole thing captures both, unfiltered, into whatever log pipeline your application ships to. Multiply this pattern across a codebase — logging whole request objects, whole error objects, whole user records “for context” — and your log aggregator quietly becomes a second copy of your most sensitive data: passwords on failed logins, Authorization headers and session tokens, API keys passed as query parameters, raw PII like addresses and phone numbers.

This matters because log infrastructure is built for a different job than a credential store is. Logs are optimized for searchability, long retention, and broad accessibility to engineers doing on-call debugging — not for the tight access control and audit trail a secrets manager or a database’s access layer provides. A secret that would never be readable outside a locked-down vault is suddenly sitting in plaintext in a system dozens of engineers can search, that may get forwarded to a third-party monitoring vendor, and that may get pasted into a support ticket or bug report months later by someone who’s never thought about where it came from.

The fix is a habit, not a tool: before adding a log statement, ask what fields are actually in the object you’re about to log, not what you expect to be in it. Prefer logging specific, known-safe fields (user.id, request.path, response.status) over serializing an entire object. Where frameworks support it, use a structured logger with field-level redaction (masking fields named password, token, authorization, ssn, and similar by convention) so an accidental console.log(req.headers) doesn’t silently defeat the whole discipline. None of this requires new infrastructure — it requires treating a log statement with the same scrutiny you’d give a response payload, because from a sensitive-data standpoint, that’s exactly what it is.

Check yourself

  1. 01Why is a secret in a `.env` file not automatically safe just because `.env` is a common convention?

    A `.env` file is a convenience for loading environment variables locally — it's just a text file on disk, with no special protection. It's only as safe as the `.gitignore` entry that keeps it out of version control; a developer who force-adds it, a misconfigured `.gitignore`, or a new team member copying it into a public template repo all defeat that protection instantly.

  2. 02Your team just discovered an AWS key was committed to a public GitHub repo six months ago and has since been removed in a later commit. Is the key safe now?

    No. The key still exists in the repository's git history (reachable by anyone who clones the repo or checks out that old commit), and worse, automated bots scan public GitHub continuously for exactly this pattern, so it may well have been harvested within minutes of the original push, long before anyone noticed. The only correct fix is to rotate (revoke and reissue) the key — rewriting history to remove it is good hygiene but doesn't undo an exposure that may have already happened.

  3. 03What's the practical difference between storing a secret in an environment variable versus a secret manager (like AWS Secrets Manager or HashiCorp Vault), and when does the distinction start to matter?

    An environment variable is read once at process startup and then sits in the process's memory and environment for its whole lifetime, with no built-in rotation, auditing, or access control beyond whatever controls access to the host or container. A secret manager adds centralized access control (who can fetch which secret), audit logs (who fetched what, when), and support for rotating a credential without redeploying every consumer. For a side project or single small service, an environment variable is often enough; once you have multiple services, multiple environments, and compliance requirements, the gap becomes a real operational cost.

  4. 04Why is logging a full request object or error object at `info` or `debug` level a common way secrets and PII leak, even when no one intended to log them?

    A request object often contains an `Authorization` header, a session cookie, or a full user payload (email, address, sometimes a password field on a failed validation) bundled together, and logging 'the whole object for context' during debugging captures all of it indiscriminately. The leak isn't intentional — it's a side effect of logging a convenient object instead of the specific fields that were actually needed, and it tends to survive into production because the debug log statement never gets removed.

  5. 05Why doesn't 'our log aggregator requires a login' fully address the risk of logging secrets in plaintext?

    A login requirement controls who can view the dashboard, but log aggregators are typically accessible to a much wider set of engineers (for debugging, on-call, analytics) than a dedicated secrets store would be, logs are often retained for months, and logs frequently get exported, forwarded to third-party monitoring tools, or included in support tickets and bug reports — each of those is another place the same plaintext secret now lives, each with its own access boundary that was never designed to protect a credential.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark