Agentic Security Pedia

Where AI agents leak your keys

In requests to model providers, once an agent reads them from .env or shell files. In plain-text MCP configs, agent settings and session transcripts. In files agents write and push. On attackers' servers, via malicious packages. Find them, rotate them, keep them out of agent context, and redact before sending.

The question Where do our secrets end up?

Six paths, side by side

Each path starts with a key an agent can reach and ends with a copy you no longer control.

PathHow it happensWhere the key ends up
Prompts and contextA key in a pasted log, a file or a command's output joins the conversationThe model provider, inside the next request
.env and shell filesThe agent opens a config or shell profile to debug or run somethingThe agent's context, then the request and the transcript
MCP configs and agent settingsA token pasted into a config, or saved with a command you approvedPlain-text files, and any package you publish with them
Session transcriptsThe agent logs every tool result to diskPlain-text files in your home folder
Generated filesThe agent writes a key into state, config or codeCloud storage, repositories and package registries
Malicious packages and extensionsInstall scripts and poisoned releases search the machine for keysAttacker servers and public repositories

In the request to the model

Each time an agent asks its model what to do next, it sends the conversation so far. That includes what it has read: a pasted stack trace, a config file, the output of env. A key in any of them leaves with the request.

GIRD's case study saw this on one company's engineering laptops, in observation mode with nothing blocked. Four named keys left inside ordinary agent requests: two GitHub access keys and two Shopify store keys. A key counts as named when its prefix shows which service it opens. Three of the four went to api.anthropic.com.

A separate, unidentified token went to the same address 216 times in 16 hours. Its shape matches a Supabase service-role key, which can read and change any row in its database. Its type was never established. These requests went to the AI vendor the team had approved, inside traffic the agents send all day. None of it looked like an incident.

In .env and shell files the agent reads

Agents read files to do their work, and keys live in files. They sit in .env files, in shell profiles such as ~/.zshrc, in ~/.aws/credentials and in kubeconfigs. When an agent opens one to debug a failing call, the key enters its context. From there it goes into the next request and the transcript.

A .gitignore entry doesn't help here. It keeps a file out of commits, not out of the agent's reach. In the case study, agents' own processes opened password and key files 19 times one week and 29 the next, across six files.

In plain text: MCP configs, agent settings and transcripts

MCP servers need credentials, and the quickest setup pastes a token into the config. Claude Code keeps personal MCP server configs in ~/.claude.json. Codex keeps them in ~/.codex/config.toml, where an env block passes variables to the server. GitGuardian's 2026 report counted 24,008 unique secrets in MCP configuration files on public GitHub, 2,117 of them valid.

Agent settings collect keys too. In Claude Code, choosing "Yes, and don't ask again" saves the approved command as an allow rule in .claude/settings.local.json. If the command carried a key, the file now holds it. Lakera scanned about 46,500 npm packages and found that file in 428. Thirty packages shipped live credentials in it, across 33 files.

Then there are transcripts. Claude Code writes every message, tool call and tool result to a plain-text file under ~/.claude/projects/, and keeps it for 30 days by default. Its documentation is direct: if a tool reads a .env file, that value is written to the transcript. Codex has a setting that controls whether it saves transcripts to history.jsonl. Anything that can read your home folder can read these files.

In files the agent writes and pushes

Agents write files, and sometimes a key goes into one. In the case study, a GitHub access key was written into a Terraform state file and uploaded to S3. The same key had gone to AWS Amplify minutes earlier. HashiCorp's documentation says Terraform stores secrets from your configuration in its state and plan files. Anyone who can read those files can read the values.

Commits carry keys too. GitGuardian found 28.65 million new hardcoded secrets in public GitHub commits in 2025, up 34% on the year before. Leaked secrets for AI services rose 81%, to 1,275,105. Commits co-authored with Claude Code leaked secrets at 3.2%, against a 1.5% baseline. GitGuardian notes that developers still decide what gets accepted or pushed.

With malicious packages and extensions

Some malware now hunts for agent keys, and some of it uses the agents. In August 2025, trojanised Nx packages ran the AI command-line tools on developers' machines with permission checks disabled. Claude Code, Gemini CLI and Amazon Q were among them, and were told to search the machine for secrets. Wiz counted over a thousand valid GitHub tokens leaked in the first phase.

In August 2026, the CHAINDROP worm reached over 400 npm packages. Elastic Security Labs found its harvester aimed at Anthropic, Claude, Codex, Cursor, OpenAI and Gemini credentials. It also planted a Claude Code SessionStart hook, so it ran again whenever someone started a new session in an infected repository.

On 21 October 2026, a compromised LiteLLM release, 1.107.0, reached PyPI and Docker Hub. It carried malware that steals credentials and ran on any Python start in an affected environment. The maintainers told users to rotate everything the host could reach.

Editor extensions do it too. In October 2025, Koi Security found GlassWorm in extensions on OpenVSX and Microsoft's VS Code marketplace, hidden in invisible Unicode characters. It went after GitHub, npm and OpenVSX credentials and used them to spread, across an estimated 35,800 installations.

