CCyberwiki

Exposed credentials in source code repositories

Exposed credentials in source code repositories
ClassSecrets
Also known asHard-coded credentials; secrets in code; secret leak
CWECWE-798: Use of Hard-coded Credentials
CWE-540: Inclusion of Sensitive Information in Source Code
OWASP Top 10A07:2025 Authentication Failures
MITRE ATT&CKT1593.003 Search Open Websites/Domains: Code Repositories
T1552.001 Unsecured Credentials: Credentials In Files
Typical severityCritical
AffectsAny 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 .env file committed before .gitignore contained 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

2017201820192020202120222023Repository with the access key publicly readableDecember 2017: source code published on GitHub15 September 2022: access to the repository restricted17 September 2022: keys of the data server changed7 October 2022: Toyota's public notice
Figure 1. Timeline of the T-Connect exposure. The access key was publicly readable for almost five years.

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 -v

Each 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

  1. 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.
    ProviderWhere to replace the key
    StripeDevelopers, API keys, then Roll key on the secret key
    OpenAIAPI keys page of the platform: delete the key, create a new one
    AnthropicAPI keys in the Console: disable or delete the key, create a new one
    SupabaseProject 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
    GitHubSettings, Developer settings, Personal access tokens: delete the token
    AWSIAM, the user's Security credentials: make the access key inactive, create a new one, delete the old one
  2. 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.
  3. Remove the credential from the code. Read it from an environment variable and add .env to .gitignore. To remove a file from every commit:
    git filter-repo --invert-paths --path .env
    Copies of the repository made before the rewrite still contain the credential.

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

References

  1. back "CWE-798: Use of Hard-coded Credentials". Common Weakness Enumeration. MITRE.
  2. back "A07:2025 Authentication Failures". OWASP Top 10:2025. OWASP Foundation.
  3. back "CWE-540: Inclusion of Sensitive Information in Source Code". Common Weakness Enumeration. MITRE.
  4. back "Search Open Websites/Domains: Code Repositories, T1593.003". MITRE ATT&CK.
  5. back "Unsecured Credentials: Credentials In Files, T1552.001". MITRE ATT&CK.
  6. a b Toyota Motor Corporation (7 October 2022). [Notice on the possible leak of T-Connect customer email addresses] (in Japanese).
  7. a b Toulas, Bill (10 October 2022). "Toyota discloses data leak after access key exposed on GitHub". BleepingComputer.
  8. a b c "Secrets Management Cheat Sheet". OWASP Cheat Sheet Series.