chiefletterAll stories

Security & privacy / CHIEFLETTER JOURNAL

Keep API Keys Out of Git and Respond to a Leak

Prevent credential leaks with secret storage, repository checks, and a clear rotation plan when a key reaches the wrong place.

A close view of electronic components on a circuit board
Photography via Unsplash

Know which values are secrets

An API key, database password, signing key, and session secret all grant some form of access. A public identifier or publishable client key may be designed for browser use, but still has limits and abuse risk. Check the provider’s documentation before deciding that a value is safe to expose. Naming a variable `PUBLIC_KEY` does not establish its permissions.

A value bundled into JavaScript, placed in HTML, or sent in a mobile app can be inspected by users. A private key belongs on a trusted server or in a managed service that does not expose it. If a feature must call an external provider from the browser, use a restricted public credential and enforce limits at the provider.

Inventory what each credential can do, which service owns it, when it expires, and who can rotate it. This list is useful before an incident. A key with read-only access to one test bucket is a different risk from an account-wide administrator credential. Least privilege reduces the damage a leak can cause.

When rotating a key, update every service that uses it before revoking the old credential, unless the provider requires immediate revocation. Confirm the new credential works from the deployed service, then verify that the old one fails. Search deployment history and automated jobs for copies. Keep a time-stamped record of those checks without recording the secret itself.

Keep secrets outside tracked files

Read secrets from a local environment file that Git ignores or from your operating system’s secret manager. Commit a sample file with placeholder names and instructions, not working values. Check that ignore rules apply before the first commit. If a secret was committed once, adding the path to `.gitignore` does not remove it from history.

In hosted environments, use a protected configuration store or secret manager. Restrict who can view and change values, and avoid printing them in deployment logs. CI jobs should receive only the credentials needed for that job. A pull request workflow from an untrusted fork should not receive production secrets.

Use separate credentials for local development, automated tests, staging, and production. Each environment should have only the access it needs. If a test value leaks, you should not have to rotate the production database password at the same time. Make the boundaries easy to understand and document how to replace each value.

Add checks before a secret reaches a branch

Enable secret scanning in the repository host when available. Add a local pre-commit check if the team finds it useful, but do not treat that check as the only defense. Developers can bypass local hooks, and a secret may enter through a copied log or an uploaded artifact. Server-side scanning provides another opportunity to catch it.

Review the staged diff before committing. Search for token-like strings, private key headers, connection strings, and values from provider dashboards. Automated scanners can produce false positives, so define a review process that checks context without copying a suspected secret into a public issue.

Teach the team how to report a suspected exposure privately. A public chat message containing the entire credential repeats the leak. Share the repository, file path, approximate commit, and credential owner first. The person rotating the key needs enough context to act, not another copy of the value.

Treat a discovered key as exposed

If a key appears in a public or shared place, assume someone else may have copied it. Revoke or rotate it through the provider as soon as the owner confirms what it controls. Removing the text from the latest file is not enough. Search logs, build artifacts, package releases, issue comments, and copies of the repository.

Check provider activity for unexpected use, new resources, unusual regions, or a rise in requests. Preserve relevant logs according to the incident process. If the key has write access or handles personal data, involve the security or privacy owner. Do not make a public statement about impact until the evidence supports it.

If immediate rotation would break a service, create a replacement credential, deploy it to the service, confirm traffic works, then revoke the old one. For an account takeover or signing key, follow the provider’s incident guidance. A simple text replacement may not address tokens already minted using the exposed key.

Clean repository history carefully

History rewriting may remove the string from reachable commits, but it does not revoke the credential and cannot erase copies already cloned or cached. Rotate first. Then follow the repository host’s supported cleanup process if removal is needed. Coordinate with contributors, because rewritten history can invalidate branches and open pull requests.

Search the current branch and previous commits for related credentials. Check generated files and package artifacts too. A leak in a compiled file often means the secret was exposed to every person who downloaded that build, even if the source history is later rewritten.

Record what happened, what access the key had, when it was revoked, and which checks were added. Keep the incident note focused on the process. Do not paste the old secret into a retrospective. Use a redacted identifier or provider reference.

Make the next leak easier to contain

Set expiration dates and ownership reminders where the provider supports them. Remove credentials that no service uses. Prefer short-lived tokens or workload identity over long-lived keys when available. These steps reduce the time a forgotten credential stays useful after a project or employee changes.

Practice a rotation in a low-risk environment. Confirm that the service can read a replacement, that the old value stops working, and that the deployment log does not reveal either secret. A written runbook is practical only if someone can follow it without guessing at dashboard names or access rights.

After the immediate response, review how the key entered the repository. Fix the workflow that caused it, such as copying a command into a tracked config file. A scanner alert is helpful, but a safer default in the development process prevents more than the same scanner rule alone.

Some providers let you restrict a key by domain, IP address, API, or quota. Apply the restrictions that match the service. These controls reduce misuse, but they do not make a key private if the browser exposes it. Treat restrictions as a second boundary, not as a reason to put a server credential into public code.

A short working note

Try this with your work

Prevent credential leaks with secret storage, repository checks, and a clear rotation plan when a key reaches the wrong place.

0 of 30 of 3 steps
Your note stays in this browser.

Video resources

For a visual walkthrough related to this topic, see this YouTube learning resource. It opens a topic search so you can choose a current explanation that fits your level.

Frequently asked questions

Does deleting an API key from Git remove the risk?

No. Revoke or rotate the credential. A value in repository history or a downloaded copy may still be usable.

What should I do first after a key leak?

Identify its owner and access, then revoke or rotate it through the provider. Check activity and involve the security owner when the key has sensitive permissions.

Are environment variables always safe?

They are safer than hard-coding values, but access controls and logs still matter. Use a secret manager or protected host configuration and limit access.

Should I rewrite Git history?

Only after the credential is revoked, and with a coordinated cleanup plan. History cleanup does not invalidate the exposed key.