Exposed credentials in source code repositories
| Class | Secrets |
|---|---|
| Also known as | Hard-coded credentials; secrets in code; secret leak |
| CWE | CWE-798: Use of Hard-coded Credentials CWE-540: Inclusion of Sensitive Information in Source Code |
| OWASP Top 10 | A07:2025 Authentication Failures |
| MITRE ATT&CK | T1593.003 Search Open Websites/Domains: Code Repositories T1552.001 Unsecured Credentials: Credentials In Files |
| Typical severity | Critical |
| Affects | Any project kept in Git; keys for Stripe, OpenAI, Anthropic, Supabase, AWS and GitHub |
Exposed credentials in source code repositories are passwords, API keys, access tokens or private keys written into files and committed to a repository that unauthorised people can read, such as a public repository on GitHub. Developers also call the problem a secret leak or secrets in code.
Description
Writing a credential into source code is a weakness on its own, grouped with other authentication failures.12 It turns into an exposure once outsiders can read the repository that holds the code.3
Git, the most widely used version control system, stores every version of every file. A credential removed in a later commit stays in the repository's history, where anyone who can clone the repository can read it. The usual routes in:
- An
.envfile committed before.gitignorecontained an entry for it. - A key pasted into code for a test, such as
new Stripe("sk_live_..."), and not removed. - A hosting configuration file, a Dockerfile or the settings file of a coding agent.
- An old commit: the key was deleted later, but the commit that added it remains.
Attackers search public code repositories for keys while they scout a target,4 then use the credentials they find in those files.5
Impact
A leaked credential gives its holder the same access as the application that owns it. A payment provider key can issue refunds or read customer data; a cloud provider key can start servers billed to the account; an AI provider key can be used until the account's spending limit is reached (see Denial of wallet); a database key can read and change every row. Deleting the repository or making it private stops new readers. Anyone who copied the key before then still holds a working credential until it is revoked.
Notable incidents
Toyota T-Connect (2022)
In October 2022, Toyota Motor Corporation disclosed that part of the source code of its T-Connect connected-car service had been published on GitHub by a development subcontractor in December 2017.6 The code contained an access key to a data server that held customer email addresses and customer management numbers. Access to the repository was restricted on 15 September 2022, and the keys of the data server were changed on 17 September 2022.7
Toyota stated that 296,019 email addresses and customer management numbers might have leaked. It could not confirm from the server's access history whether a third party had used the key, and it could not rule this out. Customer names, telephone numbers and payment card details were not stored on that server.67
Detection
The open-source tool gitleaks reads every commit of a repository, so it also finds keys that a later commit deleted. Run it inside the repository folder:
gitleaks git -vEach result shows the file, the commit and the line. Install it with brew install gitleaks on macOS, or download a release from the project's GitHub page. On GitHub, the Security tab of a repository lists credentials found by secret scanning.
Remediation
Revoking the credential is what closes the exposure, so it comes first. Rewriting the history leaves the old key working for anyone who already has it.8
- Revoke and replace the credential at the provider. Rotation means creating a new credential, deploying it where the old one was used, and deleting the old one.
Provider Where to replace the key Stripe Developers, API keys, then Roll key on the secret key OpenAI API keys page of the platform: delete the key, create a new one Anthropic API keys in the Console: disable or delete the key, create a new one Supabase Project Settings, API Keys: create a new secret key, then delete the old one. For the older service_role key, change the JWT secret, which also replaces the anon key GitHub Settings, Developer settings, Personal access tokens: delete the token AWS IAM, the user's Security credentials: make the access key inactive, create a new one, delete the old one - Review the provider's usage records for the period of exposure. Unrecognised charges, an increase in AI usage or calls at unusual hours indicate use by a third party. On AWS, CloudTrail records every call made with an access key.
- Remove the credential from the code. Read it from an environment variable and add
.envto.gitignore. To remove a file from every commit:
Copies of the repository made before the rewrite still contain the credential.git filter-repo --invert-paths --path .env
Prevention
Keep credentials out of source files and supply them as environment variables at run time.8 Add .env* to .gitignore before the first commit. In the repository's Settings, open Advanced Security and turn on push protection under secret scanning; GitHub then rejects a push that contains a credential it recognises. A pre-commit hook that runs a secret scanner gives the same protection on the developer's computer.8
Prompt for a coding agent
Search this repository, including its git history, for API keys, tokens, passwords and private keys. For each one, tell me the file, the commit and the service it belongs to, and do not print the full value. Then move every secret the code uses into environment variables read at runtime, add the env files to .gitignore, and add a .env.example file that lists the variable names with empty values. Do not rotate or delete any key yourself: list the ones I must replace at the provider.
In SecFlash
SecFlash reports SF-SEC-702 (secret in the code or git history) on the Security page, with the file and commit, and SF-EXP-301 on the Exposure page for a credential found in public JavaScript.
See also
- Common types of attack on web applications
- Abuse of leaked API keys
- Client-side code exposure
- Privileged key in client-side code
- Secret exposure in CI/CD workflows
- Secrets management
- Responding to a leaked API key
- Category: Secrets
References
- back "CWE-798: Use of Hard-coded Credentials". Common Weakness Enumeration. MITRE.
- back "A07:2025 Authentication Failures". OWASP Top 10:2025. OWASP Foundation.
- back "CWE-540: Inclusion of Sensitive Information in Source Code". Common Weakness Enumeration. MITRE.
- back "Search Open Websites/Domains: Code Repositories, T1593.003". MITRE ATT&CK.
- back "Unsecured Credentials: Credentials In Files, T1552.001". MITRE ATT&CK.
- a b Toyota Motor Corporation (7 October 2022). [Notice on the possible leak of T-Connect customer email addresses] (in Japanese).
- a b Toulas, Bill (10 October 2022). "Toyota discloses data leak after access key exposed on GitHub". BleepingComputer.
- a b c "Secrets Management Cheat Sheet". OWASP Cheat Sheet Series.
External links
- gitleaks, project page on GitHub
- About push protection, GitHub Docs