Course contentsShow
← Security
Lesson 1257 of 3,05225. Application Security - Secure CodingFree lesson

Remediating Exposed Secrets

Master the process of rotating compromised credentials, rewriting git history, and invalidating exposed secrets properly.

Remediating Exposed Secrets

You'll learn how to properly respond when credentials or API keys have been exposed in your codebase.

The Core Problem

Finding an exposed secret is only half the battle—the hard part is fixing the damage. Simply deleting the secret from your latest commit does not remove it from Git history. Anyone who cloned your repository before the fix still has access to that secret, and the credential remains visible in older commits forever.

The Three-Step Remediation Process

1. Rotate the Compromised Credential Immediately

Assume the secret has already been stolen. Generate a new password, API key, or token through your service provider and deploy it. The old credential should be invalidated immediately—this is your first priority before any code changes.

2. Rewrite Git History

Use tools like git filter-branch or BFG Repo-Cleaner to remove the secret from all commits in your repository history. This rewrites commit hashes and requires force-pushing, which affects all collaborators.

3. Invalidate and Verify

Double-check that the old secret no longer works by attempting to authenticate with it. Confirm the new secret is deployed and functioning. Notify your team that history has been rewritten and they need to re-clone or rebase their local branches.

The Analogy

Think of an exposed secret like a house key left on a public bulletin board. Removing the note from the board (deleting from latest commit) doesn't help if someone already copied it. You must change the locks (rotate), remove all copies of the note from every board where it was posted (rewrite history), and verify the old key no longer opens your door (invalidate).

Key Takeaway: Remediating exposed secrets requires rotating credentials first, rewriting repository history to remove all traces, and verifying the old secret is completely invalidated—deletion alone is never sufficient.