# Exposed API keys in code: how to find them and what to do

> Exposed API keys in code: where to look in the repository and its history, why deleting the file doesn't fix it, and how to replace every credential.

Author: José Carlos Maciel, Founder and developer. Published 2026-11-25, updated 2026-11-25.
Source: https://boosting.tech/en/blog/exposed-api-keys-in-code/

**Quick answer:** If a password or API key is written into the code, treat that credential as leaked. Search the code, the commit history, copies handed to third parties, container images and CI logs. Then revoke each credential, create a new one with minimum access stored outside the code, and only after that consider cleaning the history.

An IT analyst at a logistics company gets an email from the vendor that maintains the cargo scheduling system. The project repository was public for a few weeks by mistake, and according to the vendor "there was nothing sensitive in it". In the third file he opens, he finds the production database password written in quotes and, right below it, the token for the cloud account that stores the drivers' documents.

This article shows where to look for passwords and keys written into code, explains why deleting the file doesn't fix the problem, and gives you a step-by-step guide to find and replace every credential you discover. For a system that hasn't gone live yet, the [security checklist before production](/en/blog/security-checklist-before-production/) covers this point along with the others. If it was put together with an AI tool, see also the guide to [reviewing AI-built systems](/en/blog/ai-built-systems-what-to-review/).

## What are secrets and keys exposed in code?

In software security, a secret is any piece of information that grants access to another system: the database password, the cloud account token, the payment service API key, the password of the mailbox that sends notifications. A secret is exposed in the code when it appears in plain text in a project file instead of being read from a protected location when the system runs.