An agent nobody uses is still a target. In the case study, 8 of the 14 installed coding agents recorded no session at all, and all stayed installed. That is what the Nx malware used: coding agents already on developers' machines. To list yours, see Shadow AI agents: what they are and how to find them.

What to do about it

Find them. Start with one computer. Blindspot, GIRD's open-source, read-only check, runs on macOS, Linux and Windows. It lists the coding agents and MCP servers on the machine, and any token written into an MCP config. It finds AI and cloud keys in shell profiles, shell history, agent configs and .env files, plus keys saved in Claude Code allow lists. It names the provider and the file, never the key. Run it yourself in a terminal, not through an agent: an agent asked to find keys has to read them. Finding nothing doesn't prove a key never leaked.

Rotate them. Treat any key that reached a model provider, a public repository or a malicious package as exposed. Revoke it at the provider and issue a new one. GitHub's guidance is to revoke or rotate first. The old commits can survive a history rewrite in clones, forks, cached views and pull requests. If malware may be on the machine, take it offline and rotate from another computer. Anything typed or saved on an infected machine can be stolen too, including the new key.

Keep keys out of agent context. Deny agents read access to credential and .env files; Claude Code's settings docs show Read(./.env) as a deny rule. In Codex, set shell_environment_policy.ignore_default_excludes to false, so commands it runs don't inherit variables whose names contain KEY, SECRET or TOKEN. Reference MCP tokens through environment variables, such as Codex's bearer_token_env_var, rather than pasting them into configs. Shorten transcript retention. For how much access an agent should get at all, see Least privilege for AI agents.

Redact before sending. Rules inside one agent cover only that agent. Check what leaves the device, not only what sits on disk. A key caught in an outgoing request can be removed before it reaches the model provider.

In India, check your reporting duty too. CERT-In's directions of 28 April 2022 apply to service providers, intermediaries, data centres, body corporates and government organisations. They must report listed incidents within six hours of noticing them. The list includes unauthorised access to IT systems or data, malicious code attacks and data leaks. See CERT-In, DPDP and AI agents.

FAQs

Is a .env file safe if it's listed in .gitignore?

It's safe from commits, not from agents. An agent that reads the file sends the key to its model provider and writes it to its transcript. Deny agents read access to .env files and other credential files.

If we delete a leaked key from the repository, are we safe?

No. Revoke or rotate the key first. GitHub warns that the old commits can survive in clones, forks, cached views and pull requests. Treat any key that reached a model provider, a public repository or a malicious package as exposed.

Can we ask a coding agent to find the keys on our machines?

No. To find a key, an agent has to read it, which puts the key in its request and its transcript. Run a read-only check yourself in a terminal. Blindspot, GIRD's open-source check, names the provider and file and never prints a key.

Why rotate keys from another computer?

If malware may be running, anything you type or save on that machine can be stolen, including the new key. Take the machine offline, rotate from a clean one, and rebuild the affected environment before you use it again.

Do agents keep copies of the keys they read?

Many do. Claude Code writes every message and tool result to a plain-text transcript under ~/.claude/projects/ and keeps it for 30 days by default. If a tool reads a .env file, the value lands in that transcript. Shorten retention and deny reads of credential files.

Sources

  1. The State of Secrets Sprawl 2026: AI-Service Leaks Surge 81% and 29M Secrets Hit Public GitHub, GitGuardian, 17 Mar 2026. Read 26 Oct 2026.
  2. Explore the .claude directory, Anthropic, Claude Code documentation. Read 26 Oct 2026.
  3. Settings files and precedence, Anthropic, Claude Code documentation. Read 26 Oct 2026.
  4. Configuration Reference, OpenAI, Codex documentation. Read 26 Oct 2026.
  5. Claude Code is leaking API keys into public package registries, TechTalks, 27 Apr 2026. Read 26 Oct 2026.
  6. Manage sensitive data in your configuration, HashiCorp, Terraform documentation. Read 26 Oct 2026.
  7. s1ngularity: supply chain attack leaks secrets on GitHub: everything you need to know, Wiz, 27 Aug 2025. Read 26 Oct 2026.
  8. Shai-Hulud strikes again: CHAINDROP worm hits 400+ npm packages, Elastic Security Labs, 6 Aug 2026. Read 26 Oct 2026.
  9. Contained: Oct 21, 2026 LiteLLM release v1.107.0 Compromised, LiteLLM, 21 Oct 2026. Read 26 Oct 2026.
  10. Self-spreading GlassWorm malware hits OpenVSX, VS Code registries, BleepingComputer, 20 Oct 2025. Read 26 Oct 2026.
  11. Removing sensitive data from a repository, GitHub Docs. Read 26 Oct 2026.
  12. Directions under sub-section (6) of section 70B of the Information Technology Act, 2000 relating to information security practices, procedure, prevention, response and reporting of cyber incidents for Safe & Trusted Internet, CERT-In, 28 Apr 2022. Read 26 Oct 2026.