How to Stop API Keys and Secrets from Leaking in 2026
Your secrets are leaking — 28.65 million new hardcoded secrets were pushed to public GitHub commits in 2025 alone, up 34% year over year, and 64% of credentials confirmed valid in 2022 are still exploitable in 2026. The fix is not a tool; it is a routine: scan every repo and machine for exposed keys, rotate anything found, move credentials into a vault, and gate your AI coding tools so they never learn your secrets in the first place.
Why Secrets Leak So Often in 2026
The scale keeps climbing. GitGuardian's State of Secrets Sprawl 2026 report found 28.65 million new hardcoded secrets in public GitHub commits last year — a 34% jump and the largest single-year increase ever recorded — spread across more than 4 million repositories. Public commits grew to 1.94 billion as the active developer base passed 22.8 million.
Leaked secrets are compounding faster than the codebase. Since 2021, exposed secrets on public GitHub have grown 152% while the developer population grew 98% — exposure is climbing about 1.6x faster than the people writing code. And public repos are only part of the picture: 32.2% of internal repositories contain at least one secret, versus 5.6% of public ones. Internal repos are the biggest exposure reservoir, waiting for one access slip to go public.
The 2026 Wildcard: AI Coding Tools Leak More
AI-assisted development is making the leak rate worse. Claude Code-assisted commits showed a 3.2% secret-leak rate versus a 1.5% baseline across all public GitHub commits, and AI service secrets hit 1,275,105 in 2025 — up 81% year over year. A single scan turned up 113,000 leaked DeepSeek API keys.
AI infrastructure also opened a brand-new leak surface: MCP configuration files. In 2025, GitGuardian found 24,008 unique secrets exposed in MCP-related configs on public GitHub, 8.8% of them valid credentials at detection time. If your team runs AI agents, those config files are now a secrets vector that did not exist two years ago.
What Attackers Do with a Leaked Key
A leaked key is not a theoretical risk. In the Shai-Hulud 2 campaign, attackers compromised 6,943 developer machines and extracted 294,842 secret occurrences — 59% of them from CI/CD runners, the machines holding deployment credentials.
Abuse moves fast. CloudSEK's April 2026 analysis of 10,000 Android apps found 32 active Google API keys in 22 apps collectively installed over 500 million times; one developer reported $15,400 in unauthorized charges within hours of exposure. And most leaks are never cleaned up — 64% of valid secrets from 2022 were still valid in January 2026.
The Secrets Hygiene Checklist
- Scan everything, public and internal. Run gitleaks or trufflehog against your repos, enable GitHub and GitLab secret scanning, and scan CI variables, container images, and MCP configs.
- Rotate — do not just delete. If a secret ever touched a repo, branch, log, or prompt, it is compromised; deleting it from history does not un-leak it. Rotate the credential and revoke the old one.
- Move secrets out of code. Use environment variables, gitignored .env files, Docker secrets, or a vault such as HashiCorp Vault or a cloud secret manager. Code should never contain a raw key.
- Gate your AI tools and CI. Add pre-commit secret-scanning hooks, stop AI assistants from reading your environment, and review MCP configs for hardcoded credentials before an agent ships them.
- Harden developer machines. Compromised workstations are the biggest secrets reservoir. Enforce least privilege, patch aggressively, and keep signing keys on hardware tokens.
- Automate rotation and alerts. Set expiry on short-lived credentials, alert on any detected exposure, rotate when employees leave, and keep a kill switch for every critical key.
The Bottom Line
Secrets leak because they are convenient — hardcoded, committed, shared, forgotten. In 2026 the leak rate is climbing 34% a year and AI tools are making it worse. The teams that survive are the ones that scan constantly, rotate ruthlessly, and never let a credential live in code.
And remember: exposed secrets are only one of the things attackers look for. Know what attackers see before they see it.
Frequently Asked Questions
How many secrets leak on GitHub every year?
GitGuardian detected 28.65 million new hardcoded secrets in public GitHub commits in 2025, up 34% year over year — the largest single-year jump on record. That figure covers public repositories only; internal repositories hold even more exposure.
Do leaked API keys actually get used by attackers?
Yes, and fast. Bots scan public commits and MCP configs within minutes of a push. In one 2026 analysis, 32 active Google API keys were found in Android apps with over 500 million combined installs, and a developer saw $15,400 in unauthorized charges within hours of exposure.
Can I just delete a secret from git history?
No — treat it as compromised and rotate it. Deleting the commit or rewriting history reduces future exposure, but anyone who already cloned the repository has the key. Rotation is the only fix that matters.
What are MCP config files and why do they leak secrets?
MCP (Model Context Protocol) configs connect AI agents to tools and APIs. In 2025, GitGuardian found 24,008 unique secrets exposed in MCP-related configs on public GitHub — 8.8% confirmed valid at detection time. If you run AI agents, scan those config files like any other code.
Do AI coding assistants leak secrets?
They leak more than average. Claude Code-assisted commits showed a 3.2% secret-leak rate versus a 1.5% baseline across all public GitHub commits, and AI service secrets grew 81% in 2025. Use assistants that scan for secrets, keep raw keys out of prompts, and review everything an agent commits.
Find the secrets attackers are already looking for
RootCrak's autonomous scanner checks servers, APIs, and web apps around the clock — exposed keys, open ports, misconfigurations — and gives you a clear security score with fixes.
Get a Free Security ScanWe don't fix systems. We make them unbreakable.