MITRE, the organization that maintains the public catalog of software weaknesses known as CWE, describes the problem in the entry [CWE-798, Use of Hard-coded Credentials](https://cwe.mitre.org/data/definitions/798.html). It separates a fixed password the system itself accepts, such as a default administrator, from the credential the system uses to connect to another service. In internal systems the second case is the most common: the database password, the email service key, the user for the ERP integration. MITRE notes that if that password is ever discovered or published, anyone who knows it can access the resource.

## Why are passwords in source code a problem even in a private repository?

Hardcoded passwords are risky even in a private repository because the code travels much further than the repository itself. Every developer has a full copy on their machine, with the entire history. The vendor that did some maintenance two years ago received a zip file. The continuous integration tool downloads the code on every change and keeps logs of each run. A credential written into the code lives in all of these places, and its protection depends on the care of every person and every machine that ever had access.

In systems built in a hurry, the first version had to work by the following week, and writing the database password into the configuration file was the fastest way to get there. The temporary version became permanent, and each new integration added another key to the same file. In a system that grows for years, like the platform in the [port operations](/en/case-studies/port-operations/) case, which kept gaining modules and an integration with the WMS (warehouse management system) as operations required, every connection to another system brings a credential that needs a proper home.

When the same password is written in several files and used by several systems, changing it becomes a project, and the change keeps getting postponed. The old credential stays valid for years, including for people who have already left the company or whose contract has ended.

## Where should you look for credentials leaked in a repository?

The search starts in the current code and moves on to the history, the copies and everything generated from the code. The table shows the most common places.

| Where to look | What usually turns up | How to look |
| --- | --- | --- |
| Configuration files | Database password, cloud tokens and API keys in .env, config, appsettings, docker-compose | A scanning tool plus reading the configuration and deployment files |
| Commit history | Credentials already removed from the current code that remain in old commits and forgotten branches | Scanning the full history, across all branches |
| Copies and material handed to third parties | Zip files sent to vendors, forks, shared folders, repository backups | An inventory of who received the code and when |
| Images and containers | A configuration file copied into the image, variables baked in at build time | Scanning the images stored in the container registry |
| CI/CD logs | Variables printed during deployment, error messages containing the connection string | Reading the pipeline logs and each step's configuration |
| Code shipped to the browser or the phone | An API key embedded in the page's JavaScript or in the mobile app | Inspecting the code delivered to the end user |

The last row needs special care: any user can read what ships to the browser or the app, so a key with broad cloud access can't be there, even if the repository is private. As for container images, a credential copied inside stays in every version kept in the registry.

There are well-known open source tools for the scan. gitleaks and trufflehog look for password and key patterns in the code and in Git history, and trufflehog also tests whether some of the credentials it finds still work. git-secrets, published by AWS, blocks commits that contain key patterns. If the code is on GitHub, the platform's [secret scanning](https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning) checks the history of every branch as well as issues, pull requests and wikis. It's free on public repositories. On private ones, it depends on the plan.

## How do you find and replace each credential?

The safe path has three stages, in this order: find everything, revoke and replace each credential, and only then clean the history and put safeguards in place so the problem doesn't come back. The technical team works through the guide while the manager takes notes.

### Step-by-step guide to find and replace secrets exposed in code

Follow the steps in order. Log each credential you find in a spreadsheet: where it was, what it gives access to, who owns the service, the revocation date and where the new one is stored. Never paste the credential's value into the spreadsheet, a chat or an email. This guide is a starting point for the technical team to adapt to its own system.


**Find**

1. Run a secret scanning tool, such as gitleaks or trufflehog, on the whole repository, including the commit history of every branch
2. Read the configuration and deployment files (.env, config, appsettings, docker-compose, deploy scripts) looking for database passwords, cloud tokens and API keys
3. List the copies of the code outside the main repository: forks, zip files sent to vendors, shared folders and former employees' machines
4. Check published container images and CI/CD pipeline logs for credentials in plain text
5. Check whether the repository is or ever was public, and since when

**Revoke and replace**

1. For each credential found, write down what it gives access to (database, cloud, payments, email, ERP) and who manages that service
2. Create the new credential with the minimum access needed before disabling the old one, so the system doesn't go down
3. Store the new credential in a server environment variable or a secrets manager, never in the code
4. Revoke the old credential at the source service and test that it no longer works
5. Check the service's access logs to see whether the old credential was used from outside during the exposure period

**Clean up and prevent**

1. With the credential already revoked, decide whether rewriting the history with git filter-repo or BFG Repo-Cleaner is worth it, and agree on a date with everyone who has a copy
2. Turn on automatic scanning at commit time, with a pre-commit hook, and in the CI pipeline, plus push protection if the code is on GitHub
3. Add local configuration files to .gitignore and keep only a template in the repository with variable names and no values
4. Set a rotation schedule for credentials that aren't rotated automatically

The instinct is to delete the file and rewrite the history first, to make the evidence disappear. But as long as the old credential works, anyone with a copy still has access. GitHub's documentation on [removing sensitive data from a repository](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository) is direct about it: if the sensitive data is a password, token or credential, the first step is to revoke or rotate that secret. Once it's rotated it no longer grants access, and according to the same page that may be enough.

To avoid taking the system down, create the new credential before disabling the old one. Point the server to read the new one from outside the code, deploy, confirm everything works, and only then revoke the old one. For vendor integrations, agree on the switchover window with the other side.

The spreadsheet in the guide also answers the question that will come up: how long the credential was exposed and whether it was used. For the second part, check the access logs of the database, the cloud account and the API before they expire.

## Why doesn't deleting the file fix it?

Deleting the file removes the credential from the current version of the code, but it stays in the Git history and in every copy made before. The commit that removes the password creates a new version without it, and the previous version, with the password, remains available to anyone who looks at the history.

Rewriting the history with git filter-repo or BFG Repo-Cleaner solves part of this, with side effects. According to the same GitHub documentation, commit identifiers change, branch protections have to be turned off for the force push, and any colleague with an older copy can bring the credential back with a pull followed by a push. The page also makes it clear that you can't remove the data from copies other people already have, or from forks.

That's why the rewrite comes after revocation, and sometimes it doesn't happen at all. It makes sense when the repository will stay public, when the history holds personal data, or when an audit requires it. With the credential revoked and the repository private and under control, it may not be worth the effort. The technical lead decides and records the reason.

Old images in the container registry, CI logs and the zip file in the vendor's inbox also hold the credential. Only revocation reaches all of those copies at once.

## How do you keep secrets out of the code from now on?

The rule is that the code says where to read the credential from, and the value lives in a protected place outside the repository. In practice, the measures below cover most cases:

1. One environment variable on the server for each credential, with a template file in the repository that lists the variable names without the values.
2. A secrets manager, such as AWS Secrets Manager, Google Cloud Secret Manager or HashiCorp Vault, when there are several environments or frequent rotation.
3. Automatic scanning at two points: at commit time, with a hook that runs gitleaks or git-secrets on the developer's machine, and in the CI pipeline, which blocks the release if it finds a secret. On GitHub, push protection blocks pushes containing credentials the platform recognizes.
4. Separate credentials per environment and per integration. Production doesn't share passwords with staging, and each external system has its own key, with access only to what it needs.
5. Periodic rotation, automatic where the service allows it, so that changing a credential becomes routine instead of an emergency.

The [OWASP Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html), published by the foundation that maintains open software security references, says a secret should exist only as long as necessary, have automatic rotation, be visible only to those who need it, be revocable, and never appear in logs.

An example of least privilege: if the integration with the BI tool only reads three tables, its credential shouldn't be able to delete anything or read the users table. If it leaks, the damage stays limited, and replacing it doesn't affect the other integrations.

## What if the code is with a vendor or the system is legacy?

If the system belongs to a vendor, the guide becomes a list of requests: the result of a scan of the full history, the credentials the system uses and where each one is stored, the plan to replace any that are in the code, and who else received the code during the contract. Credentials for services your company pays for, such as the cloud account and the database, belong to your company, and someone on your side should revoke them or oversee the revocation.

If the repository was shared with a vendor whose contract has ended, treat the credentials from that period as exposed, because there's no way to prove nobody used them.

In a legacy system, the same password is often scattered across files, scripts and scheduled jobs, and nobody knows where else it shows up. Start by centralizing where the code reads the credential, make the switch, and watch the errors over the following days to find the forgotten spot. Sometimes the fix becomes part of [modernizing in stages](/en/blog/legacy-system-modernization/), because changing how the system reads its configuration opens the way for other improvements.

## When is this not enough?

The guide covers secrets written into the code and its copies. It doesn't cover employee identity management, personal password vaults or a full incident response, and it doesn't replace a broader review of permissions, APIs and the categories in the [OWASP Top 10 explained for managers](/en/blog/owasp-top-10-for-managers/).

If the logs show that an exposed credential was used by someone outside, you're dealing with an incident. If it gave access to personal data, bring in the company's Data Protection Officer and legal team before any other decision. Brazil's General Data Protection Law (LGPD) requires certain incidents to be reported to the ANPD, the national data protection authority, and companies elsewhere should check the equivalent rules in their own jurisdiction. The article on [LGPD in internal systems](/en/blog/lgpd-in-internal-systems-what-to-review/) shows what to review, and the one on [data leaks in internal systems](/en/blog/data-leaks-in-internal-systems/) covers the other paths data takes out.

Looking for exposed secrets is one of the steps in boosting.tech's security assessment for internal systems. boosting.tech is a custom software company, and the assessment has a fixed scope and delivers a list of what to replace first. To talk about your case, write to jcm@boosting.tech or use the [contact](/en/contact/) page. The first conversation is free. For keeping the system in shape after that, see the [support and modernization](/en/services/support-and-modernization/) service.

## Frequently asked questions about exposed API keys in code

### Does a private repository also need secret scanning?

Yes. Being private limits who sees the repository today, but it doesn't control the copies already made by developers, vendors and continuous integration tools, all with full history. Private repositories also become public by mistake, during a migration or a settings change. The scan shows what would be exposed and lets you replace the credentials beforehand.

### If the key was exposed for only a few minutes, do I need to replace it?

Yes. Public repositories are routinely scanned by bots looking for keys, and there's no way to know whether someone copied the credential in that window. Replacing it is usually quick when the credential lives in a single place. Afterwards, check the service's access logs to see whether it was used during that period.

### Does deleting the public repository solve the problem?

No. Anyone who cloned or forked it earlier still has the copy, and services that archive public code may have saved a version. Deleting it reduces exposure from that point on, but the credential only stops working when it's revoked at the source service. Revoke first, then decide what to do with the repository.

### Are environment variables enough, or do I need a secrets manager?

With few environments and few people with server access, well-configured environment variables are usually enough. A secrets manager starts to pay off with several environments and teams, frequent rotation, or audit requirements about who read each secret. Either way, the value stays out of the repository.

### Who should replace the credentials?

The technical team does the work, because it knows where the credential is used and how to replace it without taking the system down. The responsible manager decides and follows up, because they know what data it gives access to and who needs to be told. When the credential belongs to a service the company pays for, such as the cloud account, someone from the company should revoke it, even if the system belongs to a vendor.

## References

- [MITRE CWE-798: Use of Hard-coded Credentials](https://cwe.mitre.org/data/definitions/798.html)
- [GitHub Docs: Removing sensitive data from a repository](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository)
- [GitHub Docs: About secret scanning](https://docs.github.com/en/code-security/secret-scanning/introduction/about-secret-scanning)
- [GitHub Docs: About push protection](https://docs.github.com/en/code-security/secret-scanning/introduction/about-push-protection)
- [OWASP Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html)